@churchapps/helpers 1.2.3 → 1.2.5
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/.eslintrc.json +28 -28
- package/.github/FUNDING.yml +1 -1
- package/.prettierrc +11 -11
- package/.yarnrc.yml +5 -5
- package/LICENSE +21 -21
- package/README.md +15 -15
- package/dist/ApiHelper.d.ts.map +1 -1
- package/dist/ApiHelper.js +4 -10
- package/dist/ApiHelper.js.map +1 -1
- package/dist/ArrayHelper.js +2 -1
- package/dist/ArrayHelper.js.map +1 -1
- package/dist/CommonEnvironmentHelper.d.ts +1 -1
- package/dist/CommonEnvironmentHelper.d.ts.map +1 -1
- package/dist/CommonEnvironmentHelper.js +5 -5
- package/dist/CommonEnvironmentHelper.js.map +1 -1
- package/dist/DateHelper.d.ts.map +1 -1
- package/dist/DateHelper.js +2 -0
- package/dist/DateHelper.js.map +1 -1
- package/dist/PersonHelper.js +2 -2
- package/dist/PersonHelper.js.map +1 -1
- package/dist/UserHelper.d.ts.map +1 -1
- package/dist/UserHelper.js +6 -2
- package/dist/UserHelper.js.map +1 -1
- package/package.json +50 -49
- package/scripts/build-cjs.js +32 -32
- package/src/ApiHelper.ts +169 -176
- package/src/AppearanceHelper.ts +69 -69
- package/src/ArrayHelper.ts +157 -157
- package/src/CommonEnvironmentHelper.ts +104 -104
- package/src/CurrencyHelper.ts +10 -10
- package/src/DateHelper.ts +154 -153
- package/src/DonationHelper.ts +26 -26
- package/src/ErrorHelper.ts +39 -39
- package/src/EventHelper.ts +49 -49
- package/src/FileHelper.ts +55 -55
- package/src/PersonHelper.ts +82 -82
- package/src/UniqueIdHelper.ts +36 -36
- package/src/UserHelper.ts +62 -59
- package/src/index.ts +15 -15
- package/src/interfaces/Access.ts +138 -138
- package/src/interfaces/Attendance.ts +45 -45
- package/src/interfaces/Content.ts +84 -84
- package/src/interfaces/Doing.ts +93 -93
- package/src/interfaces/Donation.ts +190 -190
- package/src/interfaces/Error.ts +17 -17
- package/src/interfaces/Membership.ts +184 -184
- package/src/interfaces/Messaging.ts +96 -96
- package/src/interfaces/Permissions.ts +92 -92
- package/src/interfaces/Reporting.ts +41 -41
- package/src/interfaces/UserContextInterface.ts +13 -13
- package/src/interfaces/index.ts +13 -13
- package/tsconfig.json +36 -36
- package/CLAUDE.md +0 -98
package/package.json
CHANGED
|
@@ -1,49 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@churchapps/helpers",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"description": "Library of helper functions not specific to any one ChurchApps project or framework.",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
-
"clean": "rimraf dist",
|
|
10
|
-
"build": "yarn clean && tsc",
|
|
11
|
-
"lint": "eslint src/**/*.ts",
|
|
12
|
-
"lint:fix": "eslint src/**/*.ts --fix",
|
|
13
|
-
"format": "prettier --write src/**/*.ts"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"@
|
|
36
|
-
"@typescript-eslint/
|
|
37
|
-
"eslint": "^
|
|
38
|
-
"eslint
|
|
39
|
-
"eslint-
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@churchapps/helpers",
|
|
3
|
+
"version": "1.2.5",
|
|
4
|
+
"description": "Library of helper functions not specific to any one ChurchApps project or framework.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
+
"clean": "rimraf dist",
|
|
10
|
+
"build": "yarn clean && tsc",
|
|
11
|
+
"lint": "eslint src/**/*.ts",
|
|
12
|
+
"lint:fix": "eslint src/**/*.ts --fix",
|
|
13
|
+
"format": "prettier --write src/**/*.ts",
|
|
14
|
+
"publish": "npm run build && npm publish --access=public"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/LiveChurchSolutions/Helpers.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"ChurchApps"
|
|
22
|
+
],
|
|
23
|
+
"author": "ChurchApps.org",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/LiveChurchSolutions/Helpers/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/LiveChurchSolutions/Helpers#readme",
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20.0.0",
|
|
31
|
+
"yarn": ">=1.22.0",
|
|
32
|
+
"npm": ">=8.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^24.5.0",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
37
|
+
"@typescript-eslint/parser": "^7.0.0",
|
|
38
|
+
"eslint": "^8.57.0",
|
|
39
|
+
"eslint-config-prettier": "^10.1.8",
|
|
40
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
41
|
+
"npm-run-all2": "^8.0.4",
|
|
42
|
+
"prettier": "^3.6.2",
|
|
43
|
+
"rimraf": "^6.0.1",
|
|
44
|
+
"typescript": "^5.9.2"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"dayjs": "^1.11.18",
|
|
48
|
+
"rrule": "^2.8.1"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/scripts/build-cjs.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import fs from 'fs-extra';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { fileURLToPath } from 'url';
|
|
4
|
-
|
|
5
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
-
const __dirname = path.dirname(__filename);
|
|
7
|
-
|
|
8
|
-
async function buildCjs() {
|
|
9
|
-
try {
|
|
10
|
-
const distDir = path.join(__dirname, '..', 'dist');
|
|
11
|
-
|
|
12
|
-
// Read the ES module index
|
|
13
|
-
const esmContent = await fs.readFile(path.join(distDir, 'index.js'), 'utf8');
|
|
14
|
-
|
|
15
|
-
// Convert ES module syntax to CommonJS
|
|
16
|
-
let cjsContent = esmContent
|
|
17
|
-
.replace(/export \* from "(.+?)";/g, 'Object.assign(module.exports, require("$1"));')
|
|
18
|
-
.replace(/export \{ (.+?) \} from "(.+?)";/g, (match, exports, from) => {
|
|
19
|
-
const exportList = exports.split(',').map(e => e.trim());
|
|
20
|
-
return exportList.map(exp => `module.exports.${exp} = require("${from}").${exp};`).join('\n');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// Write CommonJS version
|
|
24
|
-
await fs.writeFile(path.join(distDir, 'index.cjs'), cjsContent);
|
|
25
|
-
|
|
26
|
-
console.log('CommonJS build created successfully');
|
|
27
|
-
} catch (error) {
|
|
28
|
-
console.error('Error building CommonJS version:', error);
|
|
29
|
-
process.exit(1);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
|
|
8
|
+
async function buildCjs() {
|
|
9
|
+
try {
|
|
10
|
+
const distDir = path.join(__dirname, '..', 'dist');
|
|
11
|
+
|
|
12
|
+
// Read the ES module index
|
|
13
|
+
const esmContent = await fs.readFile(path.join(distDir, 'index.js'), 'utf8');
|
|
14
|
+
|
|
15
|
+
// Convert ES module syntax to CommonJS
|
|
16
|
+
let cjsContent = esmContent
|
|
17
|
+
.replace(/export \* from "(.+?)";/g, 'Object.assign(module.exports, require("$1"));')
|
|
18
|
+
.replace(/export \{ (.+?) \} from "(.+?)";/g, (match, exports, from) => {
|
|
19
|
+
const exportList = exports.split(',').map(e => e.trim());
|
|
20
|
+
return exportList.map(exp => `module.exports.${exp} = require("${from}").${exp};`).join('\n');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Write CommonJS version
|
|
24
|
+
await fs.writeFile(path.join(distDir, 'index.cjs'), cjsContent);
|
|
25
|
+
|
|
26
|
+
console.log('CommonJS build created successfully');
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error('Error building CommonJS version:', error);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
33
|
buildCjs();
|
package/src/ApiHelper.ts
CHANGED
|
@@ -1,176 +1,169 @@
|
|
|
1
|
-
import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
|
|
2
|
-
import { ErrorHelper } from "./ErrorHelper";
|
|
3
|
-
|
|
4
|
-
// Global singleton pattern to ensure single instance across all packages
|
|
5
|
-
declare global {
|
|
6
|
-
interface Window {
|
|
7
|
-
__CHURCHAPPS_API_HELPER__?: ApiHelperClass;
|
|
8
|
-
}
|
|
9
|
-
var __CHURCHAPPS_API_HELPER__: ApiHelperClass | undefined;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
class ApiHelperClass {
|
|
13
|
-
|
|
14
|
-
apiConfigs: ApiConfig[] = [];
|
|
15
|
-
isAuthenticated = false;
|
|
16
|
-
onRequest: (url:string, requestOptions:any) => void;
|
|
17
|
-
onError: (url:string, requestOptions:any, error: any) => void;
|
|
18
|
-
|
|
19
|
-
getConfig(keyName: string) {
|
|
20
|
-
let result: ApiConfig = null;
|
|
21
|
-
this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
|
|
22
|
-
//if (result === null) throw new Error("Unconfigured API: " + keyName);
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
setDefaultPermissions(jwt: string) {
|
|
27
|
-
this.apiConfigs.forEach(config => {
|
|
28
|
-
config.jwt = jwt;
|
|
29
|
-
config.permissions = [];
|
|
30
|
-
});
|
|
31
|
-
this.isAuthenticated = true;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
|
|
35
|
-
this.apiConfigs.forEach(config => {
|
|
36
|
-
if (config.keyName === keyName) {
|
|
37
|
-
config.jwt = jwt;
|
|
38
|
-
config.permissions = permissions;
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
this.isAuthenticated = true;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
clearPermissions() {
|
|
45
|
-
this.apiConfigs.forEach(config => { config.jwt = ""; config.permissions = []; });
|
|
46
|
-
this.isAuthenticated = false;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async get(path: string, apiName: ApiListType) {
|
|
50
|
-
const config = this.getConfig(apiName);
|
|
51
|
-
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
52
|
-
const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
|
|
53
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async getAnonymous(path: string, apiName: ApiListType) {
|
|
57
|
-
const config = this.getConfig(apiName);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (this.onError) this.onError(config.url + path, requestOptions, e);
|
|
98
|
-
throw (e);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
103
|
-
const config = this.getConfig(apiName);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (typeof
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
// Export the singleton instance
|
|
173
|
-
export const ApiHelper = ensureSingleton();
|
|
174
|
-
|
|
175
|
-
// Also export the class for type usage
|
|
176
|
-
export type { ApiHelperClass };
|
|
1
|
+
import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
|
|
2
|
+
import { ErrorHelper } from "./ErrorHelper";
|
|
3
|
+
|
|
4
|
+
// Global singleton pattern to ensure single instance across all packages
|
|
5
|
+
declare global {
|
|
6
|
+
interface Window {
|
|
7
|
+
__CHURCHAPPS_API_HELPER__?: ApiHelperClass;
|
|
8
|
+
}
|
|
9
|
+
var __CHURCHAPPS_API_HELPER__: ApiHelperClass | undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class ApiHelperClass {
|
|
13
|
+
|
|
14
|
+
apiConfigs: ApiConfig[] = [];
|
|
15
|
+
isAuthenticated = false;
|
|
16
|
+
onRequest: (url:string, requestOptions:any) => void;
|
|
17
|
+
onError: (url:string, requestOptions:any, error: any) => void;
|
|
18
|
+
|
|
19
|
+
getConfig(keyName: string) {
|
|
20
|
+
let result: ApiConfig = null;
|
|
21
|
+
this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
|
|
22
|
+
//if (result === null) throw new Error("Unconfigured API: " + keyName);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
setDefaultPermissions(jwt: string) {
|
|
27
|
+
this.apiConfigs.forEach(config => {
|
|
28
|
+
config.jwt = jwt;
|
|
29
|
+
config.permissions = [];
|
|
30
|
+
});
|
|
31
|
+
this.isAuthenticated = true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
|
|
35
|
+
this.apiConfigs.forEach(config => {
|
|
36
|
+
if (config.keyName === keyName) {
|
|
37
|
+
config.jwt = jwt;
|
|
38
|
+
config.permissions = permissions;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
this.isAuthenticated = true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
clearPermissions() {
|
|
45
|
+
this.apiConfigs.forEach(config => { config.jwt = ""; config.permissions = []; });
|
|
46
|
+
this.isAuthenticated = false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async get(path: string, apiName: ApiListType) {
|
|
50
|
+
const config = this.getConfig(apiName);
|
|
51
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
52
|
+
const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
|
|
53
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async getAnonymous(path: string, apiName: ApiListType) {
|
|
57
|
+
const config = this.getConfig(apiName);
|
|
58
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
59
|
+
const requestOptions = { method: "GET" };
|
|
60
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async post(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
64
|
+
const config = this.getConfig(apiName);
|
|
65
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
66
|
+
const requestOptions = {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
69
|
+
body: JSON.stringify(data)
|
|
70
|
+
};
|
|
71
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async patch(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
75
|
+
const config = this.getConfig(apiName);
|
|
76
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
77
|
+
const requestOptions = {
|
|
78
|
+
method: "PATCH",
|
|
79
|
+
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
80
|
+
body: JSON.stringify(data)
|
|
81
|
+
};
|
|
82
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async delete(path: string, apiName: ApiListType) {
|
|
86
|
+
const config = this.getConfig(apiName);
|
|
87
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
88
|
+
const requestOptions = {
|
|
89
|
+
method: "DELETE",
|
|
90
|
+
headers: { Authorization: "Bearer " + config.jwt }
|
|
91
|
+
};
|
|
92
|
+
if (this.onRequest) this.onRequest(config.url + path, requestOptions);
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch(config.url + path, requestOptions);
|
|
95
|
+
if (!response.ok) await this.throwApiError(response);
|
|
96
|
+
} catch (e) {
|
|
97
|
+
if (this.onError) this.onError(config.url + path, requestOptions, e);
|
|
98
|
+
throw (e);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
103
|
+
const config = this.getConfig(apiName);
|
|
104
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
105
|
+
const requestOptions = {
|
|
106
|
+
method: "POST",
|
|
107
|
+
headers: { "Content-Type": "application/json" },
|
|
108
|
+
body: JSON.stringify(data)
|
|
109
|
+
};
|
|
110
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async fetchWithErrorHandling(url: string, requestOptions: any) {
|
|
114
|
+
if (this.onRequest) this.onRequest(url, requestOptions);
|
|
115
|
+
try {
|
|
116
|
+
const response = await fetch(url, requestOptions);
|
|
117
|
+
if (!response.ok) await this.throwApiError(response);
|
|
118
|
+
else {
|
|
119
|
+
if (response.status !== 204 ) {
|
|
120
|
+
return response.json();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} catch (e) {
|
|
124
|
+
throw (e);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private async throwApiError(response: Response) {
|
|
129
|
+
let msg = response.statusText;
|
|
130
|
+
try {
|
|
131
|
+
msg = await response.text();
|
|
132
|
+
} catch {}
|
|
133
|
+
try {
|
|
134
|
+
const json = await response.json();
|
|
135
|
+
msg = json.errors[0];
|
|
136
|
+
} catch { }
|
|
137
|
+
ErrorHelper.logError(response.status.toString(), response.url, msg);
|
|
138
|
+
throw new Error(msg || "Error");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Force singleton with immediate global assignment
|
|
144
|
+
const getGlobalObject = () => {
|
|
145
|
+
if (typeof window !== 'undefined') return window;
|
|
146
|
+
if (typeof global !== 'undefined') return global;
|
|
147
|
+
if (typeof globalThis !== 'undefined') return globalThis;
|
|
148
|
+
return {};
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// Get or create singleton immediately - FORCE SINGLE INSTANCE
|
|
152
|
+
const ensureSingleton = () => {
|
|
153
|
+
const globalObj = getGlobalObject() as any;
|
|
154
|
+
|
|
155
|
+
// Use a more unique key to avoid conflicts
|
|
156
|
+
const SINGLETON_KEY = '__CHURCHAPPS_API_HELPER_SINGLETON__';
|
|
157
|
+
|
|
158
|
+
if (!globalObj[SINGLETON_KEY]) {
|
|
159
|
+
globalObj[SINGLETON_KEY] = new ApiHelperClass();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return globalObj[SINGLETON_KEY];
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// Export the singleton instance
|
|
166
|
+
export const ApiHelper = ensureSingleton();
|
|
167
|
+
|
|
168
|
+
// Also export the class for type usage
|
|
169
|
+
export type { ApiHelperClass };
|
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, 3));
|
|
40
|
-
g = this.getHexValue(backgroundColor.substring(3, 5));
|
|
41
|
-
b = this.getHexValue(backgroundColor.substring(5, 7));
|
|
42
|
-
valid = true;
|
|
43
|
-
} else if (backgroundColor.match(/#[0-9a-fA-F]{3}/)) {
|
|
44
|
-
r = this.getHexValue(backgroundColor.substring(1, 2));
|
|
45
|
-
g = this.getHexValue(backgroundColor.substring(2, 3));
|
|
46
|
-
b = this.getHexValue(backgroundColor.substring(3, 4));
|
|
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, 3));
|
|
40
|
+
g = this.getHexValue(backgroundColor.substring(3, 5));
|
|
41
|
+
b = this.getHexValue(backgroundColor.substring(5, 7));
|
|
42
|
+
valid = true;
|
|
43
|
+
} else if (backgroundColor.match(/#[0-9a-fA-F]{3}/)) {
|
|
44
|
+
r = this.getHexValue(backgroundColor.substring(1, 2));
|
|
45
|
+
g = this.getHexValue(backgroundColor.substring(2, 3));
|
|
46
|
+
b = this.getHexValue(backgroundColor.substring(3, 4));
|
|
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
|
+
}
|