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