dce-reactkit 3.9.2 → 3.9.4-beta.1

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.
@@ -44,6 +44,30 @@ export declare const confirm: (title: string, text: string, opts?: {
44
44
  cancelButtonText?: string;
45
45
  cancelButtonVariant?: Variant;
46
46
  }) => Promise<boolean>;
47
+ /**
48
+ * Show a prompt modal asking the user for input
49
+ * @author Yuen Ler Chow
50
+ * @param title the title text to display at the top of the prompt
51
+ * @param text the text to display in the prompt
52
+ * @param [opts={}] additional options for the prompt dialog
53
+ * @param [opts.placeholder] the placeholder text for the input field
54
+ * @param [opts.defaultText] the default text for the input field
55
+ * @param [opts.confirmButtonText=Okay] the text of the confirm button
56
+ * @param [opts.confirmButtonVariant=Variant.Dark] the variant of the confirm button
57
+ * @param [opts.cancelButtonText=Cancel] the text of the cancel button
58
+ * @param [opts.cancelButtonVariant=Variant.Secondary] the variant of the cancel button
59
+ * @returns Promise that resolves with the input string or null if canceled
60
+ */
61
+ export declare const prompt: (title: string, text: string, currentInputFieldText: string, opts?: {
62
+ placeholder?: string | undefined;
63
+ defaultText?: string | undefined;
64
+ confirmButtonText?: string | undefined;
65
+ confirmButtonVariant?: Variant | undefined;
66
+ cancelButtonText?: string | undefined;
67
+ cancelButtonVariant?: Variant | undefined;
68
+ minNumChars?: number | undefined;
69
+ findValidationError?: ((text: string) => string | undefined) | undefined;
70
+ } | undefined) => Promise<string | null>;
47
71
  /**
48
72
  * Show a fatal error message
49
73
  * @author Gabe Abrams
@@ -9,6 +9,12 @@ import LogFunction from '../types/LogFunction';
9
9
  * @param opts.handler function that processes the request
10
10
  * @param [opts.skipSessionCheck] if true, skip the session check (allow users
11
11
  * to not be logged in and launched via LTI)
12
+ * @param [opts.allowedHosts] if included, only allow requests from these hosts
13
+ * (start a hostname with a "*" to only check the end of the hostname)
14
+ * you can include just one string instead of an array
15
+ * @param [opts.bannedHosts] if included, do not allow requests from these hosts
16
+ * (start a hostname with a "*" to only check the end of the hostname)
17
+ * you can include just one string instead of an array
12
18
  * @param [opts.unhandledErrorMessagePrefix] if included, when an error that
13
19
  * is not of type ErrorWithCode is thrown, the client will receive an error
14
20
  * where the error message is prefixed with this string. For example,
@@ -63,6 +69,8 @@ declare const genRouteHandler: (opts: {
63
69
  logServerEvent: LogFunction;
64
70
  }) => any;
65
71
  skipSessionCheck?: boolean | undefined;
72
+ allowedHosts?: string | string[] | undefined;
73
+ bannedHosts?: string | string[] | undefined;
66
74
  unhandledErrorMessagePrefix?: string | undefined;
67
75
  }) => (req: any, res: any, next: () => void) => Promise<undefined>;
68
76
  export default genRouteHandler;
@@ -4,6 +4,7 @@
4
4
  */
5
5
  declare enum ModalType {
6
6
  Okay = "okay",
7
+ Cancel = "cancel",
7
8
  OkayCancel = "okay-cancel",
8
9
  YesNo = "yes-no",
9
10
  YesNoCancel = "yes-no-cancel",
@@ -8,6 +8,8 @@ declare enum ReactKitErrorCode {
8
8
  SessionExpired = "DRK3",
9
9
  MissingParameter = "DRK4",
10
10
  InvalidParameter = "DRK5",
11
+ HostNotAllowed = "DRK17",
12
+ HostBanned = "DRK18",
11
13
  WrongCourse = "DRK6",
12
14
  NoCACCLSendRequestFunction = "DRK7",
13
15
  NoCACCLGetLaunchInfoFunction = "DRK8",
package/dist/esm/index.js CHANGED
@@ -31,7 +31,7 @@ function __awaiter(thisArg, _arguments, P, generator) {
31
31
  });
32
32
  }
33
33
 
34
- // Highest error code = DRK16
34
+ // Highest error code = DRK18
35
35
  /**
36
36
  * List of error codes built into the react kit
37
37
  * @author Gabe Abrams
@@ -43,6 +43,8 @@ var ReactKitErrorCode;
43
43
  ReactKitErrorCode["SessionExpired"] = "DRK3";
44
44
  ReactKitErrorCode["MissingParameter"] = "DRK4";
45
45
  ReactKitErrorCode["InvalidParameter"] = "DRK5";
46
+ ReactKitErrorCode["HostNotAllowed"] = "DRK17";
47
+ ReactKitErrorCode["HostBanned"] = "DRK18";
46
48
  ReactKitErrorCode["WrongCourse"] = "DRK6";
47
49
  ReactKitErrorCode["NoCACCLSendRequestFunction"] = "DRK7";
48
50
  ReactKitErrorCode["NoCACCLGetLaunchInfoFunction"] = "DRK8";
@@ -156,6 +158,7 @@ var ModalButtonType$1 = ModalButtonType;
156
158
  var ModalType;
157
159
  (function (ModalType) {
158
160
  ModalType["Okay"] = "okay";
161
+ ModalType["Cancel"] = "cancel";
159
162
  ModalType["OkayCancel"] = "okay-cancel";
160
163
  ModalType["YesNo"] = "yes-no";
161
164
  ModalType["YesNoCancel"] = "yes-no-cancel";
@@ -310,6 +313,9 @@ const modalTypeToModalButtonTypes = {
310
313
  [ModalType$1.Okay]: [
311
314
  ModalButtonType$1.Okay,
312
315
  ],
316
+ [ModalType$1.Cancel]: [
317
+ ModalButtonType$1.Cancel,
318
+ ],
313
319
  [ModalType$1.OkayCancel]: [
314
320
  ModalButtonType$1.Okay,
315
321
  ModalButtonType$1.Cancel,
@@ -1050,6 +1056,11 @@ const confirm = (title, text, opts) => __awaiter(void 0, void 0, void 0, functio
1050
1056
  });
1051
1057
  });
1052
1058
  /*----------------------------------------*/
1059
+ /* --------------- Prompt -------------- */
1060
+ /*----------------------------------------*/
1061
+ // Stored copies of setters
1062
+ let setPromptInfo;
1063
+ /*----------------------------------------*/
1053
1064
  /* ------------- Fatal Error ------------ */
1054
1065
  /*----------------------------------------*/
1055
1066
  // Stored copies of setters
@@ -1065,14 +1076,14 @@ const fatalErrorHandlers = [];
1065
1076
  * @param [errorTitle] title of the error box
1066
1077
  */
1067
1078
  const showFatalError = (error, errorTitle = 'An Error Occurred') => __awaiter(void 0, void 0, void 0, function* () {
1068
- var _a, _b;
1079
+ var _b, _c;
1069
1080
  // Determine message and code
1070
1081
  const message = (typeof error === 'string'
1071
1082
  ? error.trim()
1072
- : String((_a = error.message) !== null && _a !== void 0 ? _a : 'An unknown error occurred.'));
1083
+ : String((_b = error.message) !== null && _b !== void 0 ? _b : 'An unknown error occurred.'));
1073
1084
  const code = (typeof error === 'string'
1074
1085
  ? ReactKitErrorCode$1.NoCode
1075
- : String((_b = error.code) !== null && _b !== void 0 ? _b : ReactKitErrorCode$1.NoCode));
1086
+ : String((_c = error.code) !== null && _c !== void 0 ? _c : ReactKitErrorCode$1.NoCode));
1076
1087
  // Call all fatal error listeners
1077
1088
  try {
1078
1089
  fatalErrorHandlers.forEach((handler) => {
@@ -1199,6 +1210,9 @@ const AppWrapper = (props) => {
1199
1210
  // Confirm
1200
1211
  const [confirmInfo, setConfirmInfoInner,] = useState(undefined);
1201
1212
  setConfirmInfo = setConfirmInfoInner;
1213
+ // Prompt
1214
+ const [promptInfo, setPromptInfoInner] = useState(undefined);
1215
+ setPromptInfo = setPromptInfoInner;
1202
1216
  // Session expired
1203
1217
  const [sessionHasExpired, setSessionHasExpiredInner,] = useState(false);
1204
1218
  setSessionHasExpired = setSessionHasExpiredInner;
@@ -1229,6 +1243,34 @@ const AppWrapper = (props) => {
1229
1243
  }
1230
1244
  }, onTopOfOtherModals: true, dontAllowBackdropExit: true }, confirmInfo.text));
1231
1245
  }
1246
+ /* ------------- Prompt ------------ */
1247
+ if (promptInfo) {
1248
+ // Run min char validation
1249
+ const minNumCharsValidationError = ((promptInfo.opts.minNumChars
1250
+ && promptInfo.currentInputFieldText.length < promptInfo.opts.minNumChars)
1251
+ ? `Please enter at least ${promptInfo.opts.minNumChars} characters.`
1252
+ : undefined);
1253
+ // Run custom validation
1254
+ const customValidationError = (promptInfo.opts.findValidationError
1255
+ && promptInfo.opts.findValidationError(promptInfo.currentInputFieldText));
1256
+ modal = (React__default.createElement(Modal, { key: `prompt-${promptInfo.title}-${promptInfo.text}`, title: promptInfo.title,
1257
+ // only show the cancel button if there is a validation error
1258
+ type: ((customValidationError || minNumCharsValidationError)
1259
+ ? ModalType$1.Cancel
1260
+ : ModalType$1.OkayCancel), okayLabel: promptInfo.opts.confirmButtonText, okayVariant: promptInfo.opts.confirmButtonVariant, cancelLabel: promptInfo.opts.cancelButtonText, cancelVariant: promptInfo.opts.cancelButtonVariant, onClose: (buttonType) => {
1261
+ (buttonType === ModalButtonType$1.Okay
1262
+ ? promptInfo.currentInputFieldText
1263
+ : null);
1264
+ setPromptInfo(undefined);
1265
+ }, onTopOfOtherModals: true, dontAllowBackdropExit: true },
1266
+ React__default.createElement("div", { className: "d-flex flex-column align-items-center" },
1267
+ React__default.createElement("p", null, promptInfo.text),
1268
+ React__default.createElement("input", { type: "text", placeholder: promptInfo.opts.placeholder, value: promptInfo.currentInputFieldText, onChange: (e) => {
1269
+ return setPromptInfo(Object.assign(Object.assign({}, promptInfo), { currentInputFieldText: e.target.value }));
1270
+ } }),
1271
+ minNumCharsValidationError && (React__default.createElement("div", { className: "text-danger bg-danger bg-opacity-25 p-2 m-1 rounded" }, minNumCharsValidationError)),
1272
+ customValidationError && (React__default.createElement("div", { className: "text-danger bg-danger bg-opacity-25 p-2 m-1 rounded" }, customValidationError)))));
1273
+ }
1232
1274
  /* ------ Custom Modal Portals ------ */
1233
1275
  // Custom modal portals
1234
1276
  const customModalPortals = [];
@@ -14441,6 +14483,12 @@ const parseUserAgent = (userAgent) => {
14441
14483
  * @param opts.handler function that processes the request
14442
14484
  * @param [opts.skipSessionCheck] if true, skip the session check (allow users
14443
14485
  * to not be logged in and launched via LTI)
14486
+ * @param [opts.allowedHosts] if included, only allow requests from these hosts
14487
+ * (start a hostname with a "*" to only check the end of the hostname)
14488
+ * you can include just one string instead of an array
14489
+ * @param [opts.bannedHosts] if included, do not allow requests from these hosts
14490
+ * (start a hostname with a "*" to only check the end of the hostname)
14491
+ * you can include just one string instead of an array
14444
14492
  * @param [opts.unhandledErrorMessagePrefix] if included, when an error that
14445
14493
  * is not of type ErrorWithCode is thrown, the client will receive an error
14446
14494
  * where the error message is prefixed with this string. For example,
@@ -14472,6 +14520,72 @@ const genRouteHandler = (opts) => {
14472
14520
  // Output params
14473
14521
  const output = {};
14474
14522
  /*----------------------------------------*/
14523
+ /* ----------- Hostname Check ----------- */
14524
+ /*----------------------------------------*/
14525
+ // Get hostnames
14526
+ const originURL = String(req.get('origin')
14527
+ || req.headers.origin
14528
+ || req.headers.referer);
14529
+ const originHostname = (originURL
14530
+ // Remove protocol
14531
+ .replace(/(^\w+:|^)\/\//, '')
14532
+ // Remove port
14533
+ .replace(/:\d+$/, ''));
14534
+ const serverHostname = String(req.hostname);
14535
+ // Check allowed
14536
+ if (opts.allowedHosts) {
14537
+ // Only accept requests from allowed hosts
14538
+ const allowedArray = (Array.isArray(opts.allowedHosts)
14539
+ ? opts.allowedHosts
14540
+ : [opts.allowedHosts]);
14541
+ // Check if server is localhost
14542
+ if (serverHostname === 'localhost') {
14543
+ // Allow localhost
14544
+ allowedArray.push('localhost');
14545
+ }
14546
+ // Check if current host is allowed
14547
+ const allowed = allowedArray.some((allowedHost) => {
14548
+ if (allowedHost.startsWith('*')) {
14549
+ // Check end of hostname
14550
+ return originHostname.endsWith(allowedHost.substring(1));
14551
+ }
14552
+ // Check full hostname
14553
+ return originHostname.toLowerCase() === allowedHost.toLowerCase();
14554
+ });
14555
+ // If not allowed, return error
14556
+ if (!allowed) {
14557
+ return handleError(res, {
14558
+ message: 'You are not allowed to access this endpoint.',
14559
+ code: ReactKitErrorCode$1.HostNotAllowed,
14560
+ status: 403,
14561
+ });
14562
+ }
14563
+ }
14564
+ // Check banned
14565
+ if (opts.bannedHosts) {
14566
+ // Do not allow requests from banned hosts
14567
+ const bannedArray = (Array.isArray(opts.bannedHosts)
14568
+ ? opts.bannedHosts
14569
+ : [opts.bannedHosts]);
14570
+ // Check if current host is banned
14571
+ const banned = bannedArray.some((bannedHost) => {
14572
+ if (bannedHost.startsWith('*')) {
14573
+ // Check end of hostname
14574
+ return originHostname.endsWith(bannedHost.substring(1));
14575
+ }
14576
+ // Check full hostname
14577
+ return originHostname.toLowerCase() === bannedHost.toLowerCase();
14578
+ });
14579
+ // If banned, return error
14580
+ if (banned) {
14581
+ return handleError(res, {
14582
+ message: 'You are not allowed to access this endpoint.',
14583
+ code: ReactKitErrorCode$1.HostBanned,
14584
+ status: 403,
14585
+ });
14586
+ }
14587
+ }
14588
+ /*----------------------------------------*/
14475
14589
  /* ------------ Parse Params ------------ */
14476
14590
  /*----------------------------------------*/
14477
14591
  // Process items one by one