ggez-banking-sdk 0.2.10 → 0.2.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.
@@ -5,9 +5,36 @@ declare class CookiesHelper {
5
5
  private domain;
6
6
  private programId;
7
7
  private Cookies;
8
+ private useCookieStore;
8
9
  private errorHandler;
9
10
  private clientHelper;
10
- constructor(programId: string, errorHandler: (error: any) => void);
11
+ constructor(programId: string, domain: string, errorHandler: (error: any) => void);
12
+ /**
13
+ * Format domain to ensure proper cookie setting
14
+ * Adds leading dot if domain doesn't start with one and is not localhost
15
+ */
16
+ private formatDomain;
17
+ /**
18
+ * Check if CookieStore API is available
19
+ * CookieStore requires secure context (HTTPS or localhost)
20
+ */
21
+ private isCookieStoreAvailable;
22
+ /**
23
+ * Check if we're in a secure context (HTTPS or localhost)
24
+ */
25
+ private isSecureContext;
26
+ /**
27
+ * Fallback method to set cookie using document.cookie
28
+ */
29
+ private setCookieFallback;
30
+ /**
31
+ * Fallback method to get cookie using document.cookie
32
+ */
33
+ private getCookieFallback;
34
+ /**
35
+ * Fallback method to delete cookie using document.cookie
36
+ */
37
+ private deleteCookieFallback;
11
38
  GetIID(): Promise<string>;
12
39
  SetIID(): Promise<string>;
13
40
  ValidateIID(installationId: string): Promise<boolean>;
@@ -7,18 +7,109 @@ class CookiesHelper {
7
7
  eventHandlers = new Map();
8
8
  domain = undefined;
9
9
  programId = "0";
10
- Cookies = window.cookieStore;
10
+ Cookies = null;
11
+ useCookieStore = false;
11
12
  errorHandler;
12
13
  clientHelper;
13
14
  // #endregion
14
15
  // #region "Constructor"
15
- constructor(programId, errorHandler) {
16
+ constructor(programId, domain, errorHandler) {
16
17
  this.key = v4();
17
18
  this.programId = programId;
18
19
  this.errorHandler = errorHandler;
19
- this.Cookies = window.cookieStore;
20
20
  this.clientHelper = new ClientHelper();
21
- this.domain = "vvtx.com";
21
+ // Format domain properly (add leading dot for subdomain sharing if not present)
22
+ this.domain = this.formatDomain(domain);
23
+ // Check if cookieStore API is available (requires HTTPS or localhost)
24
+ this.useCookieStore = this.isCookieStoreAvailable();
25
+ if (this.useCookieStore && window.cookieStore) {
26
+ this.Cookies = window.cookieStore;
27
+ }
28
+ }
29
+ /**
30
+ * Format domain to ensure proper cookie setting
31
+ * Adds leading dot if domain doesn't start with one and is not localhost
32
+ */
33
+ formatDomain(domain) {
34
+ if (!domain)
35
+ return undefined;
36
+ // Don't modify localhost or IP addresses
37
+ if (domain === "localhost" || /^\d+\.\d+\.\d+\.\d+/.test(domain)) {
38
+ return domain;
39
+ }
40
+ // If domain doesn't start with dot, add it for subdomain sharing
41
+ if (!domain.startsWith(".")) {
42
+ return `.${domain}`;
43
+ }
44
+ return domain;
45
+ }
46
+ /**
47
+ * Check if CookieStore API is available
48
+ * CookieStore requires secure context (HTTPS or localhost)
49
+ */
50
+ isCookieStoreAvailable() {
51
+ return (typeof window !== "undefined" &&
52
+ "cookieStore" in window &&
53
+ window.cookieStore !== null &&
54
+ window.cookieStore !== undefined);
55
+ }
56
+ /**
57
+ * Check if we're in a secure context (HTTPS or localhost)
58
+ */
59
+ isSecureContext() {
60
+ if (typeof window === "undefined")
61
+ return false;
62
+ return (window.location.protocol === "https:" ||
63
+ window.location.hostname === "localhost" ||
64
+ window.location.hostname === "127.0.0.1");
65
+ }
66
+ /**
67
+ * Fallback method to set cookie using document.cookie
68
+ */
69
+ setCookieFallback(name, value, expires, domain) {
70
+ let cookieString = `${name}=${encodeURIComponent(value)}`;
71
+ if (expires) {
72
+ const date = new Date(expires);
73
+ cookieString += `; expires=${date.toUTCString()}`;
74
+ }
75
+ cookieString += `; path=/`;
76
+ if (domain) {
77
+ cookieString += `; domain=${domain}`;
78
+ }
79
+ // Set secure flag only in HTTPS
80
+ if (this.isSecureContext()) {
81
+ cookieString += `; secure`;
82
+ }
83
+ cookieString += `; samesite=lax`;
84
+ document.cookie = cookieString;
85
+ }
86
+ /**
87
+ * Fallback method to get cookie using document.cookie
88
+ */
89
+ getCookieFallback(name) {
90
+ if (typeof document === "undefined")
91
+ return "";
92
+ const nameEQ = name + "=";
93
+ const ca = document.cookie.split(";");
94
+ for (let i = 0; i < ca.length; i++) {
95
+ let c = ca[i];
96
+ while (c.charAt(0) === " ")
97
+ c = c.substring(1, c.length);
98
+ if (c.indexOf(nameEQ) === 0) {
99
+ return decodeURIComponent(c.substring(nameEQ.length, c.length));
100
+ }
101
+ }
102
+ return "";
103
+ }
104
+ /**
105
+ * Fallback method to delete cookie using document.cookie
106
+ */
107
+ deleteCookieFallback(name, domain) {
108
+ let cookieString = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`;
109
+ if (domain) {
110
+ cookieString += `; domain=${domain}`;
111
+ }
112
+ document.cookie = cookieString;
22
113
  }
23
114
  // #endregion
24
115
  // #region "IID"
@@ -271,27 +362,69 @@ class CookiesHelper {
271
362
  // #endregion
272
363
  // #region "Getters & Setters"
273
364
  async GET(key) {
274
- const cookie = await this.Cookies.get({ name: key, url: this.domain });
275
- return cookie?.value || "";
365
+ try {
366
+ if (this.useCookieStore && this.Cookies) {
367
+ const cookie = await this.Cookies.get({ name: key });
368
+ return cookie?.value || "";
369
+ }
370
+ else {
371
+ // Fallback to document.cookie
372
+ return this.getCookieFallback(key);
373
+ }
374
+ }
375
+ catch (error) {
376
+ // If cookieStore fails, fallback to document.cookie
377
+ this.errorHandler(error);
378
+ return this.getCookieFallback(key);
379
+ }
276
380
  }
277
381
  async SET(key, value, expires) {
278
- const defaultExpireDate = Date.now() + 1000 * 60 * 60 * 24 * 365;
279
- await this.Cookies.set({
280
- name: key,
281
- value: value,
282
- domain: this.domain,
283
- expires: expires || defaultExpireDate,
284
- path: "/",
285
- sameSite: "lax",
286
- secure: true,
287
- });
382
+ try {
383
+ const defaultExpireDate = Date.now() + 1000 * 60 * 60 * 24 * 365;
384
+ const expireTime = expires || defaultExpireDate;
385
+ const isSecure = this.isSecureContext();
386
+ if (this.useCookieStore && this.Cookies) {
387
+ await this.Cookies.set({
388
+ name: key,
389
+ value: value,
390
+ domain: this.domain,
391
+ expires: expireTime,
392
+ path: "/",
393
+ sameSite: "lax",
394
+ secure: isSecure,
395
+ });
396
+ }
397
+ else {
398
+ // Fallback to document.cookie
399
+ this.setCookieFallback(key, value, expireTime, this.domain);
400
+ }
401
+ }
402
+ catch (error) {
403
+ // If cookieStore fails, fallback to document.cookie
404
+ this.errorHandler(error);
405
+ const defaultExpireDate = Date.now() + 1000 * 60 * 60 * 24 * 365;
406
+ this.setCookieFallback(key, value, expires || defaultExpireDate, this.domain);
407
+ }
288
408
  }
289
409
  async REMOVE(key) {
290
- await this.Cookies.delete({
291
- name: key,
292
- domain: this.domain,
293
- path: "/",
294
- });
410
+ try {
411
+ if (this.useCookieStore && this.Cookies) {
412
+ await this.Cookies.delete({
413
+ name: key,
414
+ domain: this.domain,
415
+ path: "/",
416
+ });
417
+ }
418
+ else {
419
+ // Fallback to document.cookie
420
+ this.deleteCookieFallback(key, this.domain);
421
+ }
422
+ }
423
+ catch (error) {
424
+ // If cookieStore fails, fallback to document.cookie
425
+ this.errorHandler(error);
426
+ this.deleteCookieFallback(key, this.domain);
427
+ }
295
428
  }
296
429
  // #endregion
297
430
  // #region "Cookie Change Event Listener"
@@ -361,15 +494,20 @@ class CookiesHelper {
361
494
  }
362
495
  }
363
496
  addOnChangeEventListener(params) {
364
- const handler = (e) => this.cookieEventHandler(e, params);
365
- this.eventHandlers.set(this.key, handler);
366
- this.Cookies.addEventListener("change", handler);
497
+ if (this.useCookieStore && this.Cookies) {
498
+ const handler = (e) => this.cookieEventHandler(e, params);
499
+ this.eventHandlers.set(this.key, handler);
500
+ this.Cookies.addEventListener("change", handler);
501
+ }
502
+ // Note: document.cookie doesn't support change events, so this only works with cookieStore
367
503
  }
368
504
  removeOnChangeEventListener() {
369
- const handler = this.eventHandlers.get(this.key);
370
- if (handler) {
371
- this.Cookies.removeEventListener("change", handler);
372
- this.eventHandlers.delete(this.key);
505
+ if (this.useCookieStore && this.Cookies) {
506
+ const handler = this.eventHandlers.get(this.key);
507
+ if (handler) {
508
+ this.Cookies.removeEventListener("change", handler);
509
+ this.eventHandlers.delete(this.key);
510
+ }
373
511
  }
374
512
  }
375
513
  // #endregion
@@ -4,7 +4,7 @@ declare class StorageHelper {
4
4
  cookiesHelper: CookiesHelper;
5
5
  localStorageHelper: LocalStorageHelper;
6
6
  errorHandler: (error: any) => void;
7
- constructor(programId: string, errorHandler: (error: any) => void);
7
+ constructor(programId: string, domain: string, errorHandler: (error: any) => void);
8
8
  GetCookiesHelper(): CookiesHelper;
9
9
  GetLocalStorageHelper(): LocalStorageHelper;
10
10
  }
@@ -7,9 +7,9 @@ class StorageHelper {
7
7
  errorHandler;
8
8
  // #endregion
9
9
  // #region "Constructor"
10
- constructor(programId, errorHandler) {
10
+ constructor(programId, domain, errorHandler) {
11
11
  this.errorHandler = errorHandler;
12
- this.cookiesHelper = new CookiesHelper(programId, this.errorHandler);
12
+ this.cookiesHelper = new CookiesHelper(programId, domain, this.errorHandler);
13
13
  this.localStorageHelper = new LocalStorageHelper(this.cookiesHelper, this.errorHandler);
14
14
  }
15
15
  // #endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ggez-banking-sdk",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "A Node.js package to handle GGEZ Banking API endpoints, Simplify the process of managing CRUD operations with this efficient and easy-to-use package.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",