Skip to content

Commit

Permalink
Merge pull request #498 from hey-api/fix/typescript-enums
Browse files Browse the repository at this point in the history
feat: remove enum postfix, use typescript enums in types when generated, export enums from types.gen.ts
  • Loading branch information
mrlubos authored Apr 26, 2024
2 parents 2c0d370 + 59343f1 commit 0ca8b75
Show file tree
Hide file tree
Showing 41 changed files with 220 additions and 542 deletions.
6 changes: 6 additions & 0 deletions .changeset/itchy-jobs-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hey-api/openapi-ts": minor
"@hey-api/docs": patch
---

feat: remove enum postfix, use typescript enums in types when generated, export enums from types.gen.ts
14 changes: 9 additions & 5 deletions docs/openapi-ts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,34 @@ You can also prevent your client from being processed by linters by adding your

If you need to iterate through possible field values without manually typing arrays, you can export enums with

```js{2}
```js{5}
export default {
enums: 'javascript',
input: 'path/to/openapi.json',
output: 'src/client',
types: {
enums: 'javascript',
},
}
```

This will export enums as plain JavaScript objects. For example, `Foo` would become

```js
export const FooEnum = {
export const Foo = {
FOO: 'foo',
BAR: 'bar',
} as const;
```

We discourage generating [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 really need TypeScript enums, you can export them with

```js{2}
```js{5}
export default {
enums: 'typescript',
input: 'path/to/openapi.json',
output: 'src/client',
types: {
enums: 'typescript',
},
}
```

Expand Down
47 changes: 45 additions & 2 deletions docs/openapi-ts/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Currently, `index.ts` file exports all generated artifacts. We will be slowly mo
export { ApiError } from './core/ApiError';
export { CancelablePromise, CancelError } from './core/CancelablePromise';
export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI';
export * from './enums.gen'; // [!code --]
export * from './schemas.gen'; // [!code --]
export * from './services.gen'; // [!code --]
export * from './types.gen'; // [!code --]
Expand All @@ -28,7 +27,6 @@ export * from './types.gen'; // [!code --]
Any non-core related imports should be imported as

```js
import { Enum } from 'client/enums.gen'
import { $Schema } from 'client/schemas.gen';
import { DefaultService } from 'client/services.gen';
import type { Model } from 'client/types.gen';
Expand All @@ -52,6 +50,51 @@ This config option is deprecated and will be removed in favor of [clients](./cli

This config option is deprecated and will be removed in favor of [clients](./clients).

## v0.43.0

### Removed `enums.gen.ts`

This file has been removed. Instead, enums are exported from `types.gen.ts`. If you use imports from `enums.gen.ts`, you should be able to easily find and replace all instances.

```js
import { Foo } from 'client/enums.gen'; // [!code --]
import { Foo } from 'client/types.gen'; // [!code ++]
```

### Removed `Enum` postfix

Generated enum names are no longer postfixed with `Enum`. You can either alias your imports

```js
import { FooEnum } from 'client/types.gen'; // [!code --]
import { Foo as FooEnum } from 'client/types.gen'; // [!code ++]

console.log(FooEnum.value); // only import needs to be changed
```

or update all references to enums

```js
import { FooEnum } from 'client/types.gen'; // [!code --]
import { Foo } from 'client/types.gen'; // [!code ++]

console.log(Foo.value); // all references need to be changed
```

### Moved `enums`

This config option has been moved. You can now configure enums using the `types.enums` option.

```js{5}
export default {
input: 'path/to/openapi.json',
output: 'src/client',
types: {
enums: 'javascript',
},
}
```

## v0.42.0

### Changed `format`
Expand Down
3 changes: 1 addition & 2 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const getServices = (userConfig: UserConfig): Config['services'] => {
const getTypes = (userConfig: UserConfig): Config['types'] => {
let types: Config['types'] = {
dates: false,
enums: false,
export: true,
name: 'preserve',
};
Expand Down Expand Up @@ -253,7 +254,6 @@ const initConfig = async (
base,
debug = false,
dryRun = false,
enums = false,
exportCore = true,
format = false,
input,
Expand Down Expand Up @@ -296,7 +296,6 @@ const initConfig = async (
client,
debug,
dryRun,
enums,
exportCore: client.startsWith('@hey-api') ? false : exportCore,
format,
input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('getOperationName', () => {
client: 'fetch',
debug: false,
dryRun: true,
enums: false,
exportCore: false,
format: false,
input: '',
Expand All @@ -36,7 +35,6 @@ describe('getOperationName', () => {
client: 'fetch',
debug: false,
dryRun: true,
enums: false,
exportCore: false,
format: false,
input: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('getServices', () => {
client: 'fetch',
debug: false,
dryRun: true,
enums: false,
exportCore: true,
format: false,
input: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('getServices', () => {
client: 'fetch',
debug: false,
dryRun: true,
enums: false,
exportCore: true,
format: false,
input: '',
Expand Down
10 changes: 5 additions & 5 deletions packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ export interface UserConfig {
* @default false
*/
dryRun?: boolean;
/**
* Generate enum definitions?
* @default false
*/
enums?: 'javascript' | 'typescript' | false;
/**
* Generate core client classes?
* @default true
Expand Down Expand Up @@ -127,6 +122,11 @@ export interface UserConfig {
* @default false
*/
dates?: boolean;
/**
* Generate enum definitions?
* @default false
*/
enums?: 'javascript' | 'typescript' | false;
/**
* Generate types?
* @default true
Expand Down
10 changes: 6 additions & 4 deletions packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ describe('registerHandlebarHelpers', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: 'prettier',
input: '',
lint: false,
output: '',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: false,
});
registerHandlebarHelpers();
Expand All @@ -40,15 +41,16 @@ describe('registerHandlebarTemplates', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: 'prettier',
input: '',
lint: false,
output: '',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: false,
});
const templates = registerHandlebarTemplates();
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-ts/src/utils/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export const enumName = (client: Client, name?: string) => {
/[-_]([a-z])/gi,
($0, $1: string) => $1.toLocaleUpperCase(),
);
const result = `${escapedName.charAt(0).toLocaleUpperCase() + escapedName.slice(1)}Enum`;
const result =
escapedName.charAt(0).toLocaleUpperCase() + escapedName.slice(1);
if (client.enumNames.includes(result)) {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/openapi-ts/src/utils/write/__tests__/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('writeClientClass', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: false,
input: '',
Expand All @@ -24,7 +23,9 @@ describe('writeClientClass', () => {
output: '',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: true,
});

Expand Down
5 changes: 3 additions & 2 deletions packages/openapi-ts/src/utils/write/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ describe('writeClient', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: 'prettier',
input: '',
lint: false,
output: './dist',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: false,
});

Expand Down
15 changes: 9 additions & 6 deletions packages/openapi-ts/src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('writeCore', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: false,
input: '',
Expand All @@ -37,7 +36,9 @@ describe('writeCore', () => {
output: '',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: true,
});

Expand Down Expand Up @@ -82,7 +83,6 @@ describe('writeCore', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: false,
input: '',
Expand All @@ -91,7 +91,9 @@ describe('writeCore', () => {
output: '',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: true,
});

Expand Down Expand Up @@ -119,7 +121,6 @@ describe('writeCore', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: false,
input: '',
Expand All @@ -128,7 +129,9 @@ describe('writeCore', () => {
output: '',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: true,
});

Expand Down
9 changes: 3 additions & 6 deletions packages/openapi-ts/src/utils/write/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@ describe('processIndex', () => {
client: 'fetch',
debug: false,
dryRun: false,
enums: 'javascript',
exportCore: true,
format: false,
input: '',
lint: false,
output: '',
schemas: {},
services: {},
types: {},
types: {
enums: 'javascript',
},
useOptions: true,
});

const files: Parameters<typeof processIndex>[0]['files'] = {
enums: new TypeScriptFile({
dir: '/',
name: 'enums.ts',
}),
index: new TypeScriptFile({
dir: '/',
name: 'index.ts',
Expand Down
Loading

0 comments on commit 0ca8b75

Please sign in to comment.