diff --git a/README.md b/README.md index f1749cb..a18ec85 100644 --- a/README.md +++ b/README.md @@ -170,8 +170,8 @@ const run = () => { return } - generateTypeDefinitions(document, { outputPath: './client/generated/types.ts' }) - generateTypeDefinitions(document, { outputPath: './api/generated/types.ts' }) + generateTypeDefinitions(document, { outputFilePath: './client/generated/types.ts' }) + generateTypeDefinitions(document, { outputFilePath: './api/generated/types.ts' }) } run() diff --git a/packages/openapi-kit/README.md b/packages/openapi-kit/README.md index f1749cb..a18ec85 100644 --- a/packages/openapi-kit/README.md +++ b/packages/openapi-kit/README.md @@ -170,8 +170,8 @@ const run = () => { return } - generateTypeDefinitions(document, { outputPath: './client/generated/types.ts' }) - generateTypeDefinitions(document, { outputPath: './api/generated/types.ts' }) + generateTypeDefinitions(document, { outputFilePath: './client/generated/types.ts' }) + generateTypeDefinitions(document, { outputFilePath: './api/generated/types.ts' }) } run() diff --git a/packages/openapi-kit/cli/index.ts b/packages/openapi-kit/cli/index.ts index 442a998..51c00c1 100644 --- a/packages/openapi-kit/cli/index.ts +++ b/packages/openapi-kit/cli/index.ts @@ -15,9 +15,14 @@ import { createDirectory, fileExists } from '../src/utils/fileSystem' interface RunOptions { openAPIFilePath: string outputDirectoryPath: string + prettyOutput: boolean } -const run = async ({ openAPIFilePath, outputDirectoryPath }: RunOptions) => { +const run = async ({ + openAPIFilePath, + outputDirectoryPath, + prettyOutput, +}: RunOptions) => { const filePath = path.resolve(process.cwd(), openAPIFilePath) const document = await parseDocument(filePath) if (!document) { @@ -32,44 +37,50 @@ const run = async ({ openAPIFilePath, outputDirectoryPath }: RunOptions) => { '.ts', '', )}` - const typeDefinitionsOutputPath = path.resolve( + const typeDefinitionsOutputFilePath = path.resolve( process.cwd(), outputDirectoryPath, typeDefinitionsFileName, ) const apiClientFileName = 'apiClient.ts' const apiClientImportPath = `./${apiClientFileName.replace('.ts', '')}` - const apiClientOutputPath = path.resolve( + const apiClientOutputFilePath = path.resolve( process.cwd(), outputDirectoryPath, apiClientFileName, ) const mockDataFileName = 'mockData.ts' - const mockOutputPath = path.resolve( + const mockOutputFilePath = path.resolve( process.cwd(), outputDirectoryPath, mockDataFileName, ) const reactQueryHooksFileName = 'reactQuery.tsx' - const reactQueryHooksOutputPath = path.resolve( + const reactQueryHooksOutputFilePath = path.resolve( process.cwd(), outputDirectoryPath, reactQueryHooksFileName, ) - generateTypeDefinitions(document, { outputPath: typeDefinitionsOutputPath }) + generateTypeDefinitions(document, { + outputFilePath: typeDefinitionsOutputFilePath, + prettyOutput, + }) generateAPIClient(document, { - outputPath: apiClientOutputPath, + outputFilePath: apiClientOutputFilePath, typeDefinitionsImportPath, + prettyOutput, }) generateMockData(document, { - outputPath: mockOutputPath, + outputFilePath: mockOutputFilePath, typeDefinitionsImportPath, + prettyOutput, }) generateReactQueryHooks(document, { - outputPath: reactQueryHooksOutputPath, + outputFilePath: reactQueryHooksOutputFilePath, typeDefinitionsImportPath, apiClientImportPath, + prettyOutput, }) } @@ -90,6 +101,12 @@ yargs(hideBin(process.argv)) describe: 'Output directory', demandOption: true, }, + prettyOutput: { + type: 'boolean', + default: false, + describe: 'Wether or not the output should be formatted using Prettier', + demandOption: false, + }, }, (argv) => { if (!fileExists(argv.file)) { @@ -108,6 +125,7 @@ yargs(hideBin(process.argv)) run({ openAPIFilePath: argv.file, outputDirectoryPath: argv.outputDir, + prettyOutput: argv.prettyOutput, }).catch(() => { console.log('An error ocurred') process.exit(1) diff --git a/packages/openapi-kit/src/generators/apiClient/index.ts b/packages/openapi-kit/src/generators/apiClient/index.ts index b4dcdb0..4e35c13 100644 --- a/packages/openapi-kit/src/generators/apiClient/index.ts +++ b/packages/openapi-kit/src/generators/apiClient/index.ts @@ -11,7 +11,11 @@ import { APIClientGeneratorOptions } from './types' export const generateAPIClient = async ( document: OpenAPIDocument, - { outputPath, typeDefinitionsImportPath }: APIClientGeneratorOptions, + { + outputFilePath, + typeDefinitionsImportPath, + prettyOutput = false, + }: APIClientGeneratorOptions, ) => { const lines: string[] = [...getHeaderLines(typeDefinitionsImportPath)] const operations = getOperations(document) @@ -43,6 +47,6 @@ export const generateAPIClient = async ( 'export type APIClient = ReturnType', ) - const fileContent = await formatOutput(lines.join('\n')) - writeFile(outputPath, fileContent) + const fileContent = await formatOutput(lines.join('\n'), prettyOutput) + writeFile(outputFilePath, fileContent) } diff --git a/packages/openapi-kit/src/generators/apiClient/types.ts b/packages/openapi-kit/src/generators/apiClient/types.ts index 8b2879d..c8911ee 100644 --- a/packages/openapi-kit/src/generators/apiClient/types.ts +++ b/packages/openapi-kit/src/generators/apiClient/types.ts @@ -1,4 +1,5 @@ -export interface APIClientGeneratorOptions { +import { GeneratorOptions } from '../types' + +export interface APIClientGeneratorOptions extends GeneratorOptions { typeDefinitionsImportPath: string - outputPath: string } diff --git a/packages/openapi-kit/src/generators/mockData/index.ts b/packages/openapi-kit/src/generators/mockData/index.ts index 09f662a..35f096a 100644 --- a/packages/openapi-kit/src/generators/mockData/index.ts +++ b/packages/openapi-kit/src/generators/mockData/index.ts @@ -1,16 +1,18 @@ -import { capitalize } from 'lodash' - import { OpenAPIDocument } from '../../types' import { writeFile } from '../../utils/fileSystem' import { formatOutput } from '../../utils/format' import { getOperations, isResponseObject } from '../../utils/openAPI' -import { toTypeName, toValidIdentifier } from '../../utils/typescript' +import { toTypeName } from '../../utils/typescript' import { generateMock, logResolvedRefsCallStackExceeded } from './functions' import { MockDataGeneratorOptions } from './types' export const generateMockData = async ( document: OpenAPIDocument, - { outputPath, typeDefinitionsImportPath }: MockDataGeneratorOptions, + { + outputFilePath, + typeDefinitionsImportPath, + prettyOutput = false, + }: MockDataGeneratorOptions, ) => { const operations = getOperations(document) const lines: string[] = [ @@ -32,10 +34,7 @@ export const generateMockData = async ( return } - const mockName = `${pascalCaseOperationId}Response${capitalize( - toValidIdentifier(name), - )}` - + const mockName = `${pascalCaseOperationId}Response${toTypeName(name)}` const type = `Paths.${pascalCaseOperationId}.Responses.${toTypeName( name, )}` @@ -48,7 +47,7 @@ export const generateMockData = async ( } }) - const fileContent = await formatOutput(lines.join('\n')) - writeFile(outputPath, fileContent) + const fileContent = await formatOutput(lines.join('\n'), prettyOutput) + writeFile(outputFilePath, fileContent) logResolvedRefsCallStackExceeded(resolvedRefs) } diff --git a/packages/openapi-kit/src/generators/mockData/types.ts b/packages/openapi-kit/src/generators/mockData/types.ts index 544271a..28e2e29 100644 --- a/packages/openapi-kit/src/generators/mockData/types.ts +++ b/packages/openapi-kit/src/generators/mockData/types.ts @@ -1,4 +1,5 @@ -export interface MockDataGeneratorOptions { +import { GeneratorOptions } from '../types' + +export interface MockDataGeneratorOptions extends GeneratorOptions { typeDefinitionsImportPath: string - outputPath: string } diff --git a/packages/openapi-kit/src/generators/reactQuery/index.ts b/packages/openapi-kit/src/generators/reactQuery/index.ts index 7a8935c..f96be60 100644 --- a/packages/openapi-kit/src/generators/reactQuery/index.ts +++ b/packages/openapi-kit/src/generators/reactQuery/index.ts @@ -8,9 +8,10 @@ import { ReactQueryGeneratorOptions } from './types' export const generateReactQueryHooks = async ( document: OpenAPIDocument, { - outputPath, + outputFilePath, typeDefinitionsImportPath, apiClientImportPath, + prettyOutput = false, }: ReactQueryGeneratorOptions, ) => { const lines: string[] = [ @@ -30,7 +31,7 @@ export const generateReactQueryHooks = async ( lines.push(...buildMutation(operation)) }) - const fileContent = await formatOutput(lines.join('\n')) + const fileContent = await formatOutput(lines.join('\n'), prettyOutput) - writeFile(outputPath, fileContent) + writeFile(outputFilePath, fileContent) } diff --git a/packages/openapi-kit/src/generators/reactQuery/types.ts b/packages/openapi-kit/src/generators/reactQuery/types.ts index 11ea295..573b11e 100644 --- a/packages/openapi-kit/src/generators/reactQuery/types.ts +++ b/packages/openapi-kit/src/generators/reactQuery/types.ts @@ -1,5 +1,6 @@ -export interface ReactQueryGeneratorOptions { +import { GeneratorOptions } from '../types' + +export interface ReactQueryGeneratorOptions extends GeneratorOptions { typeDefinitionsImportPath: string apiClientImportPath: string - outputPath: string } diff --git a/packages/openapi-kit/src/generators/typeDefinitions/index.ts b/packages/openapi-kit/src/generators/typeDefinitions/index.ts index 6b81d06..e7ebd81 100644 --- a/packages/openapi-kit/src/generators/typeDefinitions/index.ts +++ b/packages/openapi-kit/src/generators/typeDefinitions/index.ts @@ -7,7 +7,7 @@ import { TypeDefinitionsGeneratorOptions } from './types' export const generateTypeDefinitions = async ( document: OpenAPIDocument, - { outputPath }: TypeDefinitionsGeneratorOptions, + { outputFilePath, prettyOutput = false }: TypeDefinitionsGeneratorOptions, ) => { const schema = parseSchema(document as JsonSchema) @@ -24,7 +24,8 @@ export const generateTypeDefinitions = async ( const fileContent = await formatOutput( result.replace(/declare namespace/g, 'export declare namespace'), + prettyOutput, ) - writeFile(outputPath, fileContent) + writeFile(outputFilePath, fileContent) } diff --git a/packages/openapi-kit/src/generators/typeDefinitions/types.ts b/packages/openapi-kit/src/generators/typeDefinitions/types.ts index f06de93..61e105a 100644 --- a/packages/openapi-kit/src/generators/typeDefinitions/types.ts +++ b/packages/openapi-kit/src/generators/typeDefinitions/types.ts @@ -1,3 +1,3 @@ -export interface TypeDefinitionsGeneratorOptions { - outputPath: string -} +import { GeneratorOptions } from '../types' + +export interface TypeDefinitionsGeneratorOptions extends GeneratorOptions {} diff --git a/packages/openapi-kit/src/generators/types.ts b/packages/openapi-kit/src/generators/types.ts new file mode 100644 index 0000000..dbaf910 --- /dev/null +++ b/packages/openapi-kit/src/generators/types.ts @@ -0,0 +1,16 @@ +export interface GeneratorOptions { + /** + * Where the generated file should be written to. + */ + outputFilePath: string + /** + * Whether the output should be formatted + * using Prettier or not. + * + * Disabling this option will result in + * faster generation. + * + * @default false + */ + prettyOutput?: boolean +} diff --git a/packages/openapi-kit/src/utils/format.ts b/packages/openapi-kit/src/utils/format.ts index e7c3656..a8d808c 100644 --- a/packages/openapi-kit/src/utils/format.ts +++ b/packages/openapi-kit/src/utils/format.ts @@ -1,6 +1,10 @@ import * as prettier from 'prettier' -export const formatOutput = async (content: string) => { +export const formatOutput = async (content: string, shouldRun: boolean) => { + if (!shouldRun) { + return content + } + const formattedContent = await prettier.format(content, { semi: false, parser: 'babel-ts',