1
- import { QueryReturnValue } from "@reduxjs/toolkit/dist/query/baseQueryTypes"
2
1
import {
3
- BaseQueryApi ,
4
- BaseQueryFn ,
5
- FetchArgs ,
6
- FetchBaseQueryError ,
7
- FetchBaseQueryMeta ,
8
2
fetchBaseQuery ,
3
+ type BaseQueryApi ,
4
+ type BaseQueryFn ,
5
+ type FetchArgs ,
6
+ type FetchBaseQueryError ,
9
7
} from "@reduxjs/toolkit/query"
10
8
import Cookies from "js-cookie"
11
9
import qs from "qs"
@@ -18,11 +16,6 @@ export type FetchBaseQuery = BaseQueryFn<
18
16
unknown ,
19
17
FetchBaseQueryError
20
18
>
21
- export type Result = QueryReturnValue <
22
- unknown ,
23
- FetchBaseQueryError ,
24
- FetchBaseQueryMeta
25
- >
26
19
27
20
export const fetch = fetchBaseQuery ( {
28
21
baseUrl : API_BASE_URL ,
@@ -93,35 +86,30 @@ export async function injectCsrfToken(
93
86
}
94
87
}
95
88
96
- export function handleResponseError ( result : Result ) : void {
97
- // Check if errors.
98
- if ( result . error === undefined ) return
99
-
89
+ export function handleResponseError ( error : FetchBaseQueryError ) : void {
100
90
if (
101
- result . error . status === 400 &&
102
- typeof result . error . data === "object" &&
103
- result . error . data !== null
91
+ error . status === 400 &&
92
+ typeof error . data === "object" &&
93
+ error . data !== null
104
94
) {
105
95
// Parse the error's data from snake_case to camelCase.
106
- snakeCaseToCamelCase ( result . error . data )
107
- } else if ( result . error . status === 401 ) {
96
+ snakeCaseToCamelCase ( error . data )
97
+ } else if ( error . status === 401 ) {
108
98
// TODO: redirect to appropriate login page based on user type.
109
99
window . location . href = `${ PORTAL_BASE_URL } /login/teacher`
110
100
} else {
111
101
// Catch-all error pages by status-code.
112
102
window . location . href = `${ PORTAL_BASE_URL } /error/${
113
- [ 403 , 404 ] . includes ( result . error . status as number )
114
- ? result . error . status
115
- : 500
103
+ [ 403 , 404 ] . includes ( error . status as number ) ? error . status : 500
116
104
} `
117
105
}
118
106
}
119
107
120
- export function parseResponseBody ( result : Result ) : void {
108
+ export function parseResponseBody ( data : unknown ) : void {
121
109
// Parse the response's data from snake_case to camelCase.
122
- if ( typeof result . data !== "object" || result . data === null ) return
110
+ if ( typeof data !== "object" || data === null ) return
123
111
124
- snakeCaseToCamelCase ( result . data )
112
+ snakeCaseToCamelCase ( data )
125
113
}
126
114
127
115
const baseQuery : FetchBaseQuery = async ( args , api , extraOptions ) => {
@@ -132,9 +120,9 @@ const baseQuery: FetchBaseQuery = async (args, api, extraOptions) => {
132
120
// Send the HTTP request and fetch the response.
133
121
const result = await fetch ( args , api , extraOptions )
134
122
135
- handleResponseError ( result )
123
+ if ( result . error ) handleResponseError ( result . error )
136
124
137
- parseResponseBody ( result )
125
+ parseResponseBody ( result . data )
138
126
139
127
return result
140
128
}
0 commit comments