diff --git a/docs/src/pages/reference/configuration/output.md b/docs/src/pages/reference/configuration/output.md index 621fe9dbf..bb93e0be0 100644 --- a/docs/src/pages/reference/configuration/output.md +++ b/docs/src/pages/reference/configuration/output.md @@ -1720,3 +1720,22 @@ module.exports = { }, }; ``` + +### optionsParamRequired + +Type: `Boolean` + +Valid Values: `true` or `false`. + +Default Value: `false`. +Use this property to make the second `options` parameter required (such as when using a custom axios instance) + +```js +module.exports = { + petstore: { + output: { + optionsParamRequired: true, + }, + }, +}; +``` diff --git a/packages/axios/src/index.ts b/packages/axios/src/index.ts index 3bb78c4ce..9f8390e46 100644 --- a/packages/axios/src/index.ts +++ b/packages/axios/src/index.ts @@ -140,7 +140,7 @@ const generateAxiosImplementation = ( return `const ${operationName} = (\n ${propsImplementation}\n ${ isRequestOptions && mutator.hasSecondArg - ? `options?: SecondParameter,` + ? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter,` : '' }) => {${bodyForm} return ${mutator.name}<${response.definition.success || 'unknown'}>( diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 676405031..e0ed88125 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -60,6 +60,7 @@ export type NormalizedOutputOptions = { allParamsOptional: boolean; urlEncodeParameters: boolean; unionAddMissingProperties: boolean; + optionsParamRequired: boolean; }; export type NormalizedParamsSerializerOptions = { @@ -182,6 +183,7 @@ export type OutputOptions = { allParamsOptional?: boolean; urlEncodeParameters?: boolean; unionAddMissingProperties?: boolean; + optionsParamRequired?: boolean; }; export type SwaggerParserOptions = Omit & { diff --git a/packages/orval/src/utils/options.ts b/packages/orval/src/utils/options.ts index fd0b4a253..cc90c726f 100644 --- a/packages/orval/src/utils/options.ts +++ b/packages/orval/src/utils/options.ts @@ -253,6 +253,7 @@ export const normalizeOptions = async ( }, allParamsOptional: outputOptions.allParamsOptional ?? false, urlEncodeParameters: outputOptions.urlEncodeParameters ?? false, + optionsParamRequired: outputOptions.optionsParamRequired ?? false, }, hooks: options.hooks ? normalizeHooks(options.hooks) : {}, }; diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 221c82120..df03f973e 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -453,7 +453,7 @@ const generateQueryRequestFunction = ( return (\n ${propsImplementation}\n ${ isRequestOptions && mutator.hasSecondArg - ? `options?: SecondParameter>,` + ? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter>,` : '' }${ !isBodyVerb && hasSignal ? 'signal?: AbortSignal\n' : '' @@ -468,7 +468,7 @@ const generateQueryRequestFunction = ( return `${override.query.shouldExportHttpClient ? 'export ' : ''}const ${operationName} = (\n ${propsImplementation}\n ${ isRequestOptions && mutator.hasSecondArg - ? `options?: SecondParameter,` + ? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter,` : '' }${!isBodyVerb && hasSignal ? 'signal?: AbortSignal\n' : ''}) => { ${isVue(outputClient) ? vueUnRefParams(props) : ''} diff --git a/packages/swr/src/index.ts b/packages/swr/src/index.ts index 81b21c82e..a6772230d 100644 --- a/packages/swr/src/index.ts +++ b/packages/swr/src/index.ts @@ -171,7 +171,7 @@ const generateSwrRequestFunction = ( return `export const ${operationName} = (\n ${propsImplementation}\n ${ isRequestOptions && mutator.hasSecondArg - ? `options?: SecondParameter` + ? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter` : '' }) => {${bodyForm} return ${mutator.name}<${response.definition.success || 'unknown'}>(