Skip to content

Commit 2f17cb7

Browse files
authored
feat(fetch): added support for urlEncodeParameters (#2292)
* feat(fetch): added support for urlEncodeParameters * feat(fetch): refactored urlEncodeParameters implementation * feat(fetch): improved variable naming
1 parent f3050cd commit 2f17cb7

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

docs/src/pages/reference/configuration/output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2891,7 +2891,7 @@ module.exports = {
28912891

28922892
Type: `Boolean`
28932893

2894-
Valid values: true or false. Defaults to false. **Note:** this only works for Tanstack Query clients for now.
2894+
Valid values: true or false. Defaults to false. **Note:** this only works for Tanstack Query clients and fetch httpClients for now.
28952895

28962896
Use this property to enable URL encoding of path/query parameters. This is highly recommended, and will probably become a default in the future, see https://github.com/orval-labs/orval/pull/895
28972897

packages/fetch/src/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TEMPLATE_TAG_REGEX } from '@orval/core';
12
import { ClientHeaderBuilder, pascal } from '@orval/core';
23
import {
34
camel,
@@ -38,6 +39,10 @@ export const generateRequestFunction = (
3839
}: GeneratorVerbOptions,
3940
{ route, context, pathRoute }: GeneratorOptions,
4041
) => {
42+
const implementationRoute = context.output.urlEncodeParameters
43+
? makeRouteSafe(route)
44+
: route;
45+
4146
const isRequestOptions = override?.requestOptions !== false;
4247
const isFormData = override?.formData.disabled === false;
4348
const isFormUrlEncoded = override?.formUrlEncoded !== false;
@@ -114,13 +119,19 @@ ${
114119
});`
115120
: ''
116121
}
117-
122+
${
123+
context.output.urlEncodeParameters
124+
? `for (const [key, value] of normalizedParams) {
125+
normalizedParams.set(key, encodeURIComponent(value));
126+
}`
127+
: ``
128+
}
118129
${queryParams ? `const stringifiedParams = normalizedParams.toString();` : ``}
119130
120131
${
121132
queryParams
122-
? `return stringifiedParams.length > 0 ? \`${route}${'?${stringifiedParams}'}\` : \`${route}\``
123-
: `return \`${route}\``
133+
? `return stringifiedParams.length > 0 ? \`${implementationRoute}${'?${stringifiedParams}'}\` : \`${implementationRoute}\``
134+
: `return \`${implementationRoute}\``
124135
}
125136
}\n`;
126137

@@ -306,10 +317,7 @@ export const generateClient: ClientBuilder = (verbOptions, options) => {
306317
const imports = generateVerbImports(verbOptions);
307318
const functionImplementation = generateRequestFunction(verbOptions, options);
308319

309-
return {
310-
implementation: `${functionImplementation}\n`,
311-
imports,
312-
};
320+
return { implementation: `${functionImplementation}\n`, imports };
313321
};
314322

315323
const getHTTPStatusCodes = () => `
@@ -330,6 +338,9 @@ export const generateFetchHeader: ClientHeaderBuilder = ({
330338
: '';
331339
};
332340

341+
export const makeRouteSafe = (route: string): string =>
342+
route.replaceAll(TEMPLATE_TAG_REGEX, `\${encodeURIComponent(String($1))}`);
343+
333344
const fetchClientBuilder: ClientGeneratorsBuilder = {
334345
client: generateClient,
335346
header: generateFetchHeader,

tests/configs/fetch.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ export default defineConfig({
248248
target: '../generated/fetch/dateParams/endpoints.ts',
249249
schemas: '../generated/fetch/dateParams/model',
250250
client: 'fetch',
251+
urlEncodeParameters: true,
251252
mode: 'tags-split',
252253
override: {
253254
useDates: true,

0 commit comments

Comments
 (0)