@typespec/http-server-js 0.58.0-alpha.13-dev.8 → 0.58.0-alpha.13-dev.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.testignore +0 -1
- package/README.md +6 -0
- package/build-helpers.ts +22 -9
- package/dist/generated-defs/helpers/index.d.ts.map +1 -1
- package/dist/generated-defs/helpers/index.js +1 -0
- package/dist/generated-defs/helpers/index.js.map +1 -1
- package/dist/generated-defs/helpers/temporal/index.d.ts +4 -0
- package/dist/generated-defs/helpers/temporal/index.d.ts.map +1 -0
- package/dist/generated-defs/helpers/temporal/index.js +19 -0
- package/dist/generated-defs/helpers/temporal/index.js.map +1 -0
- package/dist/generated-defs/helpers/temporal/native.d.ts +4 -0
- package/dist/generated-defs/helpers/temporal/native.d.ts.map +1 -0
- package/dist/generated-defs/helpers/temporal/native.js +125 -0
- package/dist/generated-defs/helpers/temporal/native.js.map +1 -0
- package/dist/generated-defs/helpers/temporal/polyfill.d.ts +4 -0
- package/dist/generated-defs/helpers/temporal/polyfill.d.ts.map +1 -0
- package/dist/generated-defs/helpers/temporal/polyfill.js +124 -0
- package/dist/generated-defs/helpers/temporal/polyfill.js.map +1 -0
- package/dist/generated-defs/package.json.d.ts.map +1 -1
- package/dist/generated-defs/package.json.js +1 -0
- package/dist/generated-defs/package.json.js.map +1 -1
- package/dist/src/common/declaration.js +1 -1
- package/dist/src/common/declaration.js.map +1 -1
- package/dist/src/common/scalar.d.ts +1 -1
- package/dist/src/common/scalar.d.ts.map +1 -1
- package/dist/src/common/scalar.js +341 -49
- package/dist/src/common/scalar.js.map +1 -1
- package/dist/src/common/serialization/index.d.ts +2 -2
- package/dist/src/common/serialization/index.d.ts.map +1 -1
- package/dist/src/common/serialization/index.js +3 -9
- package/dist/src/common/serialization/index.js.map +1 -1
- package/dist/src/common/serialization/json.d.ts +2 -0
- package/dist/src/common/serialization/json.d.ts.map +1 -1
- package/dist/src/common/serialization/json.js +10 -19
- package/dist/src/common/serialization/json.js.map +1 -1
- package/dist/src/helpers/temporal/native.d.ts +40 -0
- package/dist/src/helpers/temporal/native.d.ts.map +1 -0
- package/dist/src/helpers/temporal/native.js +81 -0
- package/dist/src/helpers/temporal/native.js.map +1 -0
- package/dist/src/helpers/temporal/polyfill.d.ts +41 -0
- package/dist/src/helpers/temporal/polyfill.d.ts.map +1 -0
- package/dist/src/helpers/temporal/polyfill.js +80 -0
- package/dist/src/helpers/temporal/polyfill.js.map +1 -0
- package/dist/src/http/server/index.d.ts.map +1 -1
- package/dist/src/http/server/index.js +4 -1
- package/dist/src/http/server/index.js.map +1 -1
- package/dist/src/lib.d.ts +9 -0
- package/dist/src/lib.d.ts.map +1 -1
- package/dist/src/lib.js +7 -0
- package/dist/src/lib.js.map +1 -1
- package/dist/src/scripts/scaffold/data-mocks.d.ts.map +1 -1
- package/dist/src/scripts/scaffold/data-mocks.js +62 -8
- package/dist/src/scripts/scaffold/data-mocks.js.map +1 -1
- package/generated-defs/helpers/index.ts +1 -0
- package/generated-defs/helpers/temporal/index.ts +25 -0
- package/generated-defs/helpers/temporal/native.ts +132 -0
- package/generated-defs/helpers/temporal/polyfill.ts +131 -0
- package/generated-defs/package.json.ts +1 -0
- package/package.json +4 -3
- package/src/common/declaration.ts +1 -1
- package/src/common/scalar.ts +390 -62
- package/src/common/serialization/index.ts +4 -13
- package/src/common/serialization/json.ts +22 -25
- package/src/helpers/temporal/native.ts +104 -0
- package/src/helpers/temporal/polyfill.ts +103 -0
- package/src/http/server/index.ts +6 -1
- package/src/lib.ts +17 -0
- package/src/scripts/scaffold/data-mocks.ts +68 -8
- package/temp/tsconfig.tsbuildinfo +1 -1
- package/test/scalar.test.ts +547 -97
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { isErrorType, isVoidType, } from "@typespec/compiler";
|
|
2
2
|
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
3
3
|
import { isUnspeakable, parseCase } from "../../util/case.js";
|
|
4
|
-
import { module as dateTimeHelper } from "../../../generated-defs/helpers/datetime.js";
|
|
5
4
|
import { KEYWORDS } from "../../util/keywords.js";
|
|
5
|
+
import { getFullyQualifiedTypeName } from "../../util/name.js";
|
|
6
|
+
import { module as dateTimeHelper } from "../../../generated-defs/helpers/datetime.js";
|
|
6
7
|
/**
|
|
7
8
|
* Generates a mock value for a TypeSpec Model.
|
|
8
9
|
* Handles arrays and records as special cases.
|
|
@@ -143,6 +144,7 @@ function mockUnion(ctx, module, union) {
|
|
|
143
144
|
* @returns A JavaScript string representation of a suitable mock value for the scalar type
|
|
144
145
|
*/
|
|
145
146
|
function mockScalar(ctx, module, scalar) {
|
|
147
|
+
const dateTimeMode = ctx.options.datetime ?? "temporal-polyfill";
|
|
146
148
|
if ($.scalar.isBoolean(scalar) || $.scalar.extendsBoolean(scalar)) {
|
|
147
149
|
return JSON.stringify(true);
|
|
148
150
|
}
|
|
@@ -161,18 +163,70 @@ function mockScalar(ctx, module, scalar) {
|
|
|
161
163
|
return "new Decimal(42)";
|
|
162
164
|
}
|
|
163
165
|
}
|
|
164
|
-
if ($.scalar.isUtcDateTime(scalar)
|
|
165
|
-
|
|
166
|
+
if ($.scalar.isUtcDateTime(scalar)) {
|
|
167
|
+
if (dateTimeMode === "date-duration") {
|
|
168
|
+
return "new Date()";
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
if (dateTimeMode === "temporal-polyfill") {
|
|
172
|
+
module.imports.push({
|
|
173
|
+
from: "temporal-polyfill",
|
|
174
|
+
binder: ["Temporal"],
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
// Current instant
|
|
178
|
+
return "Temporal.Now.instant()";
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if ($.scalar.isOffsetDateTime(scalar) ||
|
|
182
|
+
$.scalar.isPlainDate(scalar) ||
|
|
183
|
+
$.scalar.isPlainTime(scalar)) {
|
|
184
|
+
if (dateTimeMode === "date-duration") {
|
|
185
|
+
return "new Date()";
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
if (dateTimeMode === "temporal-polyfill") {
|
|
189
|
+
module.imports.push({
|
|
190
|
+
from: "temporal-polyfill",
|
|
191
|
+
binder: ["Temporal"],
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
// Current instant
|
|
195
|
+
switch (scalar.name) {
|
|
196
|
+
case "offsetDateTime":
|
|
197
|
+
return "Temporal.Now.zonedDateTimeISO()";
|
|
198
|
+
case "plainDate":
|
|
199
|
+
return "Temporal.Now.zonedDateTimeISO().toPlainDate()";
|
|
200
|
+
case "plainTime":
|
|
201
|
+
return "Temporal.Now.zonedDateTimeISO().toPlainTime()";
|
|
202
|
+
default:
|
|
203
|
+
throw new Error("Unexpected scalar type: " + scalar.name);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (getFullyQualifiedTypeName(scalar) === "TypeSpec.unixTimestamp32") {
|
|
208
|
+
return "Math.floor(new Date().getTime() / 1000)";
|
|
166
209
|
}
|
|
167
210
|
if ($.scalar.isBytes(scalar) || $.scalar.extendsBytes(scalar)) {
|
|
168
211
|
return "new Uint8Array()";
|
|
169
212
|
}
|
|
170
213
|
if ($.scalar.isDuration(scalar) || $.scalar.extendsDuration(scalar)) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
214
|
+
if (dateTimeMode === "date-duration") {
|
|
215
|
+
module.imports.push({
|
|
216
|
+
from: dateTimeHelper,
|
|
217
|
+
binder: ["Duration"],
|
|
218
|
+
});
|
|
219
|
+
return 'Duration.parseISO8601("P1Y2M3DT4H5M6S")';
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
if (dateTimeMode === "temporal-polyfill") {
|
|
223
|
+
module.imports.push({
|
|
224
|
+
from: "temporal-polyfill",
|
|
225
|
+
binder: ["Temporal"],
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return `Temporal.Duration.from({ years: 1, months: 2, days: 3, hours: 4, minutes: 5, seconds: 6 })`;
|
|
229
|
+
}
|
|
176
230
|
}
|
|
177
231
|
if ($.scalar.isOffsetDateTime(scalar) || $.scalar.extendsOffsetDateTime(scalar)) {
|
|
178
232
|
return JSON.stringify("2022-01-01T00:00:00Z");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-mocks.js","sourceRoot":"","sources":["../../../../src/scripts/scaffold/data-mocks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,UAAU,GAOX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,CAAC,EAAE,MAAM,yCAAyC,CAAC;AAE5D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"data-mocks.js","sourceRoot":"","sources":["../../../../src/scripts/scaffold/data-mocks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,UAAU,GAOX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,CAAC,EAAE,MAAM,yCAAyC,CAAC;AAE5D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,6CAA6C,CAAC;AAEvF;;;;;;;GAOG;AACH,SAAS,SAAS,CAAC,GAAc,EAAE,MAAc,EAAE,IAAW;IAC5D,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACrB,OAAO,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IACtC,CAAC;IAED,IAAI,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACtB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AC/C,CAAC;IAED,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QAChE,OAAO,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;IACvC,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -16,6 +16,7 @@ export async function createModule(parent: Module): Promise<Module> {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
// Child modules
|
|
19
|
+
await import("./temporal/index.js").then((m) => m.createModule(module));
|
|
19
20
|
await import("./datetime.js").then((m) => m.createModule(module));
|
|
20
21
|
await import("./header.js").then((m) => m.createModule(module));
|
|
21
22
|
await import("./http.js").then((m) => m.createModule(module));
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
|
|
4
|
+
import { Module } from "../../../src/ctx.js";
|
|
5
|
+
|
|
6
|
+
export let module: Module = undefined as any;
|
|
7
|
+
|
|
8
|
+
export async function createModule(parent: Module): Promise<Module> {
|
|
9
|
+
if (module) return module;
|
|
10
|
+
|
|
11
|
+
module = {
|
|
12
|
+
name: "temporal",
|
|
13
|
+
cursor: parent.cursor.enter("temporal"),
|
|
14
|
+
imports: [],
|
|
15
|
+
declarations: [],
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// Child modules
|
|
19
|
+
await import("./native.js").then((m) => m.createModule(module));
|
|
20
|
+
await import("./polyfill.js").then((m) => m.createModule(module));
|
|
21
|
+
|
|
22
|
+
parent.declarations.push(module);
|
|
23
|
+
|
|
24
|
+
return module;
|
|
25
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
|
|
4
|
+
import { Module } from "../../../src/ctx.js";
|
|
5
|
+
|
|
6
|
+
export let module: Module = undefined as any;
|
|
7
|
+
|
|
8
|
+
// prettier-ignore
|
|
9
|
+
const lines = [
|
|
10
|
+
"// Copyright (c) Microsoft Corporation",
|
|
11
|
+
"// Licensed under the MIT license.",
|
|
12
|
+
"",
|
|
13
|
+
"// Due to the lack of generally-available global type definitions for Temporal, we use `any` throughout this module.",
|
|
14
|
+
"// When global.d.ts eventually has definitions for Temporal, we can unify this module with `polyfill.ts`.",
|
|
15
|
+
"",
|
|
16
|
+
"/**",
|
|
17
|
+
" * Parses an HTTP date string (e.g. `Wed, 21 Oct 2015 07:28:00 GMT`) into a `Temporal.Instant`.",
|
|
18
|
+
" * The date string must be in the format specified by RFC 7231.",
|
|
19
|
+
" *",
|
|
20
|
+
" * @param httpDate - The HTTP date string to parse.",
|
|
21
|
+
" * @throws {RangeError} If the date string is invalid or cannot be parsed.",
|
|
22
|
+
" * @returns The parsed `Temporal.Instant`.",
|
|
23
|
+
" */",
|
|
24
|
+
"export function parseHttpDate(httpDate: string): any {",
|
|
25
|
+
" const timestamp = globalThis.Date.parse(httpDate);",
|
|
26
|
+
"",
|
|
27
|
+
" if (isNaN(timestamp)) {",
|
|
28
|
+
" throw new RangeError(`Invalid HTTP date: ${httpDate}`);",
|
|
29
|
+
" }",
|
|
30
|
+
"",
|
|
31
|
+
" return (globalThis as any).Temporal.Instant.fromEpochMilliseconds(timestamp);",
|
|
32
|
+
"}",
|
|
33
|
+
"",
|
|
34
|
+
"/**",
|
|
35
|
+
" * Formats a `Temporal.Instant` into an HTTP date string (e.g. `Wed, 21 Oct 2015 07:28:00 GMT`).",
|
|
36
|
+
" * The date string is formatted according to RFC 7231.",
|
|
37
|
+
" *",
|
|
38
|
+
" * @param instant - The `Temporal.Instant` to format.",
|
|
39
|
+
" * @returns The formatted HTTP date string.",
|
|
40
|
+
" */",
|
|
41
|
+
"export function formatHttpDate(instant: any) {",
|
|
42
|
+
" const date = new Date(instant.epochMilliseconds);",
|
|
43
|
+
" return date.toUTCString();",
|
|
44
|
+
"}",
|
|
45
|
+
"",
|
|
46
|
+
"/**",
|
|
47
|
+
" * Converts a `Temporal.Duration` to a number of seconds.",
|
|
48
|
+
" * This method will throw an Error if the duration contains any years, months, weeks, or days, as those require a reference",
|
|
49
|
+
" * point to calculate the total number of seconds.",
|
|
50
|
+
" *",
|
|
51
|
+
" * WARNING: If the total number of seconds is larger than the maximum safe integer in JavaScript, this method will",
|
|
52
|
+
" * lose precision. @see durationTotalSecondsBigInt for a BigInt alternative.",
|
|
53
|
+
" *",
|
|
54
|
+
" * @param duration - the duration to calculate the total number of seconds for",
|
|
55
|
+
" * @returns the total number of seconds in the duration",
|
|
56
|
+
" */",
|
|
57
|
+
"export function durationTotalSeconds(duration: any): number {",
|
|
58
|
+
" if (",
|
|
59
|
+
" duration.years !== 0 ||",
|
|
60
|
+
" duration.months !== 0 ||",
|
|
61
|
+
" duration.weeks !== 0 ||",
|
|
62
|
+
" duration.days !== 0",
|
|
63
|
+
" ) {",
|
|
64
|
+
" throw new Error(",
|
|
65
|
+
" \"Cannot calculate total seconds for a duration with years, months, weeks, or days.\",",
|
|
66
|
+
" );",
|
|
67
|
+
" }",
|
|
68
|
+
"",
|
|
69
|
+
" return (",
|
|
70
|
+
" duration.seconds +",
|
|
71
|
+
" duration.minutes * 60 +",
|
|
72
|
+
" duration.hours * 60 * 60 +",
|
|
73
|
+
" duration.days * 24 * 60 * 60",
|
|
74
|
+
" );",
|
|
75
|
+
"}",
|
|
76
|
+
"",
|
|
77
|
+
"/**",
|
|
78
|
+
" * Gets the total number of seconds in a duration.",
|
|
79
|
+
" *",
|
|
80
|
+
" * This method will throw an Error if the duration contains any years, months, weeks, or days, as those require a reference",
|
|
81
|
+
" * point to calculate the total number of seconds. It will also throw an error if any of the components are not integers.",
|
|
82
|
+
" *",
|
|
83
|
+
" * @param duration - the duration to calculate the total number of seconds for",
|
|
84
|
+
" * @returns the total number of seconds in the duration",
|
|
85
|
+
" */",
|
|
86
|
+
"export function durationTotalSecondsBigInt(duration: any): bigint {",
|
|
87
|
+
" if (",
|
|
88
|
+
" duration.years !== 0 ||",
|
|
89
|
+
" duration.months !== 0 ||",
|
|
90
|
+
" duration.weeks !== 0 ||",
|
|
91
|
+
" duration.days !== 0",
|
|
92
|
+
" ) {",
|
|
93
|
+
" throw new Error(",
|
|
94
|
+
" \"Cannot calculate total seconds for a duration with years, months, weeks, or days.\",",
|
|
95
|
+
" );",
|
|
96
|
+
" }",
|
|
97
|
+
"",
|
|
98
|
+
" if (",
|
|
99
|
+
" !Number.isInteger(duration.seconds) ||",
|
|
100
|
+
" !Number.isInteger(duration.minutes) ||",
|
|
101
|
+
" !Number.isInteger(duration.hours) ||",
|
|
102
|
+
" !Number.isInteger(duration.days)",
|
|
103
|
+
" ) {",
|
|
104
|
+
" throw new Error(\"Duration components must be integers.\");",
|
|
105
|
+
" }",
|
|
106
|
+
"",
|
|
107
|
+
" return (",
|
|
108
|
+
" BigInt(duration.seconds) +",
|
|
109
|
+
" BigInt(duration.minutes) * 60n +",
|
|
110
|
+
" BigInt(duration.hours) * 60n * 60n +",
|
|
111
|
+
" BigInt(duration.days) * 24n * 60n * 60n",
|
|
112
|
+
" );",
|
|
113
|
+
"}",
|
|
114
|
+
"",
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
export async function createModule(parent: Module): Promise<Module> {
|
|
118
|
+
if (module) return module;
|
|
119
|
+
|
|
120
|
+
module = {
|
|
121
|
+
name: "native",
|
|
122
|
+
cursor: parent.cursor.enter("native"),
|
|
123
|
+
imports: [],
|
|
124
|
+
declarations: [],
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
module.declarations.push(lines);
|
|
128
|
+
|
|
129
|
+
parent.declarations.push(module);
|
|
130
|
+
|
|
131
|
+
return module;
|
|
132
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
|
|
4
|
+
import { Module } from "../../../src/ctx.js";
|
|
5
|
+
|
|
6
|
+
export let module: Module = undefined as any;
|
|
7
|
+
|
|
8
|
+
// prettier-ignore
|
|
9
|
+
const lines = [
|
|
10
|
+
"// Copyright (c) Microsoft Corporation",
|
|
11
|
+
"// Licensed under the MIT license.",
|
|
12
|
+
"",
|
|
13
|
+
"import { Temporal } from \"temporal-polyfill\";",
|
|
14
|
+
"",
|
|
15
|
+
"/**",
|
|
16
|
+
" * Parses an HTTP date string (e.g. `Wed, 21 Oct 2015 07:28:00 GMT`) into a `Temporal.Instant`.",
|
|
17
|
+
" * The date string must be in the format specified by RFC 7231.",
|
|
18
|
+
" *",
|
|
19
|
+
" * @param httpDate - The HTTP date string to parse.",
|
|
20
|
+
" * @throws {RangeError} If the date string is invalid or cannot be parsed.",
|
|
21
|
+
" * @returns The parsed `Temporal.Instant`.",
|
|
22
|
+
" */",
|
|
23
|
+
"export function parseHttpDate(httpDate: string): Temporal.Instant {",
|
|
24
|
+
" const timestamp = globalThis.Date.parse(httpDate);",
|
|
25
|
+
"",
|
|
26
|
+
" if (isNaN(timestamp)) {",
|
|
27
|
+
" throw new RangeError(`Invalid HTTP date: ${httpDate}`);",
|
|
28
|
+
" }",
|
|
29
|
+
"",
|
|
30
|
+
" return Temporal.Instant.fromEpochMilliseconds(timestamp);",
|
|
31
|
+
"}",
|
|
32
|
+
"",
|
|
33
|
+
"/**",
|
|
34
|
+
" * Formats a `Temporal.Instant` into an HTTP date string (e.g. `Wed, 21 Oct 2015 07:28:00 GMT`).",
|
|
35
|
+
" * The date string is formatted according to RFC 7231.",
|
|
36
|
+
" *",
|
|
37
|
+
" * @param instant - The `Temporal.Instant` to format.",
|
|
38
|
+
" * @returns The formatted HTTP date string.",
|
|
39
|
+
" */",
|
|
40
|
+
"export function formatHttpDate(instant: Temporal.Instant) {",
|
|
41
|
+
" const date = new Date(instant.epochMilliseconds);",
|
|
42
|
+
" return date.toUTCString();",
|
|
43
|
+
"}",
|
|
44
|
+
"",
|
|
45
|
+
"/**",
|
|
46
|
+
" * Converts a `Temporal.Duration` to a number of seconds.",
|
|
47
|
+
" * This method will throw an Error if the duration contains any years, months, weeks, or days, as those require a reference",
|
|
48
|
+
" * point to calculate the total number of seconds.",
|
|
49
|
+
" *",
|
|
50
|
+
" * WARNING: If the total number of seconds is larger than the maximum safe integer in JavaScript, this method will",
|
|
51
|
+
" * lose precision. @see durationTotalSecondsBigInt for a BigInt alternative.",
|
|
52
|
+
" *",
|
|
53
|
+
" * @param duration - the duration to calculate the total number of seconds for",
|
|
54
|
+
" * @returns the total number of seconds in the duration",
|
|
55
|
+
" */",
|
|
56
|
+
"export function durationTotalSeconds(duration: Temporal.Duration): number {",
|
|
57
|
+
" if (",
|
|
58
|
+
" duration.years !== 0 ||",
|
|
59
|
+
" duration.months !== 0 ||",
|
|
60
|
+
" duration.weeks !== 0 ||",
|
|
61
|
+
" duration.days !== 0",
|
|
62
|
+
" ) {",
|
|
63
|
+
" throw new Error(",
|
|
64
|
+
" \"Cannot calculate total seconds for a duration with years, months, weeks, or days.\",",
|
|
65
|
+
" );",
|
|
66
|
+
" }",
|
|
67
|
+
"",
|
|
68
|
+
" return (",
|
|
69
|
+
" duration.seconds +",
|
|
70
|
+
" duration.minutes * 60 +",
|
|
71
|
+
" duration.hours * 60 * 60 +",
|
|
72
|
+
" duration.days * 24 * 60 * 60",
|
|
73
|
+
" );",
|
|
74
|
+
"}",
|
|
75
|
+
"",
|
|
76
|
+
"/**",
|
|
77
|
+
" * Gets the total number of seconds in a duration.",
|
|
78
|
+
" *",
|
|
79
|
+
" * This method will throw an Error if the duration contains any years, months, weeks, or days, as those require a reference",
|
|
80
|
+
" * point to calculate the total number of seconds. It will also throw an error if any of the components are not integers.",
|
|
81
|
+
" *",
|
|
82
|
+
" * @param duration - the duration to calculate the total number of seconds for",
|
|
83
|
+
" * @returns the total number of seconds in the duration",
|
|
84
|
+
" */",
|
|
85
|
+
"export function durationTotalSecondsBigInt(duration: Temporal.Duration): bigint {",
|
|
86
|
+
" if (",
|
|
87
|
+
" duration.years !== 0 ||",
|
|
88
|
+
" duration.months !== 0 ||",
|
|
89
|
+
" duration.weeks !== 0 ||",
|
|
90
|
+
" duration.days !== 0",
|
|
91
|
+
" ) {",
|
|
92
|
+
" throw new Error(",
|
|
93
|
+
" \"Cannot calculate total seconds for a duration with years, months, weeks, or days.\",",
|
|
94
|
+
" );",
|
|
95
|
+
" }",
|
|
96
|
+
"",
|
|
97
|
+
" if (",
|
|
98
|
+
" !Number.isInteger(duration.seconds) ||",
|
|
99
|
+
" !Number.isInteger(duration.minutes) ||",
|
|
100
|
+
" !Number.isInteger(duration.hours) ||",
|
|
101
|
+
" !Number.isInteger(duration.days)",
|
|
102
|
+
" ) {",
|
|
103
|
+
" throw new Error(\"Duration components must be integers.\");",
|
|
104
|
+
" }",
|
|
105
|
+
"",
|
|
106
|
+
" return (",
|
|
107
|
+
" BigInt(duration.seconds) +",
|
|
108
|
+
" BigInt(duration.minutes) * 60n +",
|
|
109
|
+
" BigInt(duration.hours) * 60n * 60n +",
|
|
110
|
+
" BigInt(duration.days) * 24n * 60n * 60n",
|
|
111
|
+
" );",
|
|
112
|
+
"}",
|
|
113
|
+
"",
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
export async function createModule(parent: Module): Promise<Module> {
|
|
117
|
+
if (module) return module;
|
|
118
|
+
|
|
119
|
+
module = {
|
|
120
|
+
name: "polyfill",
|
|
121
|
+
cursor: parent.cursor.enter("polyfill"),
|
|
122
|
+
imports: [],
|
|
123
|
+
declarations: [],
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
module.declarations.push(lines);
|
|
127
|
+
|
|
128
|
+
parent.declarations.push(module);
|
|
129
|
+
|
|
130
|
+
return module;
|
|
131
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/http-server-js",
|
|
3
|
-
"version": "0.58.0-alpha.13-dev.
|
|
3
|
+
"version": "0.58.0-alpha.13-dev.9",
|
|
4
4
|
"author": "Microsoft Corporation",
|
|
5
5
|
"description": "TypeSpec HTTP server code generator for JavaScript",
|
|
6
6
|
"homepage": "https://github.com/microsoft/typespec",
|
|
@@ -56,6 +56,7 @@
|
|
|
56
56
|
"@typespec/internal-build-utils": "^0.68.0 || >=0.69.0-dev <0.69.0",
|
|
57
57
|
"@typespec/openapi3": "^1.0.0-rc.0",
|
|
58
58
|
"@typespec/spector": "^0.1.0-alpha.11 || >=0.1.0-alpha.12-dev <0.1.0-alpha.12",
|
|
59
|
+
"@typespec/tspd": "^0.68.0 || >=0.69.0-dev <0.69.0",
|
|
59
60
|
"@vitest/coverage-v8": "^3.0.9",
|
|
60
61
|
"@vitest/ui": "^3.0.9",
|
|
61
62
|
"decimal.js": "^10.5.0",
|
|
@@ -69,11 +70,11 @@
|
|
|
69
70
|
"pathe": "^2.0.3",
|
|
70
71
|
"picocolors": "~1.1.1",
|
|
71
72
|
"swagger-ui-express": "^5.0.1",
|
|
73
|
+
"temporal-polyfill": "^0.3.0",
|
|
72
74
|
"tsx": "^4.19.3",
|
|
73
75
|
"typescript": "~5.8.2",
|
|
74
76
|
"vitest": "^3.0.9",
|
|
75
|
-
"yargs": "~17.7.2"
|
|
76
|
-
"@typespec/tspd": "^0.46.0"
|
|
77
|
+
"yargs": "~17.7.2"
|
|
77
78
|
},
|
|
78
79
|
"scripts": {
|
|
79
80
|
"clean": "rimraf ./dist ./temp",
|