fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #96 from nicolas-chaulet/fix/lint-flag

fix(config): set lint to false by default

authored by

Lubos and committed by
GitHub
720dd9a4 69506ace

+81 -44
+24 -4
README.md
··· 10 10 - [Installation](#installation) 11 11 - [Configuration](#configuration) 12 12 - [Formatting](#formatting) 13 + - [Linting](#linting) 13 14 - [Enums](#enums) 14 15 - [Config API](#config-api) 15 16 - [Contributing](#contributing) ··· 97 98 98 99 ### Formatting 99 100 100 - By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `autoformat` to false 101 + By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `format` to false 102 + 103 + ```ts 104 + import { defineConfig } from '@nicolas-chaulet/openapi-typescript-codegen'; 105 + 106 + export default defineConfig({ 107 + format: false, 108 + input: 'path/to/openapi.json', 109 + output: 'src/client', 110 + }) 111 + ``` 112 + 113 + You can also prevent your client from being processed by formatters by adding your output path to the tool's ignore file (e.g. `.prettierignore`). 114 + 115 + ### Linting 116 + 117 + For performance reasons, `openapi-ts` does not automatically lint your client. To enable this feature, set `lint` to true 101 118 102 119 ```ts 103 120 import { defineConfig } from '@nicolas-chaulet/openapi-typescript-codegen'; 104 121 105 122 export default defineConfig({ 106 - autoformat: false, 107 123 input: 'path/to/openapi.json', 124 + lint: true, 108 125 output: 'src/client', 109 126 }) 110 127 ``` 111 128 112 - You can also prevent your client from being processed by formatters and linters by adding your output path to the tool's ignore file (e.g. `.eslintignore`, `.prettierignore`). 129 + You can also prevent your client from being processed by linters by adding your output path to the tool's ignore file (e.g. `.eslintignore`). 113 130 114 131 ### Enums 115 132 ··· 148 165 -c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch") 149 166 --name <value> Custom client class name 150 167 --useOptions <value> Use options instead of arguments (default: false) 151 - --no-autoformat Disable processing generated files with formatter 152 168 --base <value> Manually set base in OpenAPI config instead of inferring from server value 153 169 --enums Generate JavaScript objects from enum definitions (default: false) 154 170 --exportCore <value> Write core files to disk (default: true) 155 171 --exportServices <value> Write services to disk [true, false, regexp] (default: true) 156 172 --exportModels <value> Write models to disk [true, false, regexp] (default: true) 157 173 --exportSchemas <value> Write schemas to disk (default: false) 174 + --format Process output folder with formatter? 175 + --no-format Disable processing output folder with formatter 176 + --lint Process output folder with linter? 177 + --no-lint Disable processing output folder with linter 158 178 --no-operationId Use path URL to generate operation ID 159 179 --postfixServices Service name postfix (default: "Service") 160 180 --postfixModels Model name postfix
+5 -3
bin/index.js
··· 17 17 .option('-c, --client <value>', 'HTTP client to generate [angular, axios, fetch, node, xhr]') 18 18 .option('--name <value>', 'Custom client class name') 19 19 .option('--useOptions [value]', 'Use options instead of arguments') 20 - .option('--autoformat', 'Process generated files with formatter?') 21 - .option('--no-autoformat', 'Disable processing generated files with formatter') 22 20 .option('--base [value]', 'Manually set base in OpenAPI config instead of inferring from server value') 23 21 .option('--enums', 'Generate JavaScript objects from enum definitions') 24 22 .option('--exportCore <value>', 'Write core files to disk') 25 23 .option('--exportServices <value>', 'Write services to disk') 26 24 .option('--exportModels <value>', 'Write models to disk') 27 25 .option('--exportSchemas <value>', 'Write schemas to disk') 26 + .option('--format', 'Process output folder with formatter?') 27 + .option('--no-format', 'Disable processing output folder with formatter') 28 + .option('--lint', 'Process output folder with linter?') 29 + .option('--no-lint', 'Disable processing output folder with linter') 28 30 .option('--operationId', 'Use operationd ID?') 29 31 .option('--no-operationId', 'Use path URL to generate operation ID') 30 32 .option('--postfixServices <value>', 'Service name postfix') ··· 39 41 40 42 async function start() { 41 43 try { 42 - const { createClient } = await import(new URL('../dist/index.js', import.meta.url)); 44 + const { createClient } = await import(new URL('../dist/node/index.js', import.meta.url)); 43 45 await createClient(params); 44 46 process.exit(0); 45 47 } catch (error) {
+1 -1
package.json
··· 32 32 "email": "info@madebyferdi.com" 33 33 } 34 34 ], 35 - "main": "./dist/index.js", 35 + "main": "./dist/node/index.js", 36 36 "types": "./dist/node/index.d.ts", 37 37 "bin": { 38 38 "openapi-ts": "bin/index.js"
+2 -2
rollup.config.ts
··· 72 72 function createConfig(isProduction: boolean) { 73 73 return defineConfig({ 74 74 external: [...Object.keys(pkg.dependencies)], 75 - input: path.resolve(__dirname, 'src/index.ts'), 75 + input: path.resolve(__dirname, 'src/node/index.ts'), 76 76 output: { 77 - file: path.resolve(__dirname, 'dist/index.js'), 77 + file: path.resolve(__dirname, 'dist/node/index.js'), 78 78 format: 'esm', 79 79 }, 80 80 plugins: [
+2 -1
src/index.spec.ts
··· 42 42 }); 43 43 44 44 const options: Parameters<typeof parseOpenApiSpecification>[1] = { 45 - autoformat: true, 46 45 client: 'fetch', 47 46 enums: true, 48 47 exportCore: true, 49 48 exportModels: true, 50 49 exportSchemas: true, 51 50 exportServices: true, 51 + format: true, 52 52 input: '', 53 + lint: false, 53 54 operationId: true, 54 55 output: '', 55 56 postfixModels: '',
+21 -19
src/index.ts
··· 19 19 // add support for `openapi-ts.config.ts` 20 20 const configFiles = ['openapi-ts.config.js']; 21 21 22 - export const parseOpenApiSpecification = (openApi: Awaited<ReturnType<typeof getOpenApiSpec>>, options: Config) => { 23 - if ('swagger' in openApi) { 24 - return parseV2(openApi, options); 25 - } 22 + export const parseOpenApiSpecification = (openApi: Awaited<ReturnType<typeof getOpenApiSpec>>, config: Config) => { 26 23 if ('openapi' in openApi) { 27 - return parseV3(openApi, options); 24 + return parseV3(openApi, config); 25 + } 26 + if ('swagger' in openApi) { 27 + return parseV2(openApi, config); 28 28 } 29 29 throw new Error(`Unsupported Open API specification: ${JSON.stringify(openApi, null, 2)}`); 30 30 }; 31 31 32 - const formatClient = (options: Config, dependencies: Dependencies) => { 33 - if (!options.autoformat) { 34 - return; 35 - } 36 - 37 - if (dependencies.prettier) { 38 - console.log('✨ Running Prettier'); 39 - sync('prettier', ['--ignore-unknown', options.output, '--write', '--ignore-path', './.prettierignore']); 32 + const processOutput = (config: Config, dependencies: Dependencies) => { 33 + if (config.format) { 34 + if (dependencies.prettier) { 35 + console.log('✨ Running Prettier'); 36 + sync('prettier', ['--ignore-unknown', config.output, '--write', '--ignore-path', './.prettierignore']); 37 + } 40 38 } 41 39 42 - if (dependencies.eslint) { 43 - console.log('✨ Running Eslint'); 44 - sync('eslint', [options.output, '--fix', '--quiet', '--ignore-path', './.eslintignore']); 40 + if (config.lint) { 41 + if (dependencies.eslint) { 42 + console.log('✨ Running ESLint'); 43 + sync('eslint', [config.output, '--fix', '--quiet', '--ignore-path', './.eslintignore']); 44 + } 45 45 } 46 46 }; 47 47 ··· 89 89 } 90 90 91 91 const { 92 - autoformat = true, 93 92 base, 94 93 enums = false, 95 94 exportCore = true, 96 95 exportModels = true, 97 96 exportSchemas = false, 98 97 exportServices = true, 98 + format = true, 99 99 input, 100 + lint = false, 100 101 name, 101 102 operationId = true, 102 103 output, ··· 112 113 const client = userConfig.client || inferClient(dependencies); 113 114 114 115 const config: Config = { 115 - autoformat, 116 116 base, 117 117 client, 118 118 enums, ··· 120 120 exportModels, 121 121 exportSchemas, 122 122 exportServices, 123 + format, 123 124 input, 125 + lint, 124 126 name, 125 127 operationId, 126 128 output, ··· 174 176 if (config.write) { 175 177 logClientMessage(config.client); 176 178 await writeClient(client, templates, config); 177 - formatClient(config, dependencies); 179 + processOutput(config, dependencies); 178 180 } 179 181 180 182 console.log('✨ Done! Your client is located in:', config.output);
+2 -1
src/openApi/v2/parser/getServices.spec.ts
··· 3 3 describe('getServices', () => { 4 4 it('should create a unnamed service if tags are empty', () => { 5 5 const options: Parameters<typeof getServices>[1] = { 6 - autoformat: false, 7 6 client: 'fetch', 8 7 enums: false, 9 8 exportCore: true, 10 9 exportModels: true, 11 10 exportSchemas: true, 12 11 exportServices: true, 12 + format: false, 13 13 input: '', 14 + lint: false, 14 15 operationId: false, 15 16 output: '', 16 17 postfixModels: '',
+2 -1
src/openApi/v3/parser/__tests__/getServices.spec.ts
··· 3 3 describe('getServices', () => { 4 4 it('should create a unnamed service if tags are empty', () => { 5 5 const options: Parameters<typeof getServices>[1] = { 6 - autoformat: false, 7 6 client: 'fetch', 8 7 enums: false, 9 8 exportCore: true, 10 9 exportModels: true, 11 10 exportSchemas: true, 12 11 exportServices: true, 12 + format: false, 13 13 input: '', 14 + lint: false, 14 15 operationId: true, 15 16 output: '', 16 17 postfixModels: '',
+10 -5
src/types/config.ts
··· 1 1 export interface UserConfig { 2 2 /** 3 - * Process generated files with autoformatter 4 - * @default true 5 - */ 6 - autoformat?: boolean; 7 - /** 8 3 * Manually set base in OpenAPI config instead of inferring from server value 9 4 */ 10 5 base?: string; ··· 39 34 */ 40 35 exportServices?: boolean | string; 41 36 /** 37 + * Process output folder with formatter? 38 + * @default true 39 + */ 40 + format?: boolean; 41 + /** 42 42 * The relative location of the OpenAPI spec 43 43 */ 44 44 input: string | Record<string, unknown>; 45 + /** 46 + * Process output folder with linter? 47 + * @default false 48 + */ 49 + lint?: boolean; 45 50 /** 46 51 * Custom client class name 47 52 */
+4 -2
src/utils/__tests__/handlebars.spec.ts
··· 6 6 it('should register the helpers', () => { 7 7 registerHandlebarHelpers( 8 8 { 9 - autoformat: true, 10 9 client: 'fetch', 11 10 enums: true, 12 11 exportCore: true, 13 12 exportModels: true, 14 13 exportSchemas: true, 15 14 exportServices: true, 15 + format: true, 16 16 input: '', 17 + lint: false, 17 18 operationId: true, 18 19 output: '', 19 20 postfixModels: '', ··· 58 59 it('should return correct templates', () => { 59 60 const templates = registerHandlebarTemplates( 60 61 { 61 - autoformat: true, 62 62 client: 'fetch', 63 63 enums: true, 64 64 exportCore: true, 65 65 exportModels: true, 66 66 exportSchemas: true, 67 67 exportServices: true, 68 + format: true, 68 69 input: '', 70 + lint: false, 69 71 operationId: true, 70 72 output: '', 71 73 postfixModels: '',
+2 -1
src/utils/write/__tests__/client.spec.ts
··· 35 35 }; 36 36 37 37 await writeClient(client, templates, { 38 - autoformat: true, 39 38 client: 'fetch', 40 39 enums: true, 41 40 exportCore: true, 42 41 exportModels: true, 43 42 exportSchemas: true, 44 43 exportServices: true, 44 + format: true, 45 45 input: '', 46 + lint: false, 46 47 operationId: true, 47 48 output: './dist', 48 49 postfixModels: 'AppClient',
+2 -1
src/utils/write/__tests__/services.spec.ts
··· 43 43 }; 44 44 45 45 await writeClientServices(client, templates, '/', { 46 - autoformat: false, 47 46 client: 'fetch', 48 47 enums: false, 49 48 exportCore: true, 50 49 exportModels: true, 51 50 exportSchemas: true, 52 51 exportServices: true, 52 + format: false, 53 53 input: '', 54 + lint: false, 54 55 operationId: true, 55 56 output: '', 56 57 postfixModels: '',
+4 -3
test/bin.spec.ts
··· 134 134 expect(result.stderr.toString()).toBe(''); 135 135 }); 136 136 137 - it('autoformats output with Prettier', async () => { 137 + it('formats output with Prettier', async () => { 138 138 const result = sync('node', [ 139 139 './bin/index.js', 140 140 '--input', ··· 146 146 expect(result.stderr.toString()).toBe(''); 147 147 }); 148 148 149 - it('auto fixs output with Eslint', async () => { 149 + it('lints output with ESLint', async () => { 150 150 const result = sync('node', [ 151 151 './bin/index.js', 152 152 '--input', 153 153 './test/spec/v3.json', 154 154 '--output', 155 155 './test/generated/bin', 156 + '--lint', 156 157 ]); 157 - expect(result.stdout.toString()).toContain('Eslint'); 158 + expect(result.stdout.toString()).toContain('ESLint'); 158 159 expect(result.stderr.toString()).toBe(''); 159 160 }); 160 161