import { CreateAlertParams , CreateAlertResponse , CreateQuestionParams , CreateQuestionResponse , DateRangeType , DesktopNotifBody , DesktopNotifPartial , GetAlertsResponse , GetCourseOverridesResponse , GetCourseResponse , GetInsightOutputResponse , GetProfileResponse , GetQuestionResponse , GetQueueResponse , GetReleaseNotesResponse , GroupQuestionsParams , GetSelfEnrollResponse , ListInsightsResponse , ListQuestionsResponse , RegisterCourseParams , SemesterPartial , TACheckinTimesResponse , TACheckoutResponse , TAUpdateStatusResponse , UpdateCourseOverrideBody , UpdateCourseOverrideResponse , UpdateProfileParams , UpdateQuestionParams , UpdateQuestionResponse , UpdateQueueParams , } from "@koh/common" ; import Axios , { AxiosInstance , Method } from "axios" ; import { plainToClass } from "class-transformer" ; import { ClassType } from "class-transformer/ClassTransformer" ; type ItemIfArray < T > = T extends ( infer I ) [ ] ? I : T ; class APIClient { private axios : AxiosInstance ; param method param url param responseClass param body private async req < T > ( method , url , responseClass ? , body ? , params ? ) ; private async req < T > ( method , url , responseClass ? , body ? , params ? ) { const res = ( await this . axios . request ( { method , url , data : body , params } ) ) . data ; return responseClass ? plainToClass ( responseClass , res ) : res ; } profile = { index : async ( ) : Promise < GetProfileResponse > => this . req ( "GET" , `/api/v1/profile` , GetProfileResponse ) , patch : async ( body ) : Promise < GetProfileResponse > => this . req ( "PATCH" , `/api/v1/profile` , undefined , body ) , deleteProfilePicture : async ( ) : Promise < void > => this . req ( "DELETE" , `/api/v1/profile/delete_profile_picture` ) , } ; course = { get : async ( courseId ) => this . req ( "GET" , `/api/v1/courses/${ courseId }` , GetCourseResponse ) , updateCalendar : async ( courseId ) => this . req ( "POST" , `/api/v1/courses/${ courseId }/update_calendar` ) , getCourseOverrides : async ( courseId ) => this . req ( "GET" , `/api/v1/courses/${ courseId }/course_override` , GetCourseOverridesResponse ) , addOverride : async ( courseId , params ) : Promise < UpdateCourseOverrideResponse > => this . req ( "POST" , `/api/v1/courses/${ courseId }/update_override` , UpdateCourseOverrideResponse , params ) , deleteOverride : async ( courseId , params ) : Promise < void > => this . req ( "DELETE" , `/api/v1/courses/${ courseId }/update_override` , undefined , params ) , withdrawCourse : async ( courseId ) : Promise < void > => this . req ( "DELETE" , `/api/v1/courses/${ courseId }/withdraw_course` , undefined ) , registerCourses : async ( params ) : Promise < void > => this . req ( "POST" , `/api/v1/courses/register_courses` , undefined , params ) , getTACheckinTimes : async ( courseId , startDate , endDate ) : Promise < TACheckinTimesResponse > => this . req ( "GET" , `/api/v1/courses/${ courseId }/ta_check_in_times` , TACheckinTimesResponse , { } , { startDate , endDate } ) , toggleSelfEnroll : async ( courseId ) : Promise < void > => this . req ( "POST" , `/api/v1/courses/${ courseId }/self_enroll` ) , selfEnrollCourses : async ( ) : Promise < GetSelfEnrollResponse > => this . req ( "GET" , "/api/v1/self_enroll_courses" ) , createSelfEnrollOverride : async ( courseId ) : Promise < void > => this . req ( "POST" , `/api/v1/create_self_enroll_override/${ courseId }` ) , } ; taStatus = { checkIn : async ( courseId , room ) : Promise < TAUpdateStatusResponse > => this . req ( "POST" , `/api/v1/courses/${ courseId }/ta_location/${ room }` ) , checkOut : async ( courseId , room ) : Promise < TACheckoutResponse > => this . req ( "DELETE" , `/api/v1/courses/${ courseId }/ta_location/${ room }` ) , } ; questions = { index : async ( queueId ) => this . req < ListQuestionsResponse > ( "GET" , `/api/v1/queues/${ queueId }/questions` , ListQuestionsResponse ) , create : async ( params ) => this . req ( "POST" , `/api/v1/questions` , CreateQuestionResponse , params ) , get : async ( questionId ) : Promise < GetQuestionResponse > => this . req ( "GET" , `/api/v1/questions/${ questionId }` , GetQuestionResponse ) , update : async ( questionId , params ) => this . req ( "PATCH" , `/api/v1/questions/${ questionId }` , UpdateQuestionResponse , params ) , notify : async ( questionId ) : Promise < void > => this . req ( "POST" , `/api/v1/questions/${ questionId }/notify` ) , group : async ( params ) : Promise < void > => this . req ( "POST" , "/api/v1/questions/group" , undefined , params ) , resolveGroup : async ( groupId , queueId ) : Promise < void > => this . req ( "PATCH" , `/api/v1/questions/resolveGroup/${ groupId }` , undefined , { queueId } ) , } ; queues = { get : async ( queueId ) : Promise < GetQueueResponse > => this . req ( "GET" , `/api/v1/queues/${ queueId }` , GetQueueResponse ) , update : async ( queueId , params ) => this . req ( "PATCH" , `/api/v1/queues/${ queueId }` , UpdateQuestionResponse , params ) , clean : async ( queueId ) : Promise < void > => this . req ( "POST" , `/api/v1/queues/${ queueId }/clean` ) , } ; notif = { desktop : { credentials : async ( ) : Promise < string > => this . req ( "GET" , "/api/v1/notifications/desktop/credentials" ) , register : async ( payload ) : Promise < DesktopNotifPartial > => this . req ( "POST" , `/api/v1/notifications/desktop/device` , DesktopNotifPartial , payload ) , unregister : async ( deviceId ) : Promise < string > => this . req ( "DELETE" , `/api/v1/notifications/desktop/device/${ deviceId }` , undefined ) , } , } ; seeds = { delete : async ( ) => this . req ( "GET" , `/api/v1/seeds/delete` ) , create : async ( ) => this . req ( "GET" , `/api/v1/seeds/create` ) , fillQueue : async ( ) => this . req ( "GET" , `/api/v1/seeds/fill_queue` ) , } ; semesters = { get : async ( ) : Promise < SemesterPartial [ ] > => this . req ( "GET" , `/api/v1/semesters` ) , } ; releaseNotes = { get : async ( ) : Promise < GetReleaseNotesResponse > => this . req ( "GET" , `/api/v1/release_notes` ) , } ; insights = { get : async ( courseId , insightName , params ) : Promise < GetInsightOutputResponse > => { return this . req ( "GET" , `/api/v1/insights/${ courseId }/${ insightName }` , undefined , undefined , params ) ; } , list : async ( ) : Promise < ListInsightsResponse > => this . req ( "GET" , `/api/v1/insights/list` ) , toggleOn : async ( insightName ) : Promise < void > => this . req ( "PATCH" , `/api/v1/insights` , undefined , { insightName } ) , toggleOff : async ( insightName ) : Promise < void > => this . req ( "DELETE" , `/api/v1/insights` , undefined , { insightName } ) , } ; alerts = { get : async ( courseId ) : Promise < GetAlertsResponse > => this . req ( "GET" , `/api/v1/alerts/${ courseId }` ) , create : async ( params ) : Promise < CreateAlertResponse > => this . req ( "POST" , `/api/v1/alerts` , CreateAlertResponse , params ) , close : async ( alertId ) : Promise < void > => { this . req ( "PATCH" , `/api/v1/alerts/${ alertId }` ) ; } , } ; constructor ( baseURL = "" ) { this . axios = Axios . create ( { baseURL : baseURL } ) ; } } export const API = new APIClient ( process . env . NEXT_PUBLIC_API_URL ) ;