Skip to content

Commit 0971e23

Browse files
authored
Merge pull request #1 from OskarAsplin/fix-add-transformModelCase
Fix case conversion for all fields
2 parents 6c68111 + 0ddbecc commit 0971e23

File tree

8 files changed

+59
-26
lines changed

8 files changed

+59
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ $ openapi --help
5151
--postfixServices Service name postfix (default: "Service")
5252
--postfixModels Model name postfix
5353
--request <value> Path to custom request file
54+
--transformCase <value> Transforms field names to specified case [camel, snake] (default: none)
5455
-h, --help display help for command
5556
5657
Examples

bin/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const params = program
2424
.option('--postfix <value>', 'Deprecated: Use --postfixServices instead. Service name postfix', 'Service')
2525
.option('--postfixServices <value>', 'Service name postfix', 'Service')
2626
.option('--postfixModels <value>', 'Model name postfix')
27-
.option('--transformModelCase <value>', 'Transform model case [camel, snake]', 'none')
27+
.option('--transformCase <value>', 'Transforms field names to specified case [camel, snake]', 'none')
2828
.option('--request <value>', 'Path to custom request file')
2929
.parse(process.argv)
3030
.opts();
@@ -46,7 +46,7 @@ if (OpenAPI) {
4646
indent: params.indent,
4747
postfixServices: params.postfixServices ?? params.postfix,
4848
postfixModels: params.postfixModels,
49-
transformModelCase: params.transformModelCase,
49+
transformCase: params.transformCase,
5050
request: params.request,
5151
})
5252
.then(() => {

src/Case.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { Enum } from './client/interfaces/Enum';
12
import { Model } from './client/interfaces/Model';
3+
import { OperationResponse } from './client/interfaces/OperationResponse';
4+
import { Service } from './client/interfaces/Service';
25

36
export enum Case {
47
NONE = 'none',
58
CAMEL = 'camel',
69
SNAKE = 'snake',
710
}
8-
// Convert a string from snake case or pascal case to camel case.
11+
// Convert a string from snake case to camel case.
912
const toCamelCase = (str: string): string => {
1013
return str.replace(/_([a-z])/g, match => match[1].toUpperCase());
1114
};
@@ -22,17 +25,30 @@ const transforms = {
2225

2326
// A recursive function that looks at the models and their properties and
2427
// converts each property name using the provided transform function.
25-
export const convertModelNames = (model: Model, type: Exclude<Case, Case.NONE>): Model => {
26-
if (!model.properties.length) {
27-
return {
28-
...model,
29-
name: transforms[type](model.name),
30-
};
31-
}
28+
export const convertModelNames = <T extends Model | OperationResponse>(model: T, type: Exclude<Case, Case.NONE>): T => {
3229
return {
3330
...model,
34-
properties: model.properties.map(property => {
35-
return convertModelNames(property, type);
36-
}),
31+
name: transforms[type](model.name),
32+
link: model.link ? convertModelNames(model.link, type) : null,
33+
enum: model.enum.map(modelEnum => convertEnumName(modelEnum, type)),
34+
enums: model.enums.map(property => convertModelNames(property, type)),
35+
properties: model.properties.map(property => convertModelNames(property, type)),
36+
};
37+
};
38+
39+
const convertEnumName = (modelEnum: Enum, type: Exclude<Case, Case.NONE>): Enum => {
40+
return {
41+
...modelEnum,
42+
name: transforms[type](modelEnum.name),
43+
};
44+
};
45+
46+
export const convertServiceCase = (service: Service, type: Exclude<Case, Case.NONE>): Service => {
47+
return {
48+
...service,
49+
operations: service.operations.map(op => ({
50+
...op,
51+
results: op.results.map(results => convertModelNames(results, type)),
52+
})),
3753
};
3854
};

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type Options = {
2727
indent?: Indent;
2828
postfixServices?: string;
2929
postfixModels?: string;
30-
transformModelCase?: Case;
30+
transformCase?: Case;
3131
request?: string;
3232
write?: boolean;
3333
};
@@ -49,7 +49,7 @@ export type Options = {
4949
* @param indent Indentation options (4, 2 or tab)
5050
* @param postfixServices Service name postfix
5151
* @param postfixModels Model name postfix
52-
* @param transformModelCase Transform model case (camel, snake)
52+
* @param transformCase Transform case (camel, snake)
5353
* @param request Path to custom request file
5454
* @param write Write the files to disk (true or false)
5555
*/
@@ -67,7 +67,7 @@ export const generate = async ({
6767
indent = Indent.SPACE_4,
6868
postfixServices = 'Service',
6969
postfixModels = '',
70-
transformModelCase = Case.NONE,
70+
transformCase = Case.NONE,
7171
request,
7272
write = true,
7373
}: Options): Promise<void> => {
@@ -98,7 +98,7 @@ export const generate = async ({
9898
indent,
9999
postfixServices,
100100
postfixModels,
101-
transformModelCase,
101+
transformCase,
102102
clientName,
103103
request
104104
);
@@ -123,7 +123,7 @@ export const generate = async ({
123123
indent,
124124
postfixServices,
125125
postfixModels,
126-
transformModelCase,
126+
transformCase,
127127
clientName,
128128
request
129129
);

src/utils/writeClient.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { writeClientServices } from './writeClientServices';
3131
* @param indent Indentation options (4, 2 or tab)
3232
* @param postfixServices Service name postfix
3333
* @param postfixModels Model name postfix
34-
* @param transformModelCase Transform model case (camel, snake)
34+
* @param transformCase Transform model case (camel, snake)
3535
* @param clientName Custom client class name
3636
* @param request Path to custom request file
3737
*/
@@ -49,7 +49,7 @@ export const writeClient = async (
4949
indent: Indent,
5050
postfixServices: string,
5151
postfixModels: string,
52-
transformModelCase: Case,
52+
transformCase: Case,
5353
clientName?: string,
5454
request?: string
5555
): Promise<void> => {
@@ -81,6 +81,7 @@ export const writeClient = async (
8181
useOptions,
8282
indent,
8383
postfixServices,
84+
transformCase,
8485
clientName
8586
);
8687
}
@@ -101,7 +102,7 @@ export const writeClient = async (
101102
httpClient,
102103
useUnionTypes,
103104
indent,
104-
transformModelCase
105+
transformCase
105106
);
106107
}
107108

src/utils/writeClientModels.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { Templates } from './registerHandlebarTemplates';
1717
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
1818
* @param useUnionTypes Use union types instead of enums
1919
* @param indent Indentation options (4, 2 or tab)
20-
* @param transformModelCase Transform model case (camel, snake)
20+
* @param transformCase Transform model case (camel, snake)
2121
*/
2222
export const writeClientModels = async (
2323
models: Model[],
@@ -26,10 +26,10 @@ export const writeClientModels = async (
2626
httpClient: HttpClient,
2727
useUnionTypes: boolean,
2828
indent: Indent,
29-
transformModelCase: Case
29+
transformCase: Case
3030
): Promise<void> => {
3131
for (const model of models) {
32-
const newModel = transformModelCase === Case.NONE ? model : convertModelNames(model, transformModelCase);
32+
const newModel = transformCase === Case.NONE ? model : convertModelNames(model, transformCase);
3333
const file = resolve(outputPath, `${model.name}.ts`);
3434
const templateResult = templates.exports.model({
3535
...newModel,

src/utils/writeClientServices.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EOL } from 'os';
22

3+
import { Case } from '../Case';
34
import type { Service } from '../client/interfaces/Service';
45
import { HttpClient } from '../HttpClient';
56
import { Indent } from '../Indent';
@@ -39,7 +40,17 @@ describe('writeClientServices', () => {
3940
},
4041
};
4142

42-
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service');
43+
await writeClientServices(
44+
services,
45+
templates,
46+
'/',
47+
HttpClient.FETCH,
48+
false,
49+
false,
50+
Indent.SPACE_4,
51+
'Service',
52+
Case.NONE
53+
);
4354

4455
expect(writeFile).toBeCalledWith('/UserService.ts', `service${EOL}`);
4556
});

src/utils/writeClientServices.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve } from 'path';
22

3+
import { Case, convertServiceCase } from '../Case';
34
import type { Service } from '../client/interfaces/Service';
45
import type { HttpClient } from '../HttpClient';
56
import type { Indent } from '../Indent';
@@ -19,6 +20,7 @@ import type { Templates } from './registerHandlebarTemplates';
1920
* @param useOptions Use options or arguments functions
2021
* @param indent Indentation options (4, 2 or tab)
2122
* @param postfix Service name postfix
23+
* @param transformCase Transform model case (camel, snake)
2224
* @param clientName Custom client class name
2325
*/
2426
export const writeClientServices = async (
@@ -30,12 +32,14 @@ export const writeClientServices = async (
3032
useOptions: boolean,
3133
indent: Indent,
3234
postfix: string,
35+
transformCase: Case,
3336
clientName?: string
3437
): Promise<void> => {
3538
for (const service of services) {
39+
const newService = transformCase === Case.NONE ? service : convertServiceCase(service, transformCase);
3640
const file = resolve(outputPath, `${service.name}${postfix}.ts`);
3741
const templateResult = templates.exports.service({
38-
...service,
42+
...newService,
3943
httpClient,
4044
useUnionTypes,
4145
useOptions,

0 commit comments

Comments
 (0)