Skip to content

Commit 03cee6d

Browse files
committed
maintain codebase with react as default client
1 parent 4915c16 commit 03cee6d

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/api/core/request.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
/* istanbul ignore file */
22
/* tslint:disable */
33
/* eslint-disable */
4-
import FormData from 'form-data';
5-
import fetch, { Headers } from 'node-fetch';
6-
import type { RequestInit, Response } from 'node-fetch';
7-
import type { AbortSignal } from 'node-fetch/externals';
8-
94
import { ApiError } from './ApiError';
105
import type { ApiRequestOptions } from './ApiRequestOptions';
116
import type { ApiResult } from './ApiResult';
@@ -42,6 +37,10 @@ const isFormData = (value: any): value is FormData => {
4237
return value instanceof FormData;
4338
};
4439

40+
const isSuccess = (status: number): boolean => {
41+
return status >= 200 && status < 300;
42+
};
43+
4544
const base64 = (str: string): string => {
4645
try {
4746
return btoa(str);
@@ -170,7 +169,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
170169
if (options.mediaType) {
171170
headers['Content-Type'] = options.mediaType;
172171
} else if (isBlob(options.body)) {
173-
headers['Content-Type'] = 'application/octet-stream';
172+
headers['Content-Type'] = options.body.type || 'application/octet-stream';
174173
} else if (isString(options.body)) {
175174
headers['Content-Type'] = 'text/plain';
176175
} else if (!isFormData(options.body)) {
@@ -186,7 +185,7 @@ const getRequestBody = (options: ApiRequestOptions): any => {
186185
if (options.mediaType?.includes('/json')) {
187186
return JSON.stringify(options.body)
188187
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
189-
return options.body as any;
188+
return options.body;
190189
} else {
191190
return JSON.stringify(options.body);
192191
}
@@ -195,47 +194,52 @@ const getRequestBody = (options: ApiRequestOptions): any => {
195194
};
196195

197196
export const sendRequest = async (
197+
config: OpenAPIConfig,
198198
options: ApiRequestOptions,
199199
url: string,
200200
body: any,
201201
formData: FormData | undefined,
202202
headers: Headers,
203203
onCancel: OnCancel
204-
): Promise<Response> => {
205-
const controller = new AbortController();
206-
207-
const request: RequestInit = {
208-
headers,
209-
method: options.method,
210-
body: body ?? formData,
211-
signal: controller.signal as AbortSignal,
212-
};
204+
): Promise<XMLHttpRequest> => {
205+
const xhr = new XMLHttpRequest();
206+
xhr.open(options.method, url, true);
207+
xhr.withCredentials = config.WITH_CREDENTIALS;
213208

214-
onCancel(() => controller.abort());
209+
headers.forEach((value, key) => {
210+
xhr.setRequestHeader(key, value);
211+
});
212+
213+
return new Promise<XMLHttpRequest>((resolve, reject) => {
214+
xhr.onload = () => resolve(xhr);
215+
xhr.onabort = () => reject(new Error('Request aborted'));
216+
xhr.onerror = () => reject(new Error('Network error'));
217+
xhr.send(body ?? formData);
215218

216-
return await fetch(url, request);
219+
onCancel(() => xhr.abort());
220+
});
217221
};
218222

219-
const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
223+
const getResponseHeader = (xhr: XMLHttpRequest, responseHeader?: string): string | undefined => {
220224
if (responseHeader) {
221-
const content = response.headers.get(responseHeader);
225+
const content = xhr.getResponseHeader(responseHeader);
222226
if (isString(content)) {
223227
return content;
224228
}
225229
}
226230
return undefined;
227231
};
228232

229-
const getResponseBody = async (response: Response): Promise<any> => {
230-
if (response.status !== 204) {
233+
const getResponseBody = (xhr: XMLHttpRequest): any => {
234+
if (xhr.status !== 204) {
231235
try {
232-
const contentType = response.headers.get('Content-Type');
236+
const contentType = xhr.getResponseHeader('Content-Type');
233237
if (contentType) {
234238
const isJSON = contentType.toLowerCase().startsWith('application/json');
235239
if (isJSON) {
236-
return await response.json();
240+
return JSON.parse(xhr.responseText);
237241
} else {
238-
return await response.text();
242+
return xhr.responseText;
239243
}
240244
}
241245
} catch (error) {
@@ -283,13 +287,13 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
283287
const headers = await getHeaders(config, options);
284288

285289
if (!onCancel.isCancelled) {
286-
const response = await sendRequest(options, url, body, formData, headers, onCancel);
287-
const responseBody = await getResponseBody(response);
290+
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
291+
const responseBody = getResponseBody(response);
288292
const responseHeader = getResponseHeader(response, options.responseHeader);
289293

290294
const result: ApiResult = {
291295
url,
292-
ok: response.ok,
296+
ok: isSuccess(response.status),
293297
status: response.status,
294298
statusText: response.statusText,
295299
body: responseHeader ?? responseBody,

0 commit comments

Comments
 (0)