Skip to content

Commit 1b12cb5

Browse files
committed
format
1 parent cdbdc53 commit 1b12cb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2586
-2239
lines changed

src/api/baseQuery.ts

+65-68
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,49 @@
1-
import {
2-
QueryReturnValue
3-
} from '@reduxjs/toolkit/dist/query/baseQueryTypes';
1+
import { QueryReturnValue } from "@reduxjs/toolkit/dist/query/baseQueryTypes"
42
import {
53
BaseQueryApi,
64
BaseQueryFn,
75
FetchArgs,
86
FetchBaseQueryError,
97
FetchBaseQueryMeta,
10-
fetchBaseQuery
11-
} from '@reduxjs/toolkit/query';
12-
import Cookies from 'js-cookie';
13-
import qs from 'qs';
8+
fetchBaseQuery,
9+
} from "@reduxjs/toolkit/query"
10+
import Cookies from "js-cookie"
11+
import qs from "qs"
1412

15-
import {
16-
API_BASE_URL,
17-
PORTAL_BASE_URL,
18-
SERVICE_NAME
19-
} from '../env';
20-
import {
21-
camelCaseToSnakeCase,
22-
snakeCaseToCamelCase
23-
} from '../helpers/general';
13+
import { API_BASE_URL, PORTAL_BASE_URL, SERVICE_NAME } from "../env"
14+
import { camelCaseToSnakeCase, snakeCaseToCamelCase } from "../helpers/general"
2415

2516
export type FetchBaseQuery = BaseQueryFn<
2617
FetchArgs,
2718
unknown,
2819
FetchBaseQueryError
29-
>;
20+
>
3021
export type Result = QueryReturnValue<
3122
unknown,
3223
FetchBaseQueryError,
3324
FetchBaseQueryMeta
34-
>;
25+
>
3526

3627
export const fetch = fetchBaseQuery({
3728
baseUrl: API_BASE_URL,
38-
credentials: 'include'
39-
});
29+
credentials: "include",
30+
})
4031

4132
export function parseRequestBody(args: FetchArgs): void {
4233
// Check if the request has a body and its content type is specified.
43-
if (typeof args.body !== 'object' || args.body === null) return;
34+
if (typeof args.body !== "object" || args.body === null) return
4435

45-
camelCaseToSnakeCase(args.body);
36+
camelCaseToSnakeCase(args.body)
4637

47-
if (args.headers !== undefined && 'Content-Type' in args.headers) {
38+
if (args.headers !== undefined && "Content-Type" in args.headers) {
4839
// Stringify the request body based on its content type.
49-
switch (args.headers['Content-Type']) {
50-
case 'application/x-www-form-urlencoded':
51-
args.body = qs.stringify(args.body);
52-
break;
53-
case 'application/json':
54-
args.body = JSON.stringify(args.body);
55-
break;
40+
switch (args.headers["Content-Type"]) {
41+
case "application/x-www-form-urlencoded":
42+
args.body = qs.stringify(args.body)
43+
break
44+
case "application/json":
45+
args.body = JSON.stringify(args.body)
46+
break
5647
}
5748
}
5849
}
@@ -61,85 +52,91 @@ export async function injectCsrfToken(
6152
fetch: FetchBaseQuery,
6253
args: FetchArgs,
6354
api: BaseQueryApi,
64-
serviceName: string = SERVICE_NAME
55+
serviceName: string = SERVICE_NAME,
6556
): Promise<void> {
6657
// Check if the request method is safe.
6758
// https://datatracker.ietf.org/doc/html/rfc9110.html#section-9.2.1
68-
if (args.method !== undefined &&
69-
['GET', 'HEAD', 'OPTIONS', 'TRACE'].includes(args.method)
70-
) return;
59+
if (
60+
args.method !== undefined &&
61+
["GET", "HEAD", "OPTIONS", "TRACE"].includes(args.method)
62+
)
63+
return
7164

7265
// https://docs.djangoproject.com/en/3.2/ref/csrf/
73-
const cookieName = `${serviceName}_csrftoken`;
74-
let csrfToken = Cookies.get(cookieName);
66+
const cookieName = `${serviceName}_csrftoken`
67+
let csrfToken = Cookies.get(cookieName)
7568
if (csrfToken === undefined) {
7669
// Get the CSRF token.
77-
const { error } = await fetch({
78-
url: 'csrf/cookie/',
79-
method: 'GET'
80-
}, api, {});
70+
const { error } = await fetch(
71+
{
72+
url: "csrf/cookie/",
73+
method: "GET",
74+
},
75+
api,
76+
{},
77+
)
8178

8279
// Validate we got the CSRF token.
8380
if (error !== undefined) {
84-
window.location.href = `${PORTAL_BASE_URL}/error/500`;
81+
window.location.href = `${PORTAL_BASE_URL}/error/500`
8582
}
86-
csrfToken = Cookies.get(cookieName);
83+
csrfToken = Cookies.get(cookieName)
8784
if (csrfToken === undefined) {
88-
window.location.href = `${PORTAL_BASE_URL}/error/500`;
85+
window.location.href = `${PORTAL_BASE_URL}/error/500`
8986
}
90-
};
87+
}
9188

9289
// Inject the CSRF token.
9390
args.body = {
94-
...(typeof args.body !== 'object' || args.body === null ? {} : args.body),
95-
csrfmiddlewaretoken: csrfToken
96-
};
91+
...(typeof args.body !== "object" || args.body === null ? {} : args.body),
92+
csrfmiddlewaretoken: csrfToken,
93+
}
9794
}
9895

9996
export function handleResponseError(result: Result): void {
10097
// Check if errors.
101-
if (result.error === undefined) return;
98+
if (result.error === undefined) return
10299

103-
if (result.error.status === 400 &&
104-
typeof result.error.data === 'object' &&
100+
if (
101+
result.error.status === 400 &&
102+
typeof result.error.data === "object" &&
105103
result.error.data !== null
106104
) {
107105
// Parse the error's data from snake_case to camelCase.
108-
snakeCaseToCamelCase(result.error.data);
106+
snakeCaseToCamelCase(result.error.data)
109107
} else if (result.error.status === 401) {
110108
// TODO: redirect to appropriate login page based on user type.
111-
window.location.href = `${PORTAL_BASE_URL}/login/teacher`;
109+
window.location.href = `${PORTAL_BASE_URL}/login/teacher`
112110
} else {
113111
// Catch-all error pages by status-code.
114-
window.location.href = `${PORTAL_BASE_URL}/error/${[
115-
403,
116-
404
117-
].includes(result.error.status as number)
118-
? result.error.status
119-
: 500}`;
112+
window.location.href = `${PORTAL_BASE_URL}/error/${
113+
[403, 404].includes(result.error.status as number)
114+
? result.error.status
115+
: 500
116+
}`
120117
}
121118
}
122119

123120
export function parseResponseBody(result: Result): void {
124121
// Parse the response's data from snake_case to camelCase.
125-
if (typeof result.data !== 'object' || result.data === null) return;
122+
if (typeof result.data !== "object" || result.data === null) return
126123

127-
snakeCaseToCamelCase(result.data);
124+
snakeCaseToCamelCase(result.data)
128125
}
129126

130127
const baseQuery: FetchBaseQuery = async (args, api, extraOptions) => {
131-
await injectCsrfToken(fetch, args, api);
128+
await injectCsrfToken(fetch, args, api)
132129

133-
parseRequestBody(args);
130+
parseRequestBody(args)
134131

135132
// Send the HTTP request and fetch the response.
136-
const result = await fetch(args, api, extraOptions);
133+
const result = await fetch(args, api, extraOptions)
137134

138-
handleResponseError(result);
135+
handleResponseError(result)
139136

140-
parseResponseBody(result);
137+
parseResponseBody(result)
141138

142-
return result;
143-
};
139+
return result
140+
}
144141

145-
export default baseQuery;
142+
export default baseQuery

src/api/endpoints.ts

+14-24
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import {
2-
EndpointBuilder
3-
} from '@reduxjs/toolkit/dist/query/endpointDefinitions';
4-
import {
5-
MutationDefinition
6-
} from '@reduxjs/toolkit/query/react';
1+
import { EndpointBuilder } from "@reduxjs/toolkit/dist/query/endpointDefinitions"
2+
import { MutationDefinition } from "@reduxjs/toolkit/query/react"
73

8-
import { FetchBaseQuery } from './baseQuery';
9-
import { TagTypes } from './tagTypes';
4+
import { FetchBaseQuery } from "./baseQuery"
5+
import { TagTypes } from "./tagTypes"
106

11-
export type LogoutQuery = null;
12-
export type LogoutResult = null;
7+
export type LogoutQuery = null
8+
export type LogoutResult = null
139

14-
export default function endpoints<
15-
ReducerPath extends string
16-
>(
17-
build: EndpointBuilder<FetchBaseQuery, any, ReducerPath>
10+
export default function endpoints<ReducerPath extends string>(
11+
build: EndpointBuilder<FetchBaseQuery, any, ReducerPath>,
1812
): {
1913
logout: MutationDefinition<
2014
LogoutQuery,
@@ -24,19 +18,15 @@ export default function endpoints<
2418
ReducerPath
2519
>
2620
} {
27-
const _build = build as EndpointBuilder<
28-
FetchBaseQuery,
29-
TagTypes,
30-
ReducerPath
31-
>;
21+
const _build = build as EndpointBuilder<FetchBaseQuery, TagTypes, ReducerPath>
3222

3323
return {
3424
logout: _build.mutation<LogoutResult, LogoutQuery>({
3525
query: () => ({
36-
url: 'session/logout/',
37-
method: 'GET'
26+
url: "session/logout/",
27+
method: "GET",
3828
}),
39-
invalidatesTags: ['private']
40-
})
41-
};
29+
invalidatesTags: ["private"],
30+
}),
31+
}
4232
}

src/api/index.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import baseQuery from './baseQuery';
2-
import endpoints from './endpoints';
3-
import tagTypes from './tagTypes';
1+
import baseQuery from "./baseQuery"
2+
import endpoints from "./endpoints"
3+
import tagTypes from "./tagTypes"
44

5-
export {
6-
baseQuery,
7-
endpoints,
8-
tagTypes
9-
};
5+
export { baseQuery, endpoints, tagTypes }

src/api/tagTypes.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
const tagTypes = [
22
// A special tag used to mark data as private.
33
// Private data will be invalidated on logout.
4-
'private',
4+
"private",
55

66
// These are the tags for the common models used throughout our system.
77
// https://github.com/ocadotechnology/codeforlife-package-python/tree/main/codeforlife/user/models
8-
'user',
9-
'school',
10-
'class',
11-
'teacher',
12-
'student'
13-
] as const;
8+
"user",
9+
"school",
10+
"class",
11+
"teacher",
12+
"student",
13+
] as const
1414

15-
export default tagTypes;
16-
export type TagTypes = typeof tagTypes[number];
15+
export default tagTypes
16+
export type TagTypes = (typeof tagTypes)[number]

0 commit comments

Comments
 (0)