Skip to content

Fix case conversion for all fields #1

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

Merged
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 @@ -51,6 +51,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
4 changes: 2 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const params = program
.option('--postfix <value>', 'Deprecated: Use --postfixServices instead. Service name postfix', 'Service')
.option('--postfixServices <value>', 'Service name postfix', 'Service')
.option('--postfixModels <value>', 'Model name postfix')
.option('--transformModelCase <value>', 'Transform model case [camel, snake]', 'none')
.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 @@ -46,7 +46,7 @@ if (OpenAPI) {
indent: params.indent,
postfixServices: params.postfixServices ?? params.postfix,
postfixModels: params.postfixModels,
transformModelCase: params.transformModelCase,
transformCase: params.transformCase,
request: params.request,
})
.then(() => {
Expand Down
38 changes: 27 additions & 11 deletions src/Case.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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 or pascal case to camel case.
// Convert a string from snake case to camel case.
const toCamelCase = (str: string): string => {
return str.replace(/_([a-z])/g, match => match[1].toUpperCase());
};
Expand All @@ -22,17 +25,30 @@ const transforms = {

// A recursive function that looks at the models and their properties and
// converts each property name using the provided transform function.
export const convertModelNames = (model: Model, type: Exclude<Case, Case.NONE>): Model => {
if (!model.properties.length) {
return {
...model,
name: transforms[type](model.name),
};
}
export const convertModelNames = <T extends Model | OperationResponse>(model: T, type: Exclude<Case, Case.NONE>): T => {
Copy link
Owner

Choose a reason for hiding this comment

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

@OskarAsplin What do you think about renaming all of these functions to:

  • convertModelCase
  • convertEnumCase
  • convertServiceCase

so they are consistent?

Copy link
Owner

Choose a reason for hiding this comment

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

I'm going to go ahead and merge and update them in my PR

Copy link
Author

Choose a reason for hiding this comment

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

I like it, thanks!

return {
...model,
properties: model.properties.map(property => {
return convertModelNames(property, type);
}),
name: transforms[type](model.name),
link: model.link ? convertModelNames(model.link, type) : null,
enum: model.enum.map(modelEnum => convertEnumName(modelEnum, type)),
enums: model.enums.map(property => convertModelNames(property, type)),
properties: model.properties.map(property => convertModelNames(property, type)),
};
};

const convertEnumName = (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 => convertModelNames(results, type)),
})),
};
};
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type Options = {
indent?: Indent;
postfixServices?: string;
postfixModels?: string;
transformModelCase?: Case;
transformCase?: Case;
request?: string;
write?: boolean;
};
Expand All @@ -49,7 +49,7 @@ export type Options = {
* @param indent Indentation options (4, 2 or tab)
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
* @param transformModelCase Transform model case (camel, snake)
* @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 @@ -67,7 +67,7 @@ export const generate = async ({
indent = Indent.SPACE_4,
postfixServices = 'Service',
postfixModels = '',
transformModelCase = Case.NONE,
transformCase = Case.NONE,
request,
write = true,
}: Options): Promise<void> => {
Expand Down Expand Up @@ -98,7 +98,7 @@ export const generate = async ({
indent,
postfixServices,
postfixModels,
transformModelCase,
transformCase,
clientName,
request
);
Expand All @@ -123,7 +123,7 @@ export const generate = async ({
indent,
postfixServices,
postfixModels,
transformModelCase,
transformCase,
clientName,
request
);
Expand Down
7 changes: 4 additions & 3 deletions src/utils/writeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +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 transformModelCase Transform model case (camel, snake)
* @param transformCase Transform model case (camel, snake)
* @param clientName Custom client class name
* @param request Path to custom request file
*/
Expand All @@ -49,7 +49,7 @@ export const writeClient = async (
indent: Indent,
postfixServices: string,
postfixModels: string,
transformModelCase: Case,
transformCase: Case,
clientName?: string,
request?: string
): Promise<void> => {
Expand Down Expand Up @@ -81,6 +81,7 @@ export const writeClient = async (
useOptions,
indent,
postfixServices,
transformCase,
clientName
);
}
Expand All @@ -101,7 +102,7 @@ export const writeClient = async (
httpClient,
useUnionTypes,
indent,
transformModelCase
transformCase
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/writeClientModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 transformModelCase Transform model case (camel, snake)
* @param transformCase Transform model case (camel, snake)
*/
export const writeClientModels = async (
models: Model[],
Expand All @@ -26,10 +26,10 @@ export const writeClientModels = async (
httpClient: HttpClient,
useUnionTypes: boolean,
indent: Indent,
transformModelCase: Case
transformCase: Case
): Promise<void> => {
for (const model of models) {
const newModel = transformModelCase === Case.NONE ? model : convertModelNames(model, transformModelCase);
const newModel = transformCase === Case.NONE ? model : convertModelNames(model, transformCase);
const file = resolve(outputPath, `${model.name}.ts`);
const templateResult = templates.exports.model({
...newModel,
Expand Down
13 changes: 12 additions & 1 deletion src/utils/writeClientServices.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EOL } from 'os';

import { Case } from '../Case';
import type { Service } from '../client/interfaces/Service';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
Expand Down Expand Up @@ -39,7 +40,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('/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