@@ -6,16 +6,17 @@ import {
6
6
} from '@angular/common/http' ;
7
7
import { Injectable } from '@angular/core' ;
8
8
import {
9
- ClientMethod ,
9
+ ClientObservableMethod ,
10
10
ClientOptions ,
11
+ ClientPromiseMethod ,
11
12
FetchOptions ,
12
13
} from './openapi-client.types' ;
13
14
import {
14
15
createFinalURL ,
15
16
createQuerySerializer ,
16
17
mergeHeaders ,
17
18
} from './openapi-serializer' ;
18
- import { lastValueFrom } from 'rxjs' ;
19
+ import { filter , lastValueFrom , map } from 'rxjs' ;
19
20
20
21
export const DEFAULT_HEADERS = new HttpHeaders ( {
21
22
'Content-Type' : 'application/json' ,
@@ -26,13 +27,28 @@ export const DEFAULT_HEADERS = new HttpHeaders({
26
27
} )
27
28
export abstract class OpenAPIClientService < Paths extends { } > {
28
29
get ;
30
+ getPromise ;
31
+
29
32
put ;
33
+ putPromise ;
34
+
30
35
post ;
36
+ postPromise ;
37
+
31
38
delete ;
39
+ deletePromise ;
40
+
32
41
options ;
42
+ optionsPromise ;
43
+
33
44
head ;
45
+ headPromise ;
46
+
34
47
patch ;
48
+ patchPromise ;
49
+
35
50
trace ;
51
+ tracePromise ;
36
52
37
53
constructor ( http : HttpClient , clientOptions : ClientOptions ) {
38
54
let {
@@ -48,7 +64,68 @@ export abstract class OpenAPIClientService<Paths extends {}> {
48
64
}
49
65
baseHeaders = mergeHeaders ( DEFAULT_HEADERS , baseHeaders ) ;
50
66
51
- const coreFetch = async ( url : any , fetchOptions : FetchOptions < any > ) => {
67
+ const coreObservableFetch = ( url : any , fetchOptions : FetchOptions < any > ) => {
68
+ let {
69
+ headers,
70
+ params = { } ,
71
+ querySerializer : requestQuerySerializer ,
72
+ ...init
73
+ } = fetchOptions || { } ;
74
+
75
+ let querySerializer =
76
+ typeof globalQuerySerializer === 'function'
77
+ ? globalQuerySerializer
78
+ : createQuerySerializer ( globalQuerySerializer ) ;
79
+ if ( requestQuerySerializer ) {
80
+ querySerializer =
81
+ typeof requestQuerySerializer === 'function'
82
+ ? requestQuerySerializer
83
+ : createQuerySerializer ( {
84
+ ...( typeof globalQuerySerializer === 'object'
85
+ ? globalQuerySerializer
86
+ : { } ) ,
87
+ ...requestQuerySerializer ,
88
+ } ) ;
89
+ }
90
+
91
+ const requestInit = {
92
+ ...baseOptions ,
93
+ ...init ,
94
+ headers : mergeHeaders ( baseHeaders , headers , params . headers ) ,
95
+ } ;
96
+
97
+ const request = new HttpRequest (
98
+ requestInit . method as any ,
99
+ createFinalURL ( url , { baseUrl, params, querySerializer } ) ,
100
+ requestInit . body ?? null ,
101
+ requestInit ,
102
+ ) ;
103
+
104
+ return http . request ( request ) . pipe (
105
+ filter ( ( response ) => response instanceof HttpResponse ) ,
106
+ map ( ( response ) => {
107
+ if ( ! ( response instanceof HttpResponse ) ) {
108
+ throw new Error (
109
+ 'Invalid response! This should not never happen, please report this issue.' ,
110
+ ) ;
111
+ }
112
+ return response . ok
113
+ ? {
114
+ data : response . body ,
115
+ response,
116
+ }
117
+ : {
118
+ error : response . body ,
119
+ response,
120
+ } ;
121
+ } ) ,
122
+ ) ;
123
+ } ;
124
+
125
+ const corePromiseFetch = async (
126
+ url : any ,
127
+ fetchOptions : FetchOptions < any > ,
128
+ ) => {
52
129
let {
53
130
headers,
54
131
params = { } ,
@@ -100,36 +177,60 @@ export abstract class OpenAPIClientService<Paths extends {}> {
100
177
} ;
101
178
} ;
102
179
103
- this . get = ( async ( url , init ) => {
104
- return coreFetch ( url , { ...init , method : 'GET' } ) ;
105
- } ) as ClientMethod < Paths , 'get' > ;
106
-
107
- this . put = ( async ( url , init ) => {
108
- return coreFetch ( url , { ...init , method : 'PUT' } ) ;
109
- } ) as ClientMethod < Paths , 'put' > ;
110
-
111
- this . post = ( async ( url , init ) => {
112
- return coreFetch ( url , { ...init , method : 'POST' } ) ;
113
- } ) as ClientMethod < Paths , 'post' > ;
114
-
115
- this . delete = ( async ( url , init ) => {
116
- return coreFetch ( url , { ...init , method : 'DELETE' } ) ;
117
- } ) as ClientMethod < Paths , 'delete' > ;
118
-
119
- this . options = ( async ( url , init ) => {
120
- return coreFetch ( url , { ...init , method : 'OPTIONS' } ) ;
121
- } ) as ClientMethod < Paths , 'options' > ;
122
-
123
- this . head = ( async ( url , init ) => {
124
- return coreFetch ( url , { ...init , method : 'HEAD' } ) ;
125
- } ) as ClientMethod < Paths , 'head' > ;
126
-
127
- this . patch = ( async ( url , init ) => {
128
- return coreFetch ( url , { ...init , method : 'PATCH' } ) ;
129
- } ) as ClientMethod < Paths , 'patch' > ;
130
-
131
- this . trace = ( async ( url , init ) => {
132
- return coreFetch ( url , { ...init , method : 'TRACE' } ) ;
133
- } ) as ClientMethod < Paths , 'trace' > ;
180
+ this . get = ( ( url , init ) => {
181
+ return coreObservableFetch ( url , { ...init , method : 'GET' } ) ;
182
+ } ) as ClientObservableMethod < Paths , 'get' > ;
183
+ this . getPromise = ( async ( url , init ) => {
184
+ return corePromiseFetch ( url , { ...init , method : 'GET' } ) ;
185
+ } ) as ClientPromiseMethod < Paths , 'get' > ;
186
+
187
+ this . put = ( ( url , init ) => {
188
+ return coreObservableFetch ( url , { ...init , method : 'PUT' } ) ;
189
+ } ) as ClientObservableMethod < Paths , 'get' > ;
190
+ this . putPromise = ( async ( url , init ) => {
191
+ return corePromiseFetch ( url , { ...init , method : 'PUT' } ) ;
192
+ } ) as ClientPromiseMethod < Paths , 'put' > ;
193
+
194
+ this . post = ( ( url , init ) => {
195
+ return coreObservableFetch ( url , { ...init , method : 'POST' } ) ;
196
+ } ) as ClientObservableMethod < Paths , 'post' > ;
197
+ this . postPromise = ( async ( url , init ) => {
198
+ return corePromiseFetch ( url , { ...init , method : 'POST' } ) ;
199
+ } ) as ClientPromiseMethod < Paths , 'post' > ;
200
+
201
+ this . delete = ( ( url , init ) => {
202
+ return coreObservableFetch ( url , { ...init , method : 'DELETE' } ) ;
203
+ } ) as ClientObservableMethod < Paths , 'delete' > ;
204
+ this . deletePromise = ( async ( url , init ) => {
205
+ return corePromiseFetch ( url , { ...init , method : 'DELETE' } ) ;
206
+ } ) as ClientPromiseMethod < Paths , 'delete' > ;
207
+
208
+ this . options = ( ( url , init ) => {
209
+ return coreObservableFetch ( url , { ...init , method : 'OPTIONS' } ) ;
210
+ } ) as ClientObservableMethod < Paths , 'options' > ;
211
+ this . optionsPromise = ( async ( url , init ) => {
212
+ return corePromiseFetch ( url , { ...init , method : 'OPTIONS' } ) ;
213
+ } ) as ClientPromiseMethod < Paths , 'options' > ;
214
+
215
+ this . head = ( ( url , init ) => {
216
+ return coreObservableFetch ( url , { ...init , method : 'HEAD' } ) ;
217
+ } ) as ClientObservableMethod < Paths , 'head' > ;
218
+ this . headPromise = ( async ( url , init ) => {
219
+ return corePromiseFetch ( url , { ...init , method : 'HEAD' } ) ;
220
+ } ) as ClientPromiseMethod < Paths , 'head' > ;
221
+
222
+ this . patch = ( ( url , init ) => {
223
+ return coreObservableFetch ( url , { ...init , method : 'PATCH' } ) ;
224
+ } ) as ClientObservableMethod < Paths , 'patch' > ;
225
+ this . patchPromise = ( async ( url , init ) => {
226
+ return corePromiseFetch ( url , { ...init , method : 'PATCH' } ) ;
227
+ } ) as ClientPromiseMethod < Paths , 'patch' > ;
228
+
229
+ this . trace = ( ( url , init ) => {
230
+ return coreObservableFetch ( url , { ...init , method : 'TRACE' } ) ;
231
+ } ) as ClientObservableMethod < Paths , 'trace' > ;
232
+ this . tracePromise = ( async ( url , init ) => {
233
+ return corePromiseFetch ( url , { ...init , method : 'TRACE' } ) ;
234
+ } ) as ClientPromiseMethod < Paths , 'trace' > ;
134
235
}
135
236
}
0 commit comments