Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added useLegacyEnums option #144

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ $ openapi-ts --help
--postfixModels Model name postfix
--request <value> Path to custom request file
--useDateType <value> Output Date instead of string for the format "date-time" in the models (default: false)
--useLegacyEnums Generate Typescript enum definitions (default: false)
-h, --help display help for command
```

Expand Down
1 change: 1 addition & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const params = program
.option('--request <value>', 'Path to custom request file')
.option('--write', 'Write files to disk? (used for testing)')
.option('--no-write', 'Skip writing files to disk (used for testing)')
.option('--useLegacyEnums', 'Generate Typescript enum definitions')
.parse(process.argv)
.opts();

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
useDateType = false,
useOptions = true,
write = true,
useLegacyEnums = false,
} = userConfig;

if (!input) {
Expand Down Expand Up @@ -158,6 +159,7 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
serviceResponse,
useDateType,
useOptions,
useLegacyEnums,
write,
};

Expand Down
1 change: 1 addition & 0 deletions src/openApi/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('parse', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: false,
};
Expand Down
1 change: 1 addition & 0 deletions src/openApi/v2/parser/__tests__/getServices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('getServices', () => {
useDateType: false,
useOptions: true,
write: false,
useLegacyEnums: false,
};
const services = getServices(
{
Expand Down
1 change: 1 addition & 0 deletions src/openApi/v3/parser/__tests__/getServices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('getServices', () => {
postfixServices: 'Service',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: false,
};
Expand Down
18 changes: 17 additions & 1 deletion src/templates/partials/exportEnum.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,25 @@
{{/if}}
*/
{{/ifdef}}
{{#if @root.$config.useLegacyEnums}}
export enum {{{name}}} {
{{#each enum}}
{{#if x-enum-description}}
/**
* {{{escapeComment x-enum-description}}}
*/
{{else if description}}
/**
* {{{escapeComment description}}}
*/
{{/if}}
{{{enumKey value x-enum-varname}}} = {{{enumValue value}}},
{{/each}}
}
{{else}}
export type {{{name}}} = {{{enumUnionType enum}}};
{{/if}}

{{/if}}
{{#if @root.$config.enums}}
Copy link
Collaborator

@jordanshatford jordanshatford Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add back one newline above this (below {{/if}}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlankamp Please add back one newline, run npm run test:update and then add the changeset as mentioned above, I cannot modify this PR. Then I will merge and release

export const {{{enumName name }}} = {
{{#each enum}}
Expand Down
5 changes: 5 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export interface UserConfig {
* @default true
*/
useOptions?: boolean;
/**
* Generate Typescript enum definitions
* @default false
*/
useLegacyEnums?: boolean;
/**
* Write the files to disk (true or false)
* @default true
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('registerHandlebarHelpers', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: false,
write: true,
},
Expand Down Expand Up @@ -75,6 +76,7 @@ describe('registerHandlebarTemplates', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: false,
write: true,
},
Expand Down
1 change: 1 addition & 0 deletions src/utils/write/__tests__/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('writeClientClass', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: true,
});
Expand Down
1 change: 1 addition & 0 deletions src/utils/write/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('writeClient', () => {
postfixServices: 'Service',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: false,
write: true,
});
Expand Down
3 changes: 3 additions & 0 deletions src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('writeClientCore', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: true,
};
Expand Down Expand Up @@ -79,6 +80,7 @@ describe('writeClientCore', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: true,
};
Expand Down Expand Up @@ -119,6 +121,7 @@ describe('writeClientCore', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: true,
};
Expand Down
1 change: 1 addition & 0 deletions src/utils/write/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('writeClientIndex', () => {
postfixServices: 'Service',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: true,
});
Expand Down
1 change: 1 addition & 0 deletions src/utils/write/__tests__/models.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('writeClientModels', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: true,
});
Expand Down
1 change: 1 addition & 0 deletions src/utils/write/__tests__/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('writeClientSchemas', () => {
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: true,
write: true,
});
Expand Down
1 change: 1 addition & 0 deletions src/utils/write/__tests__/services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('writeClientServices', () => {
postfixServices: 'Service',
serviceResponse: 'body',
useDateType: false,
useLegacyEnums: false,
useOptions: false,
write: true,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This is a simple enum with numbers
*/

export type EnumWithExtensions = 200 | 400 | 500;

export const EnumWithExtensionsEnum = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This is a simple enum with numbers
*/

export type EnumWithNumbers = 1 | 2 | 3 | 1.1 | 1.2 | 1.3 | 100 | 200 | 300 | -100 | -200 | -300 | -1.1 | -1.2 | -1.3;

export const EnumWithNumbersEnum = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This is a simple enum with strings
*/

export type EnumWithStrings =
| 'Success'
| 'Warning'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This is a simple enum with numbers
*/

export type EnumWithExtensions = 200 | 400 | 500;

export const EnumWithExtensionsEnum = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This is a simple enum with numbers
*/

export type EnumWithNumbers = 1 | 2 | 3 | 1.1 | 1.2 | 1.3 | 100 | 200 | 300 | -100 | -200 | -300 | -1.1 | -1.2 | -1.3;

export const EnumWithNumbersEnum = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This is a simple enum with strings
*/

export type EnumWithStrings =
| 'Success'
| 'Warning'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ApiRequestOptions } from './ApiRequestOptions';
import type { ApiResult } from './ApiResult';

export class ApiError extends Error {
public readonly url: string;
public readonly status: number;
public readonly statusText: string;
public readonly body: unknown;
public readonly request: ApiRequestOptions;

constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
super(message);

this.name = 'ApiError';
this.url = response.url;
this.status = response.status;
this.statusText = response.statusText;
this.body = response.body;
this.request = request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type ApiRequestOptions = {
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
readonly url: string;
readonly path?: Record<string, unknown>;
readonly cookies?: Record<string, unknown>;
readonly headers?: Record<string, unknown>;
readonly query?: Record<string, unknown>;
readonly formData?: Record<string, unknown>;
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ApiResult<TData = any> = {
readonly body: TData;
readonly ok: boolean;
readonly status: number;
readonly statusText: string;
readonly url: string;
};
Loading