From 241c6c9095b8d1ca9362cac56b717627a6c7a665 Mon Sep 17 00:00:00 2001 From: Kasim Ahmic Date: Sat, 20 Jul 2024 13:28:44 -0400 Subject: [PATCH 1/2] Expose HTTPResolverOptions in GenerationOptions --- packages/rtk-query-codegen-openapi/src/generate.ts | 3 ++- packages/rtk-query-codegen-openapi/src/types.ts | 5 +++++ .../rtk-query-codegen-openapi/src/utils/getV3Doc.ts | 13 +++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/rtk-query-codegen-openapi/src/generate.ts b/packages/rtk-query-codegen-openapi/src/generate.ts index fc726f0ca6..f7e9a9ffb9 100644 --- a/packages/rtk-query-codegen-openapi/src/generate.ts +++ b/packages/rtk-query-codegen-openapi/src/generate.ts @@ -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, diff --git a/packages/rtk-query-codegen-openapi/src/types.ts b/packages/rtk-query-codegen-openapi/src/types.ts index cd286ce0aa..cb80ebf10d 100644 --- a/packages/rtk-query-codegen-openapi/src/types.ts +++ b/packages/rtk-query-codegen-openapi/src/types.ts @@ -1,3 +1,4 @@ +import type SwaggerParser from '@apidevtools/swagger-parser'; import type { OpenAPIV3 } from 'openapi-types'; export type OperationDefinition = { @@ -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)[]; diff --git a/packages/rtk-query-codegen-openapi/src/utils/getV3Doc.ts b/packages/rtk-query-codegen-openapi/src/utils/getV3Doc.ts index baa6cadbf0..aed9608312 100644 --- a/packages/rtk-query-codegen-openapi/src/utils/getV3Doc.ts +++ b/packages/rtk-query-codegen-openapi/src/utils/getV3Doc.ts @@ -3,9 +3,18 @@ import type { OpenAPIV3 } from 'openapi-types'; // @ts-ignore import converter from 'swagger2openapi'; -export async function getV3Doc(spec: string): Promise { - const doc = await SwaggerParser.bundle(spec); +export async function getV3Doc( + spec: string, + httpResolverOptions?: SwaggerParser.HTTPResolverOptions +): Promise { + 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 { From bdc8f4424bf3ac003f9592c04f0c96cfaf6e7395 Mon Sep 17 00:00:00 2001 From: Kasim Ahmic Date: Sat, 20 Jul 2024 13:29:37 -0400 Subject: [PATCH 2/2] Add docs for httpResolverOptions --- docs/rtk-query/usage/code-generation.mdx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/rtk-query/usage/code-generation.mdx b/docs/rtk-query/usage/code-generation.mdx index 31ec095875..3842db6e6e 100644 --- a/docs/rtk-query/usage/code-generation.mdx +++ b/docs/rtk-query/usage/code-generation.mdx @@ -108,6 +108,7 @@ interface SimpleUsage { | Array endpointOverrides?: EndpointOverrides[] flattenArg?: boolean + httpResolverOptions?: SwaggerParser.HTTPResolverOptions } export type EndpointMatcherFunction = ( @@ -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==', + }, + }, +} +```