Skip to content

Commit dc8f9c7

Browse files
committed
fix: Request field names should not be modified
1 parent 932959a commit dc8f9c7

3 files changed

Lines changed: 9 additions & 54 deletions

File tree

src/templates/aliasIfReserved.ts

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

src/templates/clientMethodTemplate.ts

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

64
export type ClientMethodTemplateArgs = {
@@ -110,29 +108,9 @@ export const clientMethodTemplate = (
110108
})
111109
.join(' | ');
112110

113-
const paramNames = headerParams
114-
.concat(queryParams)
115-
.concat(
116-
pathParams.map(({ name, type }) => ({ name, required: true, type }))
117-
)
118-
.map(({ name }) => name)
119-
.concat(bodyArg ? [bodyArg.name] : []);
120-
121-
const decomposeParameters = paramNames.any()
122-
? `const {${paramNames
123-
.map(name => makeIdentifier(name, IdentifierFormat.camelCase))
124-
.map(aliasIfReserved)
125-
.join(', ')}} = args;`
126-
: '';
127-
128111
const composeQuery = queryParams.any()
129112
? `const query = qs.stringify({ ${queryParams
130-
.map(
131-
q =>
132-
`["${q.name}"]: ${aliasNameIfReserved(
133-
makeIdentifier(q.name, IdentifierFormat.camelCase)
134-
)}`
135-
)
113+
.map(q => `["${q.name}"]: args["${q.name}"]`)
136114
.join(', ')} }${
137115
queryArrayFormat === 'comma' ? ", { arrayFormat: 'comma' }" : ''
138116
});`
@@ -144,28 +122,24 @@ export const clientMethodTemplate = (
144122
${body.fields
145123
.map(
146124
f =>
147-
`if (body.${f.name}) formData.append('${f.name}', body.${f.name});`
125+
`if (body["${f.name}"]) formData.append('${f.name}', body["${f.name}"]);`
148126
)
149127
.join('\n')}
150128
`
151129
: '';
152130

153131
const url = `\`\${this.baseUrl}${openApiPath.replace(
154132
/\{(?:.*?)\}/g,
155-
x => `$${x}`
133+
x => `$\{args["${x.slice(1, x.length - 1)}"]}`
156134
)}${queryParams.any() ? '?${query}' : ''}\``;
157135

158136
const headers = headerParams
159137
.map(h => {
160-
const localVarName = aliasNameIfReserved(
161-
makeIdentifier(h.name, IdentifierFormat.camelCase)
162-
);
163-
164138
if (h.required) {
165-
return `["${h.name}"]: ${localVarName}`;
139+
return `["${h.name}"]: args["${h.name}"]`;
166140
}
167141

168-
return `...(typeof ${localVarName} !== 'undefined' && ${localVarName} !== null ? { ["${h.name}"]: ${localVarName} } : {})`;
142+
return `...(typeof args["${h.name}"] !== 'undefined' && args["${h.name}"] !== null ? { ["${h.name}"]: args["${h.name}"] } : {})`;
169143
})
170144
.concat(
171145
body?.type === 'json' ? ["'Content-Type': 'application/json'"] : []
@@ -175,7 +149,7 @@ export const clientMethodTemplate = (
175149
${[
176150
'method',
177151
178-
...(body?.type === 'json' ? ['body: JSON.stringify(body)'] : []),
152+
...(body?.type === 'json' ? ['body: JSON.stringify(args.body)'] : []),
179153
180154
...(body?.type === 'form' ? ['body: formData'] : []),
181155
@@ -228,8 +202,6 @@ export const clientMethodTemplate = (
228202
return `public async "${methodName}"(${functionArgumentSignature}):
229203
Promise<${responseType}> {
230204
231-
${decomposeParameters}
232-
233205
${composeQuery}
234206
235207
${composeFormData}

src/templates/fieldTemplate.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { IdentifierFormat, makeIdentifier } from './makeIdentifier';
2-
31
export const fieldTemplate = (
42
name: string,
53
required: boolean,
@@ -8,7 +6,7 @@ export const fieldTemplate = (
86
nonRequiredType: 'optional' | 'nullable' | 'both';
97
}
108
): string => {
11-
const fieldName = makeIdentifier(name, IdentifierFormat.camelCase);
9+
const fieldName = `"${name}"`;
1210

1311
if (required) {
1412
return `${fieldName}: ${type}`;
@@ -17,8 +15,8 @@ export const fieldTemplate = (
1715
const { nonRequiredType } = options;
1816

1917
const fieldNameWithOptional = ['optional', 'both'].includes(nonRequiredType)
20-
? `"${fieldName}"?`
21-
: `"${fieldName}"`;
18+
? `${fieldName}?`
19+
: fieldName;
2220

2321
const typeWithNull = ['nullable', 'both'].includes(nonRequiredType)
2422
? `${type} | null`

0 commit comments

Comments
 (0)