glitch-javascript-sdk 0.3.9 → 0.4.1
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/dist/cjs/index.js +132 -89
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/api/Competitions.d.ts +12 -3
- package/dist/esm/config/Config.d.ts +9 -0
- package/dist/esm/index.js +296 -271
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Storage.d.ts +10 -1
- package/dist/index.d.ts +31 -4
- package/package.json +1 -1
- package/src/api/Competitions.ts +15 -3
- package/src/config/Config.ts +34 -5
- package/src/routes/CompetitionRoute.ts +1 -0
- package/src/util/Storage.ts +79 -85
|
@@ -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
|
*/
|
|
@@ -589,7 +598,7 @@ declare class Competitions {
|
|
|
589
598
|
/**
|
|
590
599
|
* Get a leaderboard by a users wins.
|
|
591
600
|
*
|
|
592
|
-
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/
|
|
601
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderBoardUserWins
|
|
593
602
|
*
|
|
594
603
|
* @param competition_id
|
|
595
604
|
* @returns promise
|
|
@@ -607,7 +616,7 @@ declare class Competitions {
|
|
|
607
616
|
/**
|
|
608
617
|
* Get a leaderboard by a teams wins.
|
|
609
618
|
*
|
|
610
|
-
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/
|
|
619
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderBoardTeamWins
|
|
611
620
|
*
|
|
612
621
|
* @param competition_id
|
|
613
622
|
* @returns promise
|
|
@@ -616,12 +625,21 @@ declare class Competitions {
|
|
|
616
625
|
/**
|
|
617
626
|
* Get all leaderboards.
|
|
618
627
|
*
|
|
619
|
-
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/
|
|
628
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderBoardTeamPoints
|
|
620
629
|
*
|
|
621
630
|
* @param competition_id
|
|
622
631
|
* @returns promise
|
|
623
632
|
*/
|
|
624
633
|
static allLeaderboards<T>(competition_id: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
634
|
+
/**
|
|
635
|
+
* Gets all the information about a competition for the current user.
|
|
636
|
+
*
|
|
637
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderboardsAll
|
|
638
|
+
*
|
|
639
|
+
* @param competition_id
|
|
640
|
+
* @returns promise
|
|
641
|
+
*/
|
|
642
|
+
static me<T>(competition_id: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
625
643
|
}
|
|
626
644
|
|
|
627
645
|
declare class Communities {
|
|
@@ -1742,14 +1760,23 @@ declare class Session {
|
|
|
1742
1760
|
}
|
|
1743
1761
|
|
|
1744
1762
|
declare class Storage {
|
|
1763
|
+
private static rootDomain;
|
|
1745
1764
|
private static data;
|
|
1765
|
+
/**
|
|
1766
|
+
* Sets a root level domain so the data can persist across
|
|
1767
|
+
* subdomains
|
|
1768
|
+
*
|
|
1769
|
+
* @param rootDomain
|
|
1770
|
+
*/
|
|
1771
|
+
static setRootDomain(rootDomain: string): void;
|
|
1772
|
+
private static getStorageKey;
|
|
1746
1773
|
static set(key: string, value: any): void;
|
|
1747
1774
|
static get(key: string): any;
|
|
1748
1775
|
static setAuthToken(token: string | null): void;
|
|
1749
1776
|
static getAuthToken(): string | null;
|
|
1777
|
+
static eraseCookie(name: string): void;
|
|
1750
1778
|
private static setCookie;
|
|
1751
1779
|
private static getCookie;
|
|
1752
|
-
static eraseCookie(name: string): void;
|
|
1753
1780
|
}
|
|
1754
1781
|
|
|
1755
1782
|
declare class Data {
|
package/package.json
CHANGED
package/src/api/Competitions.ts
CHANGED
|
@@ -636,7 +636,7 @@ class Competitions {
|
|
|
636
636
|
/**
|
|
637
637
|
* Get a leaderboard by a users wins.
|
|
638
638
|
*
|
|
639
|
-
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/
|
|
639
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderBoardUserWins
|
|
640
640
|
*
|
|
641
641
|
* @param competition_id
|
|
642
642
|
* @returns promise
|
|
@@ -660,7 +660,7 @@ class Competitions {
|
|
|
660
660
|
/**
|
|
661
661
|
* Get a leaderboard by a teams wins.
|
|
662
662
|
*
|
|
663
|
-
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/
|
|
663
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderBoardTeamWins
|
|
664
664
|
*
|
|
665
665
|
* @param competition_id
|
|
666
666
|
* @returns promise
|
|
@@ -672,7 +672,7 @@ class Competitions {
|
|
|
672
672
|
/**
|
|
673
673
|
* Get all leaderboards.
|
|
674
674
|
*
|
|
675
|
-
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/
|
|
675
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderBoardTeamPoints
|
|
676
676
|
*
|
|
677
677
|
* @param competition_id
|
|
678
678
|
* @returns promise
|
|
@@ -680,6 +680,18 @@ class Competitions {
|
|
|
680
680
|
public static allLeaderboards<T>(competition_id : string, params?: Record<string, any>): AxiosPromise<Response<T>>{
|
|
681
681
|
return Requests.processRoute(CompetitionRoutes.routes.allLeaderboards, {}, {competition_id : competition_id}, params);
|
|
682
682
|
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Gets all the information about a competition for the current user.
|
|
686
|
+
*
|
|
687
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/competitionLeaderboardsAll
|
|
688
|
+
*
|
|
689
|
+
* @param competition_id
|
|
690
|
+
* @returns promise
|
|
691
|
+
*/
|
|
692
|
+
public static me<T>(competition_id : string, params?: Record<string, any>): AxiosPromise<Response<T>>{
|
|
693
|
+
return Requests.processRoute(CompetitionRoutes.routes.allLeaderboards, {}, {competition_id : competition_id}, params);
|
|
694
|
+
}
|
|
683
695
|
}
|
|
684
696
|
|
|
685
697
|
export default Competitions;
|
package/src/config/Config.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
*/
|
|
@@ -52,6 +52,7 @@ class CompetitionRoutes {
|
|
|
52
52
|
userWinsLeaderboard : { url: '/competitions/{competition_id}/userWinsLeaderboard', method: HTTP_METHODS.GET },
|
|
53
53
|
teamWinsLeaderboard : { url: '/competitions/{competition_id}/teamWinsLeaderboard', method: HTTP_METHODS.GET },
|
|
54
54
|
allLeaderboards : { url: '/competitions/{competition_id}/allLeaderboards', method: HTTP_METHODS.GET },
|
|
55
|
+
me : { url: '/competitions/{competition_id}/me', method: HTTP_METHODS.GET },
|
|
55
56
|
};
|
|
56
57
|
|
|
57
58
|
|
package/src/util/Storage.ts
CHANGED
|
@@ -1,106 +1,100 @@
|
|
|
1
1
|
class Storage {
|
|
2
|
+
private static rootDomain: string = '';
|
|
3
|
+
private static data: { [key: string]: any } = {};
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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.
|
|
26
|
+
window.sessionStorage.setItem(Storage.getStorageKey(key), serializedValue);
|
|
13
27
|
} catch (e) {
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
-
|
|
56
|
+
public static setAuthToken(token: string | null) {
|
|
57
|
+
Storage.set('glitch_auth_token', token);
|
|
58
|
+
}
|
|
40
59
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return JSON.parse(serializedValue);
|
|
45
|
-
}
|
|
60
|
+
public static getAuthToken(): string | null {
|
|
61
|
+
return Storage.get('glitch_auth_token');
|
|
62
|
+
}
|
|
46
63
|
|
|
47
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
106
|
-
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default Storage;
|