Skip to content

Commit 262a1bc

Browse files
OskarAsplinrobdodson
authored andcommitted
Fix case conversion for service operation response body
1 parent 07edda9 commit 262a1bc

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

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/utils/writeClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export const writeClient = async (
8181
useOptions,
8282
indent,
8383
postfixServices,
84+
transformCase,
8485
clientName
8586
);
8687
}

src/utils/writeClientServices.spec.ts

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

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

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

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

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)