|
1 | 1 | {{>header}} |
2 | 2 |
|
3 | | -import { HttpClient, HttpHeaders } from '@angular/common/http'; |
| 3 | +import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http'; |
4 | 4 | import { Observable } from 'rxjs'; |
5 | 5 |
|
6 | 6 | import { ApiError } from './ApiError'; |
7 | 7 | import type { ApiRequestOptions } from './ApiRequestOptions'; |
8 | 8 | import type { ApiResult } from './ApiResult'; |
9 | | -import { CancelablePromise } from './CancelablePromise'; |
10 | | -import type { OnCancel } from './CancelablePromise'; |
11 | 9 | import type { OpenAPIConfig } from './OpenAPI'; |
12 | 10 |
|
13 | 11 | {{>functions/isDefined}} |
@@ -61,37 +59,40 @@ import type { OpenAPIConfig } from './OpenAPI'; |
61 | 59 | /** |
62 | 60 | * Request method |
63 | 61 | * @param config The OpenAPI configuration object |
| 62 | + * @param http The Angular HTTP client |
64 | 63 | * @param options The request options from the service |
65 | 64 | * @returns CancelablePromise<T> |
66 | 65 | * @throws ApiError |
67 | 66 | */ |
68 | | -export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => { |
69 | | - return new CancelablePromise(async (resolve, reject, onCancel) => { |
| 67 | +export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: ApiRequestOptions): Observable<T> => { |
| 68 | + return new Observable<T>(subscriber => { |
70 | 69 | try { |
71 | 70 | const url = getUrl(config, options); |
72 | 71 | const formData = getFormData(options); |
73 | 72 | const body = getRequestBody(options); |
74 | | - const headers = await getHeaders(config, options); |
75 | | - |
76 | | - if (!onCancel.isCancelled) { |
77 | | - const response = await sendRequest(config, 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 | | - } |
| 73 | + getHeaders(config, options).then(headers => { |
| 74 | + |
| 75 | + sendRequest<T>(config, options, http, url, formData, body, headers) |
| 76 | + .subscribe(response => { |
| 77 | + const responseBody = getResponseBody(response); |
| 78 | + const responseHeader = getResponseHeader(response, options.responseHeader); |
| 79 | + |
| 80 | + const result: ApiResult = { |
| 81 | + url, |
| 82 | + ok: response.ok, |
| 83 | + status: response.status, |
| 84 | + statusText: response.statusText, |
| 85 | + body: responseHeader ?? responseBody, |
| 86 | + }; |
| 87 | + |
| 88 | + catchErrors(options, result); |
| 89 | + |
| 90 | + subscriber.next(result.body); |
| 91 | + }); |
| 92 | + }); |
93 | 93 | } catch (error) { |
94 | | - reject(error); |
| 94 | + subscriber.error(error); |
95 | 95 | } |
96 | 96 | }); |
97 | 97 | }; |
| 98 | + |
0 commit comments