api-typescript-generator
Advanced tools
Comparing version 2.1.7 to 2.2.1
@@ -21,2 +21,3 @@ [api-typescript-generator](../../README.md) / [Modules](../modules.md) / [index](../modules/index.md) / CommonOpenApiClientGeneratorConfig | ||
- [outputDirPath](index.CommonOpenApiClientGeneratorConfig.md#outputdirpath) | ||
- [postprocess](index.CommonOpenApiClientGeneratorConfig.md#postprocess) | ||
@@ -29,2 +30,4 @@ ## Properties | ||
Configuration for the OpenAPI document. | ||
___ | ||
@@ -35,1 +38,11 @@ | ||
• **outputDirPath**: `string` | ||
Output directory for the generated client files. | ||
___ | ||
### postprocess | ||
• `Optional` **postprocess**: [`CommonOpenApiClientGeneratorConfigPostprocess`](index.CommonOpenApiClientGeneratorConfigPostprocess.md) | ||
Configuration for the generated client files. |
@@ -28,2 +28,3 @@ [api-typescript-generator](../../README.md) / [Modules](../modules.md) / [openapi-client](../modules/openapi_client.md) / OpenApiClientGeneratorConfig | ||
- [outputDirPath](openapi_client.OpenApiClientGeneratorConfig.md#outputdirpath) | ||
- [postprocess](openapi_client.OpenApiClientGeneratorConfig.md#postprocess) | ||
- [services](openapi_client.OpenApiClientGeneratorConfig.md#services) | ||
@@ -78,2 +79,4 @@ - [type](openapi_client.OpenApiClientGeneratorConfig.md#type) | ||
Configuration for the OpenAPI document. | ||
#### Inherited from | ||
@@ -113,2 +116,4 @@ | ||
Output directory for the generated client files. | ||
#### Inherited from | ||
@@ -120,2 +125,14 @@ | ||
### postprocess | ||
• `Optional` **postprocess**: [`CommonOpenApiClientGeneratorConfigPostprocess`](index.CommonOpenApiClientGeneratorConfigPostprocess.md) | ||
Configuration for the generated client files. | ||
#### Inherited from | ||
[CommonOpenApiClientGeneratorConfig](index.CommonOpenApiClientGeneratorConfig.md).[postprocess](index.CommonOpenApiClientGeneratorConfig.md#postprocess) | ||
___ | ||
### services | ||
@@ -122,0 +139,0 @@ |
@@ -13,2 +13,3 @@ [api-typescript-generator](../../README.md) / [Modules](../modules.md) / index | ||
- [CommonOpenApiClientGeneratorConfigDocumentPatch](../interfaces/index.CommonOpenApiClientGeneratorConfigDocumentPatch.md) | ||
- [CommonOpenApiClientGeneratorConfigPostprocess](../interfaces/index.CommonOpenApiClientGeneratorConfigPostprocess.md) | ||
- [FilenameFormat](../interfaces/index.FilenameFormat.md) | ||
@@ -15,0 +16,0 @@ - [JsDocBlock](../interfaces/index.JsDocBlock.md) |
@@ -43,2 +43,3 @@ #!/usr/bin/env node | ||
const helpers_1 = require("yargs/helpers"); | ||
const compare_generation_result_1 = require("./compare-generation-result"); | ||
const save_generation_result_1 = require("./save-generation-result"); | ||
@@ -50,2 +51,3 @@ const client_core_1 = require("../schema-to-typescript/common/client-core"); | ||
const load_open_api_document_1 = require("../schemas/load-open-api-document"); | ||
const postprocess_files_1 = require("../utils/postprocess-files"); | ||
function loadConfig(filename) { | ||
@@ -70,4 +72,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) | ||
.command('generate <config>', 'generates API files according to the specified configuration file.', () => yargs_1.default.positional('config', { | ||
const configurationFileArgument = yargs_1.default.positional('config', { | ||
describe: 'configuration file, can be either js or ts file.', | ||
@@ -77,4 +78,19 @@ default: 'api-typescript-generator.config.js', | ||
demandOption: true | ||
}), (argv) => __awaiter(void 0, void 0, void 0, function* () { | ||
}); | ||
function getCleanupDirectories(generateConfig) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j; | ||
return [ | ||
...(((_a = generateConfig.models) === null || _a === void 0 ? void 0 : _a.cleanupFiles) | ||
? [(_c = (_b = generateConfig.models) === null || _b === void 0 ? void 0 : _b.relativeDirPath) !== null && _c !== void 0 ? _c : models_1.defaultModelsRelativeDirPath] | ||
: []), | ||
...(generateConfig.services && ((_d = generateConfig.services) === null || _d === void 0 ? void 0 : _d.cleanupFiles) | ||
? [(_f = (_e = generateConfig.services) === null || _e === void 0 ? void 0 : _e.relativeDirPath) !== null && _f !== void 0 ? _f : services_1.defaultServicesRelativeDirPath] | ||
: []), | ||
...(((_g = generateConfig.core) === null || _g === void 0 ? void 0 : _g.cleanupFiles) | ||
? [(_j = (_h = generateConfig.core) === null || _h === void 0 ? void 0 : _h.relativeDirPath) !== null && _j !== void 0 ? _j : client_core_1.defaultCoreRelativeDirPath] | ||
: []) | ||
]; | ||
} | ||
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) | ||
.command('generate <config>', 'generates API files according to the specified configuration file.', () => configurationFileArgument, (argv) => __awaiter(void 0, void 0, void 0, function* () { | ||
const config = yield loadConfig(argv.config); | ||
@@ -85,8 +101,8 @@ for (const generateConfig of config.generates) { | ||
const document = yield (0, load_open_api_document_1.loadOpenApiDocument)(generateConfig.document); | ||
const result = yield (0, openapi_to_typescript_client_1.openapiToTypescriptClient)({ | ||
const files = yield (0, postprocess_files_1.postprocessFiles)((yield (0, openapi_to_typescript_client_1.openapiToTypescriptClient)({ | ||
document, | ||
generateConfig | ||
}); | ||
})).files, generateConfig.postprocess); | ||
const allDirectories = new Set(); | ||
for (const { filename } of result.files) { | ||
for (const { filename } of files) { | ||
allDirectories.add(path_1.default.dirname(path_1.default.resolve(generateConfig.outputDirPath, filename))); | ||
@@ -103,21 +119,31 @@ } | ||
yield (0, save_generation_result_1.saveGenerationResult)({ | ||
files: result.files, | ||
files, | ||
outputDirPath: generateConfig.outputDirPath, | ||
cleanupDirectories: [ | ||
...(((_a = generateConfig.models) === null || _a === void 0 ? void 0 : _a.cleanupFiles) | ||
? [(_c = (_b = generateConfig.models) === null || _b === void 0 ? void 0 : _b.relativeDirPath) !== null && _c !== void 0 ? _c : models_1.defaultModelsRelativeDirPath] | ||
: []), | ||
...(generateConfig.services && ((_d = generateConfig.services) === null || _d === void 0 ? void 0 : _d.cleanupFiles) | ||
? [(_f = (_e = generateConfig.services) === null || _e === void 0 ? void 0 : _e.relativeDirPath) !== null && _f !== void 0 ? _f : services_1.defaultServicesRelativeDirPath] | ||
: []), | ||
...(((_g = generateConfig.core) === null || _g === void 0 ? void 0 : _g.cleanupFiles) | ||
? [(_j = (_h = generateConfig.core) === null || _h === void 0 ? void 0 : _h.relativeDirPath) !== null && _j !== void 0 ? _j : client_core_1.defaultCoreRelativeDirPath] | ||
: []) | ||
] | ||
cleanupDirectories: getCleanupDirectories(generateConfig) | ||
}); | ||
break; | ||
} | ||
} | ||
})) | ||
.command('check <config>', 'compares previously generated API files with schema according to the specified configuration file.', () => configurationFileArgument, (argv) => __awaiter(void 0, void 0, void 0, function* () { | ||
const config = yield loadConfig(argv.config); | ||
for (const generateConfig of config.generates) { | ||
switch (generateConfig.type) { | ||
case 'openapiClient': | ||
if (!(yield (0, compare_generation_result_1.compareGenerationResult)({ | ||
files: yield (0, postprocess_files_1.postprocessFiles)((yield (0, openapi_to_typescript_client_1.openapiToTypescriptClient)({ | ||
document: yield (0, load_open_api_document_1.loadOpenApiDocument)(generateConfig.document), | ||
generateConfig | ||
})).files, generateConfig.postprocess), | ||
outputDirPath: generateConfig.outputDirPath, | ||
cleanupDirectories: getCleanupDirectories(generateConfig) | ||
}))) { | ||
console.error('Generated files are not up to date. Please run "api-typescript-generator generate" to update them.'); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
})) | ||
.demandCommand(1) | ||
.strictCommands() | ||
.parse(); |
@@ -1,3 +0,3 @@ | ||
export type { ApiTypescriptGeneratorConfig, CommonOpenApiClientGeneratorConfig, CommonApiToTypescriptGeneratorSource, CommonOpenApiClientGeneratorConfigDocument, OpenApiDocumentPatchSchema, OpenApiDocumentPatchOperation, OpenApiDocumentPatchPathItem, OpenApiDocumentPatchTags, CommonOpenApiClientGeneratorConfigDocumentPatch, OpenApiDocumentPatchAllSchemas, OpenApiDocumentPatchDocument } from './schema-to-typescript/config'; | ||
export type { ApiTypescriptGeneratorConfig, CommonOpenApiClientGeneratorConfig, CommonApiToTypescriptGeneratorSource, CommonOpenApiClientGeneratorConfigDocument, OpenApiDocumentPatchSchema, OpenApiDocumentPatchOperation, OpenApiDocumentPatchPathItem, OpenApiDocumentPatchTags, CommonOpenApiClientGeneratorConfigDocumentPatch, OpenApiDocumentPatchAllSchemas, OpenApiDocumentPatchDocument, CommonOpenApiClientGeneratorConfigPostprocess } from './schema-to-typescript/config'; | ||
export type { JsDocBlockTag, JsDocBlock } from './utils/jsdoc'; | ||
export type { FilenameFormat, EntityNameCase } from './utils/string-utils'; |
@@ -40,5 +40,9 @@ import { OpenApiClientGeneratorConfig } from './openapi-to-typescript-client'; | ||
} | ||
export interface CommonOpenApiClientGeneratorConfigPostprocess { | ||
eslint?: boolean; | ||
} | ||
export interface CommonOpenApiClientGeneratorConfig { | ||
document: CommonOpenApiClientGeneratorConfigDocument; | ||
outputDirPath: string; | ||
postprocess?: CommonOpenApiClientGeneratorConfigPostprocess; | ||
} | ||
@@ -45,0 +49,0 @@ export interface ClientGenerationResult { |
{ | ||
"name": "api-typescript-generator", | ||
"version": "2.1.7", | ||
"version": "2.2.1", | ||
"description": "Generates OpenAPI TypeScript client. Extremely fast and flexible.", | ||
@@ -56,2 +56,3 @@ "license": "MIT", | ||
"@commitlint/config-conventional": "^17.6.6", | ||
"@types/eslint": "^8.56.10", | ||
"@types/esutils": "^2.0.0", | ||
@@ -58,0 +59,0 @@ "@types/glob": "^7.2.0", |
@@ -6,2 +6,3 @@ #!/usr/bin/env node | ||
import {hideBin} from 'yargs/helpers'; | ||
import {compareGenerationResult} from './compare-generation-result'; | ||
import {saveGenerationResult} from './save-generation-result'; | ||
@@ -11,5 +12,9 @@ import {defaultCoreRelativeDirPath} from '../schema-to-typescript/common/client-core'; | ||
import {defaultServicesRelativeDirPath} from '../schema-to-typescript/common/services'; | ||
import {ApiTypescriptGeneratorConfig} from '../schema-to-typescript/config'; | ||
import {openapiToTypescriptClient} from '../schema-to-typescript/openapi-to-typescript-client'; | ||
import {ApiTypescriptGeneratorConfig, CommonOpenApiClientGeneratorConfig} from '../schema-to-typescript/config'; | ||
import { | ||
OpenApiClientGeneratorConfig, | ||
openapiToTypescriptClient | ||
} from '../schema-to-typescript/openapi-to-typescript-client'; | ||
import {loadOpenApiDocument} from '../schemas/load-open-api-document'; | ||
import {postprocessFiles} from '../utils/postprocess-files'; | ||
@@ -34,2 +39,23 @@ async function loadConfig(filename: string): Promise<ApiTypescriptGeneratorConfig> { | ||
const configurationFileArgument = yargs.positional('config', { | ||
describe: 'configuration file, can be either js or ts file.', | ||
default: 'api-typescript-generator.config.js', | ||
type: 'string', | ||
demandOption: true | ||
}); | ||
function getCleanupDirectories(generateConfig: OpenApiClientGeneratorConfig & CommonOpenApiClientGeneratorConfig) { | ||
return [ | ||
...(generateConfig.models?.cleanupFiles | ||
? [generateConfig.models?.relativeDirPath ?? defaultModelsRelativeDirPath] | ||
: []), | ||
...(generateConfig.services && generateConfig.services?.cleanupFiles | ||
? [generateConfig.services?.relativeDirPath ?? defaultServicesRelativeDirPath] | ||
: []), | ||
...(generateConfig.core?.cleanupFiles | ||
? [generateConfig.core?.relativeDirPath ?? defaultCoreRelativeDirPath] | ||
: []) | ||
]; | ||
} | ||
yargs(hideBin(process.argv)) | ||
@@ -39,9 +65,3 @@ .command( | ||
'generates API files according to the specified configuration file.', | ||
() => | ||
yargs.positional('config', { | ||
describe: 'configuration file, can be either js or ts file.', | ||
default: 'api-typescript-generator.config.js', | ||
type: 'string', | ||
demandOption: true | ||
}), | ||
() => configurationFileArgument, | ||
async (argv) => { | ||
@@ -53,8 +73,13 @@ const config: ApiTypescriptGeneratorConfig = await loadConfig(argv.config); | ||
const document = await loadOpenApiDocument(generateConfig.document); | ||
const result = await openapiToTypescriptClient({ | ||
document, | ||
generateConfig | ||
}); | ||
const files = await postprocessFiles( | ||
( | ||
await openapiToTypescriptClient({ | ||
document, | ||
generateConfig | ||
}) | ||
).files, | ||
generateConfig.postprocess | ||
); | ||
const allDirectories = new Set<string>(); | ||
for (const {filename} of result.files) { | ||
for (const {filename} of files) { | ||
allDirectories.add(path.dirname(path.resolve(generateConfig.outputDirPath, filename))); | ||
@@ -72,16 +97,7 @@ } | ||
await saveGenerationResult({ | ||
files: result.files, | ||
files, | ||
outputDirPath: generateConfig.outputDirPath, | ||
cleanupDirectories: [ | ||
...(generateConfig.models?.cleanupFiles | ||
? [generateConfig.models?.relativeDirPath ?? defaultModelsRelativeDirPath] | ||
: []), | ||
...(generateConfig.services && generateConfig.services?.cleanupFiles | ||
? [generateConfig.services?.relativeDirPath ?? defaultServicesRelativeDirPath] | ||
: []), | ||
...(generateConfig.core?.cleanupFiles | ||
? [generateConfig.core?.relativeDirPath ?? defaultCoreRelativeDirPath] | ||
: []) | ||
] | ||
cleanupDirectories: getCleanupDirectories(generateConfig) | ||
}); | ||
break; | ||
} | ||
@@ -91,4 +107,37 @@ } | ||
) | ||
.command( | ||
'check <config>', | ||
'compares previously generated API files with schema according to the specified configuration file.', | ||
() => configurationFileArgument, | ||
async (argv) => { | ||
const config: ApiTypescriptGeneratorConfig = await loadConfig(argv.config); | ||
for (const generateConfig of config.generates) { | ||
switch (generateConfig.type) { | ||
case 'openapiClient': | ||
if ( | ||
!(await compareGenerationResult({ | ||
files: await postprocessFiles( | ||
( | ||
await openapiToTypescriptClient({ | ||
document: await loadOpenApiDocument(generateConfig.document), | ||
generateConfig | ||
}) | ||
).files, | ||
generateConfig.postprocess | ||
), | ||
outputDirPath: generateConfig.outputDirPath, | ||
cleanupDirectories: getCleanupDirectories(generateConfig) | ||
})) | ||
) { | ||
console.error( | ||
'Generated files are not up to date. Please run "api-typescript-generator generate" to update them.' | ||
); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
} | ||
) | ||
.demandCommand(1) | ||
.strictCommands() | ||
.parse(); |
@@ -12,5 +12,6 @@ export type { | ||
OpenApiDocumentPatchAllSchemas, | ||
OpenApiDocumentPatchDocument | ||
OpenApiDocumentPatchDocument, | ||
CommonOpenApiClientGeneratorConfigPostprocess | ||
} from './schema-to-typescript/config'; | ||
export type {JsDocBlockTag, JsDocBlock} from './utils/jsdoc'; | ||
export type {FilenameFormat, EntityNameCase} from './utils/string-utils'; |
@@ -133,7 +133,27 @@ import {OpenApiClientGeneratorConfig} from './openapi-to-typescript-client'; | ||
/** | ||
* Configuration for postprocessing the generated files. | ||
*/ | ||
export interface CommonOpenApiClientGeneratorConfigPostprocess { | ||
/** | ||
* If true, runs ESLint on the generated files. | ||
*/ | ||
eslint?: boolean; | ||
} | ||
/** | ||
* Common configuration for the API client generators. | ||
*/ | ||
export interface CommonOpenApiClientGeneratorConfig { | ||
/** | ||
* Configuration for the OpenAPI document. | ||
*/ | ||
document: CommonOpenApiClientGeneratorConfigDocument; | ||
/** | ||
* Output directory for the generated client files. | ||
*/ | ||
outputDirPath: string; | ||
/** | ||
* Configuration for the generated client files. | ||
*/ | ||
postprocess?: CommonOpenApiClientGeneratorConfigPostprocess; | ||
} | ||
@@ -140,0 +160,0 @@ |
687093
168
13081
25
7