Skip to content

Commit f3756c3

Browse files
authored
Merge pull request #4522 from KasimAhmic/feature/export-http-resolver-options
2 parents 35791bc + bdc8f44 commit f3756c3

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

docs/rtk-query/usage/code-generation.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ interface SimpleUsage {
108108
| Array<string | RegExp | EndpointMatcherFunction>
109109
endpointOverrides?: EndpointOverrides[]
110110
flattenArg?: boolean
111+
httpResolverOptions?: SwaggerParser.HTTPResolverOptions
111112
}
112113

113114
export type EndpointMatcherFunction = (
@@ -169,3 +170,24 @@ const config: ConfigFile = {
169170
},
170171
}
171172
```
173+
174+
#### Custom HTTP resolver options
175+
176+
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.
177+
178+
For example, you can pass custom headers or set a custom request timeout.
179+
180+
```ts no-transpile title="openapi-config.ts"
181+
const config: ConfigFile = {
182+
schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
183+
apiFile: './src/store/emptyApi.ts',
184+
outputFile: './src/store/petApi.ts',
185+
httpResolverOptions: {
186+
timeout: 30_000,
187+
headers: {
188+
Accept: 'application/json',
189+
Authorization: 'Basic cmVkdXgtdG9vbGtpdDppcy1ncmVhdA==',
190+
},
191+
},
192+
}
193+
```

packages/rtk-query-codegen-openapi/src/generate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ export async function generateApi(
9292
flattenArg = false,
9393
useEnumType = false,
9494
mergeReadWriteOnly = false,
95+
httpResolverOptions,
9596
}: GenerationOptions
9697
) {
97-
const v3Doc = await getV3Doc(spec);
98+
const v3Doc = await getV3Doc(spec, httpResolverOptions);
9899

99100
const apiGen = new ApiGenerator(v3Doc, {
100101
unionUndefined,

packages/rtk-query-codegen-openapi/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type SwaggerParser from '@apidevtools/swagger-parser';
12
import type { OpenAPIV3 } from 'openapi-types';
23

34
export type OperationDefinition = {
@@ -77,6 +78,10 @@ export interface CommonOptions {
7778
* `true` will not generate separate types for read-only and write-only properties.
7879
*/
7980
mergeReadWriteOnly?: boolean;
81+
/**
82+
* HTTPResolverOptions object that is passed to the SwaggerParser bundle function.
83+
*/
84+
httpResolverOptions?: SwaggerParser.HTTPResolverOptions;
8085
}
8186

8287
export type TextMatcher = string | RegExp | (string | RegExp)[];

packages/rtk-query-codegen-openapi/src/utils/getV3Doc.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ import type { OpenAPIV3 } from 'openapi-types';
33
// @ts-ignore
44
import converter from 'swagger2openapi';
55

6-
export async function getV3Doc(spec: string): Promise<OpenAPIV3.Document> {
7-
const doc = await SwaggerParser.bundle(spec);
6+
export async function getV3Doc(
7+
spec: string,
8+
httpResolverOptions?: SwaggerParser.HTTPResolverOptions
9+
): Promise<OpenAPIV3.Document> {
10+
const doc = await SwaggerParser.bundle(spec, {
11+
resolve: {
12+
http: httpResolverOptions,
13+
},
14+
});
15+
816
const isOpenApiV3 = 'openapi' in doc && doc.openapi.startsWith('3');
17+
918
if (isOpenApiV3) {
1019
return doc as OpenAPIV3.Document;
1120
} else {

0 commit comments

Comments
 (0)