glitch-javascript-sdk 0.3.9 → 0.4.0

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.
@@ -1,11 +1,20 @@
1
1
  declare class Storage {
2
+ private static rootDomain;
2
3
  private static data;
4
+ /**
5
+ * Sets a root level domain so the data can persist across
6
+ * subdomains
7
+ *
8
+ * @param rootDomain
9
+ */
10
+ static setRootDomain(rootDomain: string): void;
11
+ private static getStorageKey;
3
12
  static set(key: string, value: any): void;
4
13
  static get(key: string): any;
5
14
  static setAuthToken(token: string | null): void;
6
15
  static getAuthToken(): string | null;
16
+ static eraseCookie(name: string): void;
7
17
  private static setCookie;
8
18
  private static getCookie;
9
- static eraseCookie(name: string): void;
10
19
  }
11
20
  export default Storage;
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ declare class Config {
10
10
  private static _baseUrl;
11
11
  private static _authToken;
12
12
  private static _community;
13
+ private static _rootDomain;
13
14
  private static _baseUrlLocked;
14
15
  /**
15
16
  * Set the configuration
@@ -37,6 +38,14 @@ declare class Config {
37
38
  * @param community The object of the community
38
39
  */
39
40
  static setCommunity(community: Record<string, any>): void;
41
+ /**
42
+ * Sets the root level domain so data can accessed across
43
+ * multiple subdomains
44
+ *
45
+ * @param domain The domain ie: example.com
46
+ */
47
+ static setRootDomain(domain: string): void;
48
+ static getRootDomain(): string;
40
49
  /**
41
50
  * Gets base url
42
51
  */
@@ -1742,14 +1751,23 @@ declare class Session {
1742
1751
  }
1743
1752
 
1744
1753
  declare class Storage {
1754
+ private static rootDomain;
1745
1755
  private static data;
1756
+ /**
1757
+ * Sets a root level domain so the data can persist across
1758
+ * subdomains
1759
+ *
1760
+ * @param rootDomain
1761
+ */
1762
+ static setRootDomain(rootDomain: string): void;
1763
+ private static getStorageKey;
1746
1764
  static set(key: string, value: any): void;
1747
1765
  static get(key: string): any;
1748
1766
  static setAuthToken(token: string | null): void;
1749
1767
  static getAuthToken(): string | null;
1768
+ static eraseCookie(name: string): void;
1750
1769
  private static setCookie;
1751
1770
  private static getCookie;
1752
- static eraseCookie(name: string): void;
1753
1771
  }
1754
1772
 
1755
1773
  declare class Data {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glitch-javascript-sdk",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "Javascript SDK for Glitch",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -1,6 +1,7 @@
1
1
  import Community from "../models/community";
2
2
  import LabelManager from "../util/LabelManager";
3
3
  import Requests from "../util/Requests";
4
+ import Storage from "../util/Storage";
4
5
 
5
6
  /**
6
7
  * Config
@@ -13,6 +14,7 @@ class Config {
13
14
  private static _baseUrl: string;
14
15
  private static _authToken: string;
15
16
  private static _community: object;
17
+ private static _rootDomain: string;
16
18
 
17
19
  private static _baseUrlLocked: boolean = false;
18
20
 
@@ -22,7 +24,7 @@ class Config {
22
24
  * @param baseUrl The url base endpoint of the api
23
25
  * @param authToken The JSON Web Token
24
26
  */
25
- public static setConfig(baseUrl: string, authToken: string, lock? : boolean) {
27
+ public static setConfig(baseUrl: string, authToken: string, lock?: boolean) {
26
28
 
27
29
  this.setBaseUrl(baseUrl, lock);
28
30
 
@@ -38,15 +40,15 @@ class Config {
38
40
  * @param baseUrl The url that connects to the APIs base
39
41
  * @param lock If set to true, will lock the baseUrl so it cannot be changed
40
42
  */
41
- public static setBaseUrl(baseUrl: string, lock? : boolean) {
43
+ public static setBaseUrl(baseUrl: string, lock?: boolean) {
42
44
 
43
- if(!this._baseUrlLocked) {
45
+ if (!this._baseUrlLocked) {
44
46
  Config._baseUrl = baseUrl;
45
47
 
46
48
  Requests.setBaseUrl(baseUrl);
47
49
  }
48
50
 
49
- if(lock) {
51
+ if (lock) {
50
52
  this._baseUrlLocked = true;
51
53
  }
52
54
  }
@@ -71,10 +73,37 @@ class Config {
71
73
  Config._community = community;
72
74
 
73
75
  Requests.setCommunityID(community.id);
74
-
76
+
75
77
  LabelManager.initialize(community);
76
78
  }
77
79
 
80
+ /**
81
+ * Sets the root level domain so data can accessed across
82
+ * multiple subdomains
83
+ *
84
+ * @param domain The domain ie: example.com
85
+ */
86
+ public static setRootDomain(domain: string) {
87
+
88
+ const parts = domain.split('.');
89
+
90
+ if (parts.length > 2) {
91
+ parts.shift();
92
+ }
93
+
94
+ let formattedDomain = parts.join('.');
95
+
96
+ formattedDomain = formattedDomain.replace(/^\./, '');
97
+
98
+ this._rootDomain = formattedDomain;
99
+
100
+ Storage.setRootDomain(formattedDomain);
101
+ }
102
+
103
+ public static getRootDomain() {
104
+ return this._rootDomain;
105
+ }
106
+
78
107
  /**
79
108
  * Gets base url
80
109
  */
@@ -1,106 +1,100 @@
1
1
  class Storage {
2
+ private static rootDomain: string = '';
3
+ private static data: { [key: string]: any } = {};
2
4
 
3
- //Back up data type if no storage is working.
4
-
5
- private static data: { [key: string]: any } = {};
5
+ /**
6
+ * Sets a root level domain so the data can persist across
7
+ * subdomains
8
+ *
9
+ * @param rootDomain
10
+ */
11
+ public static setRootDomain(rootDomain: string) {
12
+ Storage.rootDomain = rootDomain;
13
+ }
6
14
 
7
-
8
- public static set(key: string, value: any) {
15
+ private static getStorageKey(key: string): string {
16
+ return Storage.rootDomain ? `${Storage.rootDomain}:${key}` : key;
17
+ }
9
18
 
19
+ public static set(key: string, value: any) {
20
+ try {
21
+ const serializedValue = JSON.stringify(value);
22
+ window.localStorage.setItem(Storage.getStorageKey(key), serializedValue);
23
+ } catch (e) {
10
24
  try {
11
25
  const serializedValue = JSON.stringify(value);
12
- window.localStorage.setItem(key, serializedValue);
26
+ window.sessionStorage.setItem(Storage.getStorageKey(key), serializedValue);
13
27
  } catch (e) {
14
-
15
- try {
16
-
17
- const serializedValue = JSON.stringify(value);
18
- window.sessionStorage.setItem(key, serializedValue);
19
-
20
- } catch (e) {
21
- //fallback
22
- this.setCookie(key, value, 31);
23
- Storage.data[key] = value;
24
- }
28
+ this.setCookie(key, value, 31);
29
+ Storage.data[key] = value;
25
30
  }
26
31
  }
27
-
28
- public static get(key: string): any {
29
- try {
32
+ }
30
33
 
31
- const serializedValue = window.localStorage.getItem(key);
32
-
34
+ public static get(key: string): any {
35
+ try {
36
+ const serializedValue = window.localStorage.getItem(Storage.getStorageKey(key));
37
+ if (serializedValue !== null) {
38
+ return JSON.parse(serializedValue);
39
+ }
40
+ } catch (e) {
41
+ try {
42
+ const serializedValue = window.sessionStorage.getItem(Storage.getStorageKey(key));
33
43
  if (serializedValue !== null) {
34
44
  return JSON.parse(serializedValue);
35
45
  }
36
-
37
46
  } catch (e) {
47
+ let value = Storage.getCookie(key);
48
+ if (!value) {
49
+ value = Storage.data[key];
50
+ }
51
+ return value;
52
+ }
53
+ }
54
+ }
38
55
 
39
- try {
56
+ public static setAuthToken(token: string | null) {
57
+ Storage.set('glitch_auth_token', token);
58
+ }
40
59
 
41
- const serializedValue = window.sessionStorage.getItem(key);
42
-
43
- if (serializedValue !== null) {
44
- return JSON.parse(serializedValue);
45
- }
60
+ public static getAuthToken(): string | null {
61
+ return Storage.get('glitch_auth_token');
62
+ }
46
63
 
47
- } catch (e) {
64
+ public static eraseCookie(name: string) {
65
+ document.cookie =
66
+ name +
67
+ '=; Secure; HttpOnly=false; SameSite=none; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
68
+ }
48
69
 
49
- let value = Storage.getCookie(key);
50
-
51
- if (!value) {
52
- value = Storage.data[key];
53
- }
54
-
55
- return value;
56
- }
57
- }
58
- }
59
-
60
-
61
- public static setAuthToken(token: string | null) {
62
- Storage.set('glitch_auth_token', token);
70
+ private static setCookie(name: string, value: string, days: number) {
71
+ let expires = '';
72
+ if (days) {
73
+ const date = new Date();
74
+ date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
75
+ expires = '; expires=' + date.toUTCString();
63
76
  }
64
-
65
- public static getAuthToken(): string | null {
66
- return Storage.get('glitch_auth_token');
67
- }
68
-
69
- private static setCookie(name: string, value: string, days: number) {
70
- let expires = '';
71
- if (days) {
72
- const date = new Date();
73
- date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
74
- expires = '; expires=' + date.toUTCString();
75
- }
76
-
77
- //IFrames require HttpyOnly to be false, Chrome require SameSite to be none, and must be secure
78
- document.cookie =
79
- name +
80
- '=' +
81
- (value || '') +
82
- expires +
83
- '; path=/; HttpOnly=false; SameSite=none; Secure';
84
- }
85
-
86
- private static getCookie(name: string): string | null {
87
- const nameEQ = name + '=';
88
- const ca = document.cookie.split(';');
89
- for (let i = 0; i < ca.length; i++) {
90
- let c = ca[i];
91
- while (c.charAt(0) == ' ') c = c.substring(1, c.length);
92
- if (c.indexOf(nameEQ) == 0)
93
- return c.substring(nameEQ.length, c.length);
94
- }
95
- return null;
96
- }
97
-
98
- public static eraseCookie(name: string) {
99
- document.cookie =
100
- name +
101
- '=; Secure; HttpOnly=false; SameSite=none; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
77
+
78
+ document.cookie =
79
+ name +
80
+ '=' +
81
+ (value || '') +
82
+ expires +
83
+ '; path=/; domain=' +
84
+ Storage.rootDomain +
85
+ '; HttpOnly=false; SameSite=none; Secure';
86
+ }
87
+
88
+ private static getCookie(name: string): string | null {
89
+ const nameEQ = name + '=';
90
+ const ca = document.cookie.split(';');
91
+ for (let i = 0; i < ca.length; i++) {
92
+ let c = ca[i];
93
+ while (c.charAt(0) == ' ') c = c.substring(1, c.length);
94
+ if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
102
95
  }
96
+ return null;
103
97
  }
104
-
105
- export default Storage;
106
-
98
+ }
99
+
100
+ export default Storage;