@churchapps/helpers 1.0.11 → 1.0.12
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/LICENSE +21 -21
- package/README.md +15 -15
- package/dist/interfaces/Messaging.d.ts +20 -2
- package/dist/interfaces/Messaging.d.ts.map +1 -1
- package/dist/interfaces/index.d.ts +1 -0
- package/dist/interfaces/index.d.ts.map +1 -1
- package/dist/interfaces/index.js +1 -0
- package/dist/interfaces/index.js.map +1 -1
- package/package.json +38 -38
- package/src/ApiHelper.ts +132 -132
- package/src/AppearanceHelper.ts +69 -69
- package/src/ArrayHelper.ts +156 -156
- package/src/CommonEnvironmentHelper.ts +80 -80
- package/src/CurrencyHelper.ts +10 -10
- package/src/DateHelper.ts +122 -122
- package/src/DonationHelper.ts +26 -26
- package/src/ErrorHelper.ts +36 -36
- package/src/EventHelper.ts +49 -49
- package/src/FileHelper.ts +31 -31
- package/src/PersonHelper.ts +75 -75
- package/src/UniqueIdHelper.ts +36 -36
- package/src/UserHelper.ts +59 -59
- package/src/index.ts +12 -12
- package/src/interfaces/Access.ts +24 -24
- package/src/interfaces/Attendance.ts +8 -8
- package/src/interfaces/Content.ts +10 -10
- package/src/interfaces/Doing.ts +24 -24
- package/src/interfaces/Donation.ts +45 -45
- package/src/interfaces/Error.ts +17 -17
- package/src/interfaces/Membership.ts +51 -51
- package/src/interfaces/Messaging.ts +11 -20
- package/src/interfaces/Permissions.ts +68 -68
- package/src/interfaces/Reporting.ts +7 -7
- package/src/interfaces/UserContextInterface.ts +13 -13
- package/src/interfaces/index.ts +14 -12
- package/tsconfig.json +36 -36
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023 Live Church Solutions
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Live Church Solutions
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
# Helpers
|
|
2
|
-
|
|
3
|
-
Library of helper functions not specific to any project or framework.
|
|
4
|
-
|
|
5
|
-
## To Test
|
|
6
|
-
|
|
7
|
-
1. After making changes run `npm build` followed by `npm link` to expose the package locally
|
|
8
|
-
2. In your test project run `@npm link @churchapps/helpers`
|
|
9
|
-
3. Rerun both after changes
|
|
10
|
-
|
|
11
|
-
## To Publish
|
|
12
|
-
|
|
13
|
-
1. Update version number in package.json
|
|
14
|
-
2. Run `npm run build`
|
|
15
|
-
3. Run `npm publish --access=public`
|
|
1
|
+
# Helpers
|
|
2
|
+
|
|
3
|
+
Library of helper functions not specific to any project or framework.
|
|
4
|
+
|
|
5
|
+
## To Test
|
|
6
|
+
|
|
7
|
+
1. After making changes run `npm build` followed by `npm link` to expose the package locally
|
|
8
|
+
2. In your test project run `@npm link @churchapps/helpers`
|
|
9
|
+
3. Rerun both after changes
|
|
10
|
+
|
|
11
|
+
## To Publish
|
|
12
|
+
|
|
13
|
+
1. Update version number in package.json
|
|
14
|
+
2. Run `npm run build`
|
|
15
|
+
3. Run `npm publish --access=public`
|
|
@@ -3,7 +3,7 @@ export interface ConnectionInterface {
|
|
|
3
3
|
id?: string;
|
|
4
4
|
churchId?: string;
|
|
5
5
|
conversationId?: string;
|
|
6
|
-
|
|
6
|
+
personId?: string;
|
|
7
7
|
displayName?: string;
|
|
8
8
|
timeJoined?: Date;
|
|
9
9
|
socketId?: string;
|
|
@@ -27,7 +27,6 @@ export interface MessageInterface {
|
|
|
27
27
|
id?: string;
|
|
28
28
|
churchId?: string;
|
|
29
29
|
conversationId?: string;
|
|
30
|
-
userId?: string;
|
|
31
30
|
personId?: string;
|
|
32
31
|
displayName?: string;
|
|
33
32
|
timeSent?: Date;
|
|
@@ -36,6 +35,24 @@ export interface MessageInterface {
|
|
|
36
35
|
content?: string;
|
|
37
36
|
person?: PersonInterface;
|
|
38
37
|
}
|
|
38
|
+
export interface NotificationInterface {
|
|
39
|
+
id?: string;
|
|
40
|
+
churchId?: string;
|
|
41
|
+
personId?: string;
|
|
42
|
+
contentType?: string;
|
|
43
|
+
contentId: string;
|
|
44
|
+
timeSent?: Date;
|
|
45
|
+
isNew: boolean;
|
|
46
|
+
message?: string;
|
|
47
|
+
deliveryMethod?: string;
|
|
48
|
+
}
|
|
49
|
+
export interface NotificationPreferenceInterface {
|
|
50
|
+
id?: string;
|
|
51
|
+
churchId?: string;
|
|
52
|
+
personId?: string;
|
|
53
|
+
allowPush: boolean;
|
|
54
|
+
emailFrequency: string;
|
|
55
|
+
}
|
|
39
56
|
export interface PrivateMessageInterface {
|
|
40
57
|
id?: string;
|
|
41
58
|
churchId?: string;
|
|
@@ -45,6 +62,7 @@ export interface PrivateMessageInterface {
|
|
|
45
62
|
notifyPersonId?: string;
|
|
46
63
|
conversation?: ConversationInterface;
|
|
47
64
|
person?: PersonInterface;
|
|
65
|
+
deliveryMethod?: string;
|
|
48
66
|
}
|
|
49
67
|
export interface SocketActionHandlerInterface {
|
|
50
68
|
action: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Messaging.d.ts","sourceRoot":"","sources":["../../src/interfaces/Messaging.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,WAAW,mBAAmB;IAAG,EAAE,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,E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CAAE;AAC5G,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,eAAe,GAAG,SAAS,GAAG,YAAY,GAAG,eAAe,GAAG,UAAU,GAAG,gBAAgB,GAAG,kBAAkB,GAAG,iBAAiB,GAAG,WAAW,CAAC;AAClM,MAAM,WAAW,sBAAsB;IAAI,MAAM,EAAE,mBAAmB,CAAC;IAAC,IAAI,EAAE,GAAG,CAAA;CAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,wBAAwB,CAAC;AACvC,cAAc,eAAe,CAAC;AAI9B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
|
package/dist/interfaces/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Error"), exports);
|
|
17
18
|
__exportStar(require("./UserContextInterface"), exports);
|
|
18
19
|
__exportStar(require("./Permissions"), exports);
|
|
19
20
|
// API interfaces
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AACvC,gDAA8B;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,yDAAuC;AACvC,gDAA8B;AAG9B,iBAAiB;AACjB,2CAAyB;AACzB,+CAA6B;AAC7B,4CAA0B;AAC1B,0CAAwB;AACxB,6CAA2B;AAC3B,+CAA6B;AAC7B,8CAA4B;AAC5B,8CAA4B"}
|
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@churchapps/helpers",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Library of helper functions not specific to any one ChurchApps project or framework.",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
-
"clean": "rimraf dist",
|
|
9
|
-
"tsc": "tsc",
|
|
10
|
-
"build": "npm-run-all clean tsc"
|
|
11
|
-
},
|
|
12
|
-
"repository": {
|
|
13
|
-
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/LiveChurchSolutions/Helpers.git"
|
|
15
|
-
},
|
|
16
|
-
"keywords": [
|
|
17
|
-
"ChurchApps"
|
|
18
|
-
],
|
|
19
|
-
"author": "ChurchApps.org",
|
|
20
|
-
"license": "MIT",
|
|
21
|
-
"bugs": {
|
|
22
|
-
"url": "https://github.com/LiveChurchSolutions/Helpers/issues"
|
|
23
|
-
},
|
|
24
|
-
"homepage": "https://github.com/LiveChurchSolutions/Helpers#readme",
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"@types/node": "^20.5.8",
|
|
27
|
-
"npm-run-all": "^4.1.5",
|
|
28
|
-
"rimraf": "^5.0.1",
|
|
29
|
-
"typescript": "^5.2.2"
|
|
30
|
-
},
|
|
31
|
-
"dependencies": {
|
|
32
|
-
"axios": "^1.5.0",
|
|
33
|
-
"date-fns": "^2.30.0",
|
|
34
|
-
"dayjs": "^1.11.9",
|
|
35
|
-
"react-ga4": "^2.1.0",
|
|
36
|
-
"rrule": "^2.7.2"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@churchapps/helpers",
|
|
3
|
+
"version": "1.0.12",
|
|
4
|
+
"description": "Library of helper functions not specific to any one ChurchApps project or framework.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"clean": "rimraf dist",
|
|
9
|
+
"tsc": "tsc",
|
|
10
|
+
"build": "npm-run-all clean tsc"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/LiveChurchSolutions/Helpers.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"ChurchApps"
|
|
18
|
+
],
|
|
19
|
+
"author": "ChurchApps.org",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/LiveChurchSolutions/Helpers/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/LiveChurchSolutions/Helpers#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.5.8",
|
|
27
|
+
"npm-run-all": "^4.1.5",
|
|
28
|
+
"rimraf": "^5.0.1",
|
|
29
|
+
"typescript": "^5.2.2"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"axios": "^1.5.0",
|
|
33
|
+
"date-fns": "^2.30.0",
|
|
34
|
+
"dayjs": "^1.11.9",
|
|
35
|
+
"react-ga4": "^2.1.0",
|
|
36
|
+
"rrule": "^2.7.2"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/ApiHelper.ts
CHANGED
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
|
|
2
|
-
import { ErrorHelper } from "./ErrorHelper";
|
|
3
|
-
|
|
4
|
-
export class ApiHelper {
|
|
5
|
-
|
|
6
|
-
static apiConfigs: ApiConfig[] = [];
|
|
7
|
-
static isAuthenticated = false;
|
|
8
|
-
static onRequest: (url:string, requestOptions:any) => void;
|
|
9
|
-
static onError: (url:string, requestOptions:any, error: any) => void;
|
|
10
|
-
|
|
11
|
-
static getConfig(keyName: string) {
|
|
12
|
-
let result: ApiConfig = null;
|
|
13
|
-
this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
|
|
14
|
-
//if (result === null) throw new Error("Unconfigured API: " + keyName);
|
|
15
|
-
return result;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
static setDefaultPermissions(jwt: string) {
|
|
19
|
-
this.apiConfigs.forEach(config => {
|
|
20
|
-
config.jwt = jwt;
|
|
21
|
-
config.permisssions = [];
|
|
22
|
-
});
|
|
23
|
-
this.isAuthenticated = true;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
|
|
27
|
-
this.apiConfigs.forEach(config => {
|
|
28
|
-
if (config.keyName === keyName) {
|
|
29
|
-
config.jwt = jwt;
|
|
30
|
-
config.permisssions = permissions;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
this.isAuthenticated = true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
static clearPermissions() {
|
|
37
|
-
this.apiConfigs.forEach(config => { config.jwt = ""; config.permisssions = []; });
|
|
38
|
-
this.isAuthenticated = false;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
static async get(path: string, apiName: ApiListType) {
|
|
42
|
-
const config = this.getConfig(apiName);
|
|
43
|
-
const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
|
|
44
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
static async getAnonymous(path: string, apiName: ApiListType) {
|
|
48
|
-
const config = this.getConfig(apiName);
|
|
49
|
-
const requestOptions = { method: "GET" };
|
|
50
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
static async post(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
54
|
-
const config = this.getConfig(apiName);
|
|
55
|
-
const requestOptions = {
|
|
56
|
-
method: "POST",
|
|
57
|
-
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
58
|
-
body: JSON.stringify(data)
|
|
59
|
-
};
|
|
60
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
static async patch(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
64
|
-
const config = this.getConfig(apiName);
|
|
65
|
-
const requestOptions = {
|
|
66
|
-
method: "PATCH",
|
|
67
|
-
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
68
|
-
body: JSON.stringify(data)
|
|
69
|
-
};
|
|
70
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
static async delete(path: string, apiName: ApiListType) {
|
|
74
|
-
const config = this.getConfig(apiName);
|
|
75
|
-
const requestOptions = {
|
|
76
|
-
method: "DELETE",
|
|
77
|
-
headers: { Authorization: "Bearer " + config.jwt }
|
|
78
|
-
};
|
|
79
|
-
if (this.onRequest) this.onRequest(config.url + path, requestOptions);
|
|
80
|
-
try {
|
|
81
|
-
const response = await fetch(config.url + path, requestOptions);
|
|
82
|
-
if (!response.ok) await this.throwApiError(response);
|
|
83
|
-
} catch (e) {
|
|
84
|
-
console.log(e);
|
|
85
|
-
if (this.onError) this.onError(config.url + path, requestOptions, e);
|
|
86
|
-
throw (e);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
static async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
91
|
-
const config = this.getConfig(apiName);
|
|
92
|
-
const requestOptions = {
|
|
93
|
-
method: "POST",
|
|
94
|
-
headers: { "Content-Type": "application/json" },
|
|
95
|
-
body: JSON.stringify(data)
|
|
96
|
-
};
|
|
97
|
-
console.log(config.url + path, requestOptions);
|
|
98
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
static async fetchWithErrorHandling(url: string, requestOptions: any) {
|
|
102
|
-
if (this.onRequest) this.onRequest(url, requestOptions);
|
|
103
|
-
try {
|
|
104
|
-
const response = await fetch(url, requestOptions);
|
|
105
|
-
if (!response.ok) await this.throwApiError(response);
|
|
106
|
-
else {
|
|
107
|
-
if (response.status !== 204 ) {
|
|
108
|
-
return response.json();
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
} catch (e) {
|
|
112
|
-
console.log("Error loading url: " + url);
|
|
113
|
-
console.log(e)
|
|
114
|
-
throw (e);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private static async throwApiError(response: Response) {
|
|
119
|
-
let msg = response.statusText;
|
|
120
|
-
try {
|
|
121
|
-
msg = await response.text();
|
|
122
|
-
} catch {}
|
|
123
|
-
try {
|
|
124
|
-
const json = await response.json();
|
|
125
|
-
msg = json.errors[0];
|
|
126
|
-
} catch { }
|
|
127
|
-
console.log("RESPONSE", response)
|
|
128
|
-
ErrorHelper.logError(response.status.toString(), response.url, msg);
|
|
129
|
-
throw new Error(msg || "Error");
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
}
|
|
1
|
+
import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
|
|
2
|
+
import { ErrorHelper } from "./ErrorHelper";
|
|
3
|
+
|
|
4
|
+
export class ApiHelper {
|
|
5
|
+
|
|
6
|
+
static apiConfigs: ApiConfig[] = [];
|
|
7
|
+
static isAuthenticated = false;
|
|
8
|
+
static onRequest: (url:string, requestOptions:any) => void;
|
|
9
|
+
static onError: (url:string, requestOptions:any, error: any) => void;
|
|
10
|
+
|
|
11
|
+
static getConfig(keyName: string) {
|
|
12
|
+
let result: ApiConfig = null;
|
|
13
|
+
this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
|
|
14
|
+
//if (result === null) throw new Error("Unconfigured API: " + keyName);
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static setDefaultPermissions(jwt: string) {
|
|
19
|
+
this.apiConfigs.forEach(config => {
|
|
20
|
+
config.jwt = jwt;
|
|
21
|
+
config.permisssions = [];
|
|
22
|
+
});
|
|
23
|
+
this.isAuthenticated = true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
|
|
27
|
+
this.apiConfigs.forEach(config => {
|
|
28
|
+
if (config.keyName === keyName) {
|
|
29
|
+
config.jwt = jwt;
|
|
30
|
+
config.permisssions = permissions;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
this.isAuthenticated = true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static clearPermissions() {
|
|
37
|
+
this.apiConfigs.forEach(config => { config.jwt = ""; config.permisssions = []; });
|
|
38
|
+
this.isAuthenticated = false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static async get(path: string, apiName: ApiListType) {
|
|
42
|
+
const config = this.getConfig(apiName);
|
|
43
|
+
const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
|
|
44
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static async getAnonymous(path: string, apiName: ApiListType) {
|
|
48
|
+
const config = this.getConfig(apiName);
|
|
49
|
+
const requestOptions = { method: "GET" };
|
|
50
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static async post(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
54
|
+
const config = this.getConfig(apiName);
|
|
55
|
+
const requestOptions = {
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
58
|
+
body: JSON.stringify(data)
|
|
59
|
+
};
|
|
60
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static async patch(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
64
|
+
const config = this.getConfig(apiName);
|
|
65
|
+
const requestOptions = {
|
|
66
|
+
method: "PATCH",
|
|
67
|
+
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
68
|
+
body: JSON.stringify(data)
|
|
69
|
+
};
|
|
70
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static async delete(path: string, apiName: ApiListType) {
|
|
74
|
+
const config = this.getConfig(apiName);
|
|
75
|
+
const requestOptions = {
|
|
76
|
+
method: "DELETE",
|
|
77
|
+
headers: { Authorization: "Bearer " + config.jwt }
|
|
78
|
+
};
|
|
79
|
+
if (this.onRequest) this.onRequest(config.url + path, requestOptions);
|
|
80
|
+
try {
|
|
81
|
+
const response = await fetch(config.url + path, requestOptions);
|
|
82
|
+
if (!response.ok) await this.throwApiError(response);
|
|
83
|
+
} catch (e) {
|
|
84
|
+
console.log(e);
|
|
85
|
+
if (this.onError) this.onError(config.url + path, requestOptions, e);
|
|
86
|
+
throw (e);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
91
|
+
const config = this.getConfig(apiName);
|
|
92
|
+
const requestOptions = {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: { "Content-Type": "application/json" },
|
|
95
|
+
body: JSON.stringify(data)
|
|
96
|
+
};
|
|
97
|
+
console.log(config.url + path, requestOptions);
|
|
98
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static async fetchWithErrorHandling(url: string, requestOptions: any) {
|
|
102
|
+
if (this.onRequest) this.onRequest(url, requestOptions);
|
|
103
|
+
try {
|
|
104
|
+
const response = await fetch(url, requestOptions);
|
|
105
|
+
if (!response.ok) await this.throwApiError(response);
|
|
106
|
+
else {
|
|
107
|
+
if (response.status !== 204 ) {
|
|
108
|
+
return response.json();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
} catch (e) {
|
|
112
|
+
console.log("Error loading url: " + url);
|
|
113
|
+
console.log(e)
|
|
114
|
+
throw (e);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private static async throwApiError(response: Response) {
|
|
119
|
+
let msg = response.statusText;
|
|
120
|
+
try {
|
|
121
|
+
msg = await response.text();
|
|
122
|
+
} catch {}
|
|
123
|
+
try {
|
|
124
|
+
const json = await response.json();
|
|
125
|
+
msg = json.errors[0];
|
|
126
|
+
} catch { }
|
|
127
|
+
console.log("RESPONSE", response)
|
|
128
|
+
ErrorHelper.logError(response.status.toString(), response.url, msg);
|
|
129
|
+
throw new Error(msg || "Error");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
}
|
package/src/AppearanceHelper.ts
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
export interface AppearanceInterface { primaryColor?: string, primaryContrast?: string, secondaryColor?: string, secondaryContrast?: string, logoLight?: string, logoDark?: string, favicon_400x400?: string, favicon_16x16?: string }
|
|
3
|
-
|
|
4
|
-
export class AppearanceHelper {
|
|
5
|
-
|
|
6
|
-
public static getLogoDark(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
7
|
-
return (appearanceSettings?.logoDark) ? appearanceSettings.logoDark : defaultLogo;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public static getLogoLight(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
11
|
-
return (appearanceSettings?.logoLight) ? appearanceSettings.logoLight : defaultLogo;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public static getFavicon(appearanceSettings: AppearanceInterface, size: "400" | "16") {
|
|
15
|
-
if (size === "400") {
|
|
16
|
-
return appearanceSettings?.favicon_400x400;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (size === "16") {
|
|
20
|
-
return appearanceSettings?.favicon_16x16;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public static getLogo(appearanceSettings: AppearanceInterface, defaultLogoLight: string, defaultLogoDark: string, backgroundColor: string) {
|
|
27
|
-
const isDark = (appearanceSettings.logoDark) ? this.isDark(backgroundColor) : false;
|
|
28
|
-
if (isDark) return this.getLogoDark(appearanceSettings, defaultLogoDark);
|
|
29
|
-
else return this.getLogoLight(appearanceSettings, defaultLogoLight);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
private static isDark(backgroundColor: string) {
|
|
33
|
-
let valid = false;
|
|
34
|
-
let r = 0;
|
|
35
|
-
let g = 0;
|
|
36
|
-
let b = 0;
|
|
37
|
-
|
|
38
|
-
if (backgroundColor.match(/#[0-9a-fA-F{6}]/)) {
|
|
39
|
-
r = this.getHexValue(backgroundColor.substring(1, 2));
|
|
40
|
-
g = this.getHexValue(backgroundColor.substring(3, 4));
|
|
41
|
-
b = this.getHexValue(backgroundColor.substring(5, 6));
|
|
42
|
-
valid = true;
|
|
43
|
-
} else if (backgroundColor.match(/#[0-9a-fA-F{3}]/)) {
|
|
44
|
-
r = this.getHexValue(backgroundColor.substring(1, 1));
|
|
45
|
-
g = this.getHexValue(backgroundColor.substring(2, 2));
|
|
46
|
-
b = this.getHexValue(backgroundColor.substring(3, 3));
|
|
47
|
-
valid = true;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (!valid) return false;
|
|
51
|
-
else {
|
|
52
|
-
//HSP brightness formula. Some colors have a bigger impact on our perceived brightness than others.
|
|
53
|
-
const rWeight = .299 * Math.pow(r, 2);
|
|
54
|
-
const gWeight = .587 * Math.pow(g, 2);
|
|
55
|
-
const bWeight = .114 * Math.pow(b, 2);
|
|
56
|
-
const brightness = Math.sqrt(rWeight + gWeight + bWeight);
|
|
57
|
-
//return brightness < 128; //
|
|
58
|
-
return brightness < 156;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private static getHexValue(hex: string) {
|
|
64
|
-
let result = parseInt(hex, 16);
|
|
65
|
-
if (hex.length === 1) result = result * 16;
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}
|
|
1
|
+
|
|
2
|
+
export interface AppearanceInterface { primaryColor?: string, primaryContrast?: string, secondaryColor?: string, secondaryContrast?: string, logoLight?: string, logoDark?: string, favicon_400x400?: string, favicon_16x16?: string }
|
|
3
|
+
|
|
4
|
+
export class AppearanceHelper {
|
|
5
|
+
|
|
6
|
+
public static getLogoDark(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
7
|
+
return (appearanceSettings?.logoDark) ? appearanceSettings.logoDark : defaultLogo;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public static getLogoLight(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
11
|
+
return (appearanceSettings?.logoLight) ? appearanceSettings.logoLight : defaultLogo;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static getFavicon(appearanceSettings: AppearanceInterface, size: "400" | "16") {
|
|
15
|
+
if (size === "400") {
|
|
16
|
+
return appearanceSettings?.favicon_400x400;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (size === "16") {
|
|
20
|
+
return appearanceSettings?.favicon_16x16;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public static getLogo(appearanceSettings: AppearanceInterface, defaultLogoLight: string, defaultLogoDark: string, backgroundColor: string) {
|
|
27
|
+
const isDark = (appearanceSettings.logoDark) ? this.isDark(backgroundColor) : false;
|
|
28
|
+
if (isDark) return this.getLogoDark(appearanceSettings, defaultLogoDark);
|
|
29
|
+
else return this.getLogoLight(appearanceSettings, defaultLogoLight);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private static isDark(backgroundColor: string) {
|
|
33
|
+
let valid = false;
|
|
34
|
+
let r = 0;
|
|
35
|
+
let g = 0;
|
|
36
|
+
let b = 0;
|
|
37
|
+
|
|
38
|
+
if (backgroundColor.match(/#[0-9a-fA-F{6}]/)) {
|
|
39
|
+
r = this.getHexValue(backgroundColor.substring(1, 2));
|
|
40
|
+
g = this.getHexValue(backgroundColor.substring(3, 4));
|
|
41
|
+
b = this.getHexValue(backgroundColor.substring(5, 6));
|
|
42
|
+
valid = true;
|
|
43
|
+
} else if (backgroundColor.match(/#[0-9a-fA-F{3}]/)) {
|
|
44
|
+
r = this.getHexValue(backgroundColor.substring(1, 1));
|
|
45
|
+
g = this.getHexValue(backgroundColor.substring(2, 2));
|
|
46
|
+
b = this.getHexValue(backgroundColor.substring(3, 3));
|
|
47
|
+
valid = true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!valid) return false;
|
|
51
|
+
else {
|
|
52
|
+
//HSP brightness formula. Some colors have a bigger impact on our perceived brightness than others.
|
|
53
|
+
const rWeight = .299 * Math.pow(r, 2);
|
|
54
|
+
const gWeight = .587 * Math.pow(g, 2);
|
|
55
|
+
const bWeight = .114 * Math.pow(b, 2);
|
|
56
|
+
const brightness = Math.sqrt(rWeight + gWeight + bWeight);
|
|
57
|
+
//return brightness < 128; //
|
|
58
|
+
return brightness < 156;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private static getHexValue(hex: string) {
|
|
64
|
+
let result = parseInt(hex, 16);
|
|
65
|
+
if (hex.length === 1) result = result * 16;
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|