@@ -85,7 +85,7 @@ export const postAuth = async (
85
85
shouldRefresh : false
86
86
}
87
87
) ;
88
- if ( ! resp ) {
88
+ if ( ! resp . ok ) {
89
89
return null ;
90
90
}
91
91
@@ -101,7 +101,7 @@ export const postAuth = async (
101
101
*/
102
102
const postRefresh = async ( refreshToken : string ) : Promise < Tokens | null > => {
103
103
const resp = await Cadet . auth . refresh ( { refresh_token : refreshToken } ) ;
104
- if ( ! resp ) {
104
+ if ( ! resp . ok ) {
105
105
return null ;
106
106
}
107
107
@@ -118,7 +118,7 @@ const postRefresh = async (refreshToken: string): Promise<Tokens | null> => {
118
118
export const getUser = async ( tokens : Tokens ) : Promise < User | null > => {
119
119
const resp = await Cadet . user . index ( { ...tokens } ) ;
120
120
// TODO
121
- return resp && resp . ok ? ( resp . data as User ) : null ;
121
+ return resp . ok ? ( resp . data as User ) : null ;
122
122
} ;
123
123
124
124
/**
@@ -129,7 +129,7 @@ export const getUser = async (tokens: Tokens): Promise<User | null> => {
129
129
export const getAchievements = async ( tokens : Tokens ) : Promise < AchievementItem [ ] | null > => {
130
130
const resp = await Cadet . incentives . indexAchievements ( { ...tokens } ) ;
131
131
132
- if ( ! resp || ! resp . ok ) {
132
+ if ( ! resp . ok ) {
133
133
return null ; // invalid accessToken _and_ refreshToken
134
134
}
135
135
@@ -195,7 +195,7 @@ export const getGoals = async (
195
195
export const getOwnGoals = async ( tokens : Tokens ) : Promise < AchievementGoal [ ] | null > => {
196
196
const resp = await Cadet . incentives . indexGoals ( { ...tokens } ) ;
197
197
198
- if ( ! resp || ! resp . ok ) {
198
+ if ( ! resp . ok ) {
199
199
return null ; // invalid accessToken _and_ refreshToken
200
200
}
201
201
@@ -316,7 +316,7 @@ export const getAssessmentOverviews = async (
316
316
tokens : Tokens
317
317
) : Promise < AssessmentOverview [ ] | null > => {
318
318
const resp = await Cadet . assessments . index ( { ...tokens } ) ;
319
- if ( ! resp || ! resp . ok ) {
319
+ if ( ! resp . ok ) {
320
320
return null ; // invalid accessToken _and_ refreshToken
321
321
}
322
322
const assessmentOverviews = resp . data ;
@@ -369,7 +369,7 @@ export const getAssessment = async (id: number, tokens: Tokens): Promise<Assessm
369
369
resp = await Cadet . assessments . unlock ( id , { password : input } , { ...tokens } ) ;
370
370
}
371
371
372
- if ( ! resp || ! resp . ok ) {
372
+ if ( ! resp . ok ) {
373
373
return null ;
374
374
}
375
375
@@ -420,17 +420,15 @@ export const postAnswer = async (
420
420
answer : string | number ,
421
421
tokens : Tokens
422
422
) : Promise < Response | null > => {
423
- const resp = await Cadet . answer . submit ( id , { answer } , tokens ) ;
423
+ const resp = await Cadet . answer . submit ( id , { answer } , { ... tokens } ) ;
424
424
return resp ;
425
425
} ;
426
426
427
427
/**
428
428
* POST /assessments/{assessmentId}/submit
429
429
*/
430
430
export const postAssessment = async ( id : number , tokens : Tokens ) : Promise < Response | null > => {
431
- const resp = await Cadet . assessments . submit ( id , tokens ) ;
432
- // shouldAutoLogout: false, // 400 if some questions unattempted
433
-
431
+ const resp = await Cadet . assessments . submit ( id , { ...tokens } ) ;
434
432
return resp ;
435
433
} ;
436
434
@@ -442,7 +440,7 @@ export const getGradingOverviews = async (
442
440
group : boolean
443
441
) : Promise < GradingOverview [ ] | null > => {
444
442
const resp = await Cadet . adminGrading . index ( { group } , { ...tokens } ) ;
445
- if ( ! resp ) {
443
+ if ( ! resp . ok ) {
446
444
return null ; // invalid accessToken _and_ refreshToken
447
445
}
448
446
const gradingOverviews = resp . data ;
@@ -495,7 +493,7 @@ export const getGradingOverviews = async (
495
493
export const getGrading = async ( submissionId : number , tokens : Tokens ) : Promise < Grading | null > => {
496
494
const resp = await Cadet . adminGrading . show ( submissionId , { ...tokens } ) ;
497
495
498
- if ( ! resp ) {
496
+ if ( ! resp . ok ) {
499
497
return null ;
500
498
}
501
499
@@ -608,7 +606,7 @@ export const getNotifications = async (tokens: Tokens): Promise<Notification[]>
608
606
609
607
let notifications : Notification [ ] = [ ] ;
610
608
611
- if ( ! resp || ! resp . ok ) {
609
+ if ( ! resp . ok ) {
612
610
return notifications ;
613
611
}
614
612
@@ -651,7 +649,7 @@ export const postAcknowledgeNotifications = async (
651
649
export const getSourcecastIndex = async ( tokens : Tokens ) : Promise < SourcecastData [ ] | null > => {
652
650
const resp = await Cadet . sourcecast . index ( { ...tokens } ) ;
653
651
// TODO
654
- return resp && resp . ok ? ( ( resp . data as unknown ) as SourcecastData [ ] ) : null ;
652
+ return resp . ok ? ( ( resp . data as unknown ) as SourcecastData [ ] ) : null ;
655
653
} ;
656
654
657
655
/**
@@ -733,15 +731,15 @@ export const uploadAssessment = async (
733
731
*/
734
732
export const getGradingSummary = async ( tokens : Tokens ) : Promise < GradingSummary | null > => {
735
733
const resp = await Cadet . adminGrading . gradingSummary ( { ...tokens } ) ;
736
- return resp && resp . ok ? resp . data : null ;
734
+ return resp . ok ? resp . data : null ;
737
735
} ;
738
736
739
737
/**
740
738
* GET /settings/sublanguage
741
739
*/
742
740
export const getSublanguage = async ( ) : Promise < SourceLanguage | null > => {
743
741
const resp = await Cadet . settings . index ( ) ;
744
- if ( ! resp || ! resp . ok ) {
742
+ if ( ! resp . ok ) {
745
743
return null ;
746
744
}
747
745
@@ -774,7 +772,7 @@ export const postSublanguage = async (
774
772
*/
775
773
export async function fetchDevices ( tokens : Tokens ) : Promise < Device [ ] | null > {
776
774
const resp = await Cadet . devices . index ( { ...tokens } ) ;
777
- return resp && resp . ok ? resp . data : null ;
775
+ return resp . ok ? resp . data : null ;
778
776
}
779
777
780
778
/**
@@ -785,7 +783,7 @@ export async function getDeviceWSEndpoint(
785
783
tokens : Tokens
786
784
) : Promise < WebSocketEndpointInformation | null > {
787
785
const resp = await Cadet . devices . getWsEndpoint ( device . id , { ...tokens } ) ;
788
- return resp && resp . ok ? resp . data : null ;
786
+ return resp . ok ? resp . data : null ;
789
787
}
790
788
791
789
/**
@@ -795,12 +793,8 @@ export async function registerDevice(device: Omit<Device, 'id'>, tokens?: Tokens
795
793
tokens = fillTokens ( tokens ) ;
796
794
const resp = await Cadet . devices . register ( device , { ...tokens } ) ;
797
795
798
- if ( ! resp ) {
799
- throw new Error ( 'Unknown error occurred.' ) ;
800
- }
801
-
802
796
if ( ! resp . ok ) {
803
- const message = resp . text ( ) ;
797
+ const message = await resp . text ( ) ;
804
798
throw new Error ( `Failed to register: ${ message } ` ) ;
805
799
}
806
800
@@ -817,10 +811,6 @@ export async function editDevice(
817
811
tokens = fillTokens ( tokens ) ;
818
812
const resp = await Cadet . devices . edit ( device . id , device , { ...tokens } ) ;
819
813
820
- if ( ! resp ) {
821
- throw new Error ( 'Unknown error occurred.' ) ;
822
- }
823
-
824
814
if ( ! resp . ok ) {
825
815
const message = await resp . text ( ) ;
826
816
throw new Error ( `Failed to edit: ${ message } ` ) ;
@@ -836,10 +826,6 @@ export async function deleteDevice(device: Pick<Device, 'id'>, tokens?: Tokens):
836
826
tokens = fillTokens ( tokens ) ;
837
827
const resp = await Cadet . devices . deregister ( device . id , { ...tokens } ) ;
838
828
839
- if ( ! resp ) {
840
- throw new Error ( 'Unknown error occurred.' ) ;
841
- }
842
-
843
829
if ( ! resp . ok ) {
844
830
const message = await resp . text ( ) ;
845
831
throw new Error ( `Failed to delete: ${ message } ` ) ;
0 commit comments