dce-reactkit 3.2.9 → 3.3.2

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.
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Type of CACCL's send request function
3
+ * @author Gabe Abrams
4
+ */
5
+ declare type SendRequestFunction = (opts: {
6
+ path: string;
7
+ method: ('GET' | 'POST' | 'DELETE' | 'PUT');
8
+ params?: {
9
+ [x: string]: any;
10
+ } | undefined;
11
+ headers?: {
12
+ [x: string]: any;
13
+ } | undefined;
14
+ numRetries?: number | undefined;
15
+ }) => Promise<{
16
+ body: any;
17
+ status: number;
18
+ headers: {
19
+ [x: string]: any;
20
+ };
21
+ }>;
22
+ /**
23
+ * Get the send request function
24
+ * @author Gabe Abrams
25
+ * @returns sendRequest function
26
+ */
27
+ export declare const getSendRequest: () => Promise<SendRequestFunction>;
28
+ declare let sessionExpiredMessage: string | undefined;
29
+ /**
30
+ * Get the custom session expired message
31
+ * @author Gabe Abrams
32
+ * @returns session expired message
33
+ */
34
+ export declare const getSessionExpiredMessage: () => string;
35
+ /**
36
+ * Initialize the client-side version of reactkit
37
+ * @author Gabe Abrams
38
+ * @param opts object containing all arguments
39
+ * @param opts.sendRequest caccl send request functions
40
+ * @param [opts.sessionExpiredMessage] a custom session expired message
41
+ */
42
+ declare const initClient: (opts: {
43
+ sendRequest: SendRequestFunction;
44
+ sessionExpiredMessage?: string;
45
+ }) => void;
46
+ export default initClient;
@@ -7,34 +7,8 @@ import React from 'react';
7
7
  import Variant from '../types/Variant';
8
8
  declare type Props = {
9
9
  children: React.ReactNode;
10
- sendRequest: SendRequestFunction;
11
10
  dark?: boolean;
12
- sessionExpiredMessage?: string;
13
11
  };
14
- declare type SendRequestFunction = (opts: {
15
- path: string;
16
- method: ('GET' | 'POST' | 'DELETE' | 'PUT');
17
- params?: {
18
- [x: string]: any;
19
- } | undefined;
20
- headers?: {
21
- [x: string]: any;
22
- } | undefined;
23
- numRetries?: number | undefined;
24
- }) => Promise<{
25
- body: any;
26
- status: number;
27
- headers: {
28
- [x: string]: any;
29
- };
30
- }>;
31
- /**
32
- * Send a request using caccl's send request feature
33
- * @author Gabe Abrams
34
- * @param opts send request options
35
- * @returns send request response
36
- */
37
- export declare const cacclSendRequest: SendRequestFunction;
38
12
  /**
39
13
  * Show an alert modal with an "Okay" button
40
14
  * @author Gabe Abrams
@@ -21,6 +21,7 @@ import MINUTE_IN_MS from './constants/MINUTE_IN_MS';
21
21
  import HOUR_IN_MS from './constants/HOUR_IN_MS';
22
22
  import DAY_IN_MS from './constants/DAY_IN_MS';
23
23
  import DynamicWord from './dynamicConstants/DynamicWord';
24
+ import initClient from './client/initClient';
24
25
  import abbreviate from './helpers/abbreviate';
25
26
  import avg from './helpers/avg';
26
27
  import ceilToNumDecimals from './helpers/ceilToNumDecimals';
@@ -68,4 +69,4 @@ import LogBuiltInMetadata from './types/LogBuiltInMetadata';
68
69
  import LogMetadataType from './types/LogMetadataType';
69
70
  import IntelliTableColumn from './types/IntelliTableColumn';
70
71
  import PickableItem from './components/ItemPicker/types/PickableItem';
71
- export { AppWrapper, LoadingSpinner, ErrorBox, Modal, TabBox, RadioButton, CheckboxButton, ButtonInputGroup, SimpleDateChooser, Drawer, PopSuccessMark, PopFailureMark, PopPendingMark, CopiableBox, ItemPicker, LogReviewer, IntelliTable, CSVDownloadButton, alert, confirm, showFatalError, ErrorWithCode, MINUTE_IN_MS, HOUR_IN_MS, DAY_IN_MS, DynamicWord, abbreviate, avg, ceilToNumDecimals, floorToNumDecimals, forceNumIntoBounds, padDecimalZeros, padZerosLeft, roundToNumDecimals, sum, waitMs, getOrdinal, getTimeInfoInET, stubServerEndpoint, startMinWait, getHumanReadableDate, getPartOfDay, stringsToHumanReadableList, onlyKeepLetters, parallelLimit, getMonthName, genCSV, canReviewLogs, isMobileOrTablet, extractProp, compareArraysByProp, visitServerEndpoint, logClientEvent, initServer, genRouteHandler, handleError, handleSuccess, initLogCollection, ModalButtonType, ModalSize, ModalType, ReactKitErrorCode, Variant, DayOfWeek, Log, LogType, LogSource, LogAction, LogBuiltInMetadata, LogMetadataType, IntelliTableColumn, PickableItem, ParamType, };
72
+ export { AppWrapper, LoadingSpinner, ErrorBox, Modal, TabBox, RadioButton, CheckboxButton, ButtonInputGroup, SimpleDateChooser, Drawer, PopSuccessMark, PopFailureMark, PopPendingMark, CopiableBox, ItemPicker, LogReviewer, IntelliTable, CSVDownloadButton, alert, confirm, showFatalError, ErrorWithCode, MINUTE_IN_MS, HOUR_IN_MS, DAY_IN_MS, DynamicWord, abbreviate, avg, ceilToNumDecimals, floorToNumDecimals, forceNumIntoBounds, padDecimalZeros, padZerosLeft, roundToNumDecimals, sum, waitMs, getOrdinal, getTimeInfoInET, stubServerEndpoint, startMinWait, getHumanReadableDate, getPartOfDay, stringsToHumanReadableList, onlyKeepLetters, parallelLimit, getMonthName, genCSV, canReviewLogs, isMobileOrTablet, extractProp, compareArraysByProp, initClient, visitServerEndpoint, logClientEvent, initServer, genRouteHandler, handleError, handleSuccess, initLogCollection, ModalButtonType, ModalSize, ModalType, ReactKitErrorCode, Variant, DayOfWeek, Log, LogType, LogSource, LogAction, LogBuiltInMetadata, LogMetadataType, IntelliTableColumn, PickableItem, ParamType, };
@@ -13,6 +13,8 @@ declare enum ReactKitErrorCode {
13
13
  NoCACCLGetLaunchInfoFunction = "DRK8",
14
14
  NotTTM = "DRK9",
15
15
  NotAdmin = "DRK10",
16
- NotAllowedToReviewLogs = "DRK11"
16
+ NotAllowedToReviewLogs = "DRK11",
17
+ ThemeCheckedBeforeReactKitReady = "DRK12",
18
+ SessionExpiredMessageGottenBeforeReactKitReady = "DRK13"
17
19
  }
18
20
  export default ReactKitErrorCode;
package/dist/esm/index.js CHANGED
@@ -28,7 +28,7 @@ function __awaiter(thisArg, _arguments, P, generator) {
28
28
  });
29
29
  }
30
30
 
31
- // Highest error code = DRK11
31
+ // Highest error code = DRK13
32
32
  /**
33
33
  * List of error codes built into the react kit
34
34
  * @author Gabe Abrams
@@ -46,6 +46,8 @@ var ReactKitErrorCode;
46
46
  ReactKitErrorCode["NotTTM"] = "DRK9";
47
47
  ReactKitErrorCode["NotAdmin"] = "DRK10";
48
48
  ReactKitErrorCode["NotAllowedToReviewLogs"] = "DRK11";
49
+ ReactKitErrorCode["ThemeCheckedBeforeReactKitReady"] = "DRK12";
50
+ ReactKitErrorCode["SessionExpiredMessageGottenBeforeReactKitReady"] = "DRK13";
49
51
  })(ReactKitErrorCode || (ReactKitErrorCode = {}));
50
52
  var ReactKitErrorCode$1 = ReactKitErrorCode;
51
53
 
@@ -141,6 +143,24 @@ var ModalType;
141
143
  })(ModalType || (ModalType = {}));
142
144
  var ModalType$1 = ModalType;
143
145
 
146
+ /**
147
+ * Built-in metadata for logs
148
+ * @author Gabe Abrams
149
+ */
150
+ const LogBuiltInMetadata = {
151
+ // Contexts
152
+ Context: {
153
+ Uncategorized: 'Uncategorized',
154
+ ServerRenderedErrorPage: 'ServerRenderedErrorPage',
155
+ ServerEndpointError: 'ServerEndpointError',
156
+ ClientFatalError: 'ClientFatalError',
157
+ },
158
+ // Targets
159
+ Target: {
160
+ NoTarget: 'NoTarget',
161
+ },
162
+ };
163
+
144
164
  /**
145
165
  * Wait for a certain number of ms
146
166
  * @author Gabe Abrams
@@ -504,24 +524,6 @@ const ROUTE_PATH_PREFIX = '/dce-reactkit';
504
524
  */
505
525
  const LOG_ROUTE_PATH = `${ROUTE_PATH_PREFIX}/log`;
506
526
 
507
- /**
508
- * Built-in metadata for logs
509
- * @author Gabe Abrams
510
- */
511
- const LogBuiltInMetadata = {
512
- // Contexts
513
- Context: {
514
- Uncategorized: 'Uncategorized',
515
- ServerRenderedErrorPage: 'ServerRenderedErrorPage',
516
- ServerEndpointError: 'ServerEndpointError',
517
- ClientFatalError: 'ClientFatalError',
518
- },
519
- // Targets
520
- Target: {
521
- NoTarget: 'NoTarget',
522
- },
523
- };
524
-
525
527
  /**
526
528
  * Allowed log levels
527
529
  * @author Gabe Abrams
@@ -534,6 +536,71 @@ var LogLevel;
534
536
  })(LogLevel || (LogLevel = {}));
535
537
  var LogLevel$1 = LogLevel;
536
538
 
539
+ /*----------------------------------------*/
540
+ /* ---- Static Variables and Getters ---- */
541
+ /*----------------------------------------*/
542
+ /* ----------- Initialized ---------- */
543
+ let onInitialized;
544
+ let initialized = new Promise((resolve) => {
545
+ onInitialized = resolve;
546
+ });
547
+ /* ---------- Send Request ---------- */
548
+ let storedSendRequest;
549
+ /**
550
+ * Get the send request function
551
+ * @author Gabe Abrams
552
+ * @returns sendRequest function
553
+ */
554
+ const getSendRequest = () => __awaiter(void 0, void 0, void 0, function* () {
555
+ // Wait for initialization or timeout
556
+ let timedOut = false;
557
+ yield Promise.all([
558
+ (() => __awaiter(void 0, void 0, void 0, function* () {
559
+ yield waitMs(1000);
560
+ timedOut = true;
561
+ }))(),
562
+ initialized,
563
+ ]);
564
+ // Error if no send request function
565
+ if (timedOut) {
566
+ showFatalError(new ErrorWithCode('Could not send a request because the request needed to be sent before dce-reactkit was properly initialized. Perhaps dce-reactkit was not initialized with initClient.', ReactKitErrorCode$1.NoCACCLSendRequestFunction));
567
+ // Return dummy function that never resolves
568
+ return (() => {
569
+ return new Promise(() => { });
570
+ });
571
+ }
572
+ // Return
573
+ return storedSendRequest;
574
+ });
575
+ /* ----- Session Expired Message ---- */
576
+ let sessionExpiredMessage;
577
+ /**
578
+ * Get the custom session expired message
579
+ * @author Gabe Abrams
580
+ * @returns session expired message
581
+ */
582
+ const getSessionExpiredMessage = () => {
583
+ // Return
584
+ return (sessionExpiredMessage !== null && sessionExpiredMessage !== void 0 ? sessionExpiredMessage : 'Your session has expired. Please go back to Canvas and start over.');
585
+ };
586
+ /*----------------------------------------*/
587
+ /* ---------------- Init ---------------- */
588
+ /*----------------------------------------*/
589
+ /**
590
+ * Initialize the client-side version of reactkit
591
+ * @author Gabe Abrams
592
+ * @param opts object containing all arguments
593
+ * @param opts.sendRequest caccl send request functions
594
+ * @param [opts.sessionExpiredMessage] a custom session expired message
595
+ */
596
+ const initClient = (opts) => {
597
+ // Store values
598
+ storedSendRequest = opts.sendRequest;
599
+ sessionExpiredMessage = opts.sessionExpiredMessage;
600
+ // Mark as initialized
601
+ onInitialized(null);
602
+ };
603
+
537
604
  // Keep track of whether or not session expiry has already been handled
538
605
  let sessionAlreadyExpired = false;
539
606
  /*------------------------------------------------------------------------*/
@@ -605,7 +672,8 @@ const visitServerEndpoint = (opts) => __awaiter(void 0, void 0, void 0, function
605
672
  throw new ErrorWithCode(stubResponse.errorMessage, stubResponse.errorCode);
606
673
  }
607
674
  // Send the request
608
- const response = yield cacclSendRequest({
675
+ const sendRequest = yield getSendRequest();
676
+ const response = yield sendRequest({
609
677
  path: opts.path,
610
678
  method: (_c = opts.method) !== null && _c !== void 0 ? _c : 'GET',
611
679
  params: opts.params,
@@ -692,24 +760,6 @@ const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* ()
692
760
  /* Static Helpers */
693
761
  /*------------------------------------------------------------------------*/
694
762
  /*----------------------------------------*/
695
- /* Send Request */
696
- /*----------------------------------------*/
697
- // Store copy of caccl send request
698
- let _cacclSendRequest;
699
- /**
700
- * Send a request using caccl's send request feature
701
- * @author Gabe Abrams
702
- * @param opts send request options
703
- * @returns send request response
704
- */
705
- const cacclSendRequest = (opts) => __awaiter(void 0, void 0, void 0, function* () {
706
- // Make sure send request has been passed in
707
- if (!_cacclSendRequest) {
708
- throw new ErrorWithCode(`\nThe request could not be sent because the AppWrapper component does not have a copy of sendRequest from CACCL.\nIf you are currently writing tests for your app, this means you did not properly stub the server response.\nMethod: ${opts.method}\nPath: ${opts.path}\n\n`, ReactKitErrorCode$1.NoCACCLSendRequestFunction);
709
- }
710
- return _cacclSendRequest(opts);
711
- });
712
- /*----------------------------------------*/
713
763
  /* Alert */
714
764
  /*----------------------------------------*/
715
765
  // Stored copies of setters
@@ -833,9 +883,7 @@ const AppWrapper = (props) => {
833
883
  /* Setup */
834
884
  /*------------------------------------------------------------------------*/
835
885
  /* -------------- Props ------------- */
836
- const { children, sendRequest, dark, sessionExpiredMessage = 'Your session has expired. Please go back to Canvas and start over.', } = props;
837
- // Store copy of send request
838
- _cacclSendRequest = sendRequest;
886
+ const { children, dark, } = props;
839
887
  /* -------------- State ------------- */
840
888
  // Fatal error
841
889
  const [fatalErrorMessage, setFatalErrorMessageInner,] = useState();
@@ -888,7 +936,7 @@ const AppWrapper = (props) => {
888
936
  if (fatalErrorMessage || fatalErrorCode || sessionHasExpired) {
889
937
  // Re-encapsulate in an error
890
938
  const error = (sessionHasExpired
891
- ? new ErrorWithCode(sessionExpiredMessage, ReactKitErrorCode$1.SessionExpired)
939
+ ? new ErrorWithCode(getSessionExpiredMessage(), ReactKitErrorCode$1.SessionExpired)
892
940
  : new ErrorWithCode((fatalErrorMessage !== null && fatalErrorMessage !== void 0 ? fatalErrorMessage : 'An unknown error has occurred. Please contact support.'), (fatalErrorCode !== null && fatalErrorCode !== void 0 ? fatalErrorCode : ReactKitErrorCode$1.NoCode)));
893
941
  // Build error screen
894
942
  body = (React.createElement("div", { style: {
@@ -5589,5 +5637,5 @@ var DayOfWeek;
5589
5637
  })(DayOfWeek || (DayOfWeek = {}));
5590
5638
  var DayOfWeek$1 = DayOfWeek;
5591
5639
 
5592
- export { AppWrapper, ButtonInputGroup, CSVDownloadButton, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, DynamicWord, ErrorBox, ErrorWithCode, HOUR_IN_MS, IntelliTable, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogBuiltInMetadata, LogReviewer, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, canReviewLogs, ceilToNumDecimals, compareArraysByProp, confirm, extractProp, floorToNumDecimals, forceNumIntoBounds, genCSV, genRouteHandler, getHumanReadableDate, getMonthName, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initLogCollection, initServer, isMobileOrTablet, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
5640
+ export { AppWrapper, ButtonInputGroup, CSVDownloadButton, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, DynamicWord, ErrorBox, ErrorWithCode, HOUR_IN_MS, IntelliTable, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogBuiltInMetadata, LogReviewer, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, canReviewLogs, ceilToNumDecimals, compareArraysByProp, confirm, extractProp, floorToNumDecimals, forceNumIntoBounds, genCSV, genRouteHandler, getHumanReadableDate, getMonthName, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initClient, initLogCollection, initServer, isMobileOrTablet, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
5593
5641
  //# sourceMappingURL=index.js.map