1
1
import type { ReadStream } from 'node:fs' ;
2
2
import type { Operation } from 'oas/operation' ;
3
- import type { ParameterObject , SchemaObject } from 'oas/types' ;
3
+ import type { DataForHAR , ParameterObject , SchemaObject } from 'oas/types' ;
4
4
5
5
import stream from 'node:stream' ;
6
6
@@ -13,6 +13,8 @@ import removeUndefinedObjects from 'remove-undefined-objects';
13
13
14
14
import getJSONSchemaDefaults from './getJSONSchemaDefaults.js' ;
15
15
16
+ type DataForHARWithFiles = DataForHAR & { files ?: Record < string , Buffer > } ;
17
+
16
18
// These headers are normally only defined by the OpenAPI definition but we allow the user to
17
19
// manually supply them in their `metadata` parameter if they wish.
18
20
const specialHeaders = [ 'accept' , 'authorization' ] ;
@@ -56,15 +58,15 @@ function isPrimitive(obj: unknown) {
56
58
return obj === null || typeof obj === 'number' || typeof obj === 'string' ;
57
59
}
58
60
59
- function merge ( src : unknown , target : unknown ) {
61
+ function merge < R = unknown > ( src : R , target : R ) : R {
60
62
if ( Array . isArray ( target ) ) {
61
63
// @todo we need to add support for merging array defaults with array body/metadata arguments
62
- return target ;
64
+ return target as R ;
63
65
} else if ( ! isObject ( target ) ) {
64
- return target ;
66
+ return target as R ;
65
67
}
66
68
67
- return lodashMerge ( src , target ) ;
69
+ return lodashMerge < R , R > ( src , target ) ;
68
70
}
69
71
70
72
/**
@@ -144,7 +146,11 @@ async function processFile(
144
146
* with `@readme/oas-to-har`.
145
147
*
146
148
*/
147
- export default async function prepareParams ( operation : Operation , body ?: unknown , metadata ?: Record < string , unknown > ) {
149
+ export default async function prepareParams (
150
+ operation : Operation ,
151
+ body ?: unknown ,
152
+ metadata ?: Record < string , unknown > ,
153
+ ) : Promise < DataForHARWithFiles > {
148
154
let metadataIntersected = false ;
149
155
const digestedParameters = digestParameters ( operation . getParameters ( ) ) ;
150
156
const jsonSchema = operation . getParametersAsJSONSchema ( ) ;
@@ -189,22 +195,7 @@ export default async function prepareParams(operation: Operation, body?: unknown
189
195
}
190
196
191
197
const jsonSchemaDefaults = jsonSchema ? getJSONSchemaDefaults ( jsonSchema ) : { } ;
192
-
193
- const params : {
194
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
195
- body ?: any ;
196
- cookie ?: Record < string , boolean | number | string > ;
197
- files ?: Record < string , Buffer > ;
198
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
199
- formData ?: any ;
200
- header ?: Record < string , boolean | number | string > ;
201
- path ?: Record < string , boolean | number | string > ;
202
- query ?: Record < string , boolean | number | string > ;
203
- server ?: {
204
- selected : number ;
205
- variables : Record < string , number | string > ;
206
- } ;
207
- } = jsonSchemaDefaults ;
198
+ const params : DataForHARWithFiles = jsonSchemaDefaults ;
208
199
209
200
// If a body argument was supplied we need to do a bit of work to see if it's actually a body
210
201
// argument or metadata because the library lets you supply either a body, metadata, or body with
@@ -321,7 +312,7 @@ export default async function prepareParams(operation: Operation, body?: unknown
321
312
// Form data should be placed within `formData` instead of `body` for it to properly get picked
322
313
// up by `fetch-har`.
323
314
if ( operation . isFormUrlEncoded ( ) ) {
324
- params . formData = merge ( params . formData , params . body ) ;
315
+ params . formData = merge < DataForHARWithFiles [ 'formData' ] > ( params . formData , params . body ) ;
325
316
delete params . body ;
326
317
}
327
318
@@ -380,7 +371,7 @@ export default async function prepareParams(operation: Operation, body?: unknown
380
371
// out anything that they sent that is a parameter from also being sent as part of a form
381
372
// data payload for `x-www-form-urlencoded` requests.
382
373
if ( metadataIntersected && operation . isFormUrlEncoded ( ) ) {
383
- if ( paramName in params . formData ) {
374
+ if ( params . formData && paramName in params . formData ) {
384
375
delete params . formData [ paramName ] ;
385
376
}
386
377
}
@@ -412,7 +403,7 @@ export default async function prepareParams(operation: Operation, body?: unknown
412
403
}
413
404
414
405
if ( operation . isFormUrlEncoded ( ) ) {
415
- params . formData = merge ( params . formData , metadata ) ;
406
+ params . formData = merge < DataForHARWithFiles [ 'formData' ] > ( params . formData , metadata ) ;
416
407
} else {
417
408
// Any other remaining unused metadata will be unused because we don't know where to place
418
409
// it in the request.
0 commit comments