@typespec/http-server-js 0.58.0-alpha.13-dev.4 → 0.58.0-alpha.13-dev.7
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/cmd/hsjs-scaffold.mjs +7 -0
- package/dist/src/common/namespace.d.ts.map +1 -1
- package/dist/src/common/namespace.js +3 -2
- package/dist/src/common/namespace.js.map +1 -1
- package/dist/src/scripts/scaffold/bin.d.mts +1 -0
- package/dist/src/scripts/scaffold/bin.d.mts.map +1 -1
- package/dist/src/scripts/scaffold/bin.mjs +6 -6
- package/dist/src/scripts/scaffold/bin.mjs.map +1 -1
- package/dist/src/scripts/scaffold/data-mocks.d.ts +2 -1
- package/dist/src/scripts/scaffold/data-mocks.d.ts.map +1 -1
- package/dist/src/scripts/scaffold/data-mocks.js +36 -23
- package/dist/src/scripts/scaffold/data-mocks.js.map +1 -1
- package/package.json +2 -2
- package/src/common/namespace.ts +12 -2
- package/src/scripts/scaffold/bin.mts +7 -6
- package/src/scripts/scaffold/data-mocks.ts +43 -23
- package/temp/tsconfig.tsbuildinfo +1 -1
|
@@ -9,7 +9,11 @@ import {
|
|
|
9
9
|
Union,
|
|
10
10
|
} from "@typespec/compiler";
|
|
11
11
|
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
12
|
-
import {
|
|
12
|
+
import { JsContext, Module } from "../../ctx.js";
|
|
13
|
+
import { isUnspeakable, parseCase } from "../../util/case.js";
|
|
14
|
+
|
|
15
|
+
import { module as dateTimeHelper } from "../../../generated-defs/helpers/datetime.js";
|
|
16
|
+
import { KEYWORDS } from "../../util/keywords.js";
|
|
13
17
|
|
|
14
18
|
/**
|
|
15
19
|
* Generates a mock value for a TypeSpec Model.
|
|
@@ -19,13 +23,13 @@ import { parseCase } from "../../util/case.js";
|
|
|
19
23
|
* @returns A JavaScript string representation of the mock data
|
|
20
24
|
* @throws Error if a property cannot be mocked
|
|
21
25
|
*/
|
|
22
|
-
function mockModel(type: Model): string {
|
|
26
|
+
function mockModel(ctx: JsContext, module: Module, type: Model): string {
|
|
23
27
|
if ($.array.is(type)) {
|
|
24
|
-
return mockArray(type);
|
|
28
|
+
return mockArray(ctx, module, type);
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
if ($.record.is(type)) {
|
|
28
|
-
return mockRecord(type);
|
|
32
|
+
return mockRecord(ctx, module, type);
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
const mock: string[][] = [];
|
|
@@ -37,18 +41,18 @@ function mockModel(type: Model): string {
|
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
for (const [name, prop] of properties) {
|
|
40
|
-
if (prop.optional) {
|
|
44
|
+
if (prop.optional || isUnspeakable(prop.name)) {
|
|
41
45
|
continue;
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
const propMock = mockType(prop.type);
|
|
48
|
+
const propMock = mockType(ctx, module, prop.type);
|
|
45
49
|
|
|
46
50
|
if (!propMock) {
|
|
47
51
|
throw new Error(`Could not mock property ${name} of type ${prop.type.kind}`);
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
const propName = parseCase(name).camelCase;
|
|
51
|
-
mock.push([propName, propMock]);
|
|
55
|
+
mock.push([KEYWORDS.has(propName) ? `_${propName}` : propName, propMock]);
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
// If all properties were optional, return an empty object
|
|
@@ -67,9 +71,9 @@ function mockModel(type: Model): string {
|
|
|
67
71
|
* @param type - The TypeSpec array Model to mock
|
|
68
72
|
* @returns A JavaScript string representation of the mock array
|
|
69
73
|
*/
|
|
70
|
-
function mockArray(type: Model): string {
|
|
74
|
+
function mockArray(ctx: JsContext, module: Module, type: Model): string {
|
|
71
75
|
const elementType = $.array.getElementType(type);
|
|
72
|
-
const mockedType = mockType(elementType);
|
|
76
|
+
const mockedType = mockType(ctx, module, elementType);
|
|
73
77
|
|
|
74
78
|
// If we can't mock the element type, return an empty array
|
|
75
79
|
if (mockedType === undefined) {
|
|
@@ -85,9 +89,9 @@ function mockArray(type: Model): string {
|
|
|
85
89
|
* @param type - The TypeSpec record Model to mock
|
|
86
90
|
* @returns A JavaScript string representation of the mock record
|
|
87
91
|
*/
|
|
88
|
-
function mockRecord(type: Model): string {
|
|
92
|
+
function mockRecord(ctx: JsContext, module: Module, type: Model): string {
|
|
89
93
|
const elementType = $.record.getElementType(type);
|
|
90
|
-
const mockedType = mockType(elementType);
|
|
94
|
+
const mockedType = mockType(ctx, module, elementType);
|
|
91
95
|
|
|
92
96
|
if (mockedType === undefined) {
|
|
93
97
|
return "{}";
|
|
@@ -104,8 +108,12 @@ function mockRecord(type: Model): string {
|
|
|
104
108
|
* @param prop - The TypeSpec model property to mock
|
|
105
109
|
* @returns A JavaScript string representation of the mocked property or undefined if it cannot be mocked
|
|
106
110
|
*/
|
|
107
|
-
function mockModelProperty(
|
|
108
|
-
|
|
111
|
+
function mockModelProperty(
|
|
112
|
+
ctx: JsContext,
|
|
113
|
+
module: Module,
|
|
114
|
+
prop: ModelProperty,
|
|
115
|
+
): string | undefined {
|
|
116
|
+
return mockType(ctx, module, prop.type);
|
|
109
117
|
}
|
|
110
118
|
|
|
111
119
|
/**
|
|
@@ -125,9 +133,9 @@ function mockLiteral(type: LiteralType): string {
|
|
|
125
133
|
* @param type - The TypeSpec type to mock
|
|
126
134
|
* @returns A JavaScript string representation of the mock data, or undefined if the type cannot be mocked
|
|
127
135
|
*/
|
|
128
|
-
export function mockType(type: Type): string | undefined {
|
|
136
|
+
export function mockType(ctx: JsContext, module: Module, type: Type): string | undefined {
|
|
129
137
|
if ($.model.is(type)) {
|
|
130
|
-
return mockModel(type);
|
|
138
|
+
return mockModel(ctx, module, type);
|
|
131
139
|
}
|
|
132
140
|
|
|
133
141
|
if ($.literal.is(type)) {
|
|
@@ -135,15 +143,15 @@ export function mockType(type: Type): string | undefined {
|
|
|
135
143
|
}
|
|
136
144
|
|
|
137
145
|
if ($.modelProperty.is(type)) {
|
|
138
|
-
return mockModelProperty(type);
|
|
146
|
+
return mockModelProperty(ctx, module, type);
|
|
139
147
|
}
|
|
140
148
|
|
|
141
149
|
if ($.scalar.is(type)) {
|
|
142
|
-
return mockScalar(type);
|
|
150
|
+
return mockScalar(ctx, module, type);
|
|
143
151
|
}
|
|
144
152
|
|
|
145
153
|
if ($.union.is(type)) {
|
|
146
|
-
return mockUnion(type);
|
|
154
|
+
return mockUnion(ctx, module, type);
|
|
147
155
|
}
|
|
148
156
|
|
|
149
157
|
if (isVoidType(type)) {
|
|
@@ -159,12 +167,12 @@ export function mockType(type: Type): string | undefined {
|
|
|
159
167
|
* @param union - The TypeSpec union to mock
|
|
160
168
|
* @returns A JavaScript string representation of a mock for one variant, or undefined if no suitable variant is found
|
|
161
169
|
*/
|
|
162
|
-
function mockUnion(union: Union): string | undefined {
|
|
170
|
+
function mockUnion(ctx: JsContext, module: Module, union: Union): string | undefined {
|
|
163
171
|
for (const variant of union.variants.values()) {
|
|
164
172
|
if (isErrorType(variant.type)) {
|
|
165
173
|
continue;
|
|
166
174
|
}
|
|
167
|
-
return mockType(variant.type);
|
|
175
|
+
return mockType(ctx, module, variant.type);
|
|
168
176
|
}
|
|
169
177
|
|
|
170
178
|
return undefined;
|
|
@@ -177,12 +185,19 @@ function mockUnion(union: Union): string | undefined {
|
|
|
177
185
|
* @param scalar - The TypeSpec scalar to mock
|
|
178
186
|
* @returns A JavaScript string representation of a suitable mock value for the scalar type
|
|
179
187
|
*/
|
|
180
|
-
function mockScalar(scalar: Scalar): string | undefined {
|
|
188
|
+
function mockScalar(ctx: JsContext, module: Module, scalar: Scalar): string | undefined {
|
|
181
189
|
if ($.scalar.isBoolean(scalar) || $.scalar.extendsBoolean(scalar)) {
|
|
182
190
|
return JSON.stringify(true);
|
|
183
191
|
}
|
|
184
192
|
if ($.scalar.isNumeric(scalar) || $.scalar.extendsNumeric(scalar)) {
|
|
185
|
-
|
|
193
|
+
switch ((scalar as Scalar).name) {
|
|
194
|
+
case "integer":
|
|
195
|
+
case "int64":
|
|
196
|
+
case "uint64":
|
|
197
|
+
return "42n";
|
|
198
|
+
default:
|
|
199
|
+
return "42";
|
|
200
|
+
}
|
|
186
201
|
}
|
|
187
202
|
|
|
188
203
|
if ($.scalar.isUtcDateTime(scalar) || $.scalar.extendsUtcDateTime(scalar)) {
|
|
@@ -194,7 +209,12 @@ function mockScalar(scalar: Scalar): string | undefined {
|
|
|
194
209
|
}
|
|
195
210
|
|
|
196
211
|
if ($.scalar.isDuration(scalar) || $.scalar.extendsDuration(scalar)) {
|
|
197
|
-
|
|
212
|
+
module.imports.push({
|
|
213
|
+
from: dateTimeHelper,
|
|
214
|
+
binder: ["Duration"],
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
return 'Duration.parseISO8601("P1Y2M3DT4H5M6S")';
|
|
198
218
|
}
|
|
199
219
|
|
|
200
220
|
if ($.scalar.isOffsetDateTime(scalar) || $.scalar.extendsOffsetDateTime(scalar)) {
|