glitch-javascript-sdk 0.1.3 → 0.1.4
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/README.md +42 -7
- package/dist/cjs/index.js +162 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +8 -2
- package/dist/esm/index.js +162 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Data.d.ts +5 -0
- package/dist/esm/util/Session.d.ts +24 -0
- package/dist/esm/util/Storage.d.ts +11 -0
- package/dist/index.d.ts +43 -0
- package/package.json +1 -1
- package/src/index.ts +9 -3
- package/src/util/Data.ts +41 -0
- package/src/util/Session.ts +53 -0
- package/src/util/Storage.ts +85 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
declare class Session {
|
|
2
|
+
private static _id_key;
|
|
3
|
+
private static _first_name_key;
|
|
4
|
+
private static _last_name_key;
|
|
5
|
+
private static _username_key;
|
|
6
|
+
private static _email_key;
|
|
7
|
+
static isLoggedIn(): boolean;
|
|
8
|
+
static getAuthToken(): string | null;
|
|
9
|
+
static getID(): string | null;
|
|
10
|
+
static getFirstName(): string | null;
|
|
11
|
+
static getLastName(): string | null;
|
|
12
|
+
static getEmail(): string | null;
|
|
13
|
+
static end(): void;
|
|
14
|
+
static processAuthentication(data: {
|
|
15
|
+
token: {
|
|
16
|
+
access_token: string;
|
|
17
|
+
};
|
|
18
|
+
id: string;
|
|
19
|
+
first_name: string;
|
|
20
|
+
last_name: string;
|
|
21
|
+
email: string;
|
|
22
|
+
}): void;
|
|
23
|
+
}
|
|
24
|
+
export default Session;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare class Storage {
|
|
2
|
+
private static data;
|
|
3
|
+
static set(key: string, value: any): void;
|
|
4
|
+
static get(key: string): any;
|
|
5
|
+
static setAuthToken(token: string | null): void;
|
|
6
|
+
static getAuthToken(): string | null;
|
|
7
|
+
private static setCookie;
|
|
8
|
+
private static getCookie;
|
|
9
|
+
static eraseCookie(name: string): void;
|
|
10
|
+
}
|
|
11
|
+
export default Storage;
|
package/dist/index.d.ts
CHANGED
|
@@ -1188,6 +1188,46 @@ declare class Parser {
|
|
|
1188
1188
|
static parseJSONFromError(error: Error): object | boolean;
|
|
1189
1189
|
}
|
|
1190
1190
|
|
|
1191
|
+
declare class Session {
|
|
1192
|
+
private static _id_key;
|
|
1193
|
+
private static _first_name_key;
|
|
1194
|
+
private static _last_name_key;
|
|
1195
|
+
private static _username_key;
|
|
1196
|
+
private static _email_key;
|
|
1197
|
+
static isLoggedIn(): boolean;
|
|
1198
|
+
static getAuthToken(): string | null;
|
|
1199
|
+
static getID(): string | null;
|
|
1200
|
+
static getFirstName(): string | null;
|
|
1201
|
+
static getLastName(): string | null;
|
|
1202
|
+
static getEmail(): string | null;
|
|
1203
|
+
static end(): void;
|
|
1204
|
+
static processAuthentication(data: {
|
|
1205
|
+
token: {
|
|
1206
|
+
access_token: string;
|
|
1207
|
+
};
|
|
1208
|
+
id: string;
|
|
1209
|
+
first_name: string;
|
|
1210
|
+
last_name: string;
|
|
1211
|
+
email: string;
|
|
1212
|
+
}): void;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
declare class Storage {
|
|
1216
|
+
private static data;
|
|
1217
|
+
static set(key: string, value: any): void;
|
|
1218
|
+
static get(key: string): any;
|
|
1219
|
+
static setAuthToken(token: string | null): void;
|
|
1220
|
+
static getAuthToken(): string | null;
|
|
1221
|
+
private static setCookie;
|
|
1222
|
+
private static getCookie;
|
|
1223
|
+
static eraseCookie(name: string): void;
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
declare class Data {
|
|
1227
|
+
static dataURItoBlob(dataURI: string): Blob;
|
|
1228
|
+
static convertToHHMMSS(time: string | undefined): string | undefined;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1191
1231
|
declare class Glitch {
|
|
1192
1232
|
static config: {
|
|
1193
1233
|
Config: typeof Config;
|
|
@@ -1203,6 +1243,9 @@ declare class Glitch {
|
|
|
1203
1243
|
static util: {
|
|
1204
1244
|
Requests: typeof Requests;
|
|
1205
1245
|
Parser: typeof Parser;
|
|
1246
|
+
Session: typeof Session;
|
|
1247
|
+
Storage: typeof Storage;
|
|
1248
|
+
Data: typeof Data;
|
|
1206
1249
|
};
|
|
1207
1250
|
}
|
|
1208
1251
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
import { Config } from "./config";
|
|
4
4
|
|
|
5
5
|
//API
|
|
6
|
-
import
|
|
7
|
-
import
|
|
6
|
+
import Auth from "./api/Auth";
|
|
7
|
+
import Competitions from "./api/Competitions";
|
|
8
8
|
import { Users } from "./api";
|
|
9
9
|
import { Events } from "./api";
|
|
10
10
|
import { Teams } from "./api";
|
|
@@ -12,6 +12,9 @@ import { Waitlists } from "./api";
|
|
|
12
12
|
|
|
13
13
|
import Requests from "./util/Requests";
|
|
14
14
|
import Parser from "./util/Parser";
|
|
15
|
+
import Session from "./util/Session";
|
|
16
|
+
import Storage from "./util/Storage";
|
|
17
|
+
import Data from './util/Data';
|
|
15
18
|
|
|
16
19
|
class Glitch {
|
|
17
20
|
|
|
@@ -30,7 +33,10 @@ class Glitch {
|
|
|
30
33
|
|
|
31
34
|
public static util = {
|
|
32
35
|
Requests : Requests,
|
|
33
|
-
Parser : Parser
|
|
36
|
+
Parser : Parser,
|
|
37
|
+
Session: Session,
|
|
38
|
+
Storage : Storage,
|
|
39
|
+
Data : Data,
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
|
package/src/util/Data.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
class Data {
|
|
2
|
+
|
|
3
|
+
public static dataURItoBlob(dataURI: string): Blob {
|
|
4
|
+
const byteString = atob(dataURI.split(',')[1]);
|
|
5
|
+
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
|
|
6
|
+
const ab = new ArrayBuffer(byteString.length);
|
|
7
|
+
const ia = new Uint8Array(ab);
|
|
8
|
+
|
|
9
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
10
|
+
ia[i] = byteString.charCodeAt(i);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const blob = new Blob([ab], { type: mimeString });
|
|
14
|
+
return blob;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static convertToHHMMSS(time: string | undefined): string | undefined {
|
|
18
|
+
if (!time) {
|
|
19
|
+
return time;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const sec_num = parseInt(time, 10);
|
|
23
|
+
let hours = Math.floor(sec_num / 3600);
|
|
24
|
+
let minutes = Math.floor((sec_num - (hours * 3600)) / 60);
|
|
25
|
+
let seconds = sec_num - (hours * 3600) - (minutes * 60);
|
|
26
|
+
|
|
27
|
+
if (hours < 10) {
|
|
28
|
+
hours = Number('0' + hours);
|
|
29
|
+
}
|
|
30
|
+
if (minutes < 10) {
|
|
31
|
+
minutes = Number('0' + minutes);
|
|
32
|
+
}
|
|
33
|
+
if (seconds < 10) {
|
|
34
|
+
seconds = Number('0' + seconds);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return `${hours}:${minutes}:${seconds}`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default Data;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import Storage from "./Storage";
|
|
2
|
+
|
|
3
|
+
class Session {
|
|
4
|
+
|
|
5
|
+
private static _id_key = 'user_id';
|
|
6
|
+
private static _first_name_key = 'user_first_name';
|
|
7
|
+
private static _last_name_key = 'user_last_name';
|
|
8
|
+
private static _username_key = 'username';
|
|
9
|
+
private static _email_key = 'email';
|
|
10
|
+
|
|
11
|
+
public static isLoggedIn(): boolean {
|
|
12
|
+
const authToken = Storage.getAuthToken();
|
|
13
|
+
return authToken !== null && authToken !== 'null' && authToken !== undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public static getAuthToken(): string | null {
|
|
17
|
+
return Storage.getAuthToken();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static getID(): string | null {
|
|
21
|
+
return Storage.get(Session._id_key);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static getFirstName(): string | null {
|
|
25
|
+
return Storage.get(Session._first_name_key);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public static getLastName(): string | null {
|
|
29
|
+
return Storage.get(Session._last_name_key);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public static getEmail(): string | null {
|
|
33
|
+
return Storage.get(Session._email_key);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public static end(): void {
|
|
37
|
+
Storage.setAuthToken(null);
|
|
38
|
+
Storage.set(Session._id_key, null);
|
|
39
|
+
Storage.set(Session._first_name_key, null);
|
|
40
|
+
Storage.set(Session._last_name_key, null);
|
|
41
|
+
Storage.set(Session._email_key, null);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public static processAuthentication(data: { token: { access_token: string }, id: string, first_name: string, last_name: string, email: string }): void {
|
|
45
|
+
Storage.setAuthToken(data.token.access_token);
|
|
46
|
+
Storage.set(Session._id_key, data.id);
|
|
47
|
+
Storage.set(Session._first_name_key, data.first_name);
|
|
48
|
+
Storage.set(Session._last_name_key, data.last_name);
|
|
49
|
+
Storage.set(Session._email_key, data.email);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default Session;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
class Storage {
|
|
2
|
+
|
|
3
|
+
//Back up data type if no storage is working.
|
|
4
|
+
|
|
5
|
+
private static data: { [key: string]: any } = {};
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
public static set(key: string, value: any) {
|
|
9
|
+
try {
|
|
10
|
+
window.localStorage.setItem(key, value);
|
|
11
|
+
} catch (e) {
|
|
12
|
+
try {
|
|
13
|
+
window.sessionStorage.setItem(key, value);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
this.setCookie(key, value, 31);
|
|
16
|
+
//fallback if set cookie fails
|
|
17
|
+
Storage.data[key] = value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static get(key: string): any {
|
|
23
|
+
try {
|
|
24
|
+
return window.localStorage.getItem(key);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
try {
|
|
27
|
+
return window.sessionStorage.getItem(key);
|
|
28
|
+
} catch (e) {
|
|
29
|
+
let value = Storage.getCookie(key);
|
|
30
|
+
|
|
31
|
+
if (!value) {
|
|
32
|
+
value = Storage.data[key];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public static setAuthToken(token: string | null) {
|
|
41
|
+
Storage.set('glitch_auth_token', token);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public static getAuthToken(): string | null {
|
|
45
|
+
return Storage.get('glitch_auth_token');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private static setCookie(name: string, value: string, days: number) {
|
|
49
|
+
let expires = '';
|
|
50
|
+
if (days) {
|
|
51
|
+
const date = new Date();
|
|
52
|
+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
53
|
+
expires = '; expires=' + date.toUTCString();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//IFrames require HttpyOnly to be false, Chrome require SameSite to be none, and must be secure
|
|
57
|
+
document.cookie =
|
|
58
|
+
name +
|
|
59
|
+
'=' +
|
|
60
|
+
(value || '') +
|
|
61
|
+
expires +
|
|
62
|
+
'; path=/; HttpOnly=false; SameSite=none; Secure';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private static getCookie(name: string): string | null {
|
|
66
|
+
const nameEQ = name + '=';
|
|
67
|
+
const ca = document.cookie.split(';');
|
|
68
|
+
for (let i = 0; i < ca.length; i++) {
|
|
69
|
+
let c = ca[i];
|
|
70
|
+
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
|
|
71
|
+
if (c.indexOf(nameEQ) == 0)
|
|
72
|
+
return c.substring(nameEQ.length, c.length);
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public static eraseCookie(name: string) {
|
|
78
|
+
document.cookie =
|
|
79
|
+
name +
|
|
80
|
+
'=; Secure; HttpOnly=false; SameSite=none; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default Storage;
|
|
85
|
+
|