Skip to content

Commit

Permalink
fix: allow making second axios, etc., param required instead of `op…
Browse files Browse the repository at this point in the history
…tional` (#1357)

* initial commit

* prettier fixes

* Update docs/src/pages/reference/configuration/output.md

Co-authored-by: Shodai Suzuki <info@soartec-lab.work>

* Update package.json

* Update yarn.lock

---------

Co-authored-by: Shodai Suzuki <info@soartec-lab.work>
  • Loading branch information
barrownicholas and soartec-lab committed May 6, 2024
1 parent 9042d14 commit b13dbb3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
19 changes: 19 additions & 0 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
};
```
2 changes: 1 addition & 1 deletion packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const generateAxiosImplementation = (

return `const ${operationName} = (\n ${propsImplementation}\n ${
isRequestOptions && mutator.hasSecondArg
? `options?: SecondParameter<typeof ${mutator.name}>,`
? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<typeof ${mutator.name}>,`
: ''
}) => {${bodyForm}
return ${mutator.name}<${response.definition.success || 'unknown'}>(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type NormalizedOutputOptions = {
allParamsOptional: boolean;
urlEncodeParameters: boolean;
unionAddMissingProperties: boolean;
optionsParamRequired: boolean;
};

export type NormalizedParamsSerializerOptions = {
Expand Down Expand Up @@ -182,6 +183,7 @@ export type OutputOptions = {
allParamsOptional?: boolean;
urlEncodeParameters?: boolean;
unionAddMissingProperties?: boolean;
optionsParamRequired?: boolean;
};

export type SwaggerParserOptions = Omit<SwaggerParser.Options, 'validate'> & {
Expand Down
1 change: 1 addition & 0 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : {},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ const generateQueryRequestFunction = (
return (\n ${propsImplementation}\n ${
isRequestOptions && mutator.hasSecondArg
? `options?: SecondParameter<ReturnType<typeof ${mutator.name}>>,`
? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<ReturnType<typeof ${mutator.name}>>,`
: ''
}${
!isBodyVerb && hasSignal ? 'signal?: AbortSignal\n' : ''
Expand All @@ -468,7 +468,7 @@ const generateQueryRequestFunction = (

return `${override.query.shouldExportHttpClient ? 'export ' : ''}const ${operationName} = (\n ${propsImplementation}\n ${
isRequestOptions && mutator.hasSecondArg
? `options?: SecondParameter<typeof ${mutator.name}>,`
? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<typeof ${mutator.name}>,`
: ''
}${!isBodyVerb && hasSignal ? 'signal?: AbortSignal\n' : ''}) => {
${isVue(outputClient) ? vueUnRefParams(props) : ''}
Expand Down
2 changes: 1 addition & 1 deletion packages/swr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const generateSwrRequestFunction = (

return `export const ${operationName} = (\n ${propsImplementation}\n ${
isRequestOptions && mutator.hasSecondArg
? `options?: SecondParameter<typeof ${mutator.name}>`
? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<typeof ${mutator.name}>`
: ''
}) => {${bodyForm}
return ${mutator.name}<${response.definition.success || 'unknown'}>(
Expand Down

0 comments on commit b13dbb3

Please sign in to comment.