Skip to content

Commit

Permalink
Merge branch 'master' into chore/various-small-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos authored Mar 21, 2024
2 parents d8c92b9 + 50d1e9a commit 4193b25
Show file tree
Hide file tree
Showing 37 changed files with 534 additions and 283 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ jobs:
- name: Run unit tests
run: npm run test

# - name: Run e2e tests
# run: npm run test:e2e
- name: Run e2e tests
run: npm run test:e2e
50 changes: 23 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,39 @@ createClient({
## Configuration

<!-- `openapi-ts` supports loading configuration from a file inside your project root directory. You can either create a `openapi-ts.config.js` file -->

`openapi-ts` supports loading configuration from a file inside your project root directory. You just need to create a `openapi-ts.config.js` file
`openapi-ts` supports loading configuration from a file inside your project root directory. You can either create a `openapi-ts.config.cjs` file

```js
/** @type {import('@nicolas-chaulet/openapi-typescript-codegen').UserConfig} */
export default {
module.exports = {
input: 'path/to/openapi.json',
output: 'src/client',
}
```

<!-- or `openapi-ts.config.ts`
```ts
import { defineConfig } from '@nicolas-chaulet/openapi-typescript-codegen';
or `openapi-ts.config.mjs`

export default defineConfig({
```js
/** @type {import('@nicolas-chaulet/openapi-typescript-codegen').UserConfig} */
export default {
input: 'path/to/openapi.json',
output: 'src/client',
})
``` -->
}
```

Alternatively, you can use `openapi-ts.config.js` and configure the export statement depending on your project setup.

### Formatting

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({
```js
/** @type {import('@nicolas-chaulet/openapi-typescript-codegen').UserConfig} */
export default {
format: false,
input: 'path/to/openapi.json',
output: 'src/client',
})
}
```

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`).
Expand All @@ -116,14 +114,13 @@ You can also prevent your client from being processed by formatters by adding yo

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({
```js
/** @type {import('@nicolas-chaulet/openapi-typescript-codegen').UserConfig} */
export default {
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`).
Expand All @@ -132,14 +129,13 @@ You can also prevent your client from being processed by linters by adding your

We do not generate TypeScript [enums](https://www.typescriptlang.org/docs/handbook/enums.html) because they are not standard JavaScript and pose [typing challenges](https://dev.to/ivanzm123/dont-use-enums-in-typescript-they-are-very-dangerous-57bh). If you want to iterate through possible field values without manually typing arrays, you can export enums by running

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

export default defineConfig({
```js
/** @type {import('@nicolas-chaulet/openapi-typescript-codegen').UserConfig} */
export default {
enums: true,
input: 'path/to/openapi.json',
output: 'src/client',
})
}
```

This will export your enums as plain JavaScript objects. For example, `Foo` will generate the following
Expand Down Expand Up @@ -170,7 +166,7 @@ $ openapi-ts --help
--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)
--exportSchemas <value> Write schemas to disk (default: true)
--format Process output folder with formatter?
--no-format Disable processing output folder with formatter
--lint Process output folder with linter?
Expand Down
91 changes: 91 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"eslint-plugin-simple-import-sort": "12.0.0",
"express": "4.18.3",
"form-data": "4.0.0",
"node-fetch": "3.3.2",
"npm-run-all2": "6.1.2",
"prettier": "3.2.5",
"puppeteer": "22.5.0",
Expand Down
4 changes: 3 additions & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ export function handlebarsPlugin(): Plugin {
escapeDescription: true,
escapeNewline: true,
exactArray: true,
exportsModels: true,
exportsSchemas: true,
exportsServices: true,
ifdef: true,
ifOperationDataOptional: true,
intersection: true,
modelImports: true,
modelsExports: true,
modelUnionType: true,
nameOperationDataType: true,
notEquals: true,
Expand Down
30 changes: 17 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import type { Client } from './types/client';
import type { Config, UserConfig } from './types/config';
import { getOpenApiSpec } from './utils/getOpenApiSpec';
import { registerHandlebarTemplates } from './utils/handlebars';
import { isSubDirectory } from './utils/isSubdirectory';
import { postProcessClient } from './utils/postprocess';
import { writeClient } from './utils/write/client';

type Dependencies = Record<string, unknown>;

// const configFiles = ['openapi-ts.config.js', 'openapi-ts.config.ts'];
// add support for `openapi-ts.config.ts`
const configFiles = ['openapi-ts.config.js'];
// TODO: add support for `openapi-ts.config.ts`
const configFiles = ['openapi-ts.config.js', 'openapi-ts.config.cjs', 'openapi-ts.config.mjs'];

export const parseOpenApiSpecification = (openApi: Awaited<ReturnType<typeof getOpenApiSpec>>, config: Config) => {
if ('openapi' in openApi) {
Expand Down Expand Up @@ -93,14 +93,13 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
enums = false,
exportCore = true,
exportModels = true,
exportSchemas = false,
exportSchemas = true,
exportServices = true,
format = true,
input,
lint = false,
name,
operationId = true,
output,
postfixModels = '',
postfixServices = 'Service',
request,
Expand All @@ -110,7 +109,20 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
write = true,
} = userConfig;

if (!input) {
throw new Error('🚫 input not provided - provide path to OpenAPI specification');
}

if (!userConfig.output) {
throw new Error('🚫 output not provided - provide path where we should generate your client');
}

if (!isSubDirectory(process.cwd(), userConfig.output)) {
throw new Error('🚫 output must be within the current working directory');
}

const client = userConfig.client || inferClient(dependencies);
const output = path.resolve(process.cwd(), userConfig.output);

const config: Config = {
base,
Expand All @@ -135,14 +147,6 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
write,
};

if (!input) {
throw new Error('🚫 input not provided - provide path to OpenAPI specification');
}

if (!output) {
throw new Error('🚫 output not provided - provide path where we should generate your client');
}

return config;
};

Expand Down
22 changes: 3 additions & 19 deletions src/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,8 @@ export { OpenAPI } from './core/OpenAPI';
export type { OpenAPIConfig } from './core/OpenAPI';
{{/if}}

{{#if @root.$config.exportModels}}
{{#if models}}
{{{modelsExports @root.$config models './models/'}}}
{{/if}}
{{/if}}
{{{exportsModels}}}

{{#if @root.$config.exportSchemas}}
{{#if models}}
{{#each models}}
export { ${{{name}}} } from './schemas/${{{name}}}';
{{/each}}
{{/if}}
{{/if}}
{{{exportsSchemas}}}

{{#if @root.$config.exportServices}}
{{#if services}}
{{#each services}}
export { {{{name}}}{{{@root.$config.postfixServices}}} } from './services/{{{name}}}{{{@root.$config.postfixServices}}}';
{{/each}}
{{/if}}
{{/if}}
{{{exportsServices}}}
12 changes: 6 additions & 6 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ export interface UserConfig {
*/
client?: 'angular' | 'axios' | 'fetch' | 'node' | 'xhr';
/**
* Generate JavaScript objects from enum definitions
* Generate JavaScript objects from enum definitions?
* @default false
*/
enums?: boolean;
/**
* Generate core client classes
* Generate core client classes?
* @default true
*/
exportCore?: boolean;
/**
* Generate models
* Generate models?
* @default true
*/
exportModels?: boolean | string;
/**
* Generate schemas
* @default false
* Generate schemas?
* @default true
*/
exportSchemas?: boolean;
/**
* Generate services
* Generate services?
* @default true
*/
exportServices?: boolean | string;
Expand Down
Loading

0 comments on commit 4193b25

Please sign in to comment.