whio-api-sdk 1.1.1 → 1.1.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.
|
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { jwtDecode } from 'jwt-decode';
|
|
11
|
+
import axios from 'axios';
|
|
11
12
|
/**
|
|
12
13
|
* Base HTTP client for making authenticated requests
|
|
13
14
|
*/
|
|
@@ -43,12 +44,14 @@ export class BaseClient {
|
|
|
43
44
|
}
|
|
44
45
|
fetchConfig(url) {
|
|
45
46
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
try {
|
|
48
|
+
const response = yield axios.get(url);
|
|
49
|
+
const conf = response.data;
|
|
50
|
+
this.baseUrl = conf.baseUrl || this.baseUrl;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
48
53
|
throw new Error(`Failed to fetch config from ${url}`);
|
|
49
54
|
}
|
|
50
|
-
const conf = yield response.json();
|
|
51
|
-
this.baseUrl = conf.baseUrl || this.baseUrl;
|
|
52
55
|
});
|
|
53
56
|
}
|
|
54
57
|
getToken() {
|
|
@@ -69,6 +72,7 @@ export class BaseClient {
|
|
|
69
72
|
});
|
|
70
73
|
}
|
|
71
74
|
request(endpoint, method = 'GET', body, headers = {}, noToken = false) {
|
|
75
|
+
var _a, _b, _c;
|
|
72
76
|
return __awaiter(this, void 0, void 0, function* () {
|
|
73
77
|
const url = `${this.baseUrl}${endpoint}`;
|
|
74
78
|
const defaultHeaders = {
|
|
@@ -81,16 +85,22 @@ export class BaseClient {
|
|
|
81
85
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
82
86
|
}
|
|
83
87
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
try {
|
|
89
|
+
const response = yield axios({
|
|
90
|
+
url,
|
|
91
|
+
method,
|
|
92
|
+
headers: Object.assign(Object.assign({}, defaultHeaders), headers),
|
|
93
|
+
data: body,
|
|
94
|
+
timeout: 5000,
|
|
95
|
+
});
|
|
96
|
+
return response.data;
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
const axiosError = error;
|
|
100
|
+
const errorMessage = ((_b = (_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ||
|
|
101
|
+
`Request failed with status ${((_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.status) || 'unknown'}`;
|
|
102
|
+
throw new Error(errorMessage);
|
|
92
103
|
}
|
|
93
|
-
return response.json();
|
|
94
104
|
});
|
|
95
105
|
}
|
|
96
106
|
fileUploadRequest(endpoint, body, headers = {}, onProgress) {
|
|
@@ -150,6 +160,7 @@ export class BaseClient {
|
|
|
150
160
|
});
|
|
151
161
|
}
|
|
152
162
|
downloadRequest(endpoint) {
|
|
163
|
+
var _a, _b, _c;
|
|
153
164
|
return __awaiter(this, void 0, void 0, function* () {
|
|
154
165
|
const url = `${this.baseUrl}${endpoint}`;
|
|
155
166
|
const defaultHeaders = {
|
|
@@ -159,18 +170,25 @@ export class BaseClient {
|
|
|
159
170
|
if (token) {
|
|
160
171
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
161
172
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
173
|
+
try {
|
|
174
|
+
const response = yield axios({
|
|
175
|
+
url,
|
|
176
|
+
method: 'GET',
|
|
177
|
+
headers: defaultHeaders,
|
|
178
|
+
responseType: 'blob',
|
|
179
|
+
});
|
|
180
|
+
return response.data;
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
const axiosError = error;
|
|
184
|
+
const errorMessage = ((_b = (_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ||
|
|
185
|
+
`Download failed with status ${((_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.status) || 'unknown'}`;
|
|
186
|
+
throw new Error(errorMessage);
|
|
169
187
|
}
|
|
170
|
-
return response.blob();
|
|
171
188
|
});
|
|
172
189
|
}
|
|
173
190
|
textRequest(endpoint) {
|
|
191
|
+
var _a, _b, _c;
|
|
174
192
|
return __awaiter(this, void 0, void 0, function* () {
|
|
175
193
|
const url = `${this.baseUrl}${endpoint}`;
|
|
176
194
|
const defaultHeaders = {
|
|
@@ -180,15 +198,22 @@ export class BaseClient {
|
|
|
180
198
|
if (token) {
|
|
181
199
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
182
200
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
201
|
+
try {
|
|
202
|
+
const response = yield axios({
|
|
203
|
+
url,
|
|
204
|
+
method: 'GET',
|
|
205
|
+
headers: defaultHeaders,
|
|
206
|
+
responseType: 'text',
|
|
207
|
+
timeout: 5000,
|
|
208
|
+
});
|
|
209
|
+
return response.data;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
const axiosError = error;
|
|
213
|
+
const errorMessage = ((_b = (_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ||
|
|
214
|
+
`Text request failed with status ${((_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.status) || 'unknown'}`;
|
|
215
|
+
throw new Error(errorMessage);
|
|
190
216
|
}
|
|
191
|
-
return response.text();
|
|
192
217
|
});
|
|
193
218
|
}
|
|
194
219
|
clearAuth() {
|
|
@@ -235,7 +260,7 @@ export class BaseClient {
|
|
|
235
260
|
});
|
|
236
261
|
}
|
|
237
262
|
isAuthenticated() {
|
|
238
|
-
return !!this.accessToken && !!this.user;
|
|
263
|
+
return !!this.accessToken && !!this.user && !this.isTokenExpired(this.accessToken);
|
|
239
264
|
}
|
|
240
265
|
getCurrentUser() {
|
|
241
266
|
return this.user;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whio-api-sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"prepare": "npm run build"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
+
"axios": "^1.13.2",
|
|
13
14
|
"jwt-decode": "^4.0.0",
|
|
14
15
|
"socket.io-client": "^4.8.1",
|
|
15
16
|
"typescript": "^4.0.0"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { jwtDecode } from 'jwt-decode';
|
|
2
|
+
import axios, { AxiosError } from 'axios';
|
|
2
3
|
import { SDKConfig, User, LoginResponse } from '../types';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -38,12 +39,13 @@ export class BaseClient {
|
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
public async fetchConfig(url: string): Promise<void> {
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
try {
|
|
43
|
+
const response = await axios.get(url);
|
|
44
|
+
const conf = response.data;
|
|
45
|
+
this.baseUrl = conf.baseUrl || this.baseUrl;
|
|
46
|
+
} catch (error) {
|
|
43
47
|
throw new Error(`Failed to fetch config from ${url}`);
|
|
44
48
|
}
|
|
45
|
-
const conf = await response.json();
|
|
46
|
-
this.baseUrl = conf.baseUrl || this.baseUrl;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
protected async getToken() {
|
|
@@ -82,20 +84,22 @@ export class BaseClient {
|
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
try {
|
|
88
|
+
const response = await axios({
|
|
89
|
+
url,
|
|
90
|
+
method,
|
|
91
|
+
headers: { ...defaultHeaders, ...headers },
|
|
92
|
+
data: body,
|
|
93
|
+
timeout: 5000,
|
|
94
|
+
});
|
|
90
95
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
+
return response.data;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
const axiosError = error as AxiosError<{ message?: string }>;
|
|
99
|
+
const errorMessage = axiosError.response?.data?.message ||
|
|
100
|
+
`Request failed with status ${axiosError.response?.status || 'unknown'}`;
|
|
101
|
+
throw new Error(errorMessage);
|
|
96
102
|
}
|
|
97
|
-
|
|
98
|
-
return response.json();
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
protected async fileUploadRequest<T>(
|
|
@@ -175,19 +179,21 @@ export class BaseClient {
|
|
|
175
179
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
176
180
|
}
|
|
177
181
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
+
try {
|
|
183
|
+
const response = await axios({
|
|
184
|
+
url,
|
|
185
|
+
method: 'GET',
|
|
186
|
+
headers: defaultHeaders,
|
|
187
|
+
responseType: 'blob',
|
|
188
|
+
});
|
|
182
189
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
190
|
+
return response.data;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
const axiosError = error as AxiosError<{ message?: string }>;
|
|
193
|
+
const errorMessage = axiosError.response?.data?.message ||
|
|
194
|
+
`Download failed with status ${axiosError.response?.status || 'unknown'}`;
|
|
195
|
+
throw new Error(errorMessage);
|
|
188
196
|
}
|
|
189
|
-
|
|
190
|
-
return response.blob();
|
|
191
197
|
}
|
|
192
198
|
|
|
193
199
|
protected async textRequest(endpoint: string): Promise<string> {
|
|
@@ -202,19 +208,22 @@ export class BaseClient {
|
|
|
202
208
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
203
209
|
}
|
|
204
210
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
211
|
+
try {
|
|
212
|
+
const response = await axios({
|
|
213
|
+
url,
|
|
214
|
+
method: 'GET',
|
|
215
|
+
headers: defaultHeaders,
|
|
216
|
+
responseType: 'text',
|
|
217
|
+
timeout: 5000,
|
|
218
|
+
});
|
|
209
219
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
220
|
+
return response.data;
|
|
221
|
+
} catch (error) {
|
|
222
|
+
const axiosError = error as AxiosError<{ message?: string }>;
|
|
223
|
+
const errorMessage = axiosError.response?.data?.message ||
|
|
224
|
+
`Text request failed with status ${axiosError.response?.status || 'unknown'}`;
|
|
225
|
+
throw new Error(errorMessage);
|
|
215
226
|
}
|
|
216
|
-
|
|
217
|
-
return response.text();
|
|
218
227
|
}
|
|
219
228
|
|
|
220
229
|
protected async clearAuth(): Promise<void> {
|
|
@@ -268,7 +277,7 @@ export class BaseClient {
|
|
|
268
277
|
}
|
|
269
278
|
|
|
270
279
|
public isAuthenticated(): boolean {
|
|
271
|
-
return !!this.accessToken && !!this.user;
|
|
280
|
+
return !!this.accessToken && !!this.user && !this.isTokenExpired(this.accessToken);
|
|
272
281
|
}
|
|
273
282
|
|
|
274
283
|
public getCurrentUser(): User | null {
|