@churchapps/helpers 1.1.7 → 1.1.9

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 (40) hide show
  1. package/.eslintrc.json +29 -0
  2. package/.github/FUNDING.yml +1 -1
  3. package/.prettierrc +11 -11
  4. package/.yarnrc.yml +6 -0
  5. package/CLAUDE.md +97 -89
  6. package/LICENSE +21 -21
  7. package/README.md +15 -15
  8. package/dist/interfaces/Donation.d.ts +14 -0
  9. package/dist/interfaces/Donation.d.ts.map +1 -1
  10. package/dist/interfaces/Donation.js.map +1 -1
  11. package/package.json +55 -54
  12. package/scripts/build-cjs.js +32 -32
  13. package/src/ApiHelper.ts +176 -176
  14. package/src/AppearanceHelper.ts +69 -69
  15. package/src/ArrayHelper.ts +157 -157
  16. package/src/CommonEnvironmentHelper.ts +104 -104
  17. package/src/CurrencyHelper.ts +10 -10
  18. package/src/DateHelper.ts +153 -153
  19. package/src/DonationHelper.ts +26 -26
  20. package/src/ErrorHelper.ts +39 -39
  21. package/src/EventHelper.ts +49 -49
  22. package/src/FileHelper.ts +55 -55
  23. package/src/PersonHelper.ts +82 -82
  24. package/src/UniqueIdHelper.ts +36 -36
  25. package/src/UserHelper.ts +59 -59
  26. package/src/index.ts +15 -15
  27. package/src/interfaces/Access.ts +138 -138
  28. package/src/interfaces/Attendance.ts +45 -45
  29. package/src/interfaces/Content.ts +84 -84
  30. package/src/interfaces/Doing.ts +93 -93
  31. package/src/interfaces/Donation.ts +198 -183
  32. package/src/interfaces/Error.ts +17 -17
  33. package/src/interfaces/Membership.ts +184 -184
  34. package/src/interfaces/Messaging.ts +96 -96
  35. package/src/interfaces/Permissions.ts +92 -92
  36. package/src/interfaces/Reporting.ts +41 -41
  37. package/src/interfaces/UserContextInterface.ts +13 -13
  38. package/src/interfaces/index.ts +13 -13
  39. package/tsconfig.json +36 -36
  40. package/eslint.config.js +0 -33
package/src/ApiHelper.ts CHANGED
@@ -1,176 +1,176 @@
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
- const requestOptions = { method: "GET" };
59
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
60
- }
61
-
62
- async post(path: string, data: any[] | {}, apiName: ApiListType) {
63
- const config = this.getConfig(apiName);
64
- if (!config) throw new Error(`API configuration not found: ${apiName}`);
65
- const requestOptions = {
66
- method: "POST",
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
- async patch(path: string, data: any[] | {}, apiName: ApiListType) {
74
- const config = this.getConfig(apiName);
75
- if (!config) throw new Error(`API configuration not found: ${apiName}`);
76
- const requestOptions = {
77
- method: "PATCH",
78
- headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
79
- body: JSON.stringify(data)
80
- };
81
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
82
- }
83
-
84
- async delete(path: string, apiName: ApiListType) {
85
- const config = this.getConfig(apiName);
86
- if (!config) throw new Error(`API configuration not found: ${apiName}`);
87
- const requestOptions = {
88
- method: "DELETE",
89
- headers: { Authorization: "Bearer " + config.jwt }
90
- };
91
- if (this.onRequest) this.onRequest(config.url + path, requestOptions);
92
- try {
93
- const response = await fetch(config.url + path, requestOptions);
94
- if (!response.ok) await this.throwApiError(response);
95
- } catch (e) {
96
- console.log(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
- const requestOptions = {
105
- method: "POST",
106
- headers: { "Content-Type": "application/json" },
107
- body: JSON.stringify(data)
108
- };
109
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
110
- }
111
-
112
- async fetchWithErrorHandling(url: string, requestOptions: any) {
113
- if (this.onRequest) this.onRequest(url, requestOptions);
114
- try {
115
- const response = await fetch(url, requestOptions);
116
- if (!response.ok) await this.throwApiError(response);
117
- else {
118
- if (response.status !== 204 ) {
119
- return response.json();
120
- }
121
- }
122
- } catch (e) {
123
- console.log("Error loading url: " + url);
124
- console.log(e)
125
- throw (e);
126
- }
127
- }
128
-
129
- private async throwApiError(response: Response) {
130
- let msg = response.statusText;
131
- try {
132
- msg = await response.text();
133
- } catch {}
134
- try {
135
- const json = await response.json();
136
- msg = json.errors[0];
137
- } catch { }
138
- console.log("RESPONSE", response)
139
- ErrorHelper.logError(response.status.toString(), response.url, msg);
140
- throw new Error(msg || "Error");
141
- }
142
-
143
- }
144
-
145
- // Force singleton with immediate global assignment
146
- const getGlobalObject = () => {
147
- if (typeof window !== 'undefined') return window;
148
- if (typeof global !== 'undefined') return global;
149
- if (typeof globalThis !== 'undefined') return globalThis;
150
- return {};
151
- };
152
-
153
- // Get or create singleton immediately - FORCE SINGLE INSTANCE
154
- const ensureSingleton = () => {
155
- const globalObj = getGlobalObject() as any;
156
-
157
- // Use a more unique key to avoid conflicts
158
- const SINGLETON_KEY = '__CHURCHAPPS_API_HELPER_SINGLETON__';
159
-
160
- // ALWAYS create a new instance and overwrite any existing one
161
- // This ensures the latest module load wins
162
- if (!globalObj[SINGLETON_KEY]) {
163
- globalObj[SINGLETON_KEY] = new ApiHelperClass();
164
- console.log('🔧 ApiHelper SINGLETON created (new instance)');
165
- } else {
166
- console.log('🔄 ApiHelper SINGLETON already exists - using existing (configs:', globalObj[SINGLETON_KEY].apiConfigs.length, ')');
167
- }
168
-
169
- return globalObj[SINGLETON_KEY];
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
+ const requestOptions = { method: "GET" };
59
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
60
+ }
61
+
62
+ async post(path: string, data: any[] | {}, apiName: ApiListType) {
63
+ const config = this.getConfig(apiName);
64
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
65
+ const requestOptions = {
66
+ method: "POST",
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
+ async patch(path: string, data: any[] | {}, apiName: ApiListType) {
74
+ const config = this.getConfig(apiName);
75
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
76
+ const requestOptions = {
77
+ method: "PATCH",
78
+ headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
79
+ body: JSON.stringify(data)
80
+ };
81
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
82
+ }
83
+
84
+ async delete(path: string, apiName: ApiListType) {
85
+ const config = this.getConfig(apiName);
86
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
87
+ const requestOptions = {
88
+ method: "DELETE",
89
+ headers: { Authorization: "Bearer " + config.jwt }
90
+ };
91
+ if (this.onRequest) this.onRequest(config.url + path, requestOptions);
92
+ try {
93
+ const response = await fetch(config.url + path, requestOptions);
94
+ if (!response.ok) await this.throwApiError(response);
95
+ } catch (e) {
96
+ console.log(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
+ const requestOptions = {
105
+ method: "POST",
106
+ headers: { "Content-Type": "application/json" },
107
+ body: JSON.stringify(data)
108
+ };
109
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
110
+ }
111
+
112
+ async fetchWithErrorHandling(url: string, requestOptions: any) {
113
+ if (this.onRequest) this.onRequest(url, requestOptions);
114
+ try {
115
+ const response = await fetch(url, requestOptions);
116
+ if (!response.ok) await this.throwApiError(response);
117
+ else {
118
+ if (response.status !== 204 ) {
119
+ return response.json();
120
+ }
121
+ }
122
+ } catch (e) {
123
+ console.log("Error loading url: " + url);
124
+ console.log(e)
125
+ throw (e);
126
+ }
127
+ }
128
+
129
+ private async throwApiError(response: Response) {
130
+ let msg = response.statusText;
131
+ try {
132
+ msg = await response.text();
133
+ } catch {}
134
+ try {
135
+ const json = await response.json();
136
+ msg = json.errors[0];
137
+ } catch { }
138
+ console.log("RESPONSE", response)
139
+ ErrorHelper.logError(response.status.toString(), response.url, msg);
140
+ throw new Error(msg || "Error");
141
+ }
142
+
143
+ }
144
+
145
+ // Force singleton with immediate global assignment
146
+ const getGlobalObject = () => {
147
+ if (typeof window !== 'undefined') return window;
148
+ if (typeof global !== 'undefined') return global;
149
+ if (typeof globalThis !== 'undefined') return globalThis;
150
+ return {};
151
+ };
152
+
153
+ // Get or create singleton immediately - FORCE SINGLE INSTANCE
154
+ const ensureSingleton = () => {
155
+ const globalObj = getGlobalObject() as any;
156
+
157
+ // Use a more unique key to avoid conflicts
158
+ const SINGLETON_KEY = '__CHURCHAPPS_API_HELPER_SINGLETON__';
159
+
160
+ // ALWAYS create a new instance and overwrite any existing one
161
+ // This ensures the latest module load wins
162
+ if (!globalObj[SINGLETON_KEY]) {
163
+ globalObj[SINGLETON_KEY] = new ApiHelperClass();
164
+ console.log('🔧 ApiHelper SINGLETON created (new instance)');
165
+ } else {
166
+ console.log('🔄 ApiHelper SINGLETON already exists - using existing (configs:', globalObj[SINGLETON_KEY].apiConfigs.length, ')');
167
+ }
168
+
169
+ return globalObj[SINGLETON_KEY];
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,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
+ }