|
1 | 1 | {{>header}} |
2 | 2 |
|
3 | 3 | import { HttpClient, HttpHeaders } from '@angular/common/http'; |
4 | | -import type { HttpResponse } from '@angular/common/http'; |
5 | | -import { Observable } from 'rxjs'; |
| 4 | +import type { HttpResponse, HttpErrorResponse } from '@angular/common/http'; |
| 5 | +import { catchError, forkJoin, map, mergeMap, of, throwError } from 'rxjs'; |
| 6 | +import type { Observable } from 'rxjs'; |
6 | 7 |
|
7 | 8 | import { ApiError } from './ApiError'; |
8 | 9 | import type { ApiRequestOptions } from './ApiRequestOptions'; |
@@ -54,46 +55,55 @@ import type { OpenAPIConfig } from './OpenAPI'; |
54 | 55 | {{>angular/getResponseBody}} |
55 | 56 |
|
56 | 57 |
|
57 | | -{{>functions/catchErrors}} |
| 58 | +{{>functions/catchErrorCodes}} |
58 | 59 |
|
59 | 60 |
|
60 | 61 | /** |
61 | 62 | * Request method |
62 | 63 | * @param config The OpenAPI configuration object |
63 | 64 | * @param http The Angular HTTP client |
64 | 65 | * @param options The request options from the service |
65 | | - * @returns CancelablePromise<T> |
| 66 | + * @returns Observable<T> |
66 | 67 | * @throws ApiError |
67 | 68 | */ |
68 | 69 | export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: ApiRequestOptions): Observable<T> => { |
69 | | - return new Observable<T>(subscriber => { |
70 | | - try { |
71 | | - const url = getUrl(config, options); |
72 | | - const formData = getFormData(options); |
73 | | - const body = getRequestBody(options); |
74 | | - getHeaders(config, options).then(headers => { |
75 | | - |
76 | | - sendRequest<T>(config, options, http, url, formData, body, headers) |
77 | | - .subscribe(response => { |
78 | | - const responseBody = 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 | | - subscriber.next(result.body); |
92 | | - }); |
93 | | - }); |
94 | | - } catch (error) { |
95 | | - subscriber.error(error); |
96 | | - } |
97 | | - }); |
| 70 | + const url = getUrl(config, options); |
| 71 | + const formData = getFormData(options); |
| 72 | + const body = getRequestBody(options); |
| 73 | + |
| 74 | + return getHeaders(config, options).pipe( |
| 75 | + mergeMap( headers => { |
| 76 | + return sendRequest<T>(config, options, http, url, formData, body, headers); |
| 77 | + }), |
| 78 | + map(response => { |
| 79 | + const responseBody = getResponseBody(response); |
| 80 | + const responseHeader = getResponseHeader(response, options.responseHeader); |
| 81 | + return { |
| 82 | + url, |
| 83 | + ok: response.ok, |
| 84 | + status: response.status, |
| 85 | + statusText: response.statusText, |
| 86 | + body: responseHeader ?? responseBody, |
| 87 | + } as ApiResult; |
| 88 | + }), |
| 89 | + catchError((error: HttpErrorResponse) => { |
| 90 | + if (!error.status) { |
| 91 | + return throwError(() => error); |
| 92 | + } |
| 93 | + return of({ |
| 94 | + url, |
| 95 | + ok: error.ok, |
| 96 | + status: error.status, |
| 97 | + statusText: error.statusText, |
| 98 | + body: error.statusText, |
| 99 | + } as ApiResult); |
| 100 | + }), |
| 101 | + map(result => { |
| 102 | + catchErrorCodes(options, result); |
| 103 | + return result.body as T; |
| 104 | + }), |
| 105 | + catchError((error: ApiError) => { |
| 106 | + return throwError(() => error); |
| 107 | + }), |
| 108 | + ); |
98 | 109 | }; |
99 | | - |
|
0 commit comments