Skip to content

Commit d46ea2b

Browse files
committed
feat: ability to specify exact model names for export
1 parent e5c876c commit d46ea2b

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
"precheck": "tsc --noEmit && jest && ",
4242
"build": "rimraf lib && tsc -p tsconfig.build.json && cp -R src/schema-to-typescript/common/core/* lib/schema-to-typescript/common/core && cp -R src/schema-to-typescript/common/validation-providers/zod-validation-provider/*-*.ts lib/schema-to-typescript/common/validation-providers/zod-validation-provider",
4343
"build:docs": "rm -Rf docs && typedoc --exclude test --excludeInternal --excludeExternals --disableSources --plugin typedoc-plugin-markdown --entryDocument ../README.md --out docs src/index.ts src/openapi.ts src/openapi-client.ts && ts-node tools/cleanup-docs.ts",
44-
"prepublishOnly": "npm run build && npm run build:docs",
44+
"prepublishOnly": "npm run build",
4545
"lint": "eslint {src,test,tools}/**/*.ts",
4646
"lint:fix": "eslint --fix {src,test,tools}/**/*.ts",
4747
"test": "jest",
4848
"test:watch": "jest --watch --watchPathIgnorePatterns tmp",
4949
"test:update": "ts-node src/cli/index.ts generate test/pet-store/api-typescript-generator-config.ts",
50-
"release": "standard-version"
50+
"release": "npm run build:docs && standard-version"
5151
},
5252
"devDependencies": {
5353
"@babel/preset-env": "^7.24.4",

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function generateClient({
6565
name,
6666
exportModels,
6767
exportServices,
68+
exportErrorClass,
6869
errorClassName,
6970
generateJsDoc,
7071
...filenameConfig
@@ -111,13 +112,17 @@ export function generateClient({
111112
);
112113

113114
const errorTypeName = errorClassName ?? `${name}Error`;
114-
const errorTypeExport = exportNamedDeclaration(
115-
classDeclaration(
116-
identifier(errorTypeName),
117-
memberExpression(identifier(commonHttpClientImportName), identifier(commonHttpClientErrorClassName)),
118-
classBody([classProperty(identifier('name'), stringLiteral(errorTypeName))])
119-
)
115+
const additionalTypeStatements: Statement[] = [];
116+
const errorClassDeclaration = classDeclaration(
117+
identifier(errorTypeName),
118+
memberExpression(identifier(commonHttpClientImportName), identifier(commonHttpClientErrorClassName)),
119+
classBody([classProperty(identifier('name'), stringLiteral(errorTypeName))])
120120
);
121+
if (exportErrorClass !== false) {
122+
additionalTypeStatements.push(exportNamedDeclaration(errorClassDeclaration));
123+
} else {
124+
additionalTypeStatements.push(errorClassDeclaration);
125+
}
121126

122127
const clientClassBody = classBody([
123128
classProperty(
@@ -281,8 +286,14 @@ export function generateClient({
281286
const exportTypes: ExportNamedDeclaration = exportNamedDeclaration(null, []);
282287
exportTypes.exportKind = 'type';
283288

284-
if (exportModels) {
289+
if (exportModels && exportModels !== 'none') {
290+
const modelsToExport = new Set(
291+
exportModels === 'all' ? modelImportInfos.map(({modelName}) => modelName) : exportModels.models
292+
);
285293
for (const {modelName, importPath} of modelImportInfos) {
294+
if (!modelsToExport.has(modelName)) {
295+
continue;
296+
}
286297
addDependencyImport(dependencyImports, getRelativeImportPath(clientImportPath, importPath), modelName, {
287298
kind: 'type',
288299
entity: {name: modelName}
@@ -293,8 +304,14 @@ export function generateClient({
293304

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

296-
if (exportServices) {
307+
if (exportServices && exportServices !== 'none') {
308+
const servicesToExport = new Set(
309+
exportServices === 'all' ? generatedServiceImports.map(({name}) => name) : exportServices.services
310+
);
297311
for (const {name, importPath} of generatedServiceImports) {
312+
if (!servicesToExport.has(name)) {
313+
continue;
314+
}
298315
addDependencyImport(dependencyImports, getRelativeImportPath(clientImportPath, importPath), name, {
299316
kind: 'value',
300317
entity: {name}
@@ -333,7 +350,7 @@ export function generateClient({
333350
),
334351
...generateTsImports(dependencyImports),
335352
optionsTypeExport,
336-
errorTypeExport,
353+
...additionalTypeStatements,
337354
clientClass,
338355
...otherStatements,
339356
...(exportTypes.specifiers.length > 0 ? [exportTypes] : []),

src/schema-to-typescript/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ export interface CommonOpenApiClientGeneratorConfigDocument {
158158
*/
159159
export interface CommonOpenApiClientGeneratorConfigPostprocess {
160160
/**
161-
* If true, runs ESLint on the generated files.
161+
* If true, runs ESLint on the generated files. ESlint should be installed in the project and the configuration
162+
* should be present. This postprocess step can take a long time for large schemas.
162163
*/
163164
eslint?: boolean;
164165
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,15 +690,31 @@ export interface OpenApiClientGeneratorConfigClient {
690690
/**
691691
* Whether to export services from the client file.
692692
*
693-
* @default false
693+
* @default 'none'
694694
*/
695-
exportServices?: boolean;
695+
exportServices?:
696+
| 'all'
697+
| 'none'
698+
| {
699+
services: string[];
700+
};
696701
/**
697702
* Whether to export models from the client file.
698703
*
699-
* @default false
704+
* @default 'none'
705+
*/
706+
exportModels?:
707+
| 'all'
708+
| 'none'
709+
| {
710+
models: string[];
711+
};
712+
/**
713+
* Whether to export the error class from the client file.
714+
*
715+
* @default true
700716
*/
701-
exportModels?: boolean;
717+
exportErrorClass?: boolean;
702718
/**
703719
* Client JSDoc generation callback.
704720
*/

0 commit comments

Comments
 (0)