dce-reactkit 2.0.3 → 2.0.6

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.
Files changed (53) hide show
  1. package/lib/components/AppWrapper.d.ts +19 -0
  2. package/lib/components/AppWrapper.js +127 -0
  3. package/lib/components/AppWrapper.js.map +1 -0
  4. package/{src → lib}/components/AppWrapper.tsx +61 -7
  5. package/lib/components/ErrorBox.d.ts +12 -0
  6. package/lib/components/ErrorBox.js +65 -0
  7. package/lib/components/ErrorBox.js.map +1 -0
  8. package/{src → lib}/components/ErrorBox.tsx +4 -1
  9. package/{src → lib}/components/LoadingSpinner.tsx +0 -0
  10. package/{src → lib}/components/Modal.tsx +0 -0
  11. package/{src → lib}/errors/ErrorWithCode.tsx +0 -0
  12. package/{src → lib}/helpers/abbreviate.tsx +0 -0
  13. package/{src → lib}/helpers/avg.tsx +0 -0
  14. package/{src → lib}/helpers/ceilToNumDecimals.tsx +0 -0
  15. package/{src → lib}/helpers/floorToNumDecimals.tsx +0 -0
  16. package/{src → lib}/helpers/forceNumIntoBounds.tsx +0 -0
  17. package/lib/helpers/handleError.d.ts +14 -0
  18. package/lib/helpers/handleError.js +54 -0
  19. package/lib/helpers/handleError.js.map +1 -0
  20. package/lib/helpers/handleError.ts +65 -0
  21. package/lib/helpers/handleSuccess.d.ts +8 -0
  22. package/lib/helpers/handleSuccess.js +20 -0
  23. package/lib/helpers/handleSuccess.js.map +1 -0
  24. package/lib/helpers/handleSuccess.ts +18 -0
  25. package/{src → lib}/helpers/padDecimalZeros.tsx +0 -0
  26. package/{src → lib}/helpers/padZerosLeft.tsx +0 -0
  27. package/lib/helpers/parseRequest.d.ts +21 -0
  28. package/lib/helpers/parseRequest.js +240 -0
  29. package/lib/helpers/parseRequest.js.map +1 -0
  30. package/lib/helpers/parseRequest.ts +299 -0
  31. package/{src → lib}/helpers/roundToNumDecimals.tsx +0 -0
  32. package/lib/helpers/showFatalError.d.ts +2 -0
  33. package/lib/helpers/showFatalError.js +5 -0
  34. package/lib/helpers/showFatalError.js.map +1 -0
  35. package/{src → lib}/helpers/showFatalError.tsx +0 -0
  36. package/{src → lib}/helpers/sum.tsx +0 -0
  37. package/lib/helpers/visitServerEndpoint.d.ts +7 -1
  38. package/lib/helpers/visitServerEndpoint.js +57 -6
  39. package/lib/helpers/visitServerEndpoint.js.map +1 -1
  40. package/lib/helpers/visitServerEndpoint.tsx +110 -0
  41. package/{src → lib}/helpers/waitMs.tsx +0 -0
  42. package/lib/types/ParamType.d.ts +17 -0
  43. package/lib/types/ParamType.js +21 -0
  44. package/lib/types/ParamType.js.map +1 -0
  45. package/lib/types/ParamType.ts +18 -0
  46. package/lib/types/ReactKitErrorCode.d.ts +5 -1
  47. package/lib/types/ReactKitErrorCode.js +4 -0
  48. package/lib/types/ReactKitErrorCode.js.map +1 -1
  49. package/{src → lib}/types/ReactKitErrorCode.tsx +4 -0
  50. package/{src → lib}/types/Variant.tsx +0 -0
  51. package/package.json +5 -2
  52. package/tsconfig.json +1 -1
  53. package/src/helpers/visitServerEndpoint.tsx +0 -59
@@ -0,0 +1,110 @@
1
+ // Initialize caccl
2
+ import { sendRequest } from 'caccl/client';
3
+
4
+ // Import custom error
5
+ import ErrorWithCode from '../errors/ErrorWithCode';
6
+ import ReactKitErrorCode from '../types/ReactKitErrorCode';
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 sendRequest({
49
+ path: opts.path,
50
+ method: opts.method,
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;
File without changes
@@ -0,0 +1,17 @@
1
+ /**
2
+ * API param types
3
+ * @author Gabe Abrams
4
+ */
5
+ declare enum ParamType {
6
+ Boolean = "boolean",
7
+ BooleanOptional = "boolean-optional",
8
+ Float = "float",
9
+ FloatOptional = "float-optional",
10
+ Int = "int",
11
+ IntOptional = "int-optional",
12
+ JSON = "json",
13
+ JSONOptional = "json-optional",
14
+ String = "string",
15
+ StringOptional = "string-optional"
16
+ }
17
+ export default ParamType;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * API param types
5
+ * @author Gabe Abrams
6
+ */
7
+ var ParamType;
8
+ (function (ParamType) {
9
+ ParamType["Boolean"] = "boolean";
10
+ ParamType["BooleanOptional"] = "boolean-optional";
11
+ ParamType["Float"] = "float";
12
+ ParamType["FloatOptional"] = "float-optional";
13
+ ParamType["Int"] = "int";
14
+ ParamType["IntOptional"] = "int-optional";
15
+ ParamType["JSON"] = "json";
16
+ ParamType["JSONOptional"] = "json-optional";
17
+ ParamType["String"] = "string";
18
+ ParamType["StringOptional"] = "string-optional";
19
+ })(ParamType || (ParamType = {}));
20
+ exports.default = ParamType;
21
+ //# sourceMappingURL=ParamType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ParamType.js","sourceRoot":"","sources":["../../src/types/ParamType.ts"],"names":[],"mappings":";;AAAA;;;GAGG;AACH,IAAK,SAWJ;AAXD,WAAK,SAAS;IACZ,gCAAmB,CAAA;IACnB,iDAAoC,CAAA;IACpC,4BAAe,CAAA;IACf,6CAAgC,CAAA;IAChC,wBAAW,CAAA;IACX,yCAA4B,CAAA;IAC5B,0BAAa,CAAA;IACb,2CAA8B,CAAA;IAC9B,8BAAiB,CAAA;IACjB,+CAAkC,CAAA;AACpC,CAAC,EAXI,SAAS,KAAT,SAAS,QAWb;AAED,kBAAe,SAAS,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * API param types
3
+ * @author Gabe Abrams
4
+ */
5
+ enum ParamType {
6
+ Boolean = 'boolean', // Boolean
7
+ BooleanOptional = 'boolean-optional', // Optional boolean
8
+ Float = 'float', // Float Number
9
+ FloatOptional = 'float-optional', // Optional Float Number
10
+ Int = 'int', // Integer Number
11
+ IntOptional = 'int-optional', // Optional Integer Number
12
+ JSON = 'json', // JSONified object
13
+ JSONOptional = 'json-optional', // Optional JSONified object
14
+ String = 'string', // String
15
+ StringOptional = 'string-optional', // Optional string
16
+ }
17
+
18
+ export default ParamType;
@@ -4,6 +4,10 @@
4
4
  */
5
5
  declare enum ReactKitErrorCode {
6
6
  NoResponse = "DRK1",
7
- NoCode = "DRK2"
7
+ NoCode = "DRK2",
8
+ SessionExpired = "DRK3",
9
+ MissingParameter = "DRK4",
10
+ InvalidParameter = "DRK5",
11
+ WrongCourse = "DRK6"
8
12
  }
9
13
  export default ReactKitErrorCode;
@@ -9,6 +9,10 @@ var ReactKitErrorCode;
9
9
  (function (ReactKitErrorCode) {
10
10
  ReactKitErrorCode["NoResponse"] = "DRK1";
11
11
  ReactKitErrorCode["NoCode"] = "DRK2";
12
+ ReactKitErrorCode["SessionExpired"] = "DRK3";
13
+ ReactKitErrorCode["MissingParameter"] = "DRK4";
14
+ ReactKitErrorCode["InvalidParameter"] = "DRK5";
15
+ ReactKitErrorCode["WrongCourse"] = "DRK6";
12
16
  })(ReactKitErrorCode || (ReactKitErrorCode = {}));
13
17
  exports.default = ReactKitErrorCode;
14
18
  //# sourceMappingURL=ReactKitErrorCode.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ReactKitErrorCode.js","sourceRoot":"","sources":["../../src/types/ReactKitErrorCode.tsx"],"names":[],"mappings":";AAAA,4BAA4B;;AAE5B;;;GAGG;AACH,IAAK,iBAGJ;AAHD,WAAK,iBAAiB;IACpB,wCAAmB,CAAA;IACnB,oCAAe,CAAA;AACjB,CAAC,EAHI,iBAAiB,KAAjB,iBAAiB,QAGrB;AAED,kBAAe,iBAAiB,CAAC"}
1
+ {"version":3,"file":"ReactKitErrorCode.js","sourceRoot":"","sources":["../../src/types/ReactKitErrorCode.tsx"],"names":[],"mappings":";AAAA,4BAA4B;;AAE5B;;;GAGG;AACH,IAAK,iBAOJ;AAPD,WAAK,iBAAiB;IACpB,wCAAmB,CAAA;IACnB,oCAAe,CAAA;IACf,4CAAuB,CAAA;IACvB,8CAAyB,CAAA;IACzB,8CAAyB,CAAA;IACzB,yCAAoB,CAAA;AACtB,CAAC,EAPI,iBAAiB,KAAjB,iBAAiB,QAOrB;AAED,kBAAe,iBAAiB,CAAC"}
@@ -7,6 +7,10 @@
7
7
  enum ReactKitErrorCode {
8
8
  NoResponse = 'DRK1',
9
9
  NoCode = 'DRK2',
10
+ SessionExpired = 'DRK3',
11
+ MissingParameter = 'DRK4',
12
+ InvalidParameter = 'DRK5',
13
+ WrongCourse = 'DRK6',
10
14
  }
11
15
 
12
16
  export default ReactKitErrorCode;
File without changes
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "2.0.3",
3
+ "version": "2.0.6",
4
4
  "main": "index.js",
5
5
  "module": "index.js",
6
6
  "scripts": {
7
- "build": "tsc --project ./tsconfig.json"
7
+ "build": "rimraf lib/ && tsc --project ./tsconfig.json && copyfiles -u 1 src/**/*.tsx src/**/*.ts lib/"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
@@ -22,8 +22,10 @@
22
22
  "react-bootstrap": "^2.2.3"
23
23
  },
24
24
  "devDependencies": {
25
+ "@types/express": "^4.17.13",
25
26
  "@typescript-eslint/eslint-plugin": "^5.18.0",
26
27
  "@typescript-eslint/parser": "^5.18.0",
28
+ "copyfiles": "^2.4.1",
27
29
  "eslint": "^8.12.0",
28
30
  "eslint-config-airbnb": "^19.0.4",
29
31
  "eslint-config-airbnb-typescript": "^17.0.0",
@@ -32,6 +34,7 @@
32
34
  "eslint-plugin-jsx-a11y": "^6.5.1",
33
35
  "eslint-plugin-react": "^7.29.4",
34
36
  "eslint-plugin-react-hooks": "^4.4.0",
37
+ "rimraf": "^3.0.2",
35
38
  "typescript": "^4.6.3"
36
39
  }
37
40
  }
package/tsconfig.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "target": "es5",
11
11
  "jsx": "react-jsx",
12
12
  "lib": [
13
- "es2015",
13
+ "es2017",
14
14
  "dom"
15
15
  ],
16
16
  "outDir": "./lib"
@@ -1,59 +0,0 @@
1
- // Initialize caccl
2
- import { sendRequest } from 'caccl/client';
3
-
4
- // Import custom error
5
- import ErrorWithCode from '../errors/ErrorWithCode';
6
- import ReactKitErrorCode from '../types/ReactKitErrorCode';
7
-
8
- /**
9
- * Visit an endpoint on the server
10
- * @author Gabe Abrams
11
- * @param opts object containing all arguments
12
- * @param opts.path - the path of the server endpoint
13
- * @param [opts.method=GET] - the method of the endpoint
14
- * @param [opts.params] - query/body parameters to include
15
- * @returns response from server
16
- */
17
- const visitServerEndpoint = async (
18
- opts: {
19
- path: string,
20
- method?: ('GET' | 'POST' | 'DELETE' | 'PUT'),
21
- params?: { [key in string]: any },
22
- },
23
- ): Promise<any> => {
24
- // Send the request
25
- const response = await sendRequest({
26
- path: opts.path,
27
- method: opts.method,
28
- params: opts.params,
29
- });
30
-
31
- // Check for failure
32
- if (!response || !response.body) {
33
- throw new ErrorWithCode(
34
- 'We didn\'t get a response from the server. Please check your internet connection.',
35
- ReactKitErrorCode.NoResponse,
36
- );
37
- }
38
- if (!response.body.success) {
39
- // Other errors
40
- throw new ErrorWithCode(
41
- (
42
- response.body.message
43
- || 'An unknown error occurred. Please contact an admin.'
44
- ),
45
- (
46
- response.body.code
47
- || ReactKitErrorCode.NoCode
48
- ),
49
- );
50
- }
51
-
52
- // Success! Extract the body
53
- const { body } = response.body;
54
-
55
- // Return
56
- return body;
57
- };
58
-
59
- export default visitServerEndpoint;