@dapex-tech/elite-online-services 0.0.10 → 0.0.12
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/core/request.d.ts +9 -5
- package/core/request.js +48 -47
- package/package.json +4 -3
package/core/request.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { AxiosResponse, AxiosInstance } from 'axios';
|
|
2
|
+
import FormData from 'form-data';
|
|
1
3
|
import type { ApiRequestOptions } from './ApiRequestOptions';
|
|
2
4
|
import type { ApiResult } from './ApiResult';
|
|
3
5
|
import { CancelablePromise } from './CancelablePromise';
|
|
@@ -8,23 +10,25 @@ export declare const isString: (value: any) => value is string;
|
|
|
8
10
|
export declare const isStringWithValue: (value: any) => value is string;
|
|
9
11
|
export declare const isBlob: (value: any) => value is Blob;
|
|
10
12
|
export declare const isFormData: (value: any) => value is FormData;
|
|
13
|
+
export declare const isSuccess: (status: number) => boolean;
|
|
11
14
|
export declare const base64: (str: string) => string;
|
|
12
15
|
export declare const getQueryString: (params: Record<string, any>) => string;
|
|
13
16
|
export declare const getFormData: (options: ApiRequestOptions) => FormData | undefined;
|
|
14
17
|
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
|
|
15
18
|
export declare const resolve: <T>(options: ApiRequestOptions, resolver?: T | Resolver<T>) => Promise<T | undefined>;
|
|
16
|
-
export declare const getHeaders: (config: OpenAPIConfig, options: ApiRequestOptions) => Promise<
|
|
19
|
+
export declare const getHeaders: (config: OpenAPIConfig, options: ApiRequestOptions, formData?: FormData) => Promise<Record<string, string>>;
|
|
17
20
|
export declare const getRequestBody: (options: ApiRequestOptions) => any;
|
|
18
|
-
export declare const sendRequest: (config: OpenAPIConfig, options: ApiRequestOptions, url: string, body: any, formData: FormData | undefined, headers:
|
|
19
|
-
export declare const getResponseHeader: (response:
|
|
20
|
-
export declare const getResponseBody: (response:
|
|
21
|
+
export declare const sendRequest: <T>(config: OpenAPIConfig, options: ApiRequestOptions, url: string, body: any, formData: FormData | undefined, headers: Record<string, string>, onCancel: OnCancel, axiosClient: AxiosInstance) => Promise<AxiosResponse<T>>;
|
|
22
|
+
export declare const getResponseHeader: (response: AxiosResponse<any>, responseHeader?: string) => string | undefined;
|
|
23
|
+
export declare const getResponseBody: (response: AxiosResponse<any>) => any;
|
|
21
24
|
export declare const catchErrorCodes: (options: ApiRequestOptions, result: ApiResult) => void;
|
|
22
25
|
/**
|
|
23
26
|
* Request method
|
|
24
27
|
* @param config The OpenAPI configuration object
|
|
25
28
|
* @param options The request options from the service
|
|
29
|
+
* @param axiosClient The axios client instance to use
|
|
26
30
|
* @returns CancelablePromise<T>
|
|
27
31
|
* @throws ApiError
|
|
28
32
|
*/
|
|
29
|
-
export declare const request: <T>(config: OpenAPIConfig, options: ApiRequestOptions) => CancelablePromise<T>;
|
|
33
|
+
export declare const request: <T>(config: OpenAPIConfig, options: ApiRequestOptions, axiosClient?: AxiosInstance) => CancelablePromise<T>;
|
|
30
34
|
export {};
|
package/core/request.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.request = exports.catchErrorCodes = exports.getResponseBody = exports.getResponseHeader = exports.sendRequest = exports.getRequestBody = exports.getHeaders = exports.resolve = exports.getFormData = exports.getQueryString = exports.base64 = exports.isFormData = exports.isBlob = exports.isStringWithValue = exports.isString = exports.isDefined = void 0;
|
|
6
|
+
exports.request = exports.catchErrorCodes = exports.getResponseBody = exports.getResponseHeader = exports.sendRequest = exports.getRequestBody = exports.getHeaders = exports.resolve = exports.getFormData = exports.getQueryString = exports.base64 = exports.isSuccess = exports.isFormData = exports.isBlob = exports.isStringWithValue = exports.isString = exports.isDefined = void 0;
|
|
4
7
|
/* generated using openapi-typescript-codegen -- do not edit */
|
|
5
8
|
/* istanbul ignore file */
|
|
6
9
|
/* tslint:disable */
|
|
7
10
|
/* eslint-disable */
|
|
11
|
+
const axios_1 = __importDefault(require("axios"));
|
|
12
|
+
const form_data_1 = __importDefault(require("form-data"));
|
|
8
13
|
const ApiError_1 = require("./ApiError");
|
|
9
14
|
const CancelablePromise_1 = require("./CancelablePromise");
|
|
10
15
|
const isDefined = (value) => {
|
|
@@ -31,9 +36,13 @@ const isBlob = (value) => {
|
|
|
31
36
|
};
|
|
32
37
|
exports.isBlob = isBlob;
|
|
33
38
|
const isFormData = (value) => {
|
|
34
|
-
return value instanceof
|
|
39
|
+
return value instanceof form_data_1.default;
|
|
35
40
|
};
|
|
36
41
|
exports.isFormData = isFormData;
|
|
42
|
+
const isSuccess = (status) => {
|
|
43
|
+
return status >= 200 && status < 300;
|
|
44
|
+
};
|
|
45
|
+
exports.isSuccess = isSuccess;
|
|
37
46
|
const base64 = (str) => {
|
|
38
47
|
try {
|
|
39
48
|
return btoa(str);
|
|
@@ -93,7 +102,7 @@ const getUrl = (config, options) => {
|
|
|
93
102
|
};
|
|
94
103
|
const getFormData = (options) => {
|
|
95
104
|
if (options.formData) {
|
|
96
|
-
const formData = new
|
|
105
|
+
const formData = new form_data_1.default();
|
|
97
106
|
const process = (key, value) => {
|
|
98
107
|
if ((0, exports.isString)(value) || (0, exports.isBlob)(value)) {
|
|
99
108
|
formData.append(key, value);
|
|
@@ -124,17 +133,19 @@ const resolve = async (options, resolver) => {
|
|
|
124
133
|
return resolver;
|
|
125
134
|
};
|
|
126
135
|
exports.resolve = resolve;
|
|
127
|
-
const getHeaders = async (config, options) => {
|
|
136
|
+
const getHeaders = async (config, options, formData) => {
|
|
128
137
|
const [token, username, password, additionalHeaders] = await Promise.all([
|
|
129
138
|
(0, exports.resolve)(options, config.TOKEN),
|
|
130
139
|
(0, exports.resolve)(options, config.USERNAME),
|
|
131
140
|
(0, exports.resolve)(options, config.PASSWORD),
|
|
132
141
|
(0, exports.resolve)(options, config.HEADERS),
|
|
133
142
|
]);
|
|
143
|
+
const formHeaders = typeof formData?.getHeaders === 'function' && formData?.getHeaders() || {};
|
|
134
144
|
const headers = Object.entries({
|
|
135
145
|
Accept: 'application/json',
|
|
136
146
|
...additionalHeaders,
|
|
137
147
|
...options.headers,
|
|
148
|
+
...formHeaders,
|
|
138
149
|
})
|
|
139
150
|
.filter(([_, value]) => (0, exports.isDefined)(value))
|
|
140
151
|
.reduce((headers, [key, value]) => ({
|
|
@@ -162,42 +173,46 @@ const getHeaders = async (config, options) => {
|
|
|
162
173
|
headers['Content-Type'] = 'application/json';
|
|
163
174
|
}
|
|
164
175
|
}
|
|
165
|
-
return
|
|
176
|
+
return headers;
|
|
166
177
|
};
|
|
167
178
|
exports.getHeaders = getHeaders;
|
|
168
179
|
const getRequestBody = (options) => {
|
|
169
|
-
if (options.body
|
|
170
|
-
|
|
171
|
-
return JSON.stringify(options.body);
|
|
172
|
-
}
|
|
173
|
-
else if ((0, exports.isString)(options.body) || (0, exports.isBlob)(options.body) || (0, exports.isFormData)(options.body)) {
|
|
174
|
-
return options.body;
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
return JSON.stringify(options.body);
|
|
178
|
-
}
|
|
180
|
+
if (options.body) {
|
|
181
|
+
return options.body;
|
|
179
182
|
}
|
|
180
183
|
return undefined;
|
|
181
184
|
};
|
|
182
185
|
exports.getRequestBody = getRequestBody;
|
|
183
|
-
const sendRequest = async (config, options, url, body, formData, headers, onCancel) => {
|
|
184
|
-
const
|
|
185
|
-
const
|
|
186
|
+
const sendRequest = async (config, options, url, body, formData, headers, onCancel, axiosClient) => {
|
|
187
|
+
const source = axios_1.default.CancelToken.source();
|
|
188
|
+
const requestConfig = {
|
|
189
|
+
url,
|
|
186
190
|
headers,
|
|
187
|
-
|
|
191
|
+
data: body ?? formData,
|
|
188
192
|
method: options.method,
|
|
189
|
-
|
|
193
|
+
withCredentials: config.WITH_CREDENTIALS,
|
|
194
|
+
withXSRFToken: config.CREDENTIALS === 'include' ? config.WITH_CREDENTIALS : false,
|
|
195
|
+
cancelToken: source.token,
|
|
190
196
|
};
|
|
191
|
-
|
|
192
|
-
|
|
197
|
+
onCancel(() => source.cancel('The user aborted a request.'));
|
|
198
|
+
try {
|
|
199
|
+
return await axiosClient.request(requestConfig);
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
const axiosError = error;
|
|
203
|
+
if (axiosError.response) {
|
|
204
|
+
if (axiosError.response.data) {
|
|
205
|
+
throw axiosError.response.data;
|
|
206
|
+
}
|
|
207
|
+
throw axiosError.response;
|
|
208
|
+
}
|
|
209
|
+
throw error;
|
|
193
210
|
}
|
|
194
|
-
onCancel(() => controller.abort());
|
|
195
|
-
return await fetch(url, request);
|
|
196
211
|
};
|
|
197
212
|
exports.sendRequest = sendRequest;
|
|
198
213
|
const getResponseHeader = (response, responseHeader) => {
|
|
199
214
|
if (responseHeader) {
|
|
200
|
-
const content = response.headers
|
|
215
|
+
const content = response.headers[responseHeader];
|
|
201
216
|
if ((0, exports.isString)(content)) {
|
|
202
217
|
return content;
|
|
203
218
|
}
|
|
@@ -205,24 +220,9 @@ const getResponseHeader = (response, responseHeader) => {
|
|
|
205
220
|
return undefined;
|
|
206
221
|
};
|
|
207
222
|
exports.getResponseHeader = getResponseHeader;
|
|
208
|
-
const getResponseBody =
|
|
223
|
+
const getResponseBody = (response) => {
|
|
209
224
|
if (response.status !== 204) {
|
|
210
|
-
|
|
211
|
-
const contentType = response.headers.get('Content-Type');
|
|
212
|
-
if (contentType) {
|
|
213
|
-
const jsonTypes = ['application/json', 'application/problem+json'];
|
|
214
|
-
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
|
|
215
|
-
if (isJSON) {
|
|
216
|
-
return await response.json();
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
return await response.text();
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
catch (error) {
|
|
224
|
-
console.error(error);
|
|
225
|
-
}
|
|
225
|
+
return response.data;
|
|
226
226
|
}
|
|
227
227
|
return undefined;
|
|
228
228
|
};
|
|
@@ -261,23 +261,24 @@ exports.catchErrorCodes = catchErrorCodes;
|
|
|
261
261
|
* Request method
|
|
262
262
|
* @param config The OpenAPI configuration object
|
|
263
263
|
* @param options The request options from the service
|
|
264
|
+
* @param axiosClient The axios client instance to use
|
|
264
265
|
* @returns CancelablePromise<T>
|
|
265
266
|
* @throws ApiError
|
|
266
267
|
*/
|
|
267
|
-
const request = (config, options) => {
|
|
268
|
+
const request = (config, options, axiosClient = axios_1.default) => {
|
|
268
269
|
return new CancelablePromise_1.CancelablePromise(async (resolve, reject, onCancel) => {
|
|
269
270
|
try {
|
|
270
271
|
const url = getUrl(config, options);
|
|
271
272
|
const formData = (0, exports.getFormData)(options);
|
|
272
273
|
const body = (0, exports.getRequestBody)(options);
|
|
273
|
-
const headers = await (0, exports.getHeaders)(config, options);
|
|
274
|
+
const headers = await (0, exports.getHeaders)(config, options, formData);
|
|
274
275
|
if (!onCancel.isCancelled) {
|
|
275
|
-
const response = await (0, exports.sendRequest)(config, options, url, body, formData, headers, onCancel);
|
|
276
|
-
const responseBody =
|
|
276
|
+
const response = await (0, exports.sendRequest)(config, options, url, body, formData, headers, onCancel, axiosClient);
|
|
277
|
+
const responseBody = (0, exports.getResponseBody)(response);
|
|
277
278
|
const responseHeader = (0, exports.getResponseHeader)(response, options.responseHeader);
|
|
278
279
|
const result = {
|
|
279
280
|
url,
|
|
280
|
-
ok: response.
|
|
281
|
+
ok: (0, exports.isSuccess)(response.status),
|
|
281
282
|
status: response.status,
|
|
282
283
|
statusText: response.statusText,
|
|
283
284
|
body: responseHeader ?? responseBody,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dapex-tech/elite-online-services",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"types": "./index.d.ts",
|
|
6
6
|
"private": false,
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"*"
|
|
12
12
|
],
|
|
13
|
-
"
|
|
14
|
-
"socket.io-client": "^4.8.1"
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"socket.io-client": "^4.8.1",
|
|
15
|
+
"axios": "^1.13.2"
|
|
15
16
|
},
|
|
16
17
|
"devDependencies": {
|
|
17
18
|
"@types/socket.io-client": "^3.0.0",
|