Skip to content

Commit 70eac77

Browse files
authored
feat: params of format date-time are now stringified using toISOString when useDates === true (#2213)
* feat: params of format date-time are now stringified using toISOString * fix: removed non-fetch-related date stringification * docs: added note about fetch client stringifying date parameters when useDates: true
1 parent 1c4bf19 commit 70eac77

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,8 @@ export function handleDates(body: any) {
24302430
}
24312431
```
24322432

2433+
**Note:** If you are using the fetch client and useDates is set to true, query parameters of type Date will be stringified using toISOString()
2434+
24332435
#### useBigInt
24342436

24352437
Type: `Boolean`

packages/fetch/src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ export const generateRequestFunction = (
7373

7474
return schema.name;
7575
});
76+
const hasDateParams =
77+
context.output.override.useDates &&
78+
parameters.some(
79+
(p) =>
80+
'schema' in p &&
81+
p.schema &&
82+
'format' in p.schema &&
83+
p.schema.format === 'date-time',
84+
);
7685

7786
const explodeArrayImplementation =
7887
explodeParameters.length > 0
7988
? `const explodeParameters = ${JSON.stringify(explodeParametersNames)};
8089
8190
if (Array.isArray(value) && explodeParameters.includes(key)) {
82-
value.forEach((v) => normalizedParams.append(key, v === null ? 'null' : v.toString()));
91+
value.forEach((v) => normalizedParams.append(key, v === null ? 'null' : ${hasDateParams ? 'v instanceof Date ? v.toISOString() : ' : ''}v.toString()));
8392
return;
8493
}
8594
`
@@ -89,7 +98,7 @@ export const generateRequestFunction = (
8998
explodeParameters.length === parameters.length;
9099

91100
const nomalParamsImplementation = `if (value !== undefined) {
92-
normalizedParams.append(key, value === null ? 'null' : value.toString())
101+
normalizedParams.append(key, value === null ? 'null' : ${hasDateParams ? 'value instanceof Date ? value.toISOString() : ' : ''}value.toString())
93102
}`;
94103

95104
const getUrlFnImplementation = `export const ${getUrlFnName} = (${getUrlFnProps}) => {

tests/configs/fetch.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,18 @@ export default defineConfig({
243243
target: '../specifications/petstore.yaml',
244244
},
245245
},
246+
dateParams: {
247+
output: {
248+
target: '../generated/fetch/dateParams/endpoints.ts',
249+
schemas: '../generated/fetch/dateParams/model',
250+
client: 'fetch',
251+
mode: 'tags-split',
252+
override: {
253+
useDates: true,
254+
},
255+
},
256+
input: {
257+
target: '../specifications/parameters.yaml',
258+
},
259+
},
246260
});

tests/specifications/parameters.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ paths:
120120
required: false
121121
schema:
122122
type: string
123+
- name: latestBirthdate
124+
in: query
125+
description: |
126+
Filter by latest birthdate.
127+
Example: 2020-01-01T00:00:00Z
128+
required: false
129+
schema:
130+
type: string
131+
format: date-time
123132
responses:
124133
'200':
125134
description: A paged array of pets

0 commit comments

Comments
 (0)