@evoke-platform/context 1.0.0-SNAPSHOT.1679791794759

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 (52) hide show
  1. package/.eslintrc.js +36 -0
  2. package/.husky/pre-commit +4 -0
  3. package/.prettierignore +1 -0
  4. package/.prettierrc +8 -0
  5. package/.vscode/extensions.json +3 -0
  6. package/.vscode/launch.json +19 -0
  7. package/.vscode/settings.json +70 -0
  8. package/LICENSE +21 -0
  9. package/README.md +7 -0
  10. package/bitbucket-pipelines.yml +27 -0
  11. package/dist/api/ApiServicesProvider.d.ts +8 -0
  12. package/dist/api/ApiServicesProvider.js +23 -0
  13. package/dist/api/ApiServicesProvider.js.map +1 -0
  14. package/dist/api/api.d.ts +29 -0
  15. package/dist/api/api.js +134 -0
  16. package/dist/api/api.js.map +1 -0
  17. package/dist/api/index.d.ts +3 -0
  18. package/dist/api/index.js +27 -0
  19. package/dist/api/index.js.map +1 -0
  20. package/dist/authentication/AuthenticationContextProvider.d.ts +13 -0
  21. package/dist/authentication/AuthenticationContextProvider.js +63 -0
  22. package/dist/authentication/AuthenticationContextProvider.js.map +1 -0
  23. package/dist/authentication/index.d.ts +2 -0
  24. package/dist/authentication/index.js +26 -0
  25. package/dist/authentication/index.js.map +1 -0
  26. package/dist/index.d.ts +3 -0
  27. package/dist/index.js +22 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/objects/filters.d.ts +35 -0
  30. package/dist/objects/filters.js +5 -0
  31. package/dist/objects/filters.js.map +1 -0
  32. package/dist/objects/index.d.ts +1 -0
  33. package/dist/objects/index.js +20 -0
  34. package/dist/objects/index.js.map +1 -0
  35. package/dist/objects/objects.d.ts +76 -0
  36. package/dist/objects/objects.js +86 -0
  37. package/dist/objects/objects.js.map +1 -0
  38. package/dist/tests/api/api.unit.d.ts +1 -0
  39. package/dist/tests/api/api.unit.js +123 -0
  40. package/dist/tests/api/api.unit.js.map +1 -0
  41. package/package.json +69 -0
  42. package/src/api/ApiServicesProvider.tsx +29 -0
  43. package/src/api/api.ts +162 -0
  44. package/src/api/index.ts +6 -0
  45. package/src/authentication/AuthenticationContextProvider.tsx +74 -0
  46. package/src/authentication/index.ts +5 -0
  47. package/src/index.ts +6 -0
  48. package/src/objects/filters.ts +45 -0
  49. package/src/objects/index.ts +4 -0
  50. package/src/objects/objects.ts +181 -0
  51. package/src/tests/api/api.unit.ts +116 -0
  52. package/tsconfig.json +16 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filters.js","sourceRoot":"","sources":["../../src/objects/filters.ts"],"names":[],"mappings":";AAAA,oDAAoD;AACpD,+CAA+C"}
@@ -0,0 +1 @@
1
+ export * from './objects';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ // Copyright (c) 2023 System Automation Corporation.
3
+ // This file is licensed under the MIT License.
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ __exportStar(require("./objects"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/objects/index.ts"],"names":[],"mappings":";AAAA,oDAAoD;AACpD,+CAA+C;;;;;;;;;;;;;;;;AAE/C,4CAA0B"}
@@ -0,0 +1,76 @@
1
+ import { ApiServices, Callback } from '../api';
2
+ import { Filter } from './filters';
3
+ export type Obj = {
4
+ id: string;
5
+ name: string;
6
+ properties?: Property[];
7
+ actions?: Action[];
8
+ };
9
+ export type PropertyType = 'array' | 'boolean' | 'date' | 'date-time' | 'integer' | 'number' | 'object' | 'string';
10
+ export type Property = {
11
+ id: string;
12
+ name: string;
13
+ type: PropertyType;
14
+ enum?: string[];
15
+ objectId?: string;
16
+ required?: boolean;
17
+ searchable?: boolean;
18
+ formula?: string;
19
+ };
20
+ export type ActionType = 'create' | 'update' | 'delete';
21
+ export type Action = {
22
+ id: string;
23
+ name: string;
24
+ type: ActionType;
25
+ outputEvent: string;
26
+ inputProperties?: Property[];
27
+ };
28
+ export type ObjectInstance = {
29
+ id: string;
30
+ objectId: string;
31
+ name: string;
32
+ [key: string]: unknown;
33
+ };
34
+ export type ActionRequest = {
35
+ actionId: string;
36
+ input?: Record<string, unknown>;
37
+ };
38
+ export type HistoryEventType = 'create' | 'update' | 'delete' | 'queue' | 'print' | 'email' | 'upload';
39
+ export type HistoryType = 'correspondence' | 'document' | 'instance';
40
+ export type History = {
41
+ user: Reference;
42
+ event: string;
43
+ eventType: HistoryEventType;
44
+ type: HistoryType;
45
+ timestamp: string;
46
+ subject: Reference;
47
+ data?: HistoryData[];
48
+ };
49
+ export type HistoryData = {
50
+ property: string;
51
+ historicalValue?: unknown;
52
+ updatedValue?: unknown;
53
+ };
54
+ export type Reference = {
55
+ id: string;
56
+ name?: string;
57
+ };
58
+ export type ObjectOptions = {
59
+ sanitized?: boolean;
60
+ };
61
+ export declare class ObjectStore {
62
+ private services;
63
+ private objectId;
64
+ constructor(services: ApiServices, objectId: string);
65
+ get(options?: ObjectOptions): Promise<Obj>;
66
+ get(cb?: Callback<Obj>): void;
67
+ get(options: ObjectOptions, cb?: Callback<Obj>): void;
68
+ findInstances(filter?: Filter): Promise<ObjectInstance[]>;
69
+ findInstances(cb: Callback<ObjectInstance[]>): void;
70
+ findInstances(filter: Filter, cb: Callback<ObjectInstance[]>): void;
71
+ getInstance(id: string, cb?: Callback<ObjectInstance>): Promise<ObjectInstance> | undefined;
72
+ getInstanceHistory(id: string, cb?: Callback<History[]>): Promise<History[]> | undefined;
73
+ newInstance(input: ActionRequest, cb?: Callback<ObjectInstance>): Promise<ObjectInstance> | undefined;
74
+ instanceAction(id: string, input: ActionRequest, cb?: Callback<ObjectInstance>): Promise<ObjectInstance> | undefined;
75
+ }
76
+ export declare function useObject(objectId: string): ObjectStore;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ // Copyright (c) 2023 System Automation Corporation.
3
+ // This file is licensed under the MIT License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.useObject = exports.ObjectStore = void 0;
6
+ const react_1 = require("react");
7
+ const api_1 = require("../api");
8
+ class ObjectStore {
9
+ constructor(services, objectId) {
10
+ this.services = services;
11
+ this.objectId = objectId;
12
+ }
13
+ get(optionsOrCallback, cb) {
14
+ let options;
15
+ if (!cb) {
16
+ options = optionsOrCallback;
17
+ }
18
+ else if (typeof optionsOrCallback === 'function') {
19
+ cb = optionsOrCallback;
20
+ }
21
+ else {
22
+ options = optionsOrCallback;
23
+ }
24
+ const config = {
25
+ params: {
26
+ sanitizedVersion: options === null || options === void 0 ? void 0 : options.sanitized,
27
+ },
28
+ };
29
+ if (!cb) {
30
+ return this.services.get(`data/objects/${this.objectId}`, config);
31
+ }
32
+ this.services.get(`data/objects/${this.objectId}`, config, cb);
33
+ }
34
+ findInstances(filterOrCallback, cb) {
35
+ let filter;
36
+ if (cb) {
37
+ filter = filterOrCallback;
38
+ }
39
+ else if (typeof filterOrCallback === 'function') {
40
+ cb = filterOrCallback;
41
+ }
42
+ else {
43
+ filter = filterOrCallback;
44
+ }
45
+ const config = {
46
+ params: {
47
+ filter,
48
+ },
49
+ };
50
+ if (!cb) {
51
+ return this.services.get(`data/objects/${this.objectId}/instances`, config);
52
+ }
53
+ this.services.get(`data/objects/${this.objectId}/instances`, config, cb);
54
+ }
55
+ getInstance(id, cb) {
56
+ if (!cb) {
57
+ return this.services.get(`data/objects/${this.objectId}/instances/${id}`);
58
+ }
59
+ this.services.get(`data/objects/${this.objectId}/instances/${id}`, cb);
60
+ }
61
+ getInstanceHistory(id, cb) {
62
+ if (!cb) {
63
+ return this.services.get(`data/objects/${this.objectId}/instances/${id}/history`);
64
+ }
65
+ this.services.get(`data/objects/${this.objectId}/instances/${id}/history`, cb);
66
+ }
67
+ newInstance(input, cb) {
68
+ if (!cb) {
69
+ return this.services.post(`data/objects/${this.objectId}/instances/actions`, input);
70
+ }
71
+ this.services.post(`data/objects/${this.objectId}/instances/actions`, input, cb);
72
+ }
73
+ instanceAction(id, input, cb) {
74
+ if (!cb) {
75
+ return this.services.post(`data/objects/${this.objectId}/instances/${id}/actions`, input);
76
+ }
77
+ this.services.post(`data/objects/${this.objectId}/instances/${id}/actions`, input, cb);
78
+ }
79
+ }
80
+ exports.ObjectStore = ObjectStore;
81
+ function useObject(objectId) {
82
+ const services = (0, api_1.useApiServices)();
83
+ return (0, react_1.useMemo)(() => new ObjectStore(services, objectId), [services, objectId]);
84
+ }
85
+ exports.useObject = useObject;
86
+ //# sourceMappingURL=objects.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"objects.js","sourceRoot":"","sources":["../../src/objects/objects.ts"],"names":[],"mappings":";AAAA,oDAAoD;AACpD,+CAA+C;;;AAG/C,iCAAgC;AAChC,gCAA+D;AAyE/D,MAAa,WAAW;IACpB,YAAoB,QAAqB,EAAU,QAAgB;QAA/C,aAAQ,GAAR,QAAQ,CAAa;QAAU,aAAQ,GAAR,QAAQ,CAAQ;IAAG,CAAC;IAMvE,GAAG,CAAC,iBAAiD,EAAE,EAAkB;QACrE,IAAI,OAAkC,CAAC;QAEvC,IAAI,CAAC,EAAE,EAAE;YACL,OAAO,GAAG,iBAAkC,CAAC;SAChD;aAAM,IAAI,OAAO,iBAAiB,KAAK,UAAU,EAAE;YAChD,EAAE,GAAG,iBAAiB,CAAC;SAC1B;aAAM;YACH,O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AAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;AACpF,CAAC;AAJD,8BAIC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ // Copyright (c) 2023 System Automation Corporation.
3
+ // This file is licensed under the MIT License.
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
17
+ }) : function(o, v) {
18
+ o["default"] = v;
19
+ });
20
+ var __importStar = (this && this.__importStar) || function (mod) {
21
+ if (mod && mod.__esModule) return mod;
22
+ var result = {};
23
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
+ __setModuleDefault(result, mod);
25
+ return result;
26
+ };
27
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
+ return new (P || (P = Promise))(function (resolve, reject) {
30
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
34
+ });
35
+ };
36
+ var __importDefault = (this && this.__importDefault) || function (mod) {
37
+ return (mod && mod.__esModule) ? mod : { "default": mod };
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ const axios_1 = __importDefault(require("axios"));
41
+ const chai_1 = __importStar(require("chai"));
42
+ const dirty_chai_1 = __importDefault(require("dirty-chai"));
43
+ const msw_1 = require("msw");
44
+ const node_1 = require("msw/node");
45
+ const api_1 = require("../../api/api");
46
+ chai_1.default.use(dirty_chai_1.default);
47
+ const server = (0, node_1.setupServer)(msw_1.rest.get('http://localhost/item', (req, res, ctx) => {
48
+ return res(ctx.json({ id: 'item1', name: 'Item 1' }));
49
+ }));
50
+ function assertionCallback(done, fn) {
51
+ return function (err, data) {
52
+ if (err) {
53
+ done(err);
54
+ }
55
+ else {
56
+ try {
57
+ fn(data);
58
+ done();
59
+ }
60
+ catch (err2) {
61
+ done(err2);
62
+ }
63
+ }
64
+ };
65
+ }
66
+ describe('ApiServices', () => {
67
+ before(() => {
68
+ server.listen();
69
+ });
70
+ afterEach(() => {
71
+ server.resetHandlers();
72
+ });
73
+ after(() => {
74
+ server.close();
75
+ });
76
+ context('without authentication context', () => {
77
+ it('does not add Authorization header', () => __awaiter(void 0, void 0, void 0, function* () {
78
+ const services = new api_1.ApiServices(axios_1.default.create());
79
+ let authHeader = null;
80
+ server.use(msw_1.rest.get('http://localhost/', (req, res) => {
81
+ authHeader = req.headers.get('Authorization');
82
+ return res();
83
+ }));
84
+ yield services.get('/');
85
+ (0, chai_1.expect)(authHeader).to.be.null();
86
+ }));
87
+ });
88
+ context('with authentication context', () => {
89
+ it('adds Authorization header', () => __awaiter(void 0, void 0, void 0, function* () {
90
+ const services = new api_1.ApiServices(axios_1.default.create(), {
91
+ account: {
92
+ username: 'user',
93
+ tenantId: 'tenantid',
94
+ environment: 'environment',
95
+ homeAccountId: 'userid',
96
+ localAccountId: 'localid',
97
+ },
98
+ logout: () => { },
99
+ getAccessToken: () => Promise.resolve('accesstoken'),
100
+ });
101
+ let authHeader = null;
102
+ server.use(msw_1.rest.get('http://localhost/', (req, res) => {
103
+ authHeader = req.headers.get('Authorization');
104
+ return res();
105
+ }));
106
+ yield services.get('/');
107
+ (0, chai_1.expect)(authHeader).to.eql('Bearer accesstoken');
108
+ }));
109
+ });
110
+ describe('#get', () => {
111
+ const services = new api_1.ApiServices(axios_1.default.create());
112
+ it('returns response data', () => __awaiter(void 0, void 0, void 0, function* () {
113
+ const data = yield services.get('/item');
114
+ (0, chai_1.expect)(data).to.eql({ id: 'item1', name: 'Item 1' });
115
+ }));
116
+ it('returns response data in callback', (done) => {
117
+ services.get('/item', assertionCallback(done, (data) => {
118
+ (0, chai_1.expect)(data).to.eql({ id: 'item1', name: 'Item 1' });
119
+ }));
120
+ });
121
+ });
122
+ });
123
+ //# sourceMappingURL=api.unit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.unit.js","sourceRoot":"","sources":["../../../src/tests/api/api.unit.ts"],"names":[],"mappings":";AAAA,oDAAoD;AACpD,+CAA+C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE/C,kDAA0B;AAC1B,6CAAoC;AACpC,4DAAmC;AAEnC,6BAA2B;AAC3B,mCAAuC;AACvC,uCAA4C;AAE5C,cAAI,CAAC,GAAG,CAAC,oBAAS,CAAC,CAAC;AAEpB,MAAM,MAAM,GAAG,IAAA,kBAAW,EACtB,UAAI,CAAC,GAAG,CAAC,uBAAuB,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;IAChD,OAAO,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC;AAC1D,CAAC,CAAC,CACL,CAAC;AAEF,SAAS,iBAAiB,CAAC,IAAU,EAAE,EAA2B;IAC9D,OAAO,UAAU,GAAkB,EAAE,IAAc;QAC/C,IAAI,GAAG,EAAE;YACL,IAAI,CAAC,GAAG,CAAC,CAAC;SACb;aAAM;YACH,IAAI;gBACA,EAAE,CA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AE,CAAC,IAAI,EAAE,EAAE;YAC7C,QAAQ,CAAC,GAAG,CACR,OAAO,EACP,iBAAiB,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE;gBAC7B,IAAA,aAAM,EAAC,IAAI,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;YACzD,CAAC,CAAC,CACL,CAAC;QACN,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@evoke-platform/context",
3
+ "version": "1.0.0-SNAPSHOT.1679791794759",
4
+ "description": "Utilities that provide context to Evoke platform widgets",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "prepare": "husky install",
9
+ "lint": "npm run eslint && npm run prettier",
10
+ "lint:fix": "npm run eslint:fix && npm run prettier:fix",
11
+ "eslint": "eslint .",
12
+ "eslint:fix": "eslint --fix .",
13
+ "prettier": "prettier -c .",
14
+ "prettier:fix": "prettier --write .",
15
+ "build": "tsc",
16
+ "pretest": "npm run build",
17
+ "test": "mocha --recursive dist/tests"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+ssh://git@bitbucket.org/evoke-platform/evoke-context.git"
22
+ },
23
+ "keywords": [
24
+ "evoke"
25
+ ],
26
+ "author": "System Automation Corp.",
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://bitbucket.org/evoke-platform/evoke-context/issues"
30
+ },
31
+ "homepage": "https://bitbucket.org/evoke-platform/evoke-context#readme",
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "devDependencies": {
36
+ "@types/chai": "^4.3.4",
37
+ "@types/dirty-chai": "^2.0.2",
38
+ "@types/mocha": "^10.0.1",
39
+ "@types/node": "^18.15.7",
40
+ "@types/react": "^18.0.28",
41
+ "@typescript-eslint/eslint-plugin": "^5.56.0",
42
+ "@typescript-eslint/parser": "^5.56.0",
43
+ "chai": "^4.3.7",
44
+ "dirty-chai": "^2.0.1",
45
+ "eslint": "^8.36.0",
46
+ "eslint-config-prettier": "^8.8.0",
47
+ "eslint-plugin-prettier": "^4.2.1",
48
+ "eslint-plugin-react": "^7.32.2",
49
+ "husky": "^8.0.3",
50
+ "lint-staged": "^13.2.0",
51
+ "mocha": "^10.2.0",
52
+ "msw": "^1.2.1",
53
+ "prettier": "^2.8.6",
54
+ "typescript": "^5.0.2"
55
+ },
56
+ "peerDependencies": {
57
+ "@azure/msal-browser": ">=2",
58
+ "@azure/msal-react": ">=1",
59
+ "react": ">=18"
60
+ },
61
+ "lint-staged": {
62
+ "*.{ts,tsx,js,json,md}": [
63
+ "npm run lint:fix"
64
+ ]
65
+ },
66
+ "dependencies": {
67
+ "axios": "^1.3.4"
68
+ }
69
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright (c) 2023 System Automation Corporation.
2
+ // This file is licensed under the MIT License.
3
+
4
+ import { createContext, ReactNode, useContext, useMemo } from 'react';
5
+ import { useAuthenticationContext } from '../authentication';
6
+ import { ApiServices, createApiServices, defaultApiServices } from './api';
7
+
8
+ const ApiServicesContext = createContext<ApiServices>(defaultApiServices);
9
+
10
+ ApiServicesContext.displayName = 'ApiServiceContext';
11
+
12
+ export type ApiServicesProviderProps = {
13
+ children?: ReactNode;
14
+ };
15
+
16
+ function ApiServicesProvider(props: ApiServicesProviderProps) {
17
+ const { children } = props;
18
+
19
+ const authContext = useAuthenticationContext();
20
+ const apiServices = useMemo(() => createApiServices(authContext), [authContext]);
21
+
22
+ return <ApiServicesContext.Provider value={apiServices}>{children}</ApiServicesContext.Provider>;
23
+ }
24
+
25
+ export function useApiServices() {
26
+ return useContext(ApiServicesContext);
27
+ }
28
+
29
+ export default ApiServicesProvider;
package/src/api/api.ts ADDED
@@ -0,0 +1,162 @@
1
+ // Copyright (c) 2023 System Automation Corporation.
2
+ // This file is licensed under the MIT License.
3
+
4
+ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
5
+ import { AuthenticationContext } from '../authentication/AuthenticationContextProvider';
6
+
7
+ export type Data = Record<string, unknown> | FormData;
8
+ export type Callback<T> = (error?: Error | null, result?: T) => void;
9
+
10
+ export class ApiServices {
11
+ constructor(private api: AxiosInstance, authContext?: AuthenticationContext) {
12
+ if (authContext) {
13
+ this.api.interceptors.request.use(async (config) => {
14
+ const token = await authContext.getAccessToken();
15
+
16
+ config.headers = Object.assign({}, config.headers, {
17
+ Authorization: `Bearer ${token}`,
18
+ });
19
+
20
+ return config;
21
+ });
22
+ }
23
+ }
24
+
25
+ private async promiseOrCallback<T>(promise: Promise<AxiosResponse<T>>, cb?: Callback<T>) {
26
+ if (!cb) {
27
+ const response = await promise;
28
+
29
+ return response.data;
30
+ }
31
+
32
+ promise.then(
33
+ (result) => cb(null, result.data),
34
+ (error) => cb(error),
35
+ );
36
+ }
37
+
38
+ get<T, D>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
39
+ get<T>(url: string, cb: Callback<T>): void;
40
+ get<T, D>(url: string, config: AxiosRequestConfig<D>, cb: Callback<T>): void;
41
+
42
+ async get<T, D>(url: string, configOrCallback?: AxiosRequestConfig<D> | Callback<T>, cb?: Callback<T>) {
43
+ let config: AxiosRequestConfig<D> | undefined;
44
+
45
+ if (cb) {
46
+ config = configOrCallback as AxiosRequestConfig<D>;
47
+ } else if (typeof configOrCallback === 'function') {
48
+ cb = configOrCallback;
49
+ } else {
50
+ config = configOrCallback;
51
+ }
52
+
53
+ return this.promiseOrCallback(this.api.get<T, AxiosResponse<T, D>>(url, config), cb);
54
+ }
55
+
56
+ post<T, D>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>;
57
+ post<T>(url: string, cb: Callback<T>): void;
58
+ post<T, D>(url: string, data: D, cb: Callback<T>): void;
59
+ post<T, D>(url: string, data: D, config: AxiosRequestConfig<D>, cb: Callback<T>): void;
60
+
61
+ async post<T, D extends Data>(
62
+ url: string,
63
+ dataOrCallback?: D | Callback<T>,
64
+ configOrCallback?: AxiosRequestConfig<D> | Callback<T>,
65
+ cb?: Callback<T>,
66
+ ) {
67
+ let data: D | undefined;
68
+ let config: AxiosRequestConfig<D> | undefined;
69
+
70
+ if (cb) {
71
+ data = dataOrCallback as D;
72
+ config = configOrCallback as AxiosRequestConfig<D>;
73
+ } else if (typeof configOrCallback === 'function') {
74
+ data = dataOrCallback as D;
75
+ cb = configOrCallback;
76
+ } else if (typeof dataOrCallback === 'function') {
77
+ cb = dataOrCallback;
78
+ }
79
+
80
+ return this.promiseOrCallback(this.api.post(url, data, config), cb);
81
+ }
82
+
83
+ patch<T, D>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>;
84
+ patch<T>(url: string, cb: Callback<T>): void;
85
+ patch<T, D>(url: string, data: D, cb: Callback<T>): void;
86
+ patch<T, D>(url: string, data: D, config: AxiosRequestConfig<D>, cb: Callback<T>): void;
87
+
88
+ async patch<T, D extends Data>(
89
+ url: string,
90
+ dataOrCallback?: D | Callback<T>,
91
+ configOrCallback?: AxiosRequestConfig<D> | Callback<T>,
92
+ cb?: Callback<T>,
93
+ ) {
94
+ let data: D | undefined;
95
+ let config: AxiosRequestConfig<D> | undefined;
96
+
97
+ if (cb) {
98
+ data = dataOrCallback as D;
99
+ config = configOrCallback as AxiosRequestConfig<D>;
100
+ } else if (typeof configOrCallback === 'function') {
101
+ data = dataOrCallback as D;
102
+ cb = configOrCallback;
103
+ } else if (typeof dataOrCallback === 'function') {
104
+ cb = dataOrCallback;
105
+ }
106
+
107
+ return this.promiseOrCallback(this.api.patch(url, data, config), cb);
108
+ }
109
+
110
+ put<T, D>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>;
111
+ put<T>(url: string, cb: Callback<T>): void;
112
+ put<T, D>(url: string, data: D, cb: Callback<T>): void;
113
+ put<T, D>(url: string, data: D, config: AxiosRequestConfig<D>, cb: Callback<T>): void;
114
+
115
+ async put<T, D extends Data>(
116
+ url: string,
117
+ dataOrCallback?: D | Callback<T>,
118
+ configOrCallback?: AxiosRequestConfig<D> | Callback<T>,
119
+ cb?: Callback<T>,
120
+ ) {
121
+ let data: D | undefined;
122
+ let config: AxiosRequestConfig<D> | undefined;
123
+
124
+ if (cb) {
125
+ data = dataOrCallback as D;
126
+ config = configOrCallback as AxiosRequestConfig<D>;
127
+ } else if (typeof configOrCallback === 'function') {
128
+ data = dataOrCallback as D;
129
+ cb = configOrCallback;
130
+ } else if (typeof dataOrCallback === 'function') {
131
+ cb = dataOrCallback;
132
+ }
133
+
134
+ return this.promiseOrCallback(this.api.put(url, data, config), cb);
135
+ }
136
+
137
+ delete<T, D>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
138
+ delete<T>(url: string, cb: Callback<T>): void;
139
+ delete<T, D>(url: string, config: AxiosRequestConfig<D>, cb: Callback<T>): void;
140
+
141
+ async delete<T, D>(url: string, configOrCallback?: AxiosRequestConfig<D> | Callback<T>, cb?: Callback<T>) {
142
+ let config: AxiosRequestConfig<D> | undefined;
143
+
144
+ if (cb) {
145
+ config = configOrCallback as AxiosRequestConfig<D>;
146
+ } else if (typeof configOrCallback === 'function') {
147
+ cb = configOrCallback;
148
+ } else {
149
+ config = configOrCallback;
150
+ }
151
+
152
+ return this.promiseOrCallback(this.api.delete<T, AxiosResponse<T, D>>(url, config), cb);
153
+ }
154
+ }
155
+
156
+ export function createApiServices(authContext?: AuthenticationContext) {
157
+ const baseURL = process.env.REACT_APP_API_ROOT || `${globalThis.location?.origin}/api`;
158
+
159
+ return new ApiServices(axios.create({ baseURL }), authContext);
160
+ }
161
+
162
+ export const defaultApiServices = createApiServices();
@@ -0,0 +1,6 @@
1
+ // Copyright (c) 2023 System Automation Corporation.
2
+ // This file is licensed under the MIT License.
3
+
4
+ export * from './api';
5
+ export * from './ApiServicesProvider';
6
+ export { default as ApiServicesProvider } from './ApiServicesProvider';