Skip to content
Open
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
35 changes: 34 additions & 1 deletion docs/rtk-query/usage/code-generation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ 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.
If you need to customize the HTTP request issued to your server, you can use 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.

Expand All @@ -334,3 +334,36 @@ const config: ConfigFile = {
},
}
```

#### Additional Schemas

By default, the only schemas that are exported are those that are referenced in endpoint definitions. Take the following NestJS example:

```ts no-transpile title="user.controller.ts"
class User {
id: string;
name: string;
}

class LogoutMessage {
reason: string;
}

@ApiExtraModels(LogoutMessage)
@Controller('user')
class UserController {
getUsers(): User[] {
return this.getUsers();
}
}
```

In this case, only the `User` schema would be exported even though we've added `LogoutMessage` as an extra model that will appear in the OpenAPI spec. Setting `exportAllSchemas: true` will export all available schemas, whether they're referenced in endpoint definitions or not.

```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',
exportAllSchemas: true,
}
29 changes: 29 additions & 0 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export async function generateApi(
useUnknown = false,
esmExtensions = false,
outputRegexConstants = false,
exportAllSchemas = false,
}: GenerationOptions
) {
const v3Doc = (v3DocCache[spec] ??= await getV3Doc(spec, httpResolverOptions));
Expand Down Expand Up @@ -269,6 +270,9 @@ export async function generateApi(
return regexConstants.length > 0 ? [alias, ...regexConstants] : [alias];
})
: apiGen.aliases),
...(exportAllSchemas && v3Doc.components?.schemas
? generateAllSchemaTypes(v3Doc.components.schemas, apiGen)
: []),
...apiGen.enumAliases,
...(hooks
? [
Expand Down Expand Up @@ -591,6 +595,31 @@ export async function generateApi(
);
}

function generateAllSchemaTypes(
schemas: Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>,
apiGen: ApiGenerator
): (ts.InterfaceDeclaration | ts.TypeAliasDeclaration)[] {
const types: (ts.InterfaceDeclaration | ts.TypeAliasDeclaration)[] = [];

for (const [schemaName, schema] of Object.entries(schemas)) {
if (apiGen.aliases.some((alias) => alias.name.escapedText === schemaName)) {
continue;
}

const typeNode = apiGen.getTypeFromSchema(schema, schemaName);
const typeAlias = factory.createTypeAliasDeclaration(
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
schemaName,
undefined,
typeNode
);

types.push(typeAlias);
}

return types;
}

// eslint-disable-next-line no-empty-pattern
function generateQueryEndpointProps({}: { operationDefinition: OperationDefinition }): ObjectPropertyDefinitions {
return {}; /* TODO needs implementation - skip for now */
Expand Down
6 changes: 6 additions & 0 deletions packages/rtk-query-codegen-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ export interface CommonOptions {
* Will generate regex constants for pattern keywords in the schema
*/
outputRegexConstants?: boolean;
/**
* @default false
* If set to `true`, all schemas from the OpenAPI document will be exported, regardless of whether they are referenced by any
* endpoint definitions.
*/
exportAllSchemas?: boolean;
}

export type TextMatcher = string | RegExp | (string | RegExp)[];
Expand Down
Loading