Skip to content

Commit cfdd547

Browse files
committed
Merge branch 'add-transformModelCase' into feature/omit-read-only
2 parents 229fd7c + fd04fd0 commit cfdd547

File tree

8 files changed

+46
-16
lines changed

8 files changed

+46
-16
lines changed

README.md

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

bin/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const params = program
2323
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
2424
.option('--postfixServices <value>', 'Service name postfix', 'Service')
2525
.option('--postfixModels <value>', 'Model name postfix')
26-
.option('--transformModelCase <value>', 'Transform model case [camel, snake]', 'none')
26+
.option('--transformCase <value>', 'Transforms field names to specified case [camel, snake]', 'none')
2727
.option('--request <value>', 'Path to custom request file')
2828
.parse(process.argv)
2929
.opts();
@@ -45,7 +45,7 @@ if (OpenAPI) {
4545
indent: params.indent,
4646
postfixServices: params.postfixServices,
4747
postfixModels: params.postfixModels,
48-
transformModelCase: params.transformModelCase,
48+
transformCase: params.transformCase,
4949
request: params.request,
5050
})
5151
.then(() => {

src/Case.ts

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

46
export enum Case {
57
NONE = 'none',
68
CAMEL = 'camel',
79
SNAKE = 'snake',
810
}
9-
// Convert a string from snake case or pascal case to camel case.
11+
// Convert a string from snake case to camel case.
1012
const toCamelCase = (str: string): string => {
1113
return str.replace(/_([a-z])/g, match => match[1].toUpperCase());
1214
};
@@ -23,7 +25,7 @@ const transforms = {
2325

2426
// A recursive function that looks at the models and their properties and
2527
// converts each property name using the provided transform function.
26-
export const convertModelNames = (model: Model, type: Exclude<Case, Case.NONE>): Model => {
28+
export const convertModelNames = <T extends Model | OperationResponse>(model: T, type: Exclude<Case, Case.NONE>): T => {
2729
return {
2830
...model,
2931
name: transforms[type](model.name),
@@ -40,3 +42,13 @@ const convertEnumName = (modelEnum: Enum, type: Exclude<Case, Case.NONE>): Enum
4042
name: transforms[type](modelEnum.name),
4143
};
4244
};
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+
})),
53+
};
54+
};

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> => {
@@ -91,6 +91,7 @@ export const writeClient = async (
9191
useOptions,
9292
indent,
9393
postfixServices,
94+
transformCase,
9495
clientName
9596
);
9697
}
@@ -111,7 +112,7 @@ export const writeClient = async (
111112
httpClient,
112113
useUnionTypes,
113114
indent,
114-
transformModelCase
115+
transformCase
115116
);
116117
}
117118

src/utils/writeClientModels.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +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 transformCase Transform model case (camel, snake)
2021
*/
2122
export const writeClientModels = async (
2223
models: Model[],
@@ -25,10 +26,10 @@ export const writeClientModels = async (
2526
httpClient: HttpClient,
2627
useUnionTypes: boolean,
2728
indent: Indent,
28-
transformModelCase: Case
29+
transformCase: Case
2930
): Promise<void> => {
3031
for (const model of models) {
31-
const newModel = transformModelCase === Case.NONE ? model : convertModelNames(model, transformModelCase);
32+
const newModel = transformCase === Case.NONE ? model : convertModelNames(model, transformCase);
3233
const file = resolve(outputPath, `${model.name}.ts`);
3334
const templateResult = templates.exports.model({
3435
...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';
@@ -42,7 +43,17 @@ describe('writeClientServices', () => {
4243
},
4344
};
4445

45-
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service');
46+
await writeClientServices(
47+
services,
48+
templates,
49+
'/',
50+
HttpClient.FETCH,
51+
false,
52+
false,
53+
Indent.SPACE_4,
54+
'Service',
55+
Case.NONE
56+
);
4657

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

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)