dce-reactkit 3.0.1 → 3.0.4
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/README.md +1 -546
- package/dist/cjs/index.js +1841 -0
- package/dist/cjs/index.js.map +1 -0
- package/{lib/esm → dist/cjs}/types/components/AppWrapper.d.ts +26 -1
- package/dist/cjs/types/components/ButtonInputGroup.d.ts +12 -0
- package/dist/cjs/types/components/CheckboxButton.d.ts +20 -0
- package/dist/cjs/types/components/Drawer.d.ts +10 -0
- package/{lib/cjs/lib → dist/cjs/types}/components/ErrorBox.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/components/LoadingSpinner.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/components/Modal.d.ts +0 -0
- package/dist/cjs/types/components/RadioButton.d.ts +20 -0
- package/dist/cjs/types/components/SimpleDateChooser.d.ts +22 -0
- package/{lib/esm/lib → dist/cjs/types}/components/TabBox.d.ts +1 -0
- package/{lib/cjs/lib → dist/cjs/types}/errors/ErrorWithCode.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/abbreviate.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/avg.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/ceilToNumDecimals.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/floorToNumDecimals.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/forceNumIntoBounds.d.ts +0 -0
- package/dist/cjs/types/helpers/genRouteHandler.d.ts +34 -0
- package/dist/cjs/types/helpers/getOrdinal.d.ts +8 -0
- package/dist/cjs/types/helpers/getTimeInfoInET.d.ts +17 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/handleError.d.ts +5 -1
- package/{lib/cjs/lib → dist/cjs/types}/helpers/handleSuccess.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/padDecimalZeros.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/padZerosLeft.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/roundToNumDecimals.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/sum.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/visitServerEndpoint.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/helpers/waitMs.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/index.d.ts +14 -6
- package/dist/cjs/types/server/initServer.d.ts +21 -0
- package/{lib/cjs/lib → dist/cjs/types}/types/ModalButtonType.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/types/ModalSize.d.ts +0 -0
- package/{lib/cjs/lib → dist/cjs/types}/types/ModalType.d.ts +0 -0
- package/{lib → dist}/cjs/types/types/ParamType.d.ts +1 -1
- package/{lib → dist}/cjs/types/types/ReactKitErrorCode.d.ts +3 -1
- package/{lib/cjs/lib → dist/cjs/types}/types/Variant.d.ts +0 -0
- package/dist/esm/index.js +1797 -0
- package/dist/esm/index.js.map +1 -0
- package/{lib/cjs → dist/esm}/types/components/AppWrapper.d.ts +26 -1
- package/dist/esm/types/components/ButtonInputGroup.d.ts +12 -0
- package/dist/esm/types/components/CheckboxButton.d.ts +20 -0
- package/dist/esm/types/components/Drawer.d.ts +10 -0
- package/{lib/cjs → dist/esm}/types/components/ErrorBox.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/components/LoadingSpinner.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/components/Modal.d.ts +0 -0
- package/dist/esm/types/components/RadioButton.d.ts +20 -0
- package/dist/esm/types/components/SimpleDateChooser.d.ts +22 -0
- package/{lib/cjs/lib → dist/esm/types}/components/TabBox.d.ts +1 -0
- package/{lib/cjs → dist/esm}/types/errors/ErrorWithCode.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/abbreviate.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/avg.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/ceilToNumDecimals.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/floorToNumDecimals.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/forceNumIntoBounds.d.ts +0 -0
- package/dist/esm/types/helpers/genRouteHandler.d.ts +34 -0
- package/dist/esm/types/helpers/getOrdinal.d.ts +8 -0
- package/dist/esm/types/helpers/getTimeInfoInET.d.ts +17 -0
- package/{lib/cjs → dist/esm}/types/helpers/handleError.d.ts +5 -1
- package/{lib/cjs → dist/esm}/types/helpers/handleSuccess.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/padDecimalZeros.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/padZerosLeft.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/roundToNumDecimals.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/sum.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/visitServerEndpoint.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/helpers/waitMs.d.ts +0 -0
- package/{lib/esm/lib → dist/esm/types}/index.d.ts +14 -6
- package/dist/esm/types/server/initServer.d.ts +21 -0
- package/{lib/cjs → dist/esm}/types/types/ModalButtonType.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/types/ModalSize.d.ts +0 -0
- package/{lib/cjs → dist/esm}/types/types/ModalType.d.ts +0 -0
- package/{lib/esm/lib → dist/esm/types}/types/ParamType.d.ts +1 -1
- package/{lib/esm/lib → dist/esm/types}/types/ReactKitErrorCode.d.ts +3 -1
- package/{lib/cjs → dist/esm}/types/types/Variant.d.ts +0 -0
- package/dist/index.d.ts +507 -0
- package/package.json +18 -29
- package/rollup.config.js +14 -13
- package/src/components/AppWrapper.tsx +74 -12
- package/src/components/ButtonInputGroup.tsx +89 -0
- package/src/components/CheckboxButton.tsx +100 -0
- package/src/components/Drawer.tsx +76 -0
- package/src/components/ErrorBox.tsx +4 -4
- package/src/components/LoadingSpinner.tsx +3 -3
- package/src/components/Modal.tsx +214 -97
- package/src/components/RadioButton.tsx +102 -0
- package/src/components/SimpleDateChooser.tsx +222 -0
- package/src/components/TabBox.tsx +65 -58
- package/src/helpers/genRouteHandler.ts +348 -0
- package/src/helpers/getOrdinal.tsx +13 -0
- package/src/helpers/getTimeInfoInET.tsx +56 -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 +29 -0
- package/src/server/initServer.ts +53 -0
- package/src/types/ParamType.ts +18 -0
- package/src/types/ReactKitErrorCode.tsx +3 -1
- package/tsconfig.json +9 -7
- package/lib/cjs/index.js +0 -2
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/lib/components/AppWrapper.d.ts +0 -35
- package/lib/cjs/lib/helpers/parseRequest.d.ts +0 -21
- package/lib/cjs/lib/types/ParamType.d.ts +0 -17
- package/lib/cjs/lib/types/ReactKitErrorCode.d.ts +0 -13
- package/lib/cjs/types/components/TabBox.d.ts +0 -11
- package/lib/cjs/types/helpers/alert.d.ts +0 -2
- package/lib/cjs/types/helpers/confirm.d.ts +0 -2
- package/lib/cjs/types/helpers/parseRequest.d.ts +0 -21
- package/lib/cjs/types/helpers/showFatalError.d.ts +0 -2
- package/lib/cjs/types/index.d.ts +0 -22
- package/lib/esm/index.js +0 -16
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/lib/components/AppWrapper.d.ts +0 -35
- package/lib/esm/lib/components/ErrorBox.d.ts +0 -12
- package/lib/esm/lib/components/LoadingSpinner.d.ts +0 -7
- package/lib/esm/lib/components/Modal.d.ts +0 -40
- package/lib/esm/lib/errors/ErrorWithCode.d.ts +0 -9
- package/lib/esm/lib/helpers/abbreviate.d.ts +0 -10
- package/lib/esm/lib/helpers/avg.d.ts +0 -8
- package/lib/esm/lib/helpers/ceilToNumDecimals.d.ts +0 -9
- package/lib/esm/lib/helpers/floorToNumDecimals.d.ts +0 -9
- package/lib/esm/lib/helpers/forceNumIntoBounds.d.ts +0 -10
- package/lib/esm/lib/helpers/handleError.d.ts +0 -14
- package/lib/esm/lib/helpers/handleSuccess.d.ts +0 -8
- package/lib/esm/lib/helpers/padDecimalZeros.d.ts +0 -10
- package/lib/esm/lib/helpers/padZerosLeft.d.ts +0 -9
- package/lib/esm/lib/helpers/parseRequest.d.ts +0 -21
- package/lib/esm/lib/helpers/roundToNumDecimals.d.ts +0 -9
- package/lib/esm/lib/helpers/sum.d.ts +0 -8
- package/lib/esm/lib/helpers/visitServerEndpoint.d.ts +0 -23
- package/lib/esm/lib/helpers/waitMs.d.ts +0 -7
- package/lib/esm/lib/types/ModalButtonType.d.ts +0 -17
- package/lib/esm/lib/types/ModalSize.d.ts +0 -11
- package/lib/esm/lib/types/ModalType.d.ts +0 -16
- package/lib/esm/lib/types/Variant.d.ts +0 -15
- package/lib/esm/types/components/ErrorBox.d.ts +0 -12
- package/lib/esm/types/components/LoadingSpinner.d.ts +0 -7
- package/lib/esm/types/components/Modal.d.ts +0 -40
- package/lib/esm/types/components/TabBox.d.ts +0 -11
- package/lib/esm/types/errors/ErrorWithCode.d.ts +0 -9
- package/lib/esm/types/helpers/abbreviate.d.ts +0 -10
- package/lib/esm/types/helpers/alert.d.ts +0 -2
- package/lib/esm/types/helpers/avg.d.ts +0 -8
- package/lib/esm/types/helpers/ceilToNumDecimals.d.ts +0 -9
- package/lib/esm/types/helpers/confirm.d.ts +0 -2
- package/lib/esm/types/helpers/floorToNumDecimals.d.ts +0 -9
- package/lib/esm/types/helpers/forceNumIntoBounds.d.ts +0 -10
- package/lib/esm/types/helpers/handleError.d.ts +0 -14
- package/lib/esm/types/helpers/handleSuccess.d.ts +0 -8
- package/lib/esm/types/helpers/padDecimalZeros.d.ts +0 -10
- package/lib/esm/types/helpers/padZerosLeft.d.ts +0 -9
- package/lib/esm/types/helpers/parseRequest.d.ts +0 -21
- package/lib/esm/types/helpers/roundToNumDecimals.d.ts +0 -9
- package/lib/esm/types/helpers/showFatalError.d.ts +0 -2
- package/lib/esm/types/helpers/sum.d.ts +0 -8
- package/lib/esm/types/helpers/visitServerEndpoint.d.ts +0 -23
- package/lib/esm/types/helpers/waitMs.d.ts +0 -7
- package/lib/esm/types/index.d.ts +0 -22
- package/lib/esm/types/types/ModalButtonType.d.ts +0 -17
- package/lib/esm/types/types/ModalSize.d.ts +0 -11
- package/lib/esm/types/types/ModalType.d.ts +0 -16
- package/lib/esm/types/types/ParamType.d.ts +0 -17
- package/lib/esm/types/types/ReactKitErrorCode.d.ts +0 -13
- package/lib/esm/types/types/Variant.d.ts +0 -15
- package/lib/index.d.ts +0 -274
|
@@ -0,0 +1,348 @@
|
|
|
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 opts object containing all arguments
|
|
16
|
+
* @param opts.paramTypes map containing the types for each parameter that is
|
|
17
|
+
* included in the request (map: param name => type)
|
|
18
|
+
* @param opts.handler function that processes the request
|
|
19
|
+
* @returns express route handler that takes the following arguments:
|
|
20
|
+
* params (map: param name => value), handleSuccess (function for handling
|
|
21
|
+
* successful requests), handleError (function for handling failed requests),
|
|
22
|
+
* req (express request object), res (express response object),
|
|
23
|
+
* next (express next function). Params also has userId, userFirstName,
|
|
24
|
+
* userLastName, isLearner, isTTM, isAdmin, and any other variables that
|
|
25
|
+
* are directly added to the session
|
|
26
|
+
*/
|
|
27
|
+
const genRouteHandler = (
|
|
28
|
+
opts: {
|
|
29
|
+
paramTypes?: {
|
|
30
|
+
[k: string]: ParamType
|
|
31
|
+
},
|
|
32
|
+
handler: (
|
|
33
|
+
opts: {
|
|
34
|
+
params: {
|
|
35
|
+
[k: string]: any
|
|
36
|
+
},
|
|
37
|
+
handleSuccess: (body: any) => void,
|
|
38
|
+
handleError: (error: any) => void,
|
|
39
|
+
req: any,
|
|
40
|
+
res: any,
|
|
41
|
+
next: () => void,
|
|
42
|
+
},
|
|
43
|
+
) => void,
|
|
44
|
+
},
|
|
45
|
+
) => {
|
|
46
|
+
// Return a route handler
|
|
47
|
+
return async (req: any, res: any, next: () => void) => {
|
|
48
|
+
// Output params
|
|
49
|
+
const output: { [k in string]: any } = {};
|
|
50
|
+
|
|
51
|
+
/*----------------------------------------*/
|
|
52
|
+
/* Parse Params */
|
|
53
|
+
/*----------------------------------------*/
|
|
54
|
+
|
|
55
|
+
// Process items one by one
|
|
56
|
+
const paramList = Object.entries(opts.paramTypes ?? {});
|
|
57
|
+
for (let i = 0; i < paramList.length; i++) {
|
|
58
|
+
const [name, type] = paramList[i];
|
|
59
|
+
|
|
60
|
+
// Find the value as a string
|
|
61
|
+
const value = (
|
|
62
|
+
req.params[name]
|
|
63
|
+
|| req.query[name]
|
|
64
|
+
|| req.body[name]
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// Parse
|
|
68
|
+
if (type === ParamType.Boolean || type === ParamType.BooleanOptional) {
|
|
69
|
+
// Boolean
|
|
70
|
+
|
|
71
|
+
// Handle case where value doesn't exist
|
|
72
|
+
if (value === undefined) {
|
|
73
|
+
if (type === ParamType.BooleanOptional) {
|
|
74
|
+
output[name] = undefined;
|
|
75
|
+
} else {
|
|
76
|
+
return handleError(
|
|
77
|
+
res,
|
|
78
|
+
{
|
|
79
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
80
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
81
|
+
status: 422,
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
// Value exists
|
|
87
|
+
|
|
88
|
+
// Simplify value
|
|
89
|
+
const simpleVal = (
|
|
90
|
+
String(value)
|
|
91
|
+
.trim()
|
|
92
|
+
.toLowerCase()
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// Parse
|
|
96
|
+
output[name] = (
|
|
97
|
+
[
|
|
98
|
+
'true',
|
|
99
|
+
'yes',
|
|
100
|
+
'y',
|
|
101
|
+
'1',
|
|
102
|
+
't',
|
|
103
|
+
].indexOf(simpleVal) >= 0
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
} else if (type === ParamType.Float || type === ParamType.FloatOptional) {
|
|
107
|
+
// Float
|
|
108
|
+
|
|
109
|
+
// Handle case where value doesn't exist
|
|
110
|
+
if (value === undefined) {
|
|
111
|
+
if (type === ParamType.FloatOptional) {
|
|
112
|
+
output[name] = undefined;
|
|
113
|
+
} else {
|
|
114
|
+
return handleError(
|
|
115
|
+
res,
|
|
116
|
+
{
|
|
117
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
118
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
119
|
+
status: 422,
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
} else if (!Number.isNaN(Number.parseFloat(String(value)))) {
|
|
124
|
+
// Value is a number
|
|
125
|
+
output[name] = Number.parseFloat(String(value));
|
|
126
|
+
} else {
|
|
127
|
+
// Issue!
|
|
128
|
+
return handleError(
|
|
129
|
+
res,
|
|
130
|
+
{
|
|
131
|
+
message: `Request data was malformed: ${name} was not a valid float.`,
|
|
132
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
133
|
+
status: 422,
|
|
134
|
+
},
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
} else if (type === ParamType.Int || type === ParamType.IntOptional) {
|
|
138
|
+
// Int
|
|
139
|
+
|
|
140
|
+
// Handle case where value doesn't exist
|
|
141
|
+
if (value === undefined) {
|
|
142
|
+
if (type === ParamType.IntOptional) {
|
|
143
|
+
output[name] = undefined;
|
|
144
|
+
} else {
|
|
145
|
+
return handleError(
|
|
146
|
+
res,
|
|
147
|
+
{
|
|
148
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
149
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
150
|
+
status: 422,
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
} else if (!Number.isNaN(Number.parseInt(String(value), 10))) {
|
|
155
|
+
// Value is a number
|
|
156
|
+
output[name] = Number.parseInt(String(value), 10);
|
|
157
|
+
} else {
|
|
158
|
+
// Issue!
|
|
159
|
+
return handleError(
|
|
160
|
+
res,
|
|
161
|
+
{
|
|
162
|
+
message: `Request data was malformed: ${name} was not a valid int.`,
|
|
163
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
164
|
+
status: 422,
|
|
165
|
+
},
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
} else if (type === ParamType.JSON || type === ParamType.JSONOptional) {
|
|
169
|
+
// Stringified JSON
|
|
170
|
+
|
|
171
|
+
// Handle case where value doesn't exist
|
|
172
|
+
if (value === undefined) {
|
|
173
|
+
if (type === ParamType.JSONOptional) {
|
|
174
|
+
output[name] = undefined;
|
|
175
|
+
} else {
|
|
176
|
+
return handleError(
|
|
177
|
+
res,
|
|
178
|
+
{
|
|
179
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
180
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
181
|
+
status: 422,
|
|
182
|
+
},
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
// Value exists
|
|
187
|
+
|
|
188
|
+
// Parse
|
|
189
|
+
try {
|
|
190
|
+
output[name] = JSON.parse(String(value));
|
|
191
|
+
} catch (err) {
|
|
192
|
+
return handleError(
|
|
193
|
+
res,
|
|
194
|
+
{
|
|
195
|
+
message: `Request data was malformed: ${name} was not a valid JSON payload.`,
|
|
196
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
197
|
+
status: 422,
|
|
198
|
+
},
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
} else if (type === ParamType.String || type === ParamType.StringOptional) {
|
|
203
|
+
// String
|
|
204
|
+
|
|
205
|
+
// Handle case where value doesn't exist
|
|
206
|
+
if (value === undefined) {
|
|
207
|
+
if (type === ParamType.StringOptional) {
|
|
208
|
+
output[name] = undefined;
|
|
209
|
+
} else {
|
|
210
|
+
return handleError(
|
|
211
|
+
res,
|
|
212
|
+
{
|
|
213
|
+
message: `Parameter ${name} is required, but it was not included.`,
|
|
214
|
+
code: ReactKitErrorCode.MissingParameter,
|
|
215
|
+
status: 422,
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
} else {
|
|
220
|
+
// Value exists
|
|
221
|
+
|
|
222
|
+
// Leave as is
|
|
223
|
+
output[name] = value;
|
|
224
|
+
}
|
|
225
|
+
} else {
|
|
226
|
+
// No valid data type
|
|
227
|
+
return handleError(
|
|
228
|
+
res,
|
|
229
|
+
{
|
|
230
|
+
message: `An internal error occurred: we could not determine the type of ${name}.`,
|
|
231
|
+
code: ReactKitErrorCode.InvalidParameter,
|
|
232
|
+
status: 422,
|
|
233
|
+
},
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/*----------------------------------------*/
|
|
239
|
+
/* Launch Info */
|
|
240
|
+
/*----------------------------------------*/
|
|
241
|
+
|
|
242
|
+
// Get launch info
|
|
243
|
+
const { launched, launchInfo } = cacclGetLaunchInfo(req);
|
|
244
|
+
if (!launched || !launchInfo) {
|
|
245
|
+
return handleError(
|
|
246
|
+
res,
|
|
247
|
+
{
|
|
248
|
+
message: 'Your session has expired. Please refresh the page and try again.',
|
|
249
|
+
code: ReactKitErrorCode.SessionExpired,
|
|
250
|
+
status: 440,
|
|
251
|
+
},
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Error if user info cannot be found
|
|
256
|
+
if (
|
|
257
|
+
!launchInfo.userId
|
|
258
|
+
|| !launchInfo.userFirstName
|
|
259
|
+
|| !launchInfo.userLastName
|
|
260
|
+
|| (
|
|
261
|
+
launchInfo.notInCourse
|
|
262
|
+
&& !launchInfo.isAdmin
|
|
263
|
+
)
|
|
264
|
+
|| (
|
|
265
|
+
!launchInfo.isTTM
|
|
266
|
+
&& !launchInfo.isLearner
|
|
267
|
+
&& !launchInfo.isAdmin
|
|
268
|
+
)
|
|
269
|
+
) {
|
|
270
|
+
return handleError(
|
|
271
|
+
res,
|
|
272
|
+
{
|
|
273
|
+
message: 'Your session was invalid. Please refresh the page and try again.',
|
|
274
|
+
code: ReactKitErrorCode.SessionExpired,
|
|
275
|
+
status: 440,
|
|
276
|
+
},
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Add launch info to output
|
|
281
|
+
output.userId = launchInfo.userId;
|
|
282
|
+
output.userFirstName = launchInfo.userFirstName;
|
|
283
|
+
output.userLastName = launchInfo.userLastName;
|
|
284
|
+
output.isLearner = !!launchInfo.isLearner;
|
|
285
|
+
output.isTTM = !!launchInfo.isTTM;
|
|
286
|
+
output.isAdmin = !!launchInfo.isAdmin;
|
|
287
|
+
|
|
288
|
+
// Add other session variables
|
|
289
|
+
Object.keys(req.session).forEach((propName) => {
|
|
290
|
+
// Skip if prop already in output
|
|
291
|
+
if (output[propName] !== undefined) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Add to output
|
|
296
|
+
const value = req.session[propName];
|
|
297
|
+
if (
|
|
298
|
+
typeof value === 'string'
|
|
299
|
+
|| typeof value === 'boolean'
|
|
300
|
+
|| typeof value === 'number'
|
|
301
|
+
) {
|
|
302
|
+
output[propName] = value;
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
/*----------------------------------------*/
|
|
307
|
+
/* Require Course Consistency */
|
|
308
|
+
/*----------------------------------------*/
|
|
309
|
+
|
|
310
|
+
// Make sure the user actually launched from the appropriate course
|
|
311
|
+
if (
|
|
312
|
+
output.courseId
|
|
313
|
+
&& launchInfo.courseId
|
|
314
|
+
&& output.courseId !== launchInfo.courseId
|
|
315
|
+
&& !output.isTTM
|
|
316
|
+
&& !output.isAdmin
|
|
317
|
+
) {
|
|
318
|
+
// Course of interest is not the launch course
|
|
319
|
+
return handleError(
|
|
320
|
+
res,
|
|
321
|
+
{
|
|
322
|
+
message: 'You switched sessions by opening Immersive Classroom in another tab. Please refresh the page and try again.',
|
|
323
|
+
code: ReactKitErrorCode.WrongCourse,
|
|
324
|
+
status: 401,
|
|
325
|
+
},
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/*------------------------------------------------------------------------*/
|
|
330
|
+
/* Call handler */
|
|
331
|
+
/*------------------------------------------------------------------------*/
|
|
332
|
+
|
|
333
|
+
opts.handler({
|
|
334
|
+
params: output,
|
|
335
|
+
handleSuccess: (body: any) => {
|
|
336
|
+
return handleSuccess(res, body);
|
|
337
|
+
},
|
|
338
|
+
handleError: (error: any) => {
|
|
339
|
+
return handleError(res, error);
|
|
340
|
+
},
|
|
341
|
+
req,
|
|
342
|
+
res,
|
|
343
|
+
next,
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
export default genRouteHandler;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const ORDINALS = ['th', 'st', 'nd', 'rd'];
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get a number's ordinal
|
|
5
|
+
* @author Gabe Abrams
|
|
6
|
+
* @param num the number being analyzed
|
|
7
|
+
* @returns ordinal
|
|
8
|
+
*/
|
|
9
|
+
const getOrdinal = (num: number): string => {
|
|
10
|
+
return (ORDINALS[(num - 20) % 10] ?? ORDINALS[num] ?? ORDINALS[0]);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default getOrdinal;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get current time info in US Boston Eastern Time, independent of machine
|
|
3
|
+
* timezone
|
|
4
|
+
* @author Gabe Abrams
|
|
5
|
+
* @param {Date} [date=now] the date to get info on
|
|
6
|
+
* @returns object with timestamp (ms since epoch) and numbers
|
|
7
|
+
* corresponding to ET time values for year, month, day, hour, minute
|
|
8
|
+
*/
|
|
9
|
+
const getTimeInfoInET = (date?: Date): {
|
|
10
|
+
timestamp: number,
|
|
11
|
+
year: number,
|
|
12
|
+
month: number,
|
|
13
|
+
day: number,
|
|
14
|
+
hour: number,
|
|
15
|
+
minute: number,
|
|
16
|
+
} => {
|
|
17
|
+
// Create a time string
|
|
18
|
+
const d = (date || new Date());
|
|
19
|
+
const str = d.toLocaleString(
|
|
20
|
+
'en-US', // Using US encoding (it's the only one installed on containers)
|
|
21
|
+
{ timeZone: 'America/New_York' } // Force EST timezone
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
// Parse the string for the date/time info
|
|
25
|
+
const [dateStr, timeStr] = str.split(', '); // Format: MM/DD/YYYY, HH:MM:SS AM
|
|
26
|
+
const [monthStr, dayStr, yearStr] = dateStr.split('/'); // Format: MM/DD/YYYY
|
|
27
|
+
const [hourStr, minStr, ending] = timeStr.split(':'); // Format: HH:MM:SS AM
|
|
28
|
+
|
|
29
|
+
// Create all time numbers
|
|
30
|
+
const timestamp = d.getTime();
|
|
31
|
+
const year = Number.parseInt(yearStr, 10);
|
|
32
|
+
const month = Number.parseInt(monthStr, 10);
|
|
33
|
+
const day = Number.parseInt(dayStr, 10);
|
|
34
|
+
const minute = Number.parseInt(minStr, 10);
|
|
35
|
+
let hour = Number.parseInt(hourStr, 10);
|
|
36
|
+
// Convert from am/pm to 24hr
|
|
37
|
+
const isAM = ending.toLowerCase().includes('am');
|
|
38
|
+
const isPM = !isAM;
|
|
39
|
+
if (isPM && hour !== 12) {
|
|
40
|
+
hour += 12;
|
|
41
|
+
} else if (isAM && hour === 12) {
|
|
42
|
+
hour = 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Return
|
|
46
|
+
return {
|
|
47
|
+
timestamp,
|
|
48
|
+
year,
|
|
49
|
+
month,
|
|
50
|
+
day,
|
|
51
|
+
hour,
|
|
52
|
+
minute,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export default getTimeInfoInET;
|
|
@@ -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
|
@@ -4,6 +4,11 @@ import LoadingSpinner from './components/LoadingSpinner';
|
|
|
4
4
|
import ErrorBox from './components/ErrorBox';
|
|
5
5
|
import Modal from './components/Modal';
|
|
6
6
|
import TabBox from './components/TabBox';
|
|
7
|
+
import RadioButton from './components/RadioButton';
|
|
8
|
+
import CheckboxButton from './components/CheckboxButton';
|
|
9
|
+
import ButtonInputGroup from './components/ButtonInputGroup';
|
|
10
|
+
import SimpleDateChooser from './components/SimpleDateChooser';
|
|
11
|
+
import Drawer from './components/Drawer';
|
|
7
12
|
|
|
8
13
|
// Import errors
|
|
9
14
|
import ErrorWithCode from './errors/ErrorWithCode';
|
|
@@ -19,6 +24,13 @@ import padZerosLeft from './helpers/padZerosLeft';
|
|
|
19
24
|
import roundToNumDecimals from './helpers/roundToNumDecimals';
|
|
20
25
|
import sum from './helpers/sum';
|
|
21
26
|
import waitMs from './helpers/waitMs';
|
|
27
|
+
import visitServerEndpoint from './helpers/visitServerEndpoint';
|
|
28
|
+
import genRouteHandler from './helpers/genRouteHandler';
|
|
29
|
+
import handleError from './helpers/handleError';
|
|
30
|
+
import handleSuccess from './helpers/handleSuccess';
|
|
31
|
+
import initServer from './server/initServer';
|
|
32
|
+
import getOrdinal from './helpers/getOrdinal';
|
|
33
|
+
import getTimeInfoInET from './helpers/getTimeInfoInET';
|
|
22
34
|
|
|
23
35
|
// Import types
|
|
24
36
|
import ModalButtonType from './types/ModalButtonType';
|
|
@@ -26,6 +38,7 @@ import ModalSize from './types/ModalSize';
|
|
|
26
38
|
import ModalType from './types/ModalType';
|
|
27
39
|
import ReactKitErrorCode from './types/ReactKitErrorCode';
|
|
28
40
|
import Variant from './types/Variant';
|
|
41
|
+
import ParamType from './types/ParamType';
|
|
29
42
|
|
|
30
43
|
// Export each item
|
|
31
44
|
export {
|
|
@@ -35,6 +48,11 @@ export {
|
|
|
35
48
|
ErrorBox,
|
|
36
49
|
Modal,
|
|
37
50
|
TabBox,
|
|
51
|
+
RadioButton,
|
|
52
|
+
CheckboxButton,
|
|
53
|
+
ButtonInputGroup,
|
|
54
|
+
SimpleDateChooser,
|
|
55
|
+
Drawer,
|
|
38
56
|
// Global functions
|
|
39
57
|
alert,
|
|
40
58
|
confirm,
|
|
@@ -52,10 +70,21 @@ export {
|
|
|
52
70
|
roundToNumDecimals,
|
|
53
71
|
sum,
|
|
54
72
|
waitMs,
|
|
73
|
+
getOrdinal,
|
|
74
|
+
getTimeInfoInET,
|
|
75
|
+
// Client helpers
|
|
76
|
+
visitServerEndpoint,
|
|
77
|
+
// Server helpers
|
|
78
|
+
initServer,
|
|
79
|
+
genRouteHandler,
|
|
80
|
+
handleError,
|
|
81
|
+
handleSuccess,
|
|
55
82
|
// Types
|
|
56
83
|
ModalButtonType,
|
|
57
84
|
ModalSize,
|
|
58
85
|
ModalType,
|
|
59
86
|
ReactKitErrorCode,
|
|
60
87
|
Variant,
|
|
88
|
+
// Server types
|
|
89
|
+
ParamType,
|
|
61
90
|
};
|