Skip to content

Fixes #1252. Adds --transformCase flag. #1465

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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 @@ -50,6 +50,7 @@ $ openapi --help
--postfixServices Service name postfix (default: "Service")
--postfixModels Model name postfix
--request <value> Path to custom request file
--transformCase <value> Transforms field names to specified case [camel, snake] (default: none)
-h, --help display help for command

Examples
Expand Down
2 changes: 2 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const params = program
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
.option('--postfixServices <value>', 'Service name postfix', 'Service')
.option('--postfixModels <value>', 'Model name postfix')
.option('--transformCase <value>', 'Transforms field names to specified case [camel, snake]', 'none')
.option('--request <value>', 'Path to custom request file')
.parse(process.argv)
.opts();
Expand All @@ -44,6 +45,7 @@ if (OpenAPI) {
indent: params.indent,
postfixServices: params.postfixServices,
postfixModels: params.postfixModels,
transformCase: params.transformCase,
request: params.request,
})
.then(() => {
Expand Down
54 changes: 54 additions & 0 deletions src/Case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Enum } from './client/interfaces/Enum';
import { Model } from './client/interfaces/Model';
import { OperationResponse } from './client/interfaces/OperationResponse';
import { Service } from './client/interfaces/Service';

export enum Case {
NONE = 'none',
CAMEL = 'camel',
SNAKE = 'snake',
}
// Convert a string from snake case to camel case.
const toCamelCase = (str: string): string => {
return str.replace(/_([a-z0-9])/g, match => match[1].toUpperCase());
};

// Convert a string from camel case or pascal case to snake case.
const toSnakeCase = (str: string): string => {
return str.replace(/([A-Z])/g, match => `_${match.toLowerCase()}`);
};

const transforms = {
[Case.CAMEL]: toCamelCase,
[Case.SNAKE]: toSnakeCase,
};

// A recursive function that looks at the models and their properties and
// converts each property name using the provided transform function.
export const convertModelCase = <T extends Model | OperationResponse>(model: T, type: Exclude<Case, Case.NONE>): T => {
return {
...model,
name: transforms[type](model.name),
link: model.link ? convertModelCase(model.link, type) : null,
enum: model.enum.map(modelEnum => convertEnumCase(modelEnum, type)),
enums: model.enums.map(property => convertModelCase(property, type)),
properties: model.properties.map(property => convertModelCase(property, type)),
};
};

const convertEnumCase = (modelEnum: Enum, type: Exclude<Case, Case.NONE>): Enum => {
return {
...modelEnum,
name: transforms[type](modelEnum.name),
};
};

export const convertServiceCase = (service: Service, type: Exclude<Case, Case.NONE>): Service => {
return {
...service,
operations: service.operations.map(op => ({
...op,
results: op.results.map(results => convertModelCase(results, type)),
})),
};
};
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Case } from './Case';
import { HttpClient } from './HttpClient';
import { Indent } from './Indent';
import { parse as parseV2 } from './openApi/v2';
Expand Down Expand Up @@ -26,6 +27,7 @@ export type Options = {
indent?: Indent;
postfixServices?: string;
postfixModels?: string;
transformCase?: Case;
request?: string;
write?: boolean;
};
Expand All @@ -47,6 +49,7 @@ export type Options = {
* @param indent Indentation options (4, 2 or tab)
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
* @param transformCase Transform case (camel, snake)
* @param request Path to custom request file
* @param write Write the files to disk (true or false)
*/
Expand All @@ -64,6 +67,7 @@ export const generate = async ({
indent = Indent.SPACE_4,
postfixServices = 'Service',
postfixModels = '',
transformCase = Case.NONE,
request,
write = true,
}: Options): Promise<void> => {
Expand Down Expand Up @@ -94,6 +98,7 @@ export const generate = async ({
indent,
postfixServices,
postfixModels,
transformCase,
clientName,
request
);
Expand All @@ -118,6 +123,7 @@ export const generate = async ({
indent,
postfixServices,
postfixModels,
transformCase,
clientName,
request
);
Expand Down
4 changes: 3 additions & 1 deletion src/utils/writeClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Case } from '../Case';
import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
Expand Down Expand Up @@ -49,7 +50,8 @@ describe('writeClient', () => {
true,
Indent.SPACE_4,
'Service',
'AppClient'
'AppClient',
Case.NONE
);

expect(rmdir).toBeCalled();
Expand Down
14 changes: 13 additions & 1 deletion src/utils/writeClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from 'path';

import type { Client } from '../client/interfaces/Client';
import { Case } from '../Case';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { mkdir, rmdir } from './fileSystem';
Expand Down Expand Up @@ -30,6 +31,7 @@ import { writeClientServices } from './writeClientServices';
* @param indent Indentation options (4, 2 or tab)
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
* @param transformCase Transform model case (camel, snake)
* @param clientName Custom client class name
* @param request Path to custom request file
*/
Expand All @@ -47,6 +49,7 @@ export const writeClient = async (
indent: Indent,
postfixServices: string,
postfixModels: string,
transformCase: Case,
clientName?: string,
request?: string
): Promise<void> => {
Expand Down Expand Up @@ -78,6 +81,7 @@ export const writeClient = async (
useOptions,
indent,
postfixServices,
transformCase,
clientName
);
}
Expand All @@ -91,7 +95,15 @@ export const writeClient = async (
if (exportModels) {
await rmdir(outputPathModels);
await mkdir(outputPathModels);
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
await writeClientModels(
client.models,
templates,
outputPathModels,
httpClient,
useUnionTypes,
indent,
transformCase
);
}

if (isDefined(clientName)) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/writeClientModels.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EOL } from 'os';
import { resolve } from 'path';

import { Case } from '../Case';
import type { Model } from '../client/interfaces/Model';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
Expand Down Expand Up @@ -52,7 +53,7 @@ describe('writeClientModels', () => {
},
};

await writeClientModels(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4);
await writeClientModels(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4, Case.NONE);

expect(writeFile).toBeCalledWith(resolve('/', '/User.ts'), `model${EOL}`);
});
Expand Down
8 changes: 6 additions & 2 deletions src/utils/writeClientModels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from 'path';

import type { Model } from '../client/interfaces/Model';
import { Case, convertModelCase } from '../Case';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { writeFile } from './fileSystem';
Expand All @@ -16,19 +17,22 @@ import type { Templates } from './registerHandlebarTemplates';
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
* @param useUnionTypes Use union types instead of enums
* @param indent Indentation options (4, 2 or tab)
* @param transformCase Transform model case (camel, snake)
*/
export const writeClientModels = async (
models: Model[],
templates: Templates,
outputPath: string,
httpClient: HttpClient,
useUnionTypes: boolean,
indent: Indent
indent: Indent,
transformCase: Case
): Promise<void> => {
for (const model of models) {
const newModel = transformCase === Case.NONE ? model : convertModelCase(model, transformCase);
const file = resolve(outputPath, `${model.name}.ts`);
const templateResult = templates.exports.model({
...model,
...newModel,
httpClient,
useUnionTypes,
});
Expand Down
13 changes: 12 additions & 1 deletion src/utils/writeClientServices.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EOL } from 'os';
import { resolve } from 'path';

import { Case } from '../Case';
import type { Service } from '../client/interfaces/Service';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
Expand Down Expand Up @@ -40,7 +41,17 @@ describe('writeClientServices', () => {
},
};

await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service');
await writeClientServices(
services,
templates,
'/',
HttpClient.FETCH,
false,
false,
Indent.SPACE_4,
'Service',
Case.NONE
);

expect(writeFile).toBeCalledWith(resolve('/', '/UserService.ts'), `service${EOL}`);
});
Expand Down
6 changes: 5 additions & 1 deletion src/utils/writeClientServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'path';

import { Case, convertServiceCase } from '../Case';
import type { Service } from '../client/interfaces/Service';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
Expand All @@ -19,6 +20,7 @@ import type { Templates } from './registerHandlebarTemplates';
* @param useOptions Use options or arguments functions
* @param indent Indentation options (4, 2 or tab)
* @param postfix Service name postfix
* @param transformCase Transform model case (camel, snake)
* @param clientName Custom client class name
*/
export const writeClientServices = async (
Expand All @@ -30,12 +32,14 @@ export const writeClientServices = async (
useOptions: boolean,
indent: Indent,
postfix: string,
transformCase: Case,
clientName?: string
): Promise<void> => {
for (const service of services) {
const newService = transformCase === Case.NONE ? service : convertServiceCase(service, transformCase);
const file = resolve(outputPath, `${service.name}${postfix}.ts`);
const templateResult = templates.exports.service({
...service,
...newService,
httpClient,
useUnionTypes,
useOptions,
Expand Down