|
| 1 | +{{>header}} |
| 2 | + |
| 3 | +import { ApiError } from './ApiError'; |
| 4 | +import type { ApiRequestOptions } from './ApiRequestOptions'; |
| 5 | +import type { ApiResult } from './ApiResult'; |
| 6 | +import { CancelablePromise } from './CancelablePromise'; |
| 7 | +import type { OnCancel } from './CancelablePromise'; |
| 8 | +import type { OpenAPIConfig } from './OpenAPI'; |
| 9 | + |
| 10 | +{{>functions/isDefined}} |
| 11 | + |
| 12 | + |
| 13 | +{{>functions/isString}} |
| 14 | + |
| 15 | + |
| 16 | +{{>functions/isStringWithValue}} |
| 17 | + |
| 18 | + |
| 19 | +{{>functions/isBlob}} |
| 20 | + |
| 21 | + |
| 22 | +{{>functions/isFormData}} |
| 23 | + |
| 24 | + |
| 25 | +{{>functions/base64}} |
| 26 | + |
| 27 | + |
| 28 | +{{>functions/getQueryString}} |
| 29 | + |
| 30 | + |
| 31 | +{{>functions/getUrl}} |
| 32 | + |
| 33 | + |
| 34 | +{{>functions/getFormData}} |
| 35 | + |
| 36 | + |
| 37 | +{{>functions/resolve}} |
| 38 | + |
| 39 | + |
| 40 | +{{>fetch/getHeaders}} |
| 41 | + |
| 42 | + |
| 43 | +{{>fetch/getRequestBody}} |
| 44 | + |
| 45 | + |
| 46 | +{{>fetch/sendRequest}} |
| 47 | + |
| 48 | + |
| 49 | +{{>fetch/getResponseHeader}} |
| 50 | + |
| 51 | + |
| 52 | +{{>fetch/getResponseBody}} |
| 53 | + |
| 54 | + |
| 55 | +{{>functions/catchErrorCodes}} |
| 56 | + |
| 57 | + |
| 58 | +/** |
| 59 | + * Request method |
| 60 | + * @param config The OpenAPI configuration object |
| 61 | + * @param options The request options from the service |
| 62 | + * @returns CancelablePromise<T> |
| 63 | + * @throws ApiError |
| 64 | + */ |
| 65 | +export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => { |
| 66 | +return new CancelablePromise(async (resolve, reject, onCancel) => { |
| 67 | +try { |
| 68 | +const url = getUrl(config, options); |
| 69 | +const formData = getFormData(options); |
| 70 | +const body = getRequestBody(options); |
| 71 | +const headers = await getHeaders(config, options); |
| 72 | + |
| 73 | +if (!onCancel.isCancelled) { |
| 74 | +const response = await sendRequest(config, options, url, body, formData, headers, onCancel); |
| 75 | +const responseBody = await getResponseBody(response); |
| 76 | +const responseHeader = getResponseHeader(response, options.responseHeader); |
| 77 | + |
| 78 | +const result: ApiResult = { |
| 79 | +url, |
| 80 | +ok: response.ok, |
| 81 | +status: response.status, |
| 82 | +statusText: response.statusText, |
| 83 | +body: responseHeader ?? responseBody, |
| 84 | +}; |
| 85 | + |
| 86 | +catchErrorCodes(options, result); |
| 87 | + |
| 88 | +resolve(result.body); |
| 89 | +} |
| 90 | +} catch (error) { |
| 91 | +reject(error); |
| 92 | +} |
| 93 | +}); |
| 94 | +}; |
0 commit comments