glitch-javascript-sdk 0.0.1 → 0.0.3

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,20 @@
1
+ import Route from "./interface";
2
+ import HTTP_METHODS from "../constants/HttpMethods";
3
+
4
+ class UserRoutes {
5
+
6
+ public static routes: { [key: string]: Route } = {
7
+ list: { url: '/users', method: HTTP_METHODS.GET },
8
+ update: { url: '/users', method: HTTP_METHODS.PUT },
9
+ follow : { url: '/users/{user_id}/follow', method: HTTP_METHODS.POST },
10
+ profile :{ url: '/users/{uuid}/profile', method: HTTP_METHODS.GET },
11
+ me : { url: '/users/me', method: HTTP_METHODS.GET },
12
+ oneTimeToken : { url: '/users/oneTimeToken', method: HTTP_METHODS.GET },
13
+ uploadAvatar : { url: '/users/uploadAvatarImage', method: HTTP_METHODS.POST },
14
+ uploadBanner : { url: '/users/uploadBannerImage', method: HTTP_METHODS.POST },
15
+ createDonationPage : { url: '/users/createDonationPage', method: HTTP_METHODS.POST },
16
+ };
17
+
18
+ }
19
+
20
+ export default UserRoutes;
@@ -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
- private static async request<T>(
52
+
53
+ private static request<T>(
35
54
  method: 'GET' | 'POST' | 'PUT' | 'DELETE',
36
55
  url: string,
37
56
  data?: any
38
- ): Promise<Response<T>> {
39
- try {
40
- const response = await axios({
41
- method,
42
- url: `${this.baseUrl}${url}`,
43
- data,
44
- headers: {
45
- Authorization: `Bearer ${this.authToken}`,
46
- 'Content-Type': 'application/json',
47
- },
48
- });
49
- return {
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
- public static async get<T>(url: string): Promise<Response<T>> {
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 async post<T>(url: string, data: any): Promise<Response<T>> {
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 async put<T>(url: string, data: any): Promise<Response<T>> {
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 async delete<T>(url: string): Promise<Response<T>> {
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 async processRoute(route : Route, data? : object, routeReplace? : object) {
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
- //url = url.replace("{" + key + "}", routeReplace[key]);
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
  }