glitch-javascript-sdk 0.0.1 → 0.0.2
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 -127
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/api/Auth.d.ts +34 -2
- package/dist/esm/api/Competitions.d.ts +50 -5
- package/dist/esm/index.js +470 -447
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Requests.d.ts +27 -5
- package/dist/index.d.ts +82 -7
- package/package.json +1 -1
- package/src/api/Auth.ts +38 -3
- package/src/api/Competitions.ts +50 -5
- package/src/util/Requests.ts +46 -31
|
@@ -1,19 +1,41 @@
|
|
|
1
1
|
import Config from '../config/Config';
|
|
2
2
|
import Route from '../routes/interface';
|
|
3
3
|
import Response from './Response';
|
|
4
|
+
import { AxiosPromise } from 'axios';
|
|
4
5
|
declare class Requests {
|
|
5
6
|
config: Config;
|
|
6
7
|
private static baseUrl;
|
|
7
8
|
private static authToken;
|
|
8
9
|
constructor(config: Config);
|
|
10
|
+
/**
|
|
11
|
+
* Sets the configuration of the system.
|
|
12
|
+
*
|
|
13
|
+
* @param config Config The config class.
|
|
14
|
+
*/
|
|
9
15
|
static setConfig(config: Config): void;
|
|
16
|
+
/**
|
|
17
|
+
* Sets the base url of the API.
|
|
18
|
+
*
|
|
19
|
+
* @param url The url to of the API.
|
|
20
|
+
*/
|
|
10
21
|
static setBaseUrl(url: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* Sets the JSON Web token
|
|
24
|
+
*
|
|
25
|
+
* @param token
|
|
26
|
+
*/
|
|
11
27
|
static setAuthToken(token: string): void;
|
|
12
28
|
private static request;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Calls a GET request to the url endpoint.
|
|
31
|
+
*
|
|
32
|
+
* @param url
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
static get<T>(url: string): AxiosPromise<Response<T>>;
|
|
36
|
+
static post<T>(url: string, data: any): AxiosPromise<Response<T>>;
|
|
37
|
+
static put<T>(url: string, data: any): AxiosPromise<Response<T>>;
|
|
38
|
+
static delete<T>(url: string): AxiosPromise<Response<T>>;
|
|
17
39
|
/**
|
|
18
40
|
* The Route class contains the method and url, thereforce items can be
|
|
19
41
|
* automatically routed depending on the configuration.
|
|
@@ -22,6 +44,6 @@ declare class Requests {
|
|
|
22
44
|
* @param data
|
|
23
45
|
* @returns
|
|
24
46
|
*/
|
|
25
|
-
static processRoute(route: Route, data?: object, routeReplace?: object):
|
|
47
|
+
static processRoute<T>(route: Route, data?: object, routeReplace?: object): AxiosPromise<Response<T>>;
|
|
26
48
|
}
|
|
27
49
|
export default Requests;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { AxiosPromise } from 'axios';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Config
|
|
3
5
|
*
|
|
@@ -19,16 +21,89 @@ interface Response<T> {
|
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
declare class Auth {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Attempts to authenticate a user using their email address.
|
|
26
|
+
*
|
|
27
|
+
* @see https://api.glitch.fun/api/documentation#/Authentication%20Route/authLogin
|
|
28
|
+
*
|
|
29
|
+
* @param email The email address of the user
|
|
30
|
+
* @param password The password of the user
|
|
31
|
+
*
|
|
32
|
+
* @returns A promise
|
|
33
|
+
*/
|
|
34
|
+
static loginWithEmail<T>(email: string, password: string): AxiosPromise<Response<T>>;
|
|
35
|
+
/**
|
|
36
|
+
* Attempts to authenticate a user using their username.
|
|
37
|
+
*
|
|
38
|
+
* @see https://api.glitch.fun/api/documentation#/Authentication%20Route/authLogin
|
|
39
|
+
*
|
|
40
|
+
* @param username The username of the user
|
|
41
|
+
* @param password The password of the user
|
|
42
|
+
*
|
|
43
|
+
* @returns A promise
|
|
44
|
+
*/
|
|
45
|
+
static loginWithUsername<T>(username: string, password: string): AxiosPromise<Response<T>>;
|
|
46
|
+
/**
|
|
47
|
+
* Attempts to register a user.
|
|
48
|
+
*
|
|
49
|
+
* @see https://api.glitch.fun/api/documentation#/Authentication%20Route/authRegister
|
|
50
|
+
*
|
|
51
|
+
* @param data The data the user can register with.
|
|
52
|
+
*
|
|
53
|
+
* @returns A promise
|
|
54
|
+
*/
|
|
55
|
+
static register<T>(data: object): AxiosPromise<Response<T>>;
|
|
24
56
|
}
|
|
25
57
|
|
|
26
58
|
declare class Competitions {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
59
|
+
/**
|
|
60
|
+
* List all the competitions
|
|
61
|
+
*
|
|
62
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/resourceList
|
|
63
|
+
*
|
|
64
|
+
* @returns promise
|
|
65
|
+
*/
|
|
66
|
+
static list<T>(): AxiosPromise<Response<T>>;
|
|
67
|
+
/**
|
|
68
|
+
* Create a new competition
|
|
69
|
+
*
|
|
70
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/newResourceStorage
|
|
71
|
+
*
|
|
72
|
+
* @param data The date to be passed when creating a competiton.
|
|
73
|
+
*
|
|
74
|
+
* @returns Promise
|
|
75
|
+
*/
|
|
76
|
+
static create<T>(data: object): AxiosPromise<Response<T>>;
|
|
77
|
+
/**
|
|
78
|
+
* Update a competition
|
|
79
|
+
*
|
|
80
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/updateStorage
|
|
81
|
+
*
|
|
82
|
+
* @param competition_id The id of the competition to update.
|
|
83
|
+
* @param data The data to update.
|
|
84
|
+
*
|
|
85
|
+
* @returns promise
|
|
86
|
+
*/
|
|
87
|
+
static update<T>(competition_id: string, data: object): AxiosPromise<Response<T>>;
|
|
88
|
+
/**
|
|
89
|
+
* Retrieve the information for a single competition.
|
|
90
|
+
*
|
|
91
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/showStorage
|
|
92
|
+
*
|
|
93
|
+
* @param competition_id The id fo the competition to retrieve.
|
|
94
|
+
*
|
|
95
|
+
* @returns promise
|
|
96
|
+
*/
|
|
97
|
+
static view<T>(competition_id: string): AxiosPromise<Response<T>>;
|
|
98
|
+
/**
|
|
99
|
+
* Deletes a competition.
|
|
100
|
+
*
|
|
101
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/destoryStorage
|
|
102
|
+
*
|
|
103
|
+
* @param competition_id The id of the competition to delete.
|
|
104
|
+
* @returns promise
|
|
105
|
+
*/
|
|
106
|
+
static delete<T>(competition_id: string): AxiosPromise<Response<T>>;
|
|
32
107
|
}
|
|
33
108
|
|
|
34
109
|
declare class Glitch {
|
package/package.json
CHANGED
package/src/api/Auth.ts
CHANGED
|
@@ -1,13 +1,48 @@
|
|
|
1
1
|
import AuthRoutes from "../routes/AuthRoute";
|
|
2
2
|
import Requests from "../util/Requests";
|
|
3
|
+
import Response from "../util/Response";
|
|
4
|
+
import { AxiosPromise } from "axios";
|
|
3
5
|
|
|
4
6
|
class Auth {
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Attempts to authenticate a user using their email address.
|
|
10
|
+
*
|
|
11
|
+
* @see https://api.glitch.fun/api/documentation#/Authentication%20Route/authLogin
|
|
12
|
+
*
|
|
13
|
+
* @param email The email address of the user
|
|
14
|
+
* @param password The password of the user
|
|
15
|
+
*
|
|
16
|
+
* @returns A promise
|
|
17
|
+
*/
|
|
18
|
+
public static loginWithEmail<T>(email: string, password: string) : AxiosPromise<Response<T>> {
|
|
19
|
+
return Requests.post(AuthRoutes.routes.login.url, {email : email, password: password});
|
|
8
20
|
}
|
|
9
21
|
|
|
10
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Attempts to authenticate a user using their username.
|
|
24
|
+
*
|
|
25
|
+
* @see https://api.glitch.fun/api/documentation#/Authentication%20Route/authLogin
|
|
26
|
+
*
|
|
27
|
+
* @param username The username of the user
|
|
28
|
+
* @param password The password of the user
|
|
29
|
+
*
|
|
30
|
+
* @returns A promise
|
|
31
|
+
*/
|
|
32
|
+
public static loginWithUsername<T>(username: string, password: string) : AxiosPromise<Response<T>> {
|
|
33
|
+
return Requests.post(AuthRoutes.routes.login.url, {username : username, password: password});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Attempts to register a user.
|
|
38
|
+
*
|
|
39
|
+
* @see https://api.glitch.fun/api/documentation#/Authentication%20Route/authRegister
|
|
40
|
+
*
|
|
41
|
+
* @param data The data the user can register with.
|
|
42
|
+
*
|
|
43
|
+
* @returns A promise
|
|
44
|
+
*/
|
|
45
|
+
public static register<T>(data : object) : AxiosPromise<Response<T>> {
|
|
11
46
|
return Requests.processRoute(AuthRoutes.routes.register, data);
|
|
12
47
|
}
|
|
13
48
|
}
|
package/src/api/Competitions.ts
CHANGED
|
@@ -1,28 +1,73 @@
|
|
|
1
1
|
import CompetitionRoutes from "../routes/CompetitionRoute";
|
|
2
2
|
import Requests from "../util/Requests";
|
|
3
|
+
import Response from "../util/Response";
|
|
4
|
+
import { AxiosPromise } from "axios";
|
|
3
5
|
|
|
4
6
|
class Competitions {
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
/**
|
|
9
|
+
* List all the competitions
|
|
10
|
+
*
|
|
11
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/resourceList
|
|
12
|
+
*
|
|
13
|
+
* @returns promise
|
|
14
|
+
*/
|
|
15
|
+
public static list<T>() : AxiosPromise<Response<T>> {
|
|
7
16
|
return Requests.processRoute(CompetitionRoutes.routes.list);
|
|
8
17
|
}
|
|
9
18
|
|
|
10
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Create a new competition
|
|
21
|
+
*
|
|
22
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/newResourceStorage
|
|
23
|
+
*
|
|
24
|
+
* @param data The date to be passed when creating a competiton.
|
|
25
|
+
*
|
|
26
|
+
* @returns Promise
|
|
27
|
+
*/
|
|
28
|
+
public static create<T>(data : object) : AxiosPromise<Response<T>> {
|
|
11
29
|
|
|
12
30
|
return Requests.processRoute(CompetitionRoutes.routes.create, data);
|
|
13
31
|
}
|
|
14
32
|
|
|
15
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Update a competition
|
|
35
|
+
*
|
|
36
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/updateStorage
|
|
37
|
+
*
|
|
38
|
+
* @param competition_id The id of the competition to update.
|
|
39
|
+
* @param data The data to update.
|
|
40
|
+
*
|
|
41
|
+
* @returns promise
|
|
42
|
+
*/
|
|
43
|
+
public static update<T>(competition_id : string, data : object) : AxiosPromise<Response<T>>{
|
|
16
44
|
|
|
17
45
|
return Requests.processRoute(CompetitionRoutes.routes.create, data, {competition_id : competition_id});
|
|
18
46
|
}
|
|
19
47
|
|
|
20
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Retrieve the information for a single competition.
|
|
50
|
+
*
|
|
51
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/showStorage
|
|
52
|
+
*
|
|
53
|
+
* @param competition_id The id fo the competition to retrieve.
|
|
54
|
+
*
|
|
55
|
+
* @returns promise
|
|
56
|
+
*/
|
|
57
|
+
public static view<T>(competition_id : string) : AxiosPromise<Response<T>> {
|
|
21
58
|
|
|
22
59
|
return Requests.processRoute(CompetitionRoutes.routes.view, {}, {competition_id : competition_id});
|
|
23
60
|
}
|
|
24
61
|
|
|
25
|
-
|
|
62
|
+
/**
|
|
63
|
+
* Deletes a competition.
|
|
64
|
+
*
|
|
65
|
+
* @see https://api.glitch.fun/api/documentation#/Competitions%20Route/destoryStorage
|
|
66
|
+
*
|
|
67
|
+
* @param competition_id The id of the competition to delete.
|
|
68
|
+
* @returns promise
|
|
69
|
+
*/
|
|
70
|
+
public static delete<T>(competition_id : string) : AxiosPromise<Response<T>> {
|
|
26
71
|
|
|
27
72
|
return Requests.processRoute(CompetitionRoutes.routes.delete, {}, {competition_id : competition_id});
|
|
28
73
|
}
|
package/src/util/Requests.ts
CHANGED
|
@@ -3,19 +3,27 @@ import Config from '../config/Config';
|
|
|
3
3
|
import HTTP_METHODS from '../constants/HttpMethods';
|
|
4
4
|
import Route from '../routes/interface';
|
|
5
5
|
import Response from './Response';
|
|
6
|
+
import { AxiosPromise } from 'axios';
|
|
6
7
|
|
|
7
8
|
class Requests {
|
|
8
9
|
|
|
9
10
|
config: Config;
|
|
10
11
|
|
|
12
|
+
//The base url of the API.
|
|
11
13
|
private static baseUrl = "";
|
|
12
14
|
|
|
15
|
+
//The Json Web Token to send in the header
|
|
13
16
|
private static authToken = "";
|
|
14
17
|
|
|
15
18
|
constructor(config: Config) {
|
|
16
19
|
this.config = config;
|
|
17
20
|
}
|
|
18
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Sets the configuration of the system.
|
|
24
|
+
*
|
|
25
|
+
* @param config Config The config class.
|
|
26
|
+
*/
|
|
19
27
|
public static setConfig(config: Config) {
|
|
20
28
|
|
|
21
29
|
this.baseUrl = config.baseUrl;
|
|
@@ -23,57 +31,62 @@ class Requests {
|
|
|
23
31
|
this.authToken = config.authToken;
|
|
24
32
|
}
|
|
25
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Sets the base url of the API.
|
|
36
|
+
*
|
|
37
|
+
* @param url The url to of the API.
|
|
38
|
+
*/
|
|
26
39
|
public static setBaseUrl(url : string) {
|
|
27
40
|
this.baseUrl = url;
|
|
28
41
|
}
|
|
29
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Sets the JSON Web token
|
|
45
|
+
*
|
|
46
|
+
* @param token
|
|
47
|
+
*/
|
|
30
48
|
public static setAuthToken(token : string) {
|
|
31
49
|
this.authToken = token;
|
|
32
50
|
}
|
|
33
51
|
|
|
34
|
-
|
|
52
|
+
|
|
53
|
+
private static request<T>(
|
|
35
54
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
|
36
55
|
url: string,
|
|
37
56
|
data?: any
|
|
38
|
-
):
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
data: response.data,
|
|
51
|
-
success: true,
|
|
52
|
-
};
|
|
53
|
-
} catch (error) {
|
|
54
|
-
|
|
55
|
-
const message = error.response?.data?.message || 'An error occurred';
|
|
56
|
-
return {
|
|
57
|
-
data: null,
|
|
58
|
-
success: false,
|
|
59
|
-
message,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
57
|
+
): AxiosPromise<Response<T>> {
|
|
58
|
+
const axiosPromise = axios({
|
|
59
|
+
method,
|
|
60
|
+
url: `${this.baseUrl}${url}`,
|
|
61
|
+
data,
|
|
62
|
+
headers: {
|
|
63
|
+
Authorization: `Bearer ${this.authToken}`,
|
|
64
|
+
'Content-Type': 'application/json',
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return axiosPromise;
|
|
62
69
|
}
|
|
63
70
|
|
|
64
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Calls a GET request to the url endpoint.
|
|
73
|
+
*
|
|
74
|
+
* @param url
|
|
75
|
+
* @returns
|
|
76
|
+
*/
|
|
77
|
+
public static get<T>(url: string): AxiosPromise<Response<T>> {
|
|
65
78
|
return this.request<T>('GET', url);
|
|
66
79
|
}
|
|
67
80
|
|
|
68
|
-
public static
|
|
81
|
+
public static post<T>(url: string, data: any): AxiosPromise<Response<T>> {
|
|
69
82
|
return this.request<T>('POST', url, data);
|
|
70
83
|
}
|
|
71
84
|
|
|
72
|
-
public static
|
|
85
|
+
public static put<T>(url: string, data: any): AxiosPromise<Response<T>> {
|
|
73
86
|
return this.request<T>('PUT', url, data);
|
|
74
87
|
}
|
|
75
88
|
|
|
76
|
-
public static
|
|
89
|
+
public static delete<T>(url: string): AxiosPromise<Response<T>> {
|
|
77
90
|
return this.request<T>('DELETE', url);
|
|
78
91
|
}
|
|
79
92
|
|
|
@@ -85,14 +98,14 @@ class Requests {
|
|
|
85
98
|
* @param data
|
|
86
99
|
* @returns
|
|
87
100
|
*/
|
|
88
|
-
public static
|
|
101
|
+
public static processRoute<T>(route : Route, data? : object, routeReplace? : object) : AxiosPromise<Response<T>> {
|
|
89
102
|
|
|
90
103
|
let url = route.url;
|
|
91
104
|
|
|
92
105
|
if(routeReplace) {
|
|
93
106
|
|
|
94
107
|
for (let key in routeReplace) {
|
|
95
|
-
|
|
108
|
+
url = url.replace("{" + key + "}", routeReplace[key]);
|
|
96
109
|
}
|
|
97
110
|
|
|
98
111
|
}
|
|
@@ -107,6 +120,8 @@ class Requests {
|
|
|
107
120
|
return this.delete(url);
|
|
108
121
|
}
|
|
109
122
|
|
|
123
|
+
return this.get(url);
|
|
124
|
+
|
|
110
125
|
}
|
|
111
126
|
|
|
112
127
|
}
|