glitch-javascript-sdk 0.0.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.
@@ -0,0 +1,16 @@
1
+ import Route from "./interface";
2
+ import HTTP_METHODS from "../constants/HttpMethods";
3
+
4
+ class CompetitionRoutes {
5
+
6
+ public static routes: { [key: string]: Route } = {
7
+ list: { url: '/competitions', method: HTTP_METHODS.GET },
8
+ create: { url: '/competitions', method: HTTP_METHODS.POST },
9
+ view : { url: '/competitions/{competition_id}', method: HTTP_METHODS.GET },
10
+ update :{ url: '/competitions/{competition_id}', method: HTTP_METHODS.PUT },
11
+ delete : { url: '/competitions/{competition_id}', method: HTTP_METHODS.DELETE },
12
+ };
13
+
14
+ }
15
+
16
+ export default CompetitionRoutes;
@@ -0,0 +1,6 @@
1
+ interface Route {
2
+ url: string;
3
+ method: string;
4
+ }
5
+
6
+ export default Route;
@@ -0,0 +1,119 @@
1
+ import axios from 'axios';
2
+ import Config from '../config/Config';
3
+ import HTTP_METHODS from '../constants/HttpMethods';
4
+ import Route from '../routes/interface';
5
+ import Response from './Response';
6
+
7
+ class Requests {
8
+
9
+ config: Config;
10
+
11
+ private static baseUrl = "";
12
+
13
+ private static authToken = "";
14
+
15
+ constructor(config: Config) {
16
+ this.config = config;
17
+ }
18
+
19
+ public static setConfig(config: Config) {
20
+
21
+ this.baseUrl = config.baseUrl;
22
+
23
+ this.authToken = config.authToken;
24
+ }
25
+
26
+ public static setBaseUrl(url : string) {
27
+ this.baseUrl = url;
28
+ }
29
+
30
+ public static setAuthToken(token : string) {
31
+ this.authToken = token;
32
+ }
33
+
34
+ private static async request<T>(
35
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE',
36
+ url: string,
37
+ 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
+ }
62
+ }
63
+
64
+ public static async get<T>(url: string): Promise<Response<T>> {
65
+ return this.request<T>('GET', url);
66
+ }
67
+
68
+ public static async post<T>(url: string, data: any): Promise<Response<T>> {
69
+ return this.request<T>('POST', url, data);
70
+ }
71
+
72
+ public static async put<T>(url: string, data: any): Promise<Response<T>> {
73
+ return this.request<T>('PUT', url, data);
74
+ }
75
+
76
+ public static async delete<T>(url: string): Promise<Response<T>> {
77
+ return this.request<T>('DELETE', url);
78
+ }
79
+
80
+ /**
81
+ * The Route class contains the method and url, thereforce items can be
82
+ * automatically routed depending on the configuration.
83
+ *
84
+ * @param route
85
+ * @param data
86
+ * @returns
87
+ */
88
+ public static async processRoute(route : Route, data? : object, routeReplace? : object) {
89
+
90
+ let url = route.url;
91
+
92
+ if(routeReplace) {
93
+
94
+ for (let key in routeReplace) {
95
+ //url = url.replace("{" + key + "}", routeReplace[key]);
96
+ }
97
+
98
+ }
99
+
100
+ if(route.method == HTTP_METHODS.GET) {
101
+ return this.get(url);
102
+ } else if(route.method == HTTP_METHODS.POST) {
103
+ return this.post(url, data);
104
+ } else if(route.method == HTTP_METHODS.PUT) {
105
+ return this.put(url, data);
106
+ } else if(route.method == HTTP_METHODS.DELETE) {
107
+ return this.delete(url);
108
+ }
109
+
110
+ }
111
+
112
+ }
113
+
114
+ export default Requests;
115
+
116
+
117
+
118
+
119
+
@@ -0,0 +1,8 @@
1
+ interface Response<T> {
2
+ data: T;
3
+ success: boolean;
4
+ message?: string;
5
+ }
6
+
7
+ export default Response;
8
+