Skip to content

Commit

Permalink
Merge branch 'master' into feat/jest-to-vitests
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford authored Mar 20, 2024
2 parents dfb098e + 13f26e6 commit 0bddfba
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 47 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Installation](#installation)
- [Configuration](#configuration)
- [Formatting](#formatting)
- [Linting](#linting)
- [Enums](#enums)
- [Config API](#config-api)
- [Contributing](#contributing)
Expand Down Expand Up @@ -97,19 +98,35 @@ export default defineConfig({

### Formatting

By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `autoformat` to false
By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `format` to false

```ts
import { defineConfig } from '@nicolas-chaulet/openapi-typescript-codegen';

export default defineConfig({
autoformat: false,
format: false,
input: 'path/to/openapi.json',
output: 'src/client',
})
```

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`).
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`).

### Linting

For performance reasons, `openapi-ts` does not automatically lint your client. To enable this feature, set `lint` to true

```ts
import { defineConfig } from '@nicolas-chaulet/openapi-typescript-codegen';

export default defineConfig({
input: 'path/to/openapi.json',
lint: true,
output: 'src/client',
})
```

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`).

### Enums

Expand Down Expand Up @@ -148,13 +165,16 @@ $ openapi-ts --help
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
--name <value> Custom client class name
--useOptions <value> Use options instead of arguments (default: false)
--no-autoformat Disable processing generated files with formatter
--base <value> Manually set base in OpenAPI config instead of inferring from server value
--enums Generate JavaScript objects from enum definitions (default: false)
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
--exportModels <value> Write models to disk [true, false, regexp] (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--format Process output folder with formatter?
--no-format Disable processing output folder with formatter
--lint Process output folder with linter?
--no-lint Disable processing output folder with linter
--no-operationId Use path URL to generate operation ID
--postfixServices Service name postfix (default: "Service")
--postfixModels Model name postfix
Expand Down
8 changes: 5 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ const params = program
.option('-c, --client <value>', 'HTTP client to generate [angular, axios, fetch, node, xhr]')
.option('--name <value>', 'Custom client class name')
.option('--useOptions [value]', 'Use options instead of arguments')
.option('--autoformat', 'Process generated files with formatter?')
.option('--no-autoformat', 'Disable processing generated files with formatter')
.option('--base [value]', 'Manually set base in OpenAPI config instead of inferring from server value')
.option('--enums', 'Generate JavaScript objects from enum definitions')
.option('--exportCore <value>', 'Write core files to disk')
.option('--exportServices <value>', 'Write services to disk')
.option('--exportModels <value>', 'Write models to disk')
.option('--exportSchemas <value>', 'Write schemas to disk')
.option('--format', 'Process output folder with formatter?')
.option('--no-format', 'Disable processing output folder with formatter')
.option('--lint', 'Process output folder with linter?')
.option('--no-lint', 'Disable processing output folder with linter')
.option('--operationId', 'Use operationd ID?')
.option('--no-operationId', 'Use path URL to generate operation ID')
.option('--postfixServices <value>', 'Service name postfix')
Expand All @@ -39,7 +41,7 @@ const params = program

async function start() {
try {
const { createClient } = await import(new URL('../dist/index.js', import.meta.url));
const { createClient } = await import(new URL('../dist/node/index.js', import.meta.url));
await createClient(params);
process.exit(0);
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nicolas-chaulet/openapi-typescript-codegen",
"version": "0.27.33",
"version": "0.27.34",
"type": "module",
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
"author": "Ferdi Koomen",
Expand Down Expand Up @@ -32,7 +32,7 @@
"email": "info@madebyferdi.com"
}
],
"main": "./dist/index.js",
"main": "./dist/node/index.js",
"types": "./dist/node/index.d.ts",
"bin": {
"openapi-ts": "bin/index.js"
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)).
function createConfig(isProduction: boolean) {
return defineConfig({
external: [...Object.keys(pkg.dependencies)],
input: path.resolve(__dirname, 'src/index.ts'),
input: path.resolve(__dirname, 'src/node/index.ts'),
output: {
file: path.resolve(__dirname, 'dist/index.js'),
file: path.resolve(__dirname, 'dist/node/index.js'),
format: 'esm',
},
plugins: [
Expand Down
3 changes: 2 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ describe('parseOpenApiSpecification', () => {
});

const options: Parameters<typeof parseOpenApiSpecification>[1] = {
autoformat: true,
client: 'fetch',
enums: true,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: '',
postfixModels: '',
Expand Down
40 changes: 21 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ type Dependencies = Record<string, unknown>;
// add support for `openapi-ts.config.ts`
const configFiles = ['openapi-ts.config.js'];

export const parseOpenApiSpecification = (openApi: Awaited<ReturnType<typeof getOpenApiSpec>>, options: Config) => {
if ('swagger' in openApi) {
return parseV2(openApi, options);
}
export const parseOpenApiSpecification = (openApi: Awaited<ReturnType<typeof getOpenApiSpec>>, config: Config) => {
if ('openapi' in openApi) {
return parseV3(openApi, options);
return parseV3(openApi, config);
}
if ('swagger' in openApi) {
return parseV2(openApi, config);
}
throw new Error(`Unsupported Open API specification: ${JSON.stringify(openApi, null, 2)}`);
};

const formatClient = (options: Config, dependencies: Dependencies) => {
if (!options.autoformat) {
return;
}

if (dependencies.prettier) {
console.log('✨ Running Prettier');
sync('prettier', ['--ignore-unknown', options.output, '--write', '--ignore-path', './.prettierignore']);
const processOutput = (config: Config, dependencies: Dependencies) => {
if (config.format) {
if (dependencies.prettier) {
console.log('✨ Running Prettier');
sync('prettier', ['--ignore-unknown', config.output, '--write', '--ignore-path', './.prettierignore']);
}
}

if (dependencies.eslint) {
console.log('✨ Running Eslint');
sync('eslint', [options.output, '--fix', '--quiet', '--ignore-path', './.eslintignore']);
if (config.lint) {
if (dependencies.eslint) {
console.log('✨ Running ESLint');
sync('eslint', [config.output, '--fix', '--quiet', '--ignore-path', './.eslintignore']);
}
}
};

Expand Down Expand Up @@ -89,14 +89,15 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
}

const {
autoformat = true,
base,
enums = false,
exportCore = true,
exportModels = true,
exportSchemas = false,
exportServices = true,
format = true,
input,
lint = false,
name,
operationId = true,
output,
Expand All @@ -112,15 +113,16 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
const client = userConfig.client || inferClient(dependencies);

const config: Config = {
autoformat,
base,
client,
enums,
exportCore,
exportModels,
exportSchemas,
exportServices,
format,
input,
lint,
name,
operationId,
output,
Expand Down Expand Up @@ -174,7 +176,7 @@ export async function createClient(userConfig: UserConfig): Promise<Client> {
if (config.write) {
logClientMessage(config.client);
await writeClient(client, templates, config);
formatClient(config, dependencies);
processOutput(config, dependencies);
}

console.log('✨ Done! Your client is located in:', config.output);
Expand Down
3 changes: 2 additions & 1 deletion src/openApi/v2/parser/getServices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { getServices } from './getServices';
describe('getServices', () => {
it('should create a unnamed service if tags are empty', () => {
const options: Parameters<typeof getServices>[1] = {
autoformat: false,
client: 'fetch',
enums: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
operationId: false,
output: '',
postfixModels: '',
Expand Down
3 changes: 2 additions & 1 deletion src/openApi/v3/parser/__tests__/getServices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { getServices } from '../getServices';
describe('getServices', () => {
it('should create a unnamed service if tags are empty', () => {
const options: Parameters<typeof getServices>[1] = {
autoformat: false,
client: 'fetch',
enums: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
operationId: true,
output: '',
postfixModels: '',
Expand Down
15 changes: 10 additions & 5 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export interface UserConfig {
/**
* Process generated files with autoformatter
* @default true
*/
autoformat?: boolean;
/**
* Manually set base in OpenAPI config instead of inferring from server value
*/
Expand Down Expand Up @@ -38,10 +33,20 @@ export interface UserConfig {
* @default true
*/
exportServices?: boolean | string;
/**
* Process output folder with formatter?
* @default true
*/
format?: boolean;
/**
* The relative location of the OpenAPI spec
*/
input: string | Record<string, unknown>;
/**
* Process output folder with linter?
* @default false
*/
lint?: boolean;
/**
* Custom client class name
*/
Expand Down
6 changes: 4 additions & 2 deletions src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ describe('registerHandlebarHelpers', () => {
it('should register the helpers', () => {
registerHandlebarHelpers(
{
autoformat: true,
client: 'fetch',
enums: true,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: '',
postfixModels: '',
Expand Down Expand Up @@ -59,14 +60,15 @@ describe('registerHandlebarTemplates', () => {
it('should return correct templates', () => {
const templates = registerHandlebarTemplates(
{
autoformat: true,
client: 'fetch',
enums: true,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: '',
postfixModels: '',
Expand Down
3 changes: 2 additions & 1 deletion src/utils/write/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ describe('writeClient', () => {
};

await writeClient(client, templates, {
autoformat: true,
client: 'fetch',
enums: true,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: true,
input: '',
lint: false,
operationId: true,
output: './dist',
postfixModels: 'AppClient',
Expand Down
3 changes: 2 additions & 1 deletion src/utils/write/__tests__/services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ describe('writeClientServices', () => {
};

await writeClientServices(client, templates, '/', {
autoformat: false,
client: 'fetch',
enums: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
operationId: true,
output: '',
postfixModels: '',
Expand Down
Loading

0 comments on commit 0bddfba

Please sign in to comment.