Skip to content

Commit 7c88543

Browse files
committed
feat: Consistent identifier names
1 parent 4685367 commit 7c88543

28 files changed

Lines changed: 135 additions & 406 deletions

example/generated/SwaggerPetstoreClient.generated.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,21 @@ export class SwaggerPetstoreClient {
289289
}
290290

291291
public async deletePet(
292-
args: { api_key?: string; petId: number },
292+
args: { apiKey?: string; petId: number },
293293
options?: RequestInit
294294
): Promise<
295295
ResponseWithData<400, undefined> | ResponseWithData<404, undefined>
296296
> {
297-
const { api_key, petId } = args;
297+
const { apiKey, petId } = args;
298298

299299
const method = 'DELETE';
300300
const url = `${this.baseUrl}/pet/${petId}`;
301301

302302
const response = await fetch(url, {
303303
method,
304304
headers: {
305-
...(typeof api_key !== 'undefined' && api_key !== null
306-
? { ['api_key']: api_key }
305+
...(typeof apiKey !== 'undefined' && apiKey !== null
306+
? { ['api_key']: apiKey }
307307
: {})
308308
},
309309
...options

example/generated/SwaggerPetstoreStubs.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type UpdatePetWithFormRequestPath = { petId: number };
2727

2828
export type UpdatePetWithFormRequestBody = { name?: string; status?: string };
2929

30-
export type DeletePetRequestHeader = { api_key?: string };
30+
export type DeletePetRequestHeader = { apiKey?: string };
3131

3232
export type DeletePetRequestPath = { petId: number };
3333

src/generateCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { assertNever } from './helpers/assertNever';
66
import { initUpper } from './helpers/initUpper';
77
import { load } from './helpers/load';
88
import { writeFile } from './helpers/writeFile';
9-
import { serviceNameTemplate } from './templates';
9+
import { makeIdentifier } from './templates/makeIdentifier';
1010
import { GenerateCodeOptions } from './types/GenerateCodeOptions';
1111
import * as OpenApi from './types/OpenApi';
1212

@@ -28,7 +28,7 @@ export const generateCode = async (
2828
)) as unknown) as OpenApi.Document;
2929

3030
const serviceName =
31-
inputOptions.serviceName ?? serviceNameTemplate(openApiDocument.info.title);
31+
inputOptions.serviceName ?? makeIdentifier(openApiDocument.info.title);
3232

3333
const filename = (() => {
3434
const outputTypeName = initUpper(

src/generators/stubs/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { generateServerTypes } from './server-types';
88
import { initLower } from '../../helpers/initLower';
99
import { fromV2 } from './v2';
1010
import { fromV3 } from './v3';
11-
import { safeName } from '../../templates/safeName';
11+
import {
12+
makeIdentifier,
13+
IdentifierFormat
14+
} from '../../templates/makeIdentifier';
1215

1316
export const generateStubs = ({
1417
logger,
@@ -126,7 +129,10 @@ export const generateStubs = ({
126129
${data
127130
.map(
128131
({ operationId, methodStubType }) =>
129-
`readonly ${safeName(operationId)}: ${methodStubType};`
132+
`readonly ${makeIdentifier(
133+
operationId,
134+
IdentifierFormat.camelCase
135+
)}: ${methodStubType};`
130136
)
131137
.join('\n')}
132138
};
@@ -145,16 +151,23 @@ export const generateStubs = ({
145151
const reset = () => {
146152
methodStubs = {
147153
${data
148-
.map(({ operationId }) => `${safeName(operationId)}: methodStub()`)
154+
.map(
155+
({ operationId }) =>
156+
`${makeIdentifier(
157+
operationId,
158+
IdentifierFormat.camelCase
159+
)}: methodStub()`
160+
)
149161
.join(',\n')}
150162
};
151163
152164
currentRouter = Router()
153165
${data
154166
.map(
155167
({ method, operationId, requestHandlerPath }) =>
156-
`.${method}('${requestHandlerPath}', methodStubs.${safeName(
157-
operationId
168+
`.${method}('${requestHandlerPath}', methodStubs.${makeIdentifier(
169+
operationId,
170+
IdentifierFormat.camelCase
158171
)}.requestHandler)`
159172
)
160173
.join('\n')}

src/generators/stubs/server-types/jsonSchema.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { JSONSchema7 } from 'json-schema';
22
import { LogFn, progress } from '../../../lib/cli-logging';
33
import { typeDefForSchema } from '../../../helpers/json-schema';
4-
import { initUpper } from '../../../helpers/initUpper';
5-
import { safeName } from '../../../templates/safeName';
4+
import { makeIdentifier } from '../../../templates/makeIdentifier';
65

76
export const fromJsonSchema = (
87
jsonSchema: JSONSchema7,
@@ -22,7 +21,7 @@ export const fromJsonSchema = (
2221

2322
log?.(progress(typeName));
2423

25-
return `export type ${initUpper(safeName(typeName))} = ${typeDefForSchema(
24+
return `export type ${makeIdentifier(typeName)} = ${typeDefForSchema(
2625
definition,
2726
options
2827
)};`;
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { safeName } from '../../templates/safeName';
2-
import { initUpper } from '../initUpper';
1+
import { makeIdentifier } from '../../templates/makeIdentifier';
32

43
export const typeDefForReference = ($ref: string): string => {
54
const name = $ref.substring($ref.lastIndexOf('/') + 1);
6-
return initUpper(safeName(name));
5+
return makeIdentifier(name);
76
};

src/helpers/open-api/typeName.spec.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/helpers/open-api/v2/typeDefForSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as OpenApiV2 from '../../../types/OpenApiV2';
22
import { typeDefForObject } from './typeDefForObject';
33
import { typeDefForReference } from './typeDefForReference';
44
import { typeDefForEnum } from './typeDefForEnum';
5-
import { typeName } from '../typeName';
5+
import { makeIdentifier } from '../../../templates/makeIdentifier';
66
import { TypeDefContext } from '../TypeDefContext';
77

88
export const typeDefForSchema = (
@@ -31,7 +31,7 @@ export const typeDefForSchema = (
3131

3232
const { enum: enumProp, allOf, title } = schema;
3333

34-
const type = title ? typeName(title) : undefined;
34+
const type = title ? makeIdentifier(title) : undefined;
3535

3636
if (type) {
3737
if (!context.shouldEmitType(type)) {

src/helpers/open-api/v3/typeDefForSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { typeDefForObject } from './typeDefForObject';
33
import { typeDefForReference } from './typeDefForReference';
44
import { typeDefForEnum } from './typeDefForEnum';
55
import { isObjectSchema } from './isObjectSchema';
6-
import { typeName } from '../typeName';
6+
import { makeIdentifier } from '../../../templates/makeIdentifier';
77
import { TypeDefContext } from '../TypeDefContext';
88

99
export const typeDefForSchema = (
@@ -29,7 +29,7 @@ export const typeDefForSchema = (
2929

3030
const { enum: enumProp, allOf, oneOf, anyOf, title } = schema;
3131

32-
const type = title ? typeName(title) : undefined;
32+
const type = title ? makeIdentifier(title) : undefined;
3333

3434
if (type) {
3535
if (!context.shouldEmitType(type)) {

src/templates/clientMethodTemplate.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-template-curly-in-string */
22
import { aliasIfReserved, aliasNameIfReserved } from './aliasIfReserved';
3+
import { IdentifierFormat, makeIdentifier } from './makeIdentifier';
34
import { objectTemplate } from './objectTemplate';
4-
import { safeName } from './safeName';
55

66
export type ClientMethodTemplateArgs = {
77
httpMethod: string;
@@ -120,14 +120,19 @@ export const clientMethodTemplate = (
120120

121121
const decomposeParameters = paramNames.any()
122122
? `const {${paramNames
123-
.map(safeName)
123+
.map(name => makeIdentifier(name, IdentifierFormat.camelCase))
124124
.map(aliasIfReserved)
125125
.join(', ')}} = args;`
126126
: '';
127127

128128
const composeQuery = queryParams.any()
129129
? `const query = qs.stringify({ ${queryParams
130-
.map(q => `["${q.name}"]: ${safeName(aliasIfReserved(q.name))}`)
130+
.map(
131+
q =>
132+
`["${q.name}"]: ${aliasNameIfReserved(
133+
makeIdentifier(q.name, IdentifierFormat.camelCase)
134+
)}`
135+
)
131136
.join(', ')} }${
132137
queryArrayFormat === 'comma' ? ", { arrayFormat: 'comma' }" : ''
133138
});`
@@ -152,17 +157,15 @@ export const clientMethodTemplate = (
152157

153158
const headers = headerParams
154159
.map(h => {
160+
const localVarName = aliasNameIfReserved(
161+
makeIdentifier(h.name, IdentifierFormat.camelCase)
162+
);
163+
155164
if (h.required) {
156-
return `["${h.name}"]: ${safeName(aliasIfReserved(h.name))}`;
165+
return `["${h.name}"]: ${localVarName}`;
157166
}
158167

159-
return `...(typeof ${safeName(
160-
aliasNameIfReserved(h.name)
161-
)} !== 'undefined' && ${safeName(
162-
aliasNameIfReserved(h.name)
163-
)} !== null ? { ["${h.name}"]: ${safeName(
164-
aliasIfReserved(h.name)
165-
)} } : {})`;
168+
return `...(typeof ${localVarName} !== 'undefined' && ${localVarName} !== null ? { ["${h.name}"]: ${localVarName} } : {})`;
166169
})
167170
.concat(
168171
body?.type === 'json' ? ["'Content-Type': 'application/json'"] : []

0 commit comments

Comments
 (0)