Skip to content

Commit 4a9bfd4

Browse files
committed
fix: add ability to specify schema names to exportModels
1 parent de6a616 commit 4a9bfd4

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

docs/interfaces/openapi_client.OpenApiClientGeneratorConfigClient.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ___
5959

6060
### exportModels
6161

62-
`Optional` **exportModels**: ``"all"`` \| ``"none"`` \| \{ `models`: `string`[] }
62+
`Optional` **exportModels**: ``"all"`` \| ``"none"`` \| \{ `models`: `string`[] } \| \{ `schemas`: `string`[] }
6363

6464
Whether to export models from the client file.
6565

src/schema-to-typescript/common/client.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ import {
5555

5656
const defaultServerUrl = 'http://example.com';
5757

58+
/**
59+
* Calculate model names to export based on the exportModels configuration.
60+
*/
61+
function calculateModelNamesToExport(
62+
exportModels: Exclude<OpenApiClientGeneratorConfig['client'], false>['exportModels'] = 'none',
63+
modelImportInfos: ModelImportInfo[]
64+
): Set<string> {
65+
if (exportModels === 'none') {
66+
return new Set();
67+
}
68+
if (exportModels === 'all') {
69+
return new Set(modelImportInfos.map(({modelName}) => modelName));
70+
}
71+
if ('models' in exportModels) {
72+
return new Set(exportModels.models);
73+
}
74+
if ('schemas' in exportModels) {
75+
const schemaNamesSet = new Set(exportModels.schemas);
76+
return new Set(
77+
modelImportInfos.filter(({schemaName}) => schemaNamesSet.has(schemaName)).map(({modelName}) => modelName)
78+
);
79+
}
80+
throw new Error('Invalid exportModels configuration.');
81+
}
82+
5883
export function generateClient({
5984
commonHttpClientClassName,
6085
commonHttpClientClassOptionsName,
@@ -298,20 +323,16 @@ export function generateClient({
298323
const exportTypes: ExportNamedDeclaration = exportNamedDeclaration(null, []);
299324
exportTypes.exportKind = 'type';
300325

301-
if (exportModels && exportModels !== 'none') {
302-
const modelsToExport = new Set(
303-
exportModels === 'all' ? modelImportInfos.map(({modelName}) => modelName) : exportModels.models
304-
);
305-
for (const {modelName, importPath} of modelImportInfos) {
306-
if (!modelsToExport.has(modelName)) {
307-
continue;
308-
}
309-
addDependencyImport(dependencyImports, getRelativeImportPath(clientImportPath, importPath), modelName, {
310-
kind: 'type',
311-
entity: {name: modelName}
312-
});
313-
exportTypes.specifiers.push(exportSpecifier(identifier(modelName), identifier(modelName)));
326+
const modelNamesToExport = calculateModelNamesToExport(exportModels, modelImportInfos);
327+
for (const {modelName, importPath} of modelImportInfos) {
328+
if (!modelNamesToExport.has(modelName)) {
329+
continue;
314330
}
331+
addDependencyImport(dependencyImports, getRelativeImportPath(clientImportPath, importPath), modelName, {
332+
kind: 'type',
333+
entity: {name: modelName}
334+
});
335+
exportTypes.specifiers.push(exportSpecifier(identifier(modelName), identifier(modelName)));
315336
}
316337

317338
const exports: ExportNamedDeclaration = exportNamedDeclaration(null, []);

src/schema-to-typescript/common/models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050

5151
export interface ModelImportInfo {
5252
modelName: string;
53+
schemaName: string;
5354
importPath: string;
5455
registerValidationSchemasImportName?: string;
5556
}
@@ -358,6 +359,7 @@ export function generateModels({
358359
}
359360
modelsIndex[schemaName] = {
360361
modelName,
362+
schemaName,
361363
importPath,
362364
registerValidationSchemasImportName
363365
};

src/schema-to-typescript/openapi-to-typescript-client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,16 @@ export interface OpenApiClientGeneratorConfigClient {
755755
| 'all'
756756
| 'none'
757757
| {
758+
/**
759+
* Model names to be exported.
760+
*/
758761
models: string[];
762+
}
763+
| {
764+
/**
765+
* Original schema names of the models to be exported.
766+
*/
767+
schemas: string[];
759768
};
760769
/**
761770
* Whether to export the error class from the client file.

0 commit comments

Comments
 (0)