whio-api-sdk 1.1.2 → 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,17 +85,22 @@ export class BaseClient {
|
|
|
81
85
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
82
86
|
}
|
|
83
87
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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);
|
|
93
103
|
}
|
|
94
|
-
return response.json();
|
|
95
104
|
});
|
|
96
105
|
}
|
|
97
106
|
fileUploadRequest(endpoint, body, headers = {}, onProgress) {
|
|
@@ -151,6 +160,7 @@ export class BaseClient {
|
|
|
151
160
|
});
|
|
152
161
|
}
|
|
153
162
|
downloadRequest(endpoint) {
|
|
163
|
+
var _a, _b, _c;
|
|
154
164
|
return __awaiter(this, void 0, void 0, function* () {
|
|
155
165
|
const url = `${this.baseUrl}${endpoint}`;
|
|
156
166
|
const defaultHeaders = {
|
|
@@ -160,18 +170,25 @@ export class BaseClient {
|
|
|
160
170
|
if (token) {
|
|
161
171
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
162
172
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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);
|
|
170
187
|
}
|
|
171
|
-
return response.blob();
|
|
172
188
|
});
|
|
173
189
|
}
|
|
174
190
|
textRequest(endpoint) {
|
|
191
|
+
var _a, _b, _c;
|
|
175
192
|
return __awaiter(this, void 0, void 0, function* () {
|
|
176
193
|
const url = `${this.baseUrl}${endpoint}`;
|
|
177
194
|
const defaultHeaders = {
|
|
@@ -181,16 +198,22 @@ export class BaseClient {
|
|
|
181
198
|
if (token) {
|
|
182
199
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
183
200
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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);
|
|
192
216
|
}
|
|
193
|
-
return response.text();
|
|
194
217
|
});
|
|
195
218
|
}
|
|
196
219
|
clearAuth() {
|
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,21 +84,22 @@ export class BaseClient {
|
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
try {
|
|
88
|
+
const response = await axios({
|
|
89
|
+
url,
|
|
90
|
+
method,
|
|
91
|
+
headers: { ...defaultHeaders, ...headers },
|
|
92
|
+
data: body,
|
|
93
|
+
timeout: 5000,
|
|
94
|
+
});
|
|
91
95
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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);
|
|
97
102
|
}
|
|
98
|
-
|
|
99
|
-
return response.json();
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
protected async fileUploadRequest<T>(
|
|
@@ -176,19 +179,21 @@ export class BaseClient {
|
|
|
176
179
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
177
180
|
}
|
|
178
181
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
182
|
+
try {
|
|
183
|
+
const response = await axios({
|
|
184
|
+
url,
|
|
185
|
+
method: 'GET',
|
|
186
|
+
headers: defaultHeaders,
|
|
187
|
+
responseType: 'blob',
|
|
188
|
+
});
|
|
183
189
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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);
|
|
189
196
|
}
|
|
190
|
-
|
|
191
|
-
return response.blob();
|
|
192
197
|
}
|
|
193
198
|
|
|
194
199
|
protected async textRequest(endpoint: string): Promise<string> {
|
|
@@ -203,20 +208,22 @@ export class BaseClient {
|
|
|
203
208
|
defaultHeaders['Authorization'] = `Bearer ${token}`;
|
|
204
209
|
}
|
|
205
210
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
+
try {
|
|
212
|
+
const response = await axios({
|
|
213
|
+
url,
|
|
214
|
+
method: 'GET',
|
|
215
|
+
headers: defaultHeaders,
|
|
216
|
+
responseType: 'text',
|
|
217
|
+
timeout: 5000,
|
|
218
|
+
});
|
|
211
219
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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);
|
|
217
226
|
}
|
|
218
|
-
|
|
219
|
-
return response.text();
|
|
220
227
|
}
|
|
221
228
|
|
|
222
229
|
protected async clearAuth(): Promise<void> {
|