ggez-banking-sdk 0.2.11 → 0.2.13

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,30 @@ declare class CookiesHelper {
5
5
  private domain;
6
6
  private programId;
7
7
  private Cookies;
8
+ private useCookieStore;
8
9
  private errorHandler;
9
- private clientHelper;
10
10
  constructor(programId: string, domain: string, errorHandler: (error: any) => void);
11
+ /**
12
+ * Check if CookieStore API is available
13
+ * CookieStore requires secure context (HTTPS or localhost)
14
+ */
15
+ private isCookieStoreAvailable;
16
+ /**
17
+ * Check if we're in a secure context (HTTPS or localhost)
18
+ */
19
+ private isSecureContext;
20
+ /**
21
+ * Fallback method to set cookie using document.cookie
22
+ */
23
+ private setCookieFallback;
24
+ /**
25
+ * Fallback method to get cookie using document.cookie
26
+ */
27
+ private getCookieFallback;
28
+ /**
29
+ * Fallback method to delete cookie using document.cookie
30
+ */
31
+ private deleteCookieFallback;
11
32
  GetIID(): Promise<string>;
12
33
  SetIID(): Promise<string>;
13
34
  ValidateIID(installationId: string): Promise<boolean>;
@@ -1,24 +1,95 @@
1
1
  import { v4 } from "uuid";
2
2
  import { CipherHelper } from "../cipherHelper";
3
- import { ClientHelper } from "../..";
4
3
  class CookiesHelper {
5
4
  // #region "Properties"
6
5
  key;
7
6
  eventHandlers = new Map();
8
7
  domain = undefined;
9
8
  programId = "0";
10
- Cookies = window.cookieStore;
9
+ Cookies = null;
10
+ useCookieStore = false;
11
11
  errorHandler;
12
- clientHelper;
13
12
  // #endregion
14
13
  // #region "Constructor"
15
14
  constructor(programId, domain, errorHandler) {
16
15
  this.key = v4();
17
16
  this.programId = programId;
18
17
  this.errorHandler = errorHandler;
19
- this.Cookies = window.cookieStore;
20
- this.clientHelper = new ClientHelper();
18
+ // Format domain properly (add leading dot for subdomain sharing if not present)
21
19
  this.domain = domain;
20
+ // Check if cookieStore API is available (requires HTTPS or localhost)
21
+ this.useCookieStore = this.isCookieStoreAvailable();
22
+ if (this.useCookieStore && window.cookieStore) {
23
+ this.Cookies = window.cookieStore;
24
+ }
25
+ }
26
+ /**
27
+ * Check if CookieStore API is available
28
+ * CookieStore requires secure context (HTTPS or localhost)
29
+ */
30
+ isCookieStoreAvailable() {
31
+ return (typeof window !== "undefined" &&
32
+ "cookieStore" in window &&
33
+ window.cookieStore !== null &&
34
+ window.cookieStore !== undefined);
35
+ }
36
+ /**
37
+ * Check if we're in a secure context (HTTPS or localhost)
38
+ */
39
+ isSecureContext() {
40
+ if (typeof window === "undefined")
41
+ return false;
42
+ return (window.location.protocol === "https:" ||
43
+ window.location.hostname === "localhost" ||
44
+ window.location.hostname === "127.0.0.1");
45
+ }
46
+ /**
47
+ * Fallback method to set cookie using document.cookie
48
+ */
49
+ setCookieFallback(name, value, expires, domain) {
50
+ let cookieString = `${name}=${encodeURIComponent(value)}`;
51
+ if (expires) {
52
+ const date = new Date(expires);
53
+ cookieString += `; expires=${date.toUTCString()}`;
54
+ }
55
+ cookieString += `; path=/`;
56
+ if (domain) {
57
+ cookieString += `; domain=${domain}`;
58
+ }
59
+ // Set secure flag only in HTTPS
60
+ if (this.isSecureContext()) {
61
+ cookieString += `; secure`;
62
+ }
63
+ cookieString += `; samesite=lax`;
64
+ document.cookie = cookieString;
65
+ }
66
+ /**
67
+ * Fallback method to get cookie using document.cookie
68
+ */
69
+ getCookieFallback(name) {
70
+ if (typeof document === "undefined")
71
+ return "";
72
+ const nameEQ = name + "=";
73
+ const ca = document.cookie.split(";");
74
+ for (let i = 0; i < ca.length; i++) {
75
+ let c = ca[i];
76
+ while (c.charAt(0) === " ")
77
+ c = c.substring(1, c.length);
78
+ if (c.indexOf(nameEQ) === 0) {
79
+ return decodeURIComponent(c.substring(nameEQ.length, c.length));
80
+ }
81
+ }
82
+ return "";
83
+ }
84
+ /**
85
+ * Fallback method to delete cookie using document.cookie
86
+ */
87
+ deleteCookieFallback(name, domain) {
88
+ let cookieString = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`;
89
+ if (domain) {
90
+ cookieString += `; domain=${domain}`;
91
+ }
92
+ document.cookie = cookieString;
22
93
  }
23
94
  // #endregion
24
95
  // #region "IID"
@@ -271,27 +342,69 @@ class CookiesHelper {
271
342
  // #endregion
272
343
  // #region "Getters & Setters"
273
344
  async GET(key) {
274
- const cookie = await this.Cookies.get({ name: key, url: this.domain });
275
- return cookie?.value || "";
345
+ try {
346
+ if (this.useCookieStore && this.Cookies) {
347
+ const cookie = await this.Cookies.get({ name: key });
348
+ return cookie?.value || "";
349
+ }
350
+ else {
351
+ // Fallback to document.cookie
352
+ return this.getCookieFallback(key);
353
+ }
354
+ }
355
+ catch (error) {
356
+ // If cookieStore fails, fallback to document.cookie
357
+ this.errorHandler(error);
358
+ return this.getCookieFallback(key);
359
+ }
276
360
  }
277
361
  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: !this.clientHelper.isHostOnly(),
287
- });
362
+ try {
363
+ const defaultExpireDate = Date.now() + 1000 * 60 * 60 * 24 * 365;
364
+ const expireTime = expires || defaultExpireDate;
365
+ const isSecure = this.isSecureContext();
366
+ if (this.useCookieStore && this.Cookies) {
367
+ await this.Cookies.set({
368
+ name: key,
369
+ value: value,
370
+ domain: this.domain,
371
+ expires: expireTime,
372
+ path: "/",
373
+ sameSite: "lax",
374
+ secure: isSecure,
375
+ });
376
+ }
377
+ else {
378
+ // Fallback to document.cookie
379
+ this.setCookieFallback(key, value, expireTime, this.domain);
380
+ }
381
+ }
382
+ catch (error) {
383
+ // If cookieStore fails, fallback to document.cookie
384
+ this.errorHandler(error);
385
+ const defaultExpireDate = Date.now() + 1000 * 60 * 60 * 24 * 365;
386
+ this.setCookieFallback(key, value, expires || defaultExpireDate, this.domain);
387
+ }
288
388
  }
289
389
  async REMOVE(key) {
290
- await this.Cookies.delete({
291
- name: key,
292
- domain: this.domain,
293
- path: "/",
294
- });
390
+ try {
391
+ if (this.useCookieStore && this.Cookies) {
392
+ await this.Cookies.delete({
393
+ name: key,
394
+ domain: this.domain,
395
+ path: "/",
396
+ });
397
+ }
398
+ else {
399
+ // Fallback to document.cookie
400
+ this.deleteCookieFallback(key, this.domain);
401
+ }
402
+ }
403
+ catch (error) {
404
+ // If cookieStore fails, fallback to document.cookie
405
+ this.errorHandler(error);
406
+ this.deleteCookieFallback(key, this.domain);
407
+ }
295
408
  }
296
409
  // #endregion
297
410
  // #region "Cookie Change Event Listener"
@@ -361,15 +474,20 @@ class CookiesHelper {
361
474
  }
362
475
  }
363
476
  addOnChangeEventListener(params) {
364
- const handler = (e) => this.cookieEventHandler(e, params);
365
- this.eventHandlers.set(this.key, handler);
366
- this.Cookies.addEventListener("change", handler);
477
+ if (this.useCookieStore && this.Cookies) {
478
+ const handler = (e) => this.cookieEventHandler(e, params);
479
+ this.eventHandlers.set(this.key, handler);
480
+ this.Cookies.addEventListener("change", handler);
481
+ }
482
+ // Note: document.cookie doesn't support change events, so this only works with cookieStore
367
483
  }
368
484
  removeOnChangeEventListener() {
369
- const handler = this.eventHandlers.get(this.key);
370
- if (handler) {
371
- this.Cookies.removeEventListener("change", handler);
372
- this.eventHandlers.delete(this.key);
485
+ if (this.useCookieStore && this.Cookies) {
486
+ const handler = this.eventHandlers.get(this.key);
487
+ if (handler) {
488
+ this.Cookies.removeEventListener("change", handler);
489
+ this.eventHandlers.delete(this.key);
490
+ }
373
491
  }
374
492
  }
375
493
  // #endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ggez-banking-sdk",
3
- "version": "0.2.11",
3
+ "version": "0.2.13",
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",