-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
[typescript-fetch] Add Temporal support #24924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -350,9 +350,19 @@ function querystringSingleKey(key: string, value: string | number | null | undef | |
| const valueAsArray = Array.from(value); | ||
| return querystringSingleKey(key, valueAsArray, keyPrefix); | ||
| } | ||
| {{#isDateLibraryTemporal}} | ||
| if (value instanceof Temporal.Instant) { | ||
| return `${encodeURIComponent(fullKey)}=${encodeURIComponent(serializeDateTime(value))}`; | ||
| } | ||
| if (value instanceof Temporal.PlainDate) { | ||
| return `${encodeURIComponent(fullKey)}=${encodeURIComponent(serializeDate(value))}`; | ||
| } | ||
| {{/isDateLibraryTemporal}} | ||
| {{^isDateLibraryTemporal}} | ||
| if (value instanceof Date) { | ||
| return `${encodeURIComponent(fullKey)}=${encodeURIComponent(serializeDateTime(value))}`; | ||
| } | ||
| {{/isDateLibraryTemporal}} | ||
| if (value instanceof Object) { | ||
| return querystring(value as HTTPQuery, fullKey); | ||
| } | ||
|
|
@@ -364,6 +374,30 @@ export function exists(json: any, key: string) { | |
| return value !== null && value !== undefined; | ||
| } | ||
|
|
||
| {{#isDateLibraryTemporal}} | ||
| export function serializeDateTime(value: Temporal.Instant): string { | ||
| return value.toString(); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth noting that Temporal.Instant's toString() method gives something like this (based on my experiments):
So in Node, the precision is up to nanoseconds. Also, the Temporal specification says that the number of places after the decimal point may differ because trailing zeroes are removed. Date's toISOString() method uses precision only down to milliseconds in all three runtimes I've tried.
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export function serializeDate(value: Temporal.PlainDate): string { | ||
| return value.toString({ calendarName: "never" }); | ||
| } | ||
|
|
||
| export function parseDateTime(value: Temporal.Instant | string): Temporal.Instant { | ||
| if (value instanceof Temporal.Instant) { | ||
| return value; | ||
| } | ||
| return Temporal.Instant.from(value); | ||
| } | ||
|
|
||
| export function parseDate(value: Temporal.PlainDate | string): Temporal.PlainDate { | ||
| if (value instanceof Temporal.PlainDate) { | ||
| return value; | ||
| } | ||
| return Temporal.PlainDate.from(value); | ||
| } | ||
| {{/isDateLibraryTemporal}} | ||
| {{^isDateLibraryTemporal}} | ||
| /** | ||
| * Every generated date call site routes through these. | ||
| * | ||
|
|
@@ -416,6 +450,7 @@ export function parseDateTime(value: any): Date { | |
| return new Date(value); | ||
| } | ||
| {{/isDateLibraryDate}} | ||
| {{/isDateLibraryTemporal}} | ||
|
|
||
| {{^withoutRuntimeChecks}} | ||
| export function mapValues(data: any, fn: (item: any) => any) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: When
dateLibrary=temporalis used for a path parameter, the generated API referencesTemporalwithout providing its TypeScript declaration. Add an explicit Temporal type dependency/reference, or otherwise generate the required ambient typing so the generated package builds without manual consumer setup.Prompt for AI agents