glitch-javascript-sdk 0.1.2 → 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.
@@ -0,0 +1,5 @@
1
+ declare class Data {
2
+ static dataURItoBlob(dataURI: string): Blob;
3
+ static convertToHHMMSS(time: string | undefined): string | undefined;
4
+ }
5
+ export default Data;
@@ -0,0 +1,11 @@
1
+ declare class Parser {
2
+ /**
3
+ * To be used inside a catch close, this function will parse out any JSON in a error response from the api.
4
+ *
5
+ * @param error The Error object from the catch clause
6
+ *
7
+ * @returns Either returns a JSON object or false.
8
+ */
9
+ static parseJSONFromError(error: Error): object | boolean;
10
+ }
11
+ export default Parser;
@@ -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
@@ -1129,6 +1129,105 @@ declare class Waitlists {
1129
1129
  static delete<T>(waitlist_id: string): AxiosPromise<Response<T>>;
1130
1130
  }
1131
1131
 
1132
+ interface Route {
1133
+ url: string;
1134
+ method: string;
1135
+ }
1136
+
1137
+ declare class Requests {
1138
+ config: Config;
1139
+ private static baseUrl;
1140
+ private static authToken;
1141
+ constructor(config: Config);
1142
+ /**
1143
+ * Sets the base url of the API.
1144
+ *
1145
+ * @param url The url to of the API.
1146
+ */
1147
+ static setBaseUrl(url: string): void;
1148
+ /**
1149
+ * Sets the JSON Web token
1150
+ *
1151
+ * @param token
1152
+ */
1153
+ static setAuthToken(token: string): void;
1154
+ private static request;
1155
+ /**
1156
+ * Calls a GET request to the url endpoint.
1157
+ *
1158
+ * @param url
1159
+ * @returns
1160
+ */
1161
+ static get<T>(url: string): AxiosPromise<Response<T>>;
1162
+ static post<T>(url: string, data: any): AxiosPromise<Response<T>>;
1163
+ static put<T>(url: string, data: any): AxiosPromise<Response<T>>;
1164
+ static delete<T>(url: string): AxiosPromise<Response<T>>;
1165
+ static uploadFile<T>(url: string, filename: string, file: File, data?: any): AxiosPromise<Response<T>>;
1166
+ static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any): AxiosPromise<Response<T>>;
1167
+ /**
1168
+ * The Route class contains the method and url, thereforce items can be
1169
+ * automatically routed depending on the configuration.
1170
+ *
1171
+ * @param route
1172
+ * @param data
1173
+ * @returns
1174
+ */
1175
+ static processRoute<T>(route: Route, data?: object, routeReplace?: {
1176
+ [key: string]: any;
1177
+ }): AxiosPromise<Response<T>>;
1178
+ }
1179
+
1180
+ declare class Parser {
1181
+ /**
1182
+ * To be used inside a catch close, this function will parse out any JSON in a error response from the api.
1183
+ *
1184
+ * @param error The Error object from the catch clause
1185
+ *
1186
+ * @returns Either returns a JSON object or false.
1187
+ */
1188
+ static parseJSONFromError(error: Error): object | boolean;
1189
+ }
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
+
1132
1231
  declare class Glitch {
1133
1232
  static config: {
1134
1233
  Config: typeof Config;
@@ -1141,6 +1240,13 @@ declare class Glitch {
1141
1240
  Teams: typeof Teams;
1142
1241
  Waitlists: typeof Waitlists;
1143
1242
  };
1243
+ static util: {
1244
+ Requests: typeof Requests;
1245
+ Parser: typeof Parser;
1246
+ Session: typeof Session;
1247
+ Storage: typeof Storage;
1248
+ Data: typeof Data;
1249
+ };
1144
1250
  }
1145
1251
 
1146
1252
  export { Glitch as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glitch-javascript-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Javascrip SDK for GLitch",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/index.ts CHANGED
@@ -3,13 +3,19 @@
3
3
  import { Config } from "./config";
4
4
 
5
5
  //API
6
- import { Auth } from "./api";
7
- import { Competitions } from "./api";
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";
11
11
  import { Waitlists } from "./api";
12
12
 
13
+ import Requests from "./util/Requests";
14
+ import Parser from "./util/Parser";
15
+ import Session from "./util/Session";
16
+ import Storage from "./util/Storage";
17
+ import Data from './util/Data';
18
+
13
19
  class Glitch {
14
20
 
15
21
  public static config = {
@@ -25,6 +31,14 @@ class Glitch {
25
31
  Waitlists: Waitlists
26
32
  }
27
33
 
34
+ public static util = {
35
+ Requests : Requests,
36
+ Parser : Parser,
37
+ Session: Session,
38
+ Storage : Storage,
39
+ Data : Data,
40
+ }
41
+
28
42
 
29
43
  }
30
44
 
@@ -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,24 @@
1
+ class Parser {
2
+
3
+ /**
4
+ * To be used inside a catch close, this function will parse out any JSON in a error response from the api.
5
+ *
6
+ * @param error The Error object from the catch clause
7
+ *
8
+ * @returns Either returns a JSON object or false.
9
+ */
10
+ public static parseJSONFromError(error: Error): object | boolean {
11
+
12
+ let errorString = error.toString();
13
+
14
+ errorString = errorString.replace('Error: ', '');
15
+
16
+ try {
17
+ return JSON.parse(errorString);
18
+ } catch (e) {
19
+ return false;
20
+ }
21
+ }
22
+ }
23
+
24
+ export default Parser;
@@ -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
+