1
- import { getJSDoc , getKindValue , getSafePropertyName , getTypeName , getURL , HTTPMethod } from '../../common/utils' ;
1
+ import {
2
+ getJSDoc ,
3
+ getKindValue ,
4
+ getSafePropertyName ,
5
+ getTypeName ,
6
+ getURL ,
7
+ HTTPMethod ,
8
+ SUCCESSFUL_CODES ,
9
+ XHRResponseType ,
10
+ } from '../../common/utils' ;
2
11
import {
3
12
getSerializedPropertyType ,
4
13
getSerializedObjectType ,
@@ -34,7 +43,7 @@ import { ResolveRefContext, fromString, getRelativePath, Ref } from '../../../..
34
43
import { OperationObject } from '../../../../schema/3.0/operation-object' ;
35
44
import { ParameterObject , ParameterObjectCodec } from '../../../../schema/3.0/parameter-object' ;
36
45
import { RequestBodyObjectCodec } from '../../../../schema/3.0/request-body-object' ;
37
- import { isSome , none , Option , some } from 'fp-ts/lib/Option' ;
46
+ import { chain , isSome , none , Option , some , map , fromEither , fold } from 'fp-ts/lib/Option' ;
38
47
import { constFalse } from 'fp-ts/lib/function' ;
39
48
import { clientRef } from '../../common/bundled/client' ;
40
49
import { Kind } from '../../../../utils/types' ;
@@ -49,6 +58,8 @@ import {
49
58
SerializedFragment ,
50
59
} from '../../common/data/serialized-fragment' ;
51
60
import { SchemaObjectCodec } from '../../../../schema/3.0/schema-object' ;
61
+ import { lookup , keys } from 'fp-ts/lib/Record' ;
62
+ import { ResponseObjectCodec } from '../../../../schema/3.0/response-object' ;
52
63
import {
53
64
fromSerializedHeaderParameter ,
54
65
getSerializedHeaderParameterType ,
@@ -256,8 +267,9 @@ export const getParameters = combineReader(
256
267
) ;
257
268
258
269
export const serializeOperationObject = combineReader (
270
+ ask < ResolveRefContext > ( ) ,
259
271
getParameters ,
260
- getParameters => (
272
+ ( e , getParameters ) => (
261
273
pattern : string ,
262
274
method : HTTPMethod ,
263
275
from : Ref ,
@@ -274,6 +286,29 @@ export const serializeOperationObject = combineReader(
274
286
) ;
275
287
276
288
const serializedResponses = serializeResponsesObject ( from ) ( operation . responses ) ;
289
+ const responseType : XHRResponseType = pipe (
290
+ SUCCESSFUL_CODES ,
291
+ array . findFirstMap ( code => lookup ( code , operation . responses ) ) ,
292
+ chain ( response =>
293
+ ReferenceObjectCodec . is ( response )
294
+ ? fromEither ( e . resolveRef ( response . $ref , ResponseObjectCodec ) )
295
+ : some ( response ) ,
296
+ ) ,
297
+ chain ( response => response . content ) ,
298
+ map ( keys ) ,
299
+ fold (
300
+ ( ) => 'json' ,
301
+ types => {
302
+ if ( types . includes ( 'application/octet-stream' ) ) {
303
+ return 'blob' ;
304
+ }
305
+ if ( types . includes ( 'text/plain' ) ) {
306
+ return 'text' ;
307
+ }
308
+ return 'json' ;
309
+ } ,
310
+ ) ,
311
+ ) ;
277
312
278
313
return combineEither (
279
314
parameters ,
@@ -346,6 +381,7 @@ export const serializeOperationObject = combineReader(
346
381
e.httpClient.request({
347
382
url: ${ getURL ( pattern , parameters . serializedPathParameters ) } ,
348
383
method: '${ method } ',
384
+ responseType: '${ responseType } ',
349
385
${ when ( hasQueryParameters , 'query,' ) }
350
386
${ when ( hasBodyParameter , 'body,' ) }
351
387
${ when ( hasHeaderParameters , 'headers' ) }
0 commit comments