dce-reactkit 2.0.1 → 2.0.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.
- package/lib/errors/ErrorWithCode.d.ts +9 -0
- package/lib/errors/ErrorWithCode.js +33 -0
- package/lib/errors/ErrorWithCode.js.map +1 -0
- package/lib/helpers/visitServerEndpoint.d.ts +17 -0
- package/lib/helpers/visitServerEndpoint.js +84 -0
- package/lib/helpers/visitServerEndpoint.js.map +1 -0
- package/lib/types/ReactKitErrorCode.d.ts +9 -0
- package/lib/types/ReactKitErrorCode.js +14 -0
- package/lib/types/ReactKitErrorCode.js.map +1 -0
- package/package.json +2 -1
- package/src/errors/ErrorWithCode.tsx +15 -0
- package/src/helpers/visitServerEndpoint.tsx +59 -0
- package/src/types/ReactKitErrorCode.tsx +12 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
/**
|
|
19
|
+
* An error with a code
|
|
20
|
+
* @author Gabe Abrams
|
|
21
|
+
*/
|
|
22
|
+
var ErrorWithCode = /** @class */ (function (_super) {
|
|
23
|
+
__extends(ErrorWithCode, _super);
|
|
24
|
+
function ErrorWithCode(message, code) {
|
|
25
|
+
var _this = _super.call(this, message) || this;
|
|
26
|
+
_this.name = 'ErrorWithCode';
|
|
27
|
+
_this.code = code;
|
|
28
|
+
return _this;
|
|
29
|
+
}
|
|
30
|
+
return ErrorWithCode;
|
|
31
|
+
}(Error));
|
|
32
|
+
exports.default = ErrorWithCode;
|
|
33
|
+
//# sourceMappingURL=ErrorWithCode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ErrorWithCode.js","sourceRoot":"","sources":["../../src/errors/ErrorWithCode.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA;;;GAGG;AACH;IAA4B,iCAAK;IAG/B,uBAAY,OAAe,EAAE,IAAY;QAAzC,YACE,kBAAM,OAAO,CAAC,SAGf;QAFC,KAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,KAAI,CAAC,IAAI,GAAG,IAAI,CAAC;;IACnB,CAAC;IACH,oBAAC;AAAD,CAAC,AARD,CAA4B,KAAK,GAQhC;AAED,kBAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visit an endpoint on the server
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
* @param opts object containing all arguments
|
|
5
|
+
* @param opts.path - the path of the server endpoint
|
|
6
|
+
* @param [opts.method=GET] - the method of the endpoint
|
|
7
|
+
* @param [opts.params] - query/body parameters to include
|
|
8
|
+
* @returns response from server
|
|
9
|
+
*/
|
|
10
|
+
declare const visitServerEndpoint: (opts: {
|
|
11
|
+
path: string;
|
|
12
|
+
method?: ('GET' | 'POST' | 'DELETE' | 'PUT');
|
|
13
|
+
params?: {
|
|
14
|
+
[x: string]: any;
|
|
15
|
+
};
|
|
16
|
+
}) => Promise<any>;
|
|
17
|
+
export default visitServerEndpoint;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
// Initialize caccl
|
|
43
|
+
var client_1 = require("caccl/client");
|
|
44
|
+
// Import custom error
|
|
45
|
+
var ErrorWithCode_1 = __importDefault(require("../errors/ErrorWithCode"));
|
|
46
|
+
var ReactKitErrorCode_1 = __importDefault(require("../types/ReactKitErrorCode"));
|
|
47
|
+
/**
|
|
48
|
+
* Visit an endpoint on the server
|
|
49
|
+
* @author Gabe Abrams
|
|
50
|
+
* @param opts object containing all arguments
|
|
51
|
+
* @param opts.path - the path of the server endpoint
|
|
52
|
+
* @param [opts.method=GET] - the method of the endpoint
|
|
53
|
+
* @param [opts.params] - query/body parameters to include
|
|
54
|
+
* @returns response from server
|
|
55
|
+
*/
|
|
56
|
+
var visitServerEndpoint = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
|
|
57
|
+
var response, body;
|
|
58
|
+
return __generator(this, function (_a) {
|
|
59
|
+
switch (_a.label) {
|
|
60
|
+
case 0: return [4 /*yield*/, (0, client_1.sendRequest)({
|
|
61
|
+
path: opts.path,
|
|
62
|
+
method: opts.method,
|
|
63
|
+
params: opts.params,
|
|
64
|
+
})];
|
|
65
|
+
case 1:
|
|
66
|
+
response = _a.sent();
|
|
67
|
+
// Check for failure
|
|
68
|
+
if (!response || !response.body) {
|
|
69
|
+
throw new ErrorWithCode_1.default('We didn\'t get a response from the server. Please check your internet connection.', ReactKitErrorCode_1.default.NoResponse);
|
|
70
|
+
}
|
|
71
|
+
if (!response.body.success) {
|
|
72
|
+
// Other errors
|
|
73
|
+
throw new ErrorWithCode_1.default((response.body.message
|
|
74
|
+
|| 'An unknown error occurred. Please contact an admin.'), (response.body.code
|
|
75
|
+
|| ReactKitErrorCode_1.default.NoCode));
|
|
76
|
+
}
|
|
77
|
+
body = response.body.body;
|
|
78
|
+
// Return
|
|
79
|
+
return [2 /*return*/, body];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}); };
|
|
83
|
+
exports.default = visitServerEndpoint;
|
|
84
|
+
//# sourceMappingURL=visitServerEndpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"visitServerEndpoint.js","sourceRoot":"","sources":["../../src/helpers/visitServerEndpoint.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mBAAmB;AACnB,uCAA2C;AAE3C,sBAAsB;AACtB,0EAAoD;AACpD,iFAA2D;AAE3D;;;;;;;;GAQG;AACH,IAAM,mBAAmB,GAAG,UAC1B,IAIC;;;;oBAGgB,qBAAM,IAAA,oBAAW,EAAC;oBACjC,IAAI,EAAE,IAAI,CAAC,IAAI;oBACf,MAAM,EAAE,IAAI,CAAC,MAAM;oBACnB,MAAM,EAAE,IAAI,CAAC,MAAM;iBACpB,CAAC,EAAA;;gBAJI,QAAQ,GAAG,SAIf;gBAEF,oBAAoB;gBACpB,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;oBAC/B,MAAM,IAAI,uBAAa,CACrB,mFAAmF,EACnF,2BAAiB,CAAC,UAAU,CAC7B,CAAC;iBACH;gBACD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE;oBAC1B,eAAe;oBACf,MAAM,IAAI,uBAAa,CACrB,CACE,QAAQ,CAAC,IAAI,CAAC,OAAO;2BAClB,qDAAqD,CACzD,EACD,CACE,QAAQ,CAAC,IAAI,CAAC,IAAI;2BACf,2BAAiB,CAAC,MAAM,CAC5B,CACF,CAAC;iBACH;gBAGO,IAAI,GAAK,QAAQ,CAAC,IAAI,KAAlB,CAAmB;gBAE/B,SAAS;gBACT,sBAAO,IAAI,EAAC;;;KACb,CAAC;AAEF,kBAAe,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Highest error code = DRK2
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
/**
|
|
5
|
+
* List of error codes built into the react kit
|
|
6
|
+
* @author Gabe Abrams
|
|
7
|
+
*/
|
|
8
|
+
var ReactKitErrorCode;
|
|
9
|
+
(function (ReactKitErrorCode) {
|
|
10
|
+
ReactKitErrorCode["NoResponse"] = "DRK1";
|
|
11
|
+
ReactKitErrorCode["NoCode"] = "DRK2";
|
|
12
|
+
})(ReactKitErrorCode || (ReactKitErrorCode = {}));
|
|
13
|
+
exports.default = ReactKitErrorCode;
|
|
14
|
+
//# sourceMappingURL=ReactKitErrorCode.js.map
|
|
@@ -0,0 +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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dce-reactkit",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"module": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"@fortawesome/free-regular-svg-icons": "^6.1.1",
|
|
19
19
|
"@fortawesome/free-solid-svg-icons": "^6.1.1",
|
|
20
20
|
"@fortawesome/react-fontawesome": "^0.1.18",
|
|
21
|
+
"caccl": "^2.0.0-beta.39",
|
|
21
22
|
"react-bootstrap": "^2.2.3"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error with a code
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
*/
|
|
5
|
+
class ErrorWithCode extends Error {
|
|
6
|
+
code: string;
|
|
7
|
+
|
|
8
|
+
constructor(message: string, code: string) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'ErrorWithCode';
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default ErrorWithCode;
|
|
@@ -0,0 +1,59 @@
|
|
|
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;
|