Skip to content

Commit

Permalink
Merge pull request #4522 from KasimAhmic/feature/export-http-resolver…
Browse files Browse the repository at this point in the history
…-options
  • Loading branch information
markerikson authored Aug 30, 2024
2 parents 35791bc + bdc8f44 commit f3756c3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
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

0 comments on commit f3756c3

Please sign in to comment.