dce-reactkit 3.0.0-beta.23 → 3.0.0-beta.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +438 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/components/AppWrapper.d.ts +25 -0
- package/dist/cjs/types/helpers/genRouteHandler.d.ts +26 -0
- package/dist/cjs/types/helpers/handleError.d.ts +18 -0
- package/dist/cjs/types/helpers/handleSuccess.d.ts +8 -0
- package/dist/cjs/types/helpers/visitServerEndpoint.d.ts +23 -0
- package/dist/cjs/types/index.d.ts +6 -1
- package/dist/cjs/types/server/initServer.d.ts +21 -0
- package/dist/cjs/types/types/ParamType.d.ts +17 -0
- package/dist/cjs/types/types/ReactKitErrorCode.d.ts +3 -1
- package/dist/esm/index.js +433 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/components/AppWrapper.d.ts +25 -0
- package/dist/esm/types/helpers/genRouteHandler.d.ts +26 -0
- package/dist/esm/types/helpers/handleError.d.ts +18 -0
- package/dist/esm/types/helpers/handleSuccess.d.ts +8 -0
- package/dist/esm/types/helpers/visitServerEndpoint.d.ts +23 -0
- package/dist/esm/types/index.d.ts +6 -1
- package/dist/esm/types/server/initServer.d.ts +21 -0
- package/dist/esm/types/types/ParamType.d.ts +17 -0
- package/dist/esm/types/types/ReactKitErrorCode.d.ts +3 -1
- package/dist/index.d.ts +105 -2
- package/package.json +2 -1
- package/rollup.config.js +0 -1
- package/src/components/AppWrapper.tsx +69 -10
- package/src/helpers/genRouteHandler.ts +323 -0
- package/src/helpers/handleError.ts +65 -0
- package/src/helpers/handleSuccess.ts +18 -0
- package/src/helpers/visitServerEndpoint.tsx +110 -0
- package/src/index.ts +13 -0
- package/src/server/initServer.ts +53 -0
- package/src/types/ParamType.tsx +18 -0
- package/src/types/ReactKitErrorCode.tsx +3 -1
|
@@ -28,22 +28,70 @@ import ErrorWithCode from '../errors/ErrorWithCode';
|
|
|
28
28
|
type Props = {
|
|
29
29
|
// The entire app
|
|
30
30
|
children: React.ReactNode,
|
|
31
|
+
// Copy of CACCL's send request function
|
|
32
|
+
sendRequest: SendRequestFunction,
|
|
31
33
|
// True if this app is a dark-themed app
|
|
32
34
|
dark?: boolean,
|
|
33
35
|
// Custom session expired message
|
|
34
36
|
sessionExpiredMessage?: string,
|
|
35
37
|
};
|
|
36
38
|
|
|
39
|
+
// Type of CACCL's send request function
|
|
40
|
+
type SendRequestFunction = (
|
|
41
|
+
opts: {
|
|
42
|
+
path: string;
|
|
43
|
+
method: ('GET' | 'POST' | 'DELETE' | 'PUT');
|
|
44
|
+
params?: {
|
|
45
|
+
[x: string]: any;
|
|
46
|
+
} | undefined;
|
|
47
|
+
headers?: {
|
|
48
|
+
[x: string]: any;
|
|
49
|
+
} | undefined;
|
|
50
|
+
numRetries?: number | undefined;
|
|
51
|
+
},
|
|
52
|
+
) => Promise<{
|
|
53
|
+
body: any;
|
|
54
|
+
status: number;
|
|
55
|
+
headers: {
|
|
56
|
+
[x: string]: any;
|
|
57
|
+
};
|
|
58
|
+
}>;
|
|
59
|
+
|
|
37
60
|
/*------------------------------------------------------------------------*/
|
|
38
61
|
/* Static Helpers */
|
|
39
62
|
/*------------------------------------------------------------------------*/
|
|
40
63
|
|
|
64
|
+
/*----------------------------------------*/
|
|
65
|
+
/* Send Request */
|
|
66
|
+
/*----------------------------------------*/
|
|
67
|
+
|
|
68
|
+
// Store copy of caccl send request
|
|
69
|
+
let _cacclSendRequest: SendRequestFunction;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Send a request using caccl's send request feature
|
|
73
|
+
* @author Gabe Abrams
|
|
74
|
+
* @param opts send request options
|
|
75
|
+
* @returns send request response
|
|
76
|
+
*/
|
|
77
|
+
export const cacclSendRequest: SendRequestFunction = async (opts) => {
|
|
78
|
+
// Make sure send request has been passed in
|
|
79
|
+
if (!_cacclSendRequest) {
|
|
80
|
+
throw new ErrorWithCode(
|
|
81
|
+
'The request could not be sent because the AppWrapper component does not have a copy of sendRequest from CACCL.',
|
|
82
|
+
ReactKitErrorCode.NoCACCLSendRequestFunction,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return _cacclSendRequest(opts);
|
|
87
|
+
};
|
|
88
|
+
|
|
41
89
|
/*----------------------------------------*/
|
|
42
90
|
/* Alert */
|
|
43
91
|
/*----------------------------------------*/
|
|
44
92
|
|
|
45
93
|
// Stored copies of setters
|
|
46
|
-
let setAlertInfo: (info: { title: string, text: string }) => void;
|
|
94
|
+
let setAlertInfo: (info: undefined | { title: string, text: string }) => void;
|
|
47
95
|
let onAlertClosed: () => void;
|
|
48
96
|
|
|
49
97
|
/**
|
|
@@ -79,7 +127,7 @@ export const alert = async (title: string, text: string): Promise<undefined> =>
|
|
|
79
127
|
/*----------------------------------------*/
|
|
80
128
|
|
|
81
129
|
// Stored copies of setters
|
|
82
|
-
let setConfirmInfo: (info: { title: string, text: string }) => void;
|
|
130
|
+
let setConfirmInfo: (info: undefined | { title: string, text: string }) => void;
|
|
83
131
|
let onConfirmClosed: (confirmed: boolean) => void;
|
|
84
132
|
|
|
85
133
|
/**
|
|
@@ -190,10 +238,14 @@ const AppWrapper: React.FC<Props> = (props: Props): React.ReactElement => {
|
|
|
190
238
|
|
|
191
239
|
const {
|
|
192
240
|
children,
|
|
241
|
+
sendRequest,
|
|
193
242
|
dark,
|
|
194
243
|
sessionExpiredMessage = 'Your session has expired. Please go back to Canvas and start over.',
|
|
195
244
|
} = props;
|
|
196
245
|
|
|
246
|
+
// Store copy of send request
|
|
247
|
+
_cacclSendRequest = sendRequest;
|
|
248
|
+
|
|
197
249
|
/* -------------- State ------------- */
|
|
198
250
|
|
|
199
251
|
// Fatal error
|
|
@@ -217,20 +269,26 @@ const AppWrapper: React.FC<Props> = (props: Props): React.ReactElement => {
|
|
|
217
269
|
const [
|
|
218
270
|
alertInfo,
|
|
219
271
|
setAlertInfoInner,
|
|
220
|
-
] = useState<
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
272
|
+
] = useState<
|
|
273
|
+
undefined
|
|
274
|
+
| {
|
|
275
|
+
title: string,
|
|
276
|
+
text: string
|
|
277
|
+
}
|
|
278
|
+
>(undefined);
|
|
224
279
|
setAlertInfo = setAlertInfoInner;
|
|
225
280
|
|
|
226
281
|
// Confirm
|
|
227
282
|
const [
|
|
228
283
|
confirmInfo,
|
|
229
284
|
setConfirmInfoInner,
|
|
230
|
-
] = useState<
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
285
|
+
] = useState<
|
|
286
|
+
undefined
|
|
287
|
+
| {
|
|
288
|
+
title: string,
|
|
289
|
+
text: string
|
|
290
|
+
}
|
|
291
|
+
>(undefined);
|
|
234
292
|
setConfirmInfo = setConfirmInfoInner;
|
|
235
293
|
|
|
236
294
|
// Session expired
|
|
@@ -282,6 +340,7 @@ const AppWrapper: React.FC<Props> = (props: Props): React.ReactElement => {
|
|
|
282
340
|
if (onConfirmClosed) {
|
|
283
341
|
onConfirmClosed(buttonType === ModalButtonType.Okay);
|
|
284
342
|
}
|
|
343
|
+
setConfirmInfo(undefined);
|
|
285
344
|
}}
|
|
286
345
|
dontAllowBackdropExit
|
|
287
346
|
>
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// Import caccl functions
|
|
2
|
+
import { cacclGetLaunchInfo } from '../server/initServer';
|
|
3
|
+
|
|
4
|
+
// Import shared types
|
|
5
|
+
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
6
|
+
import ParamType from '../types/ParamType';
|
|
7
|
+
|
|
8
|
+
// Import helpers
|
|
9
|
+
import handleError from './handleError';
|
|
10
|
+
import handleSuccess from './handleSuccess';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generate an express API route handler
|
|
14
|
+
* @author Gabe Abrams
|
|
15
|
+
* @param params map containing parameters that are included in the request
|
|
16
|
+
* (map: param name => type)
|
|
17
|
+
* @param handler function that processes the request
|
|
18
|
+
* @returns express route handler that takes the following arguments:
|
|
19
|
+
* params (map: param name => value), handleSuccess (function for handling
|
|
20
|
+
* successful requests), handleError (function for handling failed requests),
|
|
21
|
+
* req (express request object), res (express response object)
|
|
22
|
+
*/
|
|
23
|
+
const genRouteHandler = (
|
|
24
|
+
params: {
|
|
25
|
+
[k: string]: ParamType
|
|
26
|
+
},
|
|
27
|
+
handler: (
|
|
28
|
+
opts: {
|
|
29
|
+
params: {
|
|
30
|
+
[k: string]: any
|
|
31
|
+
},
|
|
32
|
+
handleSuccess: (body: any) => void,
|
|
33
|
+
handleError: (error: any) => void,
|
|
34
|
+
req: any,
|
|
35
|
+
res: any,
|
|
36
|
+
},
|
|
37
|
+
) => void,
|
|
38
|
+
) => {
|
|
39
|
+
// Return a route handler
|
|
40
|
+
return async (req: any, res: any) => {
|
|
41
|
+
// Output params
|
|
42
|
+
const output: { [k in string]: any } = {};
|
|
43
|
+
|
|
44
|
+
/*----------------------------------------*/
|
|
45
|
+
/* Parse Params */
|
|
46
|
+
/*----------------------------------------*/
|
|
47
|
+
|
|
48
|
+
// Process items one by one
|
|
49
|
+
const paramList = Object.entries(params);
|
|
50
|
+
for (let i = 0; i < paramList.length; i++) {
|
|
51
|
+
const [name, type] = paramList[i];
|
|
52
|
+
|
|
53
|
+
// Find the value as a string
|
|
54
|
+
const value = (
|
|
55
|
+
req.params[name]
|
|
56
|
+
|| req.query[name]
|
|
57
|
+
|| req.body[name]
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// Parse
|
|
61
|
+
if (type === ParamType.Boolean || type === ParamType.BooleanOptional) {
|
|
62
|
+
// Boolean
|
|
63
|
+
|
|
64
|
+
// Handle case where value doesn't exist
|
|
65
|
+
if (value === undefined) {
|
|
66
|
+
if (type === ParamType.BooleanOptional) {
|
|
67
|
+
output[name] = undefined;
|
|
68
|
+
} else {
|
|
69
|
+
return handleError(
|
|
70
|
+
res,
|
|
71
|
+
{
|
|
72
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
73
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
74
|
+
status: 422,
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
// Value exists
|
|
80
|
+
|
|
81
|
+
// Simplify value
|
|
82
|
+
const simpleVal = (
|
|
83
|
+
String(value)
|
|
84
|
+
.trim()
|
|
85
|
+
.toLowerCase()
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
// Parse
|
|
89
|
+
output[name] = (
|
|
90
|
+
[
|
|
91
|
+
'true',
|
|
92
|
+
'yes',
|
|
93
|
+
'y',
|
|
94
|
+
'1',
|
|
95
|
+
't',
|
|
96
|
+
].indexOf(simpleVal) >= 0
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
} else if (type === ParamType.Float || type === ParamType.FloatOptional) {
|
|
100
|
+
// Float
|
|
101
|
+
|
|
102
|
+
// Handle case where value doesn't exist
|
|
103
|
+
if (value === undefined) {
|
|
104
|
+
if (type === ParamType.FloatOptional) {
|
|
105
|
+
output[name] = undefined;
|
|
106
|
+
} else {
|
|
107
|
+
return handleError(
|
|
108
|
+
res,
|
|
109
|
+
{
|
|
110
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
111
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
112
|
+
status: 422,
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
} else if (!Number.isNaN(Number.parseFloat(String(value)))) {
|
|
117
|
+
// Value is a number
|
|
118
|
+
output[name] = Number.parseFloat(String(value));
|
|
119
|
+
} else {
|
|
120
|
+
// Issue!
|
|
121
|
+
return handleError(
|
|
122
|
+
res,
|
|
123
|
+
{
|
|
124
|
+
message: `Request data was malformed: ${name} was not a valid float.`,
|
|
125
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
126
|
+
status: 422,
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
} else if (type === ParamType.Int || type === ParamType.IntOptional) {
|
|
131
|
+
// Int
|
|
132
|
+
|
|
133
|
+
// Handle case where value doesn't exist
|
|
134
|
+
if (value === undefined) {
|
|
135
|
+
if (type === ParamType.IntOptional) {
|
|
136
|
+
output[name] = undefined;
|
|
137
|
+
} else {
|
|
138
|
+
return handleError(
|
|
139
|
+
res,
|
|
140
|
+
{
|
|
141
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
142
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
143
|
+
status: 422,
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
} else if (!Number.isNaN(Number.parseInt(String(value), 10))) {
|
|
148
|
+
// Value is a number
|
|
149
|
+
output[name] = Number.parseInt(String(value), 10);
|
|
150
|
+
} else {
|
|
151
|
+
// Issue!
|
|
152
|
+
return handleError(
|
|
153
|
+
res,
|
|
154
|
+
{
|
|
155
|
+
message: `Request data was malformed: ${name} was not a valid int.`,
|
|
156
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
157
|
+
status: 422,
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
} else if (type === ParamType.JSON || type === ParamType.JSONOptional) {
|
|
162
|
+
// Stringified JSON
|
|
163
|
+
|
|
164
|
+
// Handle case where value doesn't exist
|
|
165
|
+
if (value === undefined) {
|
|
166
|
+
if (type === ParamType.JSONOptional) {
|
|
167
|
+
output[name] = undefined;
|
|
168
|
+
} else {
|
|
169
|
+
return handleError(
|
|
170
|
+
res,
|
|
171
|
+
{
|
|
172
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
173
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
174
|
+
status: 422,
|
|
175
|
+
},
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
// Value exists
|
|
180
|
+
|
|
181
|
+
// Parse
|
|
182
|
+
try {
|
|
183
|
+
output[name] = JSON.parse(String(value));
|
|
184
|
+
} catch (err) {
|
|
185
|
+
return handleError(
|
|
186
|
+
res,
|
|
187
|
+
{
|
|
188
|
+
message: `Request data was malformed: ${name} was not a valid JSON payload.`,
|
|
189
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
190
|
+
status: 422,
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
} else if (type === ParamType.String || type === ParamType.StringOptional) {
|
|
196
|
+
// String
|
|
197
|
+
|
|
198
|
+
// Handle case where value doesn't exist
|
|
199
|
+
if (value === undefined) {
|
|
200
|
+
if (type === ParamType.StringOptional) {
|
|
201
|
+
output[name] = undefined;
|
|
202
|
+
} else {
|
|
203
|
+
return handleError(
|
|
204
|
+
res,
|
|
205
|
+
{
|
|
206
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
207
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
208
|
+
status: 422,
|
|
209
|
+
},
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
// Value exists
|
|
214
|
+
|
|
215
|
+
// Leave as is
|
|
216
|
+
output[name] = value;
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
// No valid data type
|
|
220
|
+
return handleError(
|
|
221
|
+
res,
|
|
222
|
+
{
|
|
223
|
+
message: `An internal error occurred: we could not determine the type of ${name}.`,
|
|
224
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
225
|
+
status: 422,
|
|
226
|
+
},
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/*----------------------------------------*/
|
|
232
|
+
/* Launch Info */
|
|
233
|
+
/*----------------------------------------*/
|
|
234
|
+
|
|
235
|
+
// Get launch info
|
|
236
|
+
const { launched, launchInfo } = cacclGetLaunchInfo(req);
|
|
237
|
+
if (!launched || !launchInfo) {
|
|
238
|
+
return handleError(
|
|
239
|
+
res,
|
|
240
|
+
{
|
|
241
|
+
message: 'Your session has expired. Please refresh the page and try again.',
|
|
242
|
+
code: ReactKitErrorCode.SessionExpired,
|
|
243
|
+
status: 440,
|
|
244
|
+
},
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Error if user info cannot be found
|
|
249
|
+
if (
|
|
250
|
+
!launchInfo.userId
|
|
251
|
+
|| !launchInfo.userFirstName
|
|
252
|
+
|| !launchInfo.userLastName
|
|
253
|
+
|| (
|
|
254
|
+
launchInfo.notInCourse
|
|
255
|
+
&& !launchInfo.isAdmin
|
|
256
|
+
)
|
|
257
|
+
|| (
|
|
258
|
+
!launchInfo.isTTM
|
|
259
|
+
&& !launchInfo.isLearner
|
|
260
|
+
&& !launchInfo.isAdmin
|
|
261
|
+
)
|
|
262
|
+
) {
|
|
263
|
+
return handleError(
|
|
264
|
+
res,
|
|
265
|
+
{
|
|
266
|
+
message: 'Your session was invalid. Please refresh the page and try again.',
|
|
267
|
+
code: ReactKitErrorCode.SessionExpired,
|
|
268
|
+
status: 440,
|
|
269
|
+
},
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Add launch info to output
|
|
274
|
+
output.userId = launchInfo.userId;
|
|
275
|
+
output.userFirstName = launchInfo.userFirstName;
|
|
276
|
+
output.userLastName = launchInfo.userLastName;
|
|
277
|
+
output.isLearner = !!launchInfo.isLearner;
|
|
278
|
+
output.isTTM = !!launchInfo.isTTM;
|
|
279
|
+
output.isAdmin = !!launchInfo.isAdmin;
|
|
280
|
+
output.isWatchingInPrivate = !!(req.session.isWatchingInPrivate);
|
|
281
|
+
|
|
282
|
+
/*----------------------------------------*/
|
|
283
|
+
/* Require Course Consistency */
|
|
284
|
+
/*----------------------------------------*/
|
|
285
|
+
|
|
286
|
+
// Make sure the user actually launched from the appropriate course
|
|
287
|
+
if (
|
|
288
|
+
output.courseId
|
|
289
|
+
&& launchInfo.courseId
|
|
290
|
+
&& output.courseId !== launchInfo.courseId
|
|
291
|
+
&& !output.isTTM
|
|
292
|
+
&& !output.isAdmin
|
|
293
|
+
) {
|
|
294
|
+
// Course of interest is not the launch course
|
|
295
|
+
return handleError(
|
|
296
|
+
res,
|
|
297
|
+
{
|
|
298
|
+
message: 'You switched sessions by opening Immersive Classroom in another tab. Please refresh the page and try again.',
|
|
299
|
+
code: ReactKitErrorCode.WrongCourse,
|
|
300
|
+
status: 401,
|
|
301
|
+
},
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/*------------------------------------------------------------------------*/
|
|
306
|
+
/* Call handler */
|
|
307
|
+
/*------------------------------------------------------------------------*/
|
|
308
|
+
|
|
309
|
+
handler({
|
|
310
|
+
params: output,
|
|
311
|
+
handleSuccess: (body: any) => {
|
|
312
|
+
return handleSuccess(res, body);
|
|
313
|
+
},
|
|
314
|
+
handleError: (error: any) => {
|
|
315
|
+
return handleError(res, error);
|
|
316
|
+
},
|
|
317
|
+
req,
|
|
318
|
+
res,
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export default genRouteHandler;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Import shared types
|
|
2
|
+
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Handle an error and respond to the client
|
|
6
|
+
* @author Gabe Abrams
|
|
7
|
+
* @param res express response
|
|
8
|
+
* @param error error info
|
|
9
|
+
* @param opts.err the error to send to the client
|
|
10
|
+
* or the error message
|
|
11
|
+
* @param [opts.code] an error code (only used if err.code is not
|
|
12
|
+
* included)
|
|
13
|
+
* @param [opts.status=500] the https status code to use
|
|
14
|
+
* defined)
|
|
15
|
+
*/
|
|
16
|
+
const handleError = (
|
|
17
|
+
res: any,
|
|
18
|
+
error: (
|
|
19
|
+
| {
|
|
20
|
+
message: any,
|
|
21
|
+
code?: string,
|
|
22
|
+
status?: number,
|
|
23
|
+
}
|
|
24
|
+
| Error
|
|
25
|
+
| string
|
|
26
|
+
| any
|
|
27
|
+
),
|
|
28
|
+
): undefined => {
|
|
29
|
+
// Get the error message
|
|
30
|
+
let message;
|
|
31
|
+
if (error && (error as any).message) {
|
|
32
|
+
message = (error.message || 'An unknown error occurred.');
|
|
33
|
+
} else if (typeof error === 'string') {
|
|
34
|
+
message = (
|
|
35
|
+
error.trim().length > 0
|
|
36
|
+
? error
|
|
37
|
+
: 'An unknown error occurred.'
|
|
38
|
+
);
|
|
39
|
+
} else {
|
|
40
|
+
message = 'An unknown error occurred.';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Get the error code
|
|
44
|
+
const code = (error.code || ReactKitErrorCode.NoCode);
|
|
45
|
+
|
|
46
|
+
// Get the status code
|
|
47
|
+
const status = (error.status || 500);
|
|
48
|
+
|
|
49
|
+
// Respond to user
|
|
50
|
+
res
|
|
51
|
+
// Set the http status code
|
|
52
|
+
.status(status)
|
|
53
|
+
// Send a JSON response
|
|
54
|
+
.json({
|
|
55
|
+
// Error message
|
|
56
|
+
message,
|
|
57
|
+
// Error code
|
|
58
|
+
code,
|
|
59
|
+
// Success = false flag so client can detect server-side errors
|
|
60
|
+
success: false,
|
|
61
|
+
});
|
|
62
|
+
return undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export default handleError;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send successful API response
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
* @param res express response
|
|
5
|
+
* @param body the body of the response to send to the client
|
|
6
|
+
*/
|
|
7
|
+
const handleSuccess = (res: any, body: any): undefined => {
|
|
8
|
+
// Send a http 200 json response
|
|
9
|
+
res.json({
|
|
10
|
+
// Include the body as a parameter
|
|
11
|
+
body,
|
|
12
|
+
// Success = true flag so client can detect successful responses
|
|
13
|
+
success: true,
|
|
14
|
+
});
|
|
15
|
+
return undefined;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default handleSuccess;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Import custom error
|
|
2
|
+
import ErrorWithCode from '../errors/ErrorWithCode';
|
|
3
|
+
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
4
|
+
|
|
5
|
+
// Import helpers from app wrapper
|
|
6
|
+
import { cacclSendRequest } from '../components/AppWrapper';
|
|
7
|
+
|
|
8
|
+
/*------------------------------------------------------------------------*/
|
|
9
|
+
/* Listener */
|
|
10
|
+
/*------------------------------------------------------------------------*/
|
|
11
|
+
|
|
12
|
+
// Handler for session expiry
|
|
13
|
+
let sessionExpiryHandler: () => void;
|
|
14
|
+
|
|
15
|
+
// Keep track of whether or not session expiry has already been handled
|
|
16
|
+
let sessionAlreadyExpired = false;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Set the session expiry handler
|
|
20
|
+
* @author Gabe Abrams
|
|
21
|
+
* @param handler new handler to use when session expires
|
|
22
|
+
*/
|
|
23
|
+
export const setSessionExpiryHandler = (handler: () => void) => {
|
|
24
|
+
sessionExpiryHandler = handler;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/*------------------------------------------------------------------------*/
|
|
28
|
+
/* Main */
|
|
29
|
+
/*------------------------------------------------------------------------*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Visit an endpoint on the server [for client only]
|
|
33
|
+
* @author Gabe Abrams
|
|
34
|
+
* @param opts object containing all arguments
|
|
35
|
+
* @param opts.path - the path of the server endpoint
|
|
36
|
+
* @param [opts.method=GET] - the method of the endpoint
|
|
37
|
+
* @param [opts.params] - query/body parameters to include
|
|
38
|
+
* @returns response from server
|
|
39
|
+
*/
|
|
40
|
+
const visitServerEndpoint = async (
|
|
41
|
+
opts: {
|
|
42
|
+
path: string,
|
|
43
|
+
method?: ('GET' | 'POST' | 'DELETE' | 'PUT'),
|
|
44
|
+
params?: { [key in string]: any },
|
|
45
|
+
},
|
|
46
|
+
): Promise<any> => {
|
|
47
|
+
// Send the request
|
|
48
|
+
const response = await cacclSendRequest({
|
|
49
|
+
path: opts.path,
|
|
50
|
+
method: opts.method ?? 'GET',
|
|
51
|
+
params: opts.params,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Check for failure
|
|
55
|
+
if (!response || !response.body) {
|
|
56
|
+
throw new ErrorWithCode(
|
|
57
|
+
'We didn\'t get a response from the server. Please check your internet connection.',
|
|
58
|
+
ReactKitErrorCode.NoResponse,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
if (!response.body.success) {
|
|
62
|
+
// Session expired
|
|
63
|
+
if (response.body.code === ReactKitErrorCode.SessionExpired) {
|
|
64
|
+
// Skip notice if session was already expired
|
|
65
|
+
if (sessionAlreadyExpired) {
|
|
66
|
+
// Never return (browser is already reloading)
|
|
67
|
+
await new Promise<{ [key in string]: any }>(() => {
|
|
68
|
+
// Promise that never returns
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
sessionAlreadyExpired = true;
|
|
72
|
+
|
|
73
|
+
// Show session expiration message
|
|
74
|
+
if (sessionExpiryHandler) {
|
|
75
|
+
// Use handler
|
|
76
|
+
sessionExpiryHandler();
|
|
77
|
+
} else {
|
|
78
|
+
// Fallback to alert
|
|
79
|
+
|
|
80
|
+
// eslint-disable-next-line no-alert
|
|
81
|
+
alert('Your session has expired. Please start over.');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Never return (don't continue execution)
|
|
85
|
+
await new Promise<{ [key in string]: any }>(() => {
|
|
86
|
+
// Promise that never returns
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Other errors
|
|
91
|
+
throw new ErrorWithCode(
|
|
92
|
+
(
|
|
93
|
+
response.body.message
|
|
94
|
+
|| 'An unknown error occurred. Please contact an admin.'
|
|
95
|
+
),
|
|
96
|
+
(
|
|
97
|
+
response.body.code
|
|
98
|
+
|| ReactKitErrorCode.NoCode
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Success! Extract the body
|
|
104
|
+
const { body } = response.body;
|
|
105
|
+
|
|
106
|
+
// Return
|
|
107
|
+
return body;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export default visitServerEndpoint;
|
package/src/index.ts
CHANGED
|
@@ -19,6 +19,10 @@ import padZerosLeft from './helpers/padZerosLeft';
|
|
|
19
19
|
import roundToNumDecimals from './helpers/roundToNumDecimals';
|
|
20
20
|
import sum from './helpers/sum';
|
|
21
21
|
import waitMs from './helpers/waitMs';
|
|
22
|
+
import visitServerEndpoint from './helpers/visitServerEndpoint';
|
|
23
|
+
import genRouteHandler from './helpers/genRouteHandler';
|
|
24
|
+
import handleError from './helpers/handleError';
|
|
25
|
+
import handleSuccess from './helpers/handleSuccess';
|
|
22
26
|
|
|
23
27
|
// Import types
|
|
24
28
|
import ModalButtonType from './types/ModalButtonType';
|
|
@@ -26,6 +30,7 @@ import ModalSize from './types/ModalSize';
|
|
|
26
30
|
import ModalType from './types/ModalType';
|
|
27
31
|
import ReactKitErrorCode from './types/ReactKitErrorCode';
|
|
28
32
|
import Variant from './types/Variant';
|
|
33
|
+
import ParamType from './types/ParamType';
|
|
29
34
|
|
|
30
35
|
// Export each item
|
|
31
36
|
export {
|
|
@@ -52,10 +57,18 @@ export {
|
|
|
52
57
|
roundToNumDecimals,
|
|
53
58
|
sum,
|
|
54
59
|
waitMs,
|
|
60
|
+
// Client helpers
|
|
61
|
+
visitServerEndpoint,
|
|
62
|
+
// Server helpers
|
|
63
|
+
genRouteHandler,
|
|
64
|
+
handleError,
|
|
65
|
+
handleSuccess,
|
|
55
66
|
// Types
|
|
56
67
|
ModalButtonType,
|
|
57
68
|
ModalSize,
|
|
58
69
|
ModalType,
|
|
59
70
|
ReactKitErrorCode,
|
|
60
71
|
Variant,
|
|
72
|
+
// Server types
|
|
73
|
+
ParamType,
|
|
61
74
|
};
|