mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Working indentation
This commit is contained in:
parent
6f2a714a15
commit
15f7955ce4
@ -47,7 +47,8 @@ $ openapi --help
|
||||
--exportServices <value> Write services to disk (default: true)
|
||||
--exportModels <value> Write models to disk (default: true)
|
||||
--exportSchemas <value> Write schemas to disk (default: false)
|
||||
--postfix <value> Service name postfix (default: "Service")
|
||||
--indent <value> Service name postfix (default: "Service")
|
||||
--postfix <value> Indentation options [4, 2, tab] (default: "5")
|
||||
--request <value> Path to custom request file
|
||||
-h, --help display help for command
|
||||
|
||||
|
||||
@ -19,6 +19,7 @@ const params = program
|
||||
.option('--exportServices <value>', 'Write services to disk', true)
|
||||
.option('--exportModels <value>', 'Write models to disk', true)
|
||||
.option('--exportSchemas <value>', 'Write schemas to disk', false)
|
||||
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
|
||||
.option('--postfix <value>', 'Service name postfix', 'Service')
|
||||
.option('--request <value>', 'Path to custom request file')
|
||||
.parse(process.argv)
|
||||
@ -37,6 +38,7 @@ if (OpenAPI) {
|
||||
exportServices: JSON.parse(params.exportServices) === true,
|
||||
exportModels: JSON.parse(params.exportModels) === true,
|
||||
exportSchemas: JSON.parse(params.exportSchemas) === true,
|
||||
indent: params.indent,
|
||||
postfix: params.postfix,
|
||||
request: params.request,
|
||||
})
|
||||
|
||||
5
src/Indent.ts
Normal file
5
src/Indent.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export enum Indent {
|
||||
SPACE_4 = '4',
|
||||
SPACE_2 = '2',
|
||||
TAB = 'tab',
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
import { HttpClient } from './HttpClient';
|
||||
import { Indent } from './Indent';
|
||||
import { parse as parseV2 } from './openApi/v2';
|
||||
import { parse as parseV3 } from './openApi/v3';
|
||||
import { getOpenApiSpec } from './utils/getOpenApiSpec';
|
||||
@ -9,6 +10,7 @@ import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
|
||||
import { writeClient } from './utils/writeClient';
|
||||
|
||||
export { HttpClient } from './HttpClient';
|
||||
export { Indent } from './Indent';
|
||||
|
||||
export type Options = {
|
||||
input: string | Record<string, any>;
|
||||
@ -20,6 +22,7 @@ export type Options = {
|
||||
exportServices?: boolean;
|
||||
exportModels?: boolean;
|
||||
exportSchemas?: boolean;
|
||||
indent?: Indent;
|
||||
postfix?: string;
|
||||
request?: string;
|
||||
write?: boolean;
|
||||
@ -38,6 +41,7 @@ export type Options = {
|
||||
* @param exportServices: Generate services
|
||||
* @param exportModels: Generate models
|
||||
* @param exportSchemas: Generate schemas
|
||||
* @param indent: Indentation options (4, 2 or tab)
|
||||
* @param postfix: Service name postfix
|
||||
* @param request: Path to custom request file
|
||||
* @param write Write the files to disk (true or false)
|
||||
@ -52,6 +56,7 @@ export async function generate({
|
||||
exportServices = true,
|
||||
exportModels = true,
|
||||
exportSchemas = false,
|
||||
indent = Indent.SPACE_4,
|
||||
postfix = 'Service',
|
||||
request,
|
||||
write = true,
|
||||
@ -80,6 +85,7 @@ export async function generate({
|
||||
exportServices,
|
||||
exportModels,
|
||||
exportSchemas,
|
||||
indent,
|
||||
postfix,
|
||||
request
|
||||
);
|
||||
@ -101,6 +107,7 @@ export async function generate({
|
||||
exportServices,
|
||||
exportModels,
|
||||
exportSchemas,
|
||||
indent,
|
||||
postfix,
|
||||
request
|
||||
);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { format } from './format';
|
||||
import { formatCode } from './formatCode';
|
||||
|
||||
const input1 = `{ foo: true }`;
|
||||
|
||||
@ -30,11 +30,11 @@ const output4 = `{
|
||||
|
||||
describe('format', () => {
|
||||
it('should produce correct result', () => {
|
||||
expect(format(``)).toEqual('');
|
||||
expect(format(`{}`)).toEqual('{}');
|
||||
expect(format(input1)).toEqual(output1);
|
||||
expect(format(input2)).toEqual(output2);
|
||||
expect(format(input3)).toEqual(output3);
|
||||
expect(format(input4)).toEqual(output4);
|
||||
expect(formatCode(``)).toEqual('');
|
||||
expect(formatCode(`{}`)).toEqual('{}');
|
||||
expect(formatCode(input1)).toEqual(output1);
|
||||
expect(formatCode(input2)).toEqual(output2);
|
||||
expect(formatCode(input3)).toEqual(output3);
|
||||
expect(formatCode(input4)).toEqual(output4);
|
||||
});
|
||||
});
|
||||
@ -1,6 +1,6 @@
|
||||
import { EOL } from 'os';
|
||||
|
||||
export function format(s: string): string {
|
||||
export function formatCode(s: string): string {
|
||||
let indent: number = 0;
|
||||
let lines = s.split(EOL);
|
||||
lines = lines.map(line => {
|
||||
18
src/utils/formatIndentation.ts
Normal file
18
src/utils/formatIndentation.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { EOL } from 'os';
|
||||
|
||||
import { Indent } from '../Indent';
|
||||
|
||||
export function formatIndentation(s: string, indent: Indent): string {
|
||||
let lines = s.split(EOL);
|
||||
lines = lines.map(line => {
|
||||
switch (indent) {
|
||||
case Indent.SPACE_4:
|
||||
return line.replace(/\t/g, ' ');
|
||||
case Indent.SPACE_2:
|
||||
return line.replace(/\t/g, ' ');
|
||||
case Indent.TAB:
|
||||
return line; // Default output is tab formatted
|
||||
}
|
||||
});
|
||||
return lines.join(EOL);
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
import { EOL } from 'os';
|
||||
|
||||
export function indent(s: string): string {
|
||||
let lines = s.split(EOL);
|
||||
lines = lines.map(line => {
|
||||
return line.replace(/\t/g, ' ');
|
||||
});
|
||||
return lines.join(EOL);
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
import type { Client } from '../client/interfaces/Client';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { mkdir, rmdir, writeFile } from './fileSystem';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
import { writeClient } from './writeClient';
|
||||
@ -32,7 +33,20 @@ describe('writeClient', () => {
|
||||
},
|
||||
};
|
||||
|
||||
await writeClient(client, templates, './dist', HttpClient.FETCH, false, false, true, true, true, true, '');
|
||||
await writeClient(
|
||||
client,
|
||||
templates,
|
||||
'./dist',
|
||||
HttpClient.FETCH,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
Indent.SPACE_4,
|
||||
''
|
||||
);
|
||||
|
||||
expect(rmdir).toBeCalled();
|
||||
expect(mkdir).toBeCalled();
|
||||
|
||||
@ -2,6 +2,7 @@ import { resolve } from 'path';
|
||||
|
||||
import type { Client } from '../client/interfaces/Client';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { mkdir, rmdir } from './fileSystem';
|
||||
import { isSubDirectory } from './isSubdirectory';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
@ -24,6 +25,7 @@ import { writeClientServices } from './writeClientServices';
|
||||
* @param exportModels: Generate models
|
||||
* @param exportSchemas: Generate schemas
|
||||
* @param exportSchemas: Generate schemas
|
||||
* @param indent: Indentation options (4, 2 or tab)
|
||||
* @param postfix: Service name postfix
|
||||
* @param request: Path to custom request file
|
||||
*/
|
||||
@ -38,6 +40,7 @@ export async function writeClient(
|
||||
exportServices: boolean,
|
||||
exportModels: boolean,
|
||||
exportSchemas: boolean,
|
||||
indent: Indent,
|
||||
postfix: string,
|
||||
request?: string
|
||||
): Promise<void> {
|
||||
@ -54,7 +57,7 @@ export async function writeClient(
|
||||
if (exportCore) {
|
||||
await rmdir(outputPathCore);
|
||||
await mkdir(outputPathCore);
|
||||
await writeClientCore(client, templates, outputPathCore, httpClient, request);
|
||||
await writeClientCore(client, templates, outputPathCore, httpClient, indent, request);
|
||||
}
|
||||
|
||||
if (exportServices) {
|
||||
@ -67,6 +70,7 @@ export async function writeClient(
|
||||
httpClient,
|
||||
useUnionTypes,
|
||||
useOptions,
|
||||
indent,
|
||||
postfix
|
||||
);
|
||||
}
|
||||
@ -74,13 +78,13 @@ export async function writeClient(
|
||||
if (exportSchemas) {
|
||||
await rmdir(outputPathSchemas);
|
||||
await mkdir(outputPathSchemas);
|
||||
await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes);
|
||||
await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes, indent);
|
||||
}
|
||||
|
||||
if (exportModels) {
|
||||
await rmdir(outputPathModels);
|
||||
await mkdir(outputPathModels);
|
||||
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes);
|
||||
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
|
||||
}
|
||||
|
||||
if (exportCore || exportServices || exportSchemas || exportModels) {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import type { Client } from '../client/interfaces/Client';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { writeFile } from './fileSystem';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
import { writeClientCore } from './writeClientCore';
|
||||
@ -32,7 +33,7 @@ describe('writeClientCore', () => {
|
||||
},
|
||||
};
|
||||
|
||||
await writeClientCore(client, templates, '/', HttpClient.FETCH);
|
||||
await writeClientCore(client, templates, '/', HttpClient.FETCH, Indent.SPACE_4);
|
||||
|
||||
expect(writeFile).toBeCalledWith('/OpenAPI.ts', 'settings');
|
||||
expect(writeFile).toBeCalledWith('/ApiError.ts', 'apiError');
|
||||
|
||||
@ -2,8 +2,9 @@ import { resolve } from 'path';
|
||||
|
||||
import type { Client } from '../client/interfaces/Client';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { copyFile, exists, writeFile } from './fileSystem';
|
||||
import { indent } from './indent';
|
||||
import { formatIndentation as i } from './formatIndentation';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
|
||||
/**
|
||||
@ -12,6 +13,7 @@ import { Templates } from './registerHandlebarTemplates';
|
||||
* @param templates The loaded handlebar templates
|
||||
* @param outputPath Directory to write the generated files to
|
||||
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
|
||||
* @param indent: Indentation options (4, 2 or tab)
|
||||
* @param request: Path to custom request file
|
||||
*/
|
||||
export async function writeClientCore(
|
||||
@ -19,6 +21,7 @@ export async function writeClientCore(
|
||||
templates: Templates,
|
||||
outputPath: string,
|
||||
httpClient: HttpClient,
|
||||
indent: Indent,
|
||||
request?: string
|
||||
): Promise<void> {
|
||||
const context = {
|
||||
@ -27,12 +30,12 @@ export async function writeClientCore(
|
||||
version: client.version,
|
||||
};
|
||||
|
||||
await writeFile(resolve(outputPath, 'OpenAPI.ts'), indent(templates.core.settings(context)));
|
||||
await writeFile(resolve(outputPath, 'ApiError.ts'), indent(templates.core.apiError({})));
|
||||
await writeFile(resolve(outputPath, 'ApiRequestOptions.ts'), indent(templates.core.apiRequestOptions({})));
|
||||
await writeFile(resolve(outputPath, 'ApiResult.ts'), indent(templates.core.apiResult({})));
|
||||
await writeFile(resolve(outputPath, 'CancelablePromise.ts'), indent(templates.core.cancelablePromise({})));
|
||||
await writeFile(resolve(outputPath, 'request.ts'), indent(templates.core.request(context)));
|
||||
await writeFile(resolve(outputPath, 'OpenAPI.ts'), i(templates.core.settings(context), indent));
|
||||
await writeFile(resolve(outputPath, 'ApiError.ts'), i(templates.core.apiError(context), indent));
|
||||
await writeFile(resolve(outputPath, 'ApiRequestOptions.ts'), i(templates.core.apiRequestOptions(context), indent));
|
||||
await writeFile(resolve(outputPath, 'ApiResult.ts'), i(templates.core.apiResult(context), indent));
|
||||
await writeFile(resolve(outputPath, 'CancelablePromise.ts'), i(templates.core.cancelablePromise(context), indent));
|
||||
await writeFile(resolve(outputPath, 'request.ts'), i(templates.core.request(context), indent));
|
||||
|
||||
if (request) {
|
||||
const requestFile = resolve(process.cwd(), request);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import type { Model } from '../client/interfaces/Model';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { writeFile } from './fileSystem';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
import { writeClientModels } from './writeClientModels';
|
||||
@ -45,7 +46,7 @@ describe('writeClientModels', () => {
|
||||
},
|
||||
};
|
||||
|
||||
await writeClientModels(models, templates, '/', HttpClient.FETCH, false);
|
||||
await writeClientModels(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4);
|
||||
|
||||
expect(writeFile).toBeCalledWith('/User.ts', 'model');
|
||||
});
|
||||
|
||||
@ -2,9 +2,10 @@ import { resolve } from 'path';
|
||||
|
||||
import type { Model } from '../client/interfaces/Model';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { writeFile } from './fileSystem';
|
||||
import { format } from './format';
|
||||
import { indent } from './indent';
|
||||
import { formatCode as f } from './formatCode';
|
||||
import { formatIndentation as i } from './formatIndentation';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
|
||||
/**
|
||||
@ -14,13 +15,15 @@ import { Templates } from './registerHandlebarTemplates';
|
||||
* @param outputPath Directory to write the generated files to
|
||||
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
|
||||
* @param useUnionTypes Use union types instead of enums
|
||||
* @param indent: Indentation options (4, 2 or tab)
|
||||
*/
|
||||
export async function writeClientModels(
|
||||
models: Model[],
|
||||
templates: Templates,
|
||||
outputPath: string,
|
||||
httpClient: HttpClient,
|
||||
useUnionTypes: boolean
|
||||
useUnionTypes: boolean,
|
||||
indent: Indent
|
||||
): Promise<void> {
|
||||
for (const model of models) {
|
||||
const file = resolve(outputPath, `${model.name}.ts`);
|
||||
@ -29,6 +32,6 @@ export async function writeClientModels(
|
||||
httpClient,
|
||||
useUnionTypes,
|
||||
});
|
||||
await writeFile(file, indent(format(templateResult)));
|
||||
await writeFile(file, i(f(templateResult), indent));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import type { Model } from '../client/interfaces/Model';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { writeFile } from './fileSystem';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
import { writeClientSchemas } from './writeClientSchemas';
|
||||
@ -45,7 +46,7 @@ describe('writeClientSchemas', () => {
|
||||
},
|
||||
};
|
||||
|
||||
await writeClientSchemas(models, templates, '/', HttpClient.FETCH, false);
|
||||
await writeClientSchemas(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4);
|
||||
|
||||
expect(writeFile).toBeCalledWith('/$User.ts', 'schema');
|
||||
});
|
||||
|
||||
@ -2,9 +2,10 @@ import { resolve } from 'path';
|
||||
|
||||
import type { Model } from '../client/interfaces/Model';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { writeFile } from './fileSystem';
|
||||
import { format } from './format';
|
||||
import { indent } from './indent';
|
||||
import { formatCode as f } from './formatCode';
|
||||
import { formatIndentation as i } from './formatIndentation';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
|
||||
/**
|
||||
@ -14,13 +15,15 @@ import { Templates } from './registerHandlebarTemplates';
|
||||
* @param outputPath Directory to write the generated files to
|
||||
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
|
||||
* @param useUnionTypes Use union types instead of enums
|
||||
* @param indent: Indentation options (4, 2 or tab)
|
||||
*/
|
||||
export async function writeClientSchemas(
|
||||
models: Model[],
|
||||
templates: Templates,
|
||||
outputPath: string,
|
||||
httpClient: HttpClient,
|
||||
useUnionTypes: boolean
|
||||
useUnionTypes: boolean,
|
||||
indent: Indent
|
||||
): Promise<void> {
|
||||
for (const model of models) {
|
||||
const file = resolve(outputPath, `$${model.name}.ts`);
|
||||
@ -29,6 +32,6 @@ export async function writeClientSchemas(
|
||||
httpClient,
|
||||
useUnionTypes,
|
||||
});
|
||||
await writeFile(file, indent(format(templateResult)));
|
||||
await writeFile(file, i(f(templateResult), indent));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import type { Service } from '../client/interfaces/Service';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { writeFile } from './fileSystem';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
import { writeClientServices } from './writeClientServices';
|
||||
@ -33,7 +34,7 @@ describe('writeClientServices', () => {
|
||||
},
|
||||
};
|
||||
|
||||
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, 'Service');
|
||||
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service');
|
||||
|
||||
expect(writeFile).toBeCalledWith('/UserService.ts', 'service');
|
||||
});
|
||||
|
||||
@ -2,9 +2,10 @@ import { resolve } from 'path';
|
||||
|
||||
import type { Service } from '../client/interfaces/Service';
|
||||
import { HttpClient } from '../HttpClient';
|
||||
import { Indent } from '../Indent';
|
||||
import { writeFile } from './fileSystem';
|
||||
import { format } from './format';
|
||||
import { indent } from './indent';
|
||||
import { formatCode as f } from './formatCode';
|
||||
import { formatIndentation as i } from './formatIndentation';
|
||||
import { Templates } from './registerHandlebarTemplates';
|
||||
|
||||
const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION';
|
||||
@ -17,6 +18,7 @@ const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION';
|
||||
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
|
||||
* @param useUnionTypes Use union types instead of enums
|
||||
* @param useOptions Use options or arguments functions
|
||||
* @param indent: Indentation options (4, 2 or tab)
|
||||
* @param postfix: Service name postfix
|
||||
*/
|
||||
export async function writeClientServices(
|
||||
@ -26,6 +28,7 @@ export async function writeClientServices(
|
||||
httpClient: HttpClient,
|
||||
useUnionTypes: boolean,
|
||||
useOptions: boolean,
|
||||
indent: Indent,
|
||||
postfix: string
|
||||
): Promise<void> {
|
||||
for (const service of services) {
|
||||
@ -39,6 +42,6 @@ export async function writeClientServices(
|
||||
useOptions,
|
||||
postfix,
|
||||
});
|
||||
await writeFile(file, indent(format(templateResult)));
|
||||
await writeFile(file, i(f(templateResult), indent));
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@ const generate = async (input, output) => {
|
||||
exportSchemas: true,
|
||||
exportModels: true,
|
||||
exportServices: true,
|
||||
// indent: OpenAPI.Indent.SPACE_2,
|
||||
// postfix: 'Api',
|
||||
// request: './test/custom/request.ts',
|
||||
});
|
||||
|
||||
7
types/index.d.ts
vendored
7
types/index.d.ts
vendored
@ -5,6 +5,12 @@ export declare enum HttpClient {
|
||||
AXIOS = 'axios',
|
||||
}
|
||||
|
||||
export declare enum Indent {
|
||||
SPACE_4 = '4',
|
||||
SPACE_2 = '2',
|
||||
TAB = 'tab',
|
||||
}
|
||||
|
||||
export type Options = {
|
||||
input: string | Record<string, any>;
|
||||
output: string;
|
||||
@ -15,6 +21,7 @@ export type Options = {
|
||||
exportServices?: boolean;
|
||||
exportModels?: boolean;
|
||||
exportSchemas?: boolean;
|
||||
indent?: Indent | '4' | '2' | 'tab';
|
||||
postfix?: string;
|
||||
request?: string;
|
||||
write?: boolean;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user