Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Expose HTTP resolver options #4522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/rtk-query/usage/code-generation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ interface SimpleUsage {
| Array<string | RegExp | EndpointMatcherFunction>
endpointOverrides?: EndpointOverrides[]
flattenArg?: boolean
httpResolverOptions?: SwaggerParser.HTTPResolverOptions
}

export type EndpointMatcherFunction = (
Expand Down Expand Up @@ -169,3 +170,24 @@ const config: ConfigFile = {
},
}
```

#### Custom HTTP resolver options

If you need to customize the HTTP request issued to your server, you user the `httpResolverOptions` option. This object is passed directly to the `SwaggerParser` instance that fetches the OpenAPI schema.

For example, you can pass custom headers or set a custom request timeout.

```ts no-transpile title="openapi-config.ts"
const config: ConfigFile = {
schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
apiFile: './src/store/emptyApi.ts',
outputFile: './src/store/petApi.ts',
httpResolverOptions: {
timeout: 30_000,
headers: {
Accept: 'application/json',
Authorization: 'Basic cmVkdXgtdG9vbGtpdDppcy1ncmVhdA==',
},
},
}
```
3 changes: 2 additions & 1 deletion packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ export async function generateApi(
flattenArg = false,
useEnumType = false,
mergeReadWriteOnly = false,
httpResolverOptions,
}: GenerationOptions
) {
const v3Doc = await getV3Doc(spec);
const v3Doc = await getV3Doc(spec, httpResolverOptions);

const apiGen = new ApiGenerator(v3Doc, {
unionUndefined,
Expand Down
5 changes: 5 additions & 0 deletions packages/rtk-query-codegen-openapi/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type SwaggerParser from '@apidevtools/swagger-parser';
import type { OpenAPIV3 } from 'openapi-types';

export type OperationDefinition = {
Expand Down Expand Up @@ -77,6 +78,10 @@ export interface CommonOptions {
* `true` will not generate separate types for read-only and write-only properties.
*/
mergeReadWriteOnly?: boolean;
/**
* HTTPResolverOptions object that is passed to the SwaggerParser bundle function.
*/
httpResolverOptions?: SwaggerParser.HTTPResolverOptions;
}

export type TextMatcher = string | RegExp | (string | RegExp)[];
Expand Down
13 changes: 11 additions & 2 deletions packages/rtk-query-codegen-openapi/src/utils/getV3Doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import type { OpenAPIV3 } from 'openapi-types';
// @ts-ignore
import converter from 'swagger2openapi';

export async function getV3Doc(spec: string): Promise<OpenAPIV3.Document> {
const doc = await SwaggerParser.bundle(spec);
export async function getV3Doc(
spec: string,
httpResolverOptions?: SwaggerParser.HTTPResolverOptions
): Promise<OpenAPIV3.Document> {
const doc = await SwaggerParser.bundle(spec, {
resolve: {
http: httpResolverOptions,
},
});

const isOpenApiV3 = 'openapi' in doc && doc.openapi.startsWith('3');

if (isOpenApiV3) {
return doc as OpenAPIV3.Document;
} else {
Expand Down