11import type { ReadStream } from 'node:fs' ;
22import type { Operation } from 'oas/operation' ;
3- import type { ParameterObject , SchemaObject } from 'oas/types' ;
3+ import type { DataForHAR , ParameterObject , SchemaObject } from 'oas/types' ;
44
55import stream from 'node:stream' ;
66
@@ -13,6 +13,8 @@ import removeUndefinedObjects from 'remove-undefined-objects';
1313
1414import getJSONSchemaDefaults from './getJSONSchemaDefaults.js' ;
1515
16+ type DataForHARWithFiles = DataForHAR & { files ?: Record < string , Buffer > } ;
17+
1618// These headers are normally only defined by the OpenAPI definition but we allow the user to
1719// manually supply them in their `metadata` parameter if they wish.
1820const specialHeaders = [ 'accept' , 'authorization' ] ;
@@ -56,15 +58,15 @@ function isPrimitive(obj: unknown) {
5658 return obj === null || typeof obj === 'number' || typeof obj === 'string' ;
5759}
5860
59- function merge ( src : unknown , target : unknown ) {
61+ function merge < R = unknown > ( src : R , target : R ) : R {
6062 if ( Array . isArray ( target ) ) {
6163 // @todo we need to add support for merging array defaults with array body/metadata arguments
62- return target ;
64+ return target as R ;
6365 } else if ( ! isObject ( target ) ) {
64- return target ;
66+ return target as R ;
6567 }
6668
67- return lodashMerge ( src , target ) ;
69+ return lodashMerge < R , R > ( src , target ) ;
6870}
6971
7072/**
@@ -144,7 +146,11 @@ async function processFile(
144146 * with `@readme/oas-to-har`.
145147 *
146148 */
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 > {
148154 let metadataIntersected = false ;
149155 const digestedParameters = digestParameters ( operation . getParameters ( ) ) ;
150156 const jsonSchema = operation . getParametersAsJSONSchema ( ) ;
@@ -189,22 +195,7 @@ export default async function prepareParams(operation: Operation, body?: unknown
189195 }
190196
191197 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 ;
208199
209200 // If a body argument was supplied we need to do a bit of work to see if it's actually a body
210201 // 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
321312 // Form data should be placed within `formData` instead of `body` for it to properly get picked
322313 // up by `fetch-har`.
323314 if ( operation . isFormUrlEncoded ( ) ) {
324- params . formData = merge ( params . formData , params . body ) ;
315+ params . formData = merge < DataForHARWithFiles [ 'formData' ] > ( params . formData , params . body ) ;
325316 delete params . body ;
326317 }
327318
@@ -380,7 +371,7 @@ export default async function prepareParams(operation: Operation, body?: unknown
380371 // out anything that they sent that is a parameter from also being sent as part of a form
381372 // data payload for `x-www-form-urlencoded` requests.
382373 if ( metadataIntersected && operation . isFormUrlEncoded ( ) ) {
383- if ( paramName in params . formData ) {
374+ if ( params . formData && paramName in params . formData ) {
384375 delete params . formData [ paramName ] ;
385376 }
386377 }
@@ -412,7 +403,7 @@ export default async function prepareParams(operation: Operation, body?: unknown
412403 }
413404
414405 if ( operation . isFormUrlEncoded ( ) ) {
415- params . formData = merge ( params . formData , metadata ) ;
406+ params . formData = merge < DataForHARWithFiles [ 'formData' ] > ( params . formData , metadata ) ;
416407 } else {
417408 // Any other remaining unused metadata will be unused because we don't know where to place
418409 // it in the request.
0 commit comments