@typespec/http-specs 0.1.0-alpha.24-dev.1 → 0.1.0-alpha.24-dev.2
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/dist/specs/type/union/discriminated/mockapi.d.ts +3 -0
- package/dist/specs/type/union/discriminated/mockapi.d.ts.map +1 -0
- package/dist/specs/type/union/discriminated/mockapi.js +212 -0
- package/dist/specs/type/union/discriminated/mockapi.js.map +1 -0
- package/package.json +1 -1
- package/spec-summary.md +172 -0
- package/specs/type/union/discriminated/main.tsp +251 -0
- package/specs/type/union/discriminated/mockapi.ts +230 -0
- package/temp/.tsbuildinfo +1 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { json, MockRequest, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";
|
|
2
|
+
|
|
3
|
+
export const Scenarios: Record<string, ScenarioMockApi> = {};
|
|
4
|
+
|
|
5
|
+
// Test data for discriminated union scenarios
|
|
6
|
+
const catData = {
|
|
7
|
+
name: "Whiskers",
|
|
8
|
+
meow: true,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const dogData = {
|
|
12
|
+
name: "Rex",
|
|
13
|
+
bark: false,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Envelope discriminated union (default serialization)
|
|
17
|
+
const envelopeCatBody = {
|
|
18
|
+
kind: "cat",
|
|
19
|
+
value: catData,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const envelopeDogBody = {
|
|
23
|
+
kind: "dog",
|
|
24
|
+
value: dogData,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
Scenarios.Type_Union_Discriminated_Envelope_Object_Default_get = passOnSuccess({
|
|
28
|
+
uri: "/type/union/discriminated/envelope/object/default",
|
|
29
|
+
method: "get",
|
|
30
|
+
request: {},
|
|
31
|
+
response: {
|
|
32
|
+
status: 200,
|
|
33
|
+
body: json(envelopeCatBody),
|
|
34
|
+
},
|
|
35
|
+
handler: (req: MockRequest) => {
|
|
36
|
+
const kind = req.query.kind as string | undefined;
|
|
37
|
+
|
|
38
|
+
// When kind is null or "cat", return response for "cat"
|
|
39
|
+
// When kind is "dog", return response for "dog"
|
|
40
|
+
if (kind === "dog") {
|
|
41
|
+
return {
|
|
42
|
+
status: 200,
|
|
43
|
+
body: json(envelopeDogBody),
|
|
44
|
+
};
|
|
45
|
+
} else {
|
|
46
|
+
// Default case: when kind is null, undefined, or "cat"
|
|
47
|
+
return {
|
|
48
|
+
status: 200,
|
|
49
|
+
body: json(envelopeCatBody),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
kind: "MockApiDefinition",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
Scenarios.Type_Union_Discriminated_Envelope_Object_Default_put = passOnSuccess({
|
|
57
|
+
uri: "/type/union/discriminated/envelope/object/default",
|
|
58
|
+
method: "put",
|
|
59
|
+
request: {
|
|
60
|
+
body: json(envelopeCatBody),
|
|
61
|
+
},
|
|
62
|
+
response: {
|
|
63
|
+
status: 200,
|
|
64
|
+
body: json(envelopeCatBody),
|
|
65
|
+
},
|
|
66
|
+
kind: "MockApiDefinition",
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Custom names discriminated union
|
|
70
|
+
const customNamesCatBody = {
|
|
71
|
+
petType: "cat",
|
|
72
|
+
petData: catData,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const customNamesDogBody = {
|
|
76
|
+
petType: "dog",
|
|
77
|
+
petData: dogData,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
Scenarios.Type_Union_Discriminated_Envelope_Object_CustomProperties_get = passOnSuccess({
|
|
81
|
+
uri: "/type/union/discriminated/envelope/object/custom-properties",
|
|
82
|
+
method: "get",
|
|
83
|
+
request: {},
|
|
84
|
+
response: {
|
|
85
|
+
status: 200,
|
|
86
|
+
body: json(customNamesCatBody),
|
|
87
|
+
},
|
|
88
|
+
handler: (req: MockRequest) => {
|
|
89
|
+
const petType = req.query.petType as string | undefined;
|
|
90
|
+
|
|
91
|
+
// When petType is null or "cat", return response for "cat"
|
|
92
|
+
// When petType is "dog", return response for "dog"
|
|
93
|
+
if (petType === "dog") {
|
|
94
|
+
return {
|
|
95
|
+
status: 200,
|
|
96
|
+
body: json(customNamesDogBody),
|
|
97
|
+
};
|
|
98
|
+
} else {
|
|
99
|
+
// Default case: when petType is null, undefined, or "cat"
|
|
100
|
+
return {
|
|
101
|
+
status: 200,
|
|
102
|
+
body: json(customNamesCatBody),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
kind: "MockApiDefinition",
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
Scenarios.Type_Union_Discriminated_Envelope_Object_CustomProperties_put = passOnSuccess({
|
|
110
|
+
uri: "/type/union/discriminated/envelope/object/custom-properties",
|
|
111
|
+
method: "put",
|
|
112
|
+
request: {
|
|
113
|
+
body: json(customNamesCatBody),
|
|
114
|
+
},
|
|
115
|
+
response: {
|
|
116
|
+
status: 200,
|
|
117
|
+
body: json(customNamesCatBody),
|
|
118
|
+
},
|
|
119
|
+
kind: "MockApiDefinition",
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Inline discriminated union (no envelope)
|
|
123
|
+
const inlineCatBody = {
|
|
124
|
+
kind: "cat",
|
|
125
|
+
name: "Whiskers",
|
|
126
|
+
meow: true,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const inlineDogBody = {
|
|
130
|
+
kind: "dog",
|
|
131
|
+
name: "Rex",
|
|
132
|
+
bark: false,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
Scenarios.Type_Union_Discriminated_NoEnvelope_Default_get = passOnSuccess({
|
|
136
|
+
uri: "/type/union/discriminated/no-envelope/default",
|
|
137
|
+
method: "get",
|
|
138
|
+
request: {},
|
|
139
|
+
response: {
|
|
140
|
+
status: 200,
|
|
141
|
+
body: json(inlineCatBody),
|
|
142
|
+
},
|
|
143
|
+
handler: (req: MockRequest) => {
|
|
144
|
+
const kind = req.query.kind as string | undefined;
|
|
145
|
+
|
|
146
|
+
// When kind is null or "cat", return response for "cat"
|
|
147
|
+
// When kind is "dog", return response for "dog"
|
|
148
|
+
if (kind === "dog") {
|
|
149
|
+
return {
|
|
150
|
+
status: 200,
|
|
151
|
+
body: json(inlineDogBody),
|
|
152
|
+
};
|
|
153
|
+
} else {
|
|
154
|
+
// Default case: when kind is null, undefined, or "cat"
|
|
155
|
+
return {
|
|
156
|
+
status: 200,
|
|
157
|
+
body: json(inlineCatBody),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
kind: "MockApiDefinition",
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
Scenarios.Type_Union_Discriminated_NoEnvelope_Default_put = passOnSuccess({
|
|
165
|
+
uri: "/type/union/discriminated/no-envelope/default",
|
|
166
|
+
method: "put",
|
|
167
|
+
request: {
|
|
168
|
+
body: json(inlineCatBody),
|
|
169
|
+
},
|
|
170
|
+
response: {
|
|
171
|
+
status: 200,
|
|
172
|
+
body: json(inlineCatBody),
|
|
173
|
+
},
|
|
174
|
+
kind: "MockApiDefinition",
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Inline discriminated union with custom discriminator property name
|
|
178
|
+
const inlineCustomCatBody = {
|
|
179
|
+
type: "cat",
|
|
180
|
+
name: "Whiskers",
|
|
181
|
+
meow: true,
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const inlineCustomDogBody = {
|
|
185
|
+
type: "dog",
|
|
186
|
+
name: "Rex",
|
|
187
|
+
bark: false,
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
Scenarios.Type_Union_Discriminated_NoEnvelope_CustomDiscriminator_get = passOnSuccess({
|
|
191
|
+
uri: "/type/union/discriminated/no-envelope/custom-discriminator",
|
|
192
|
+
method: "get",
|
|
193
|
+
request: {},
|
|
194
|
+
response: {
|
|
195
|
+
status: 200,
|
|
196
|
+
body: json(inlineCustomCatBody),
|
|
197
|
+
},
|
|
198
|
+
handler: (req: MockRequest) => {
|
|
199
|
+
const type = req.query.type as string | undefined;
|
|
200
|
+
|
|
201
|
+
// When type is null or "cat", return response for "cat"
|
|
202
|
+
// When type is "dog", return response for "dog"
|
|
203
|
+
if (type === "dog") {
|
|
204
|
+
return {
|
|
205
|
+
status: 200,
|
|
206
|
+
body: json(inlineCustomDogBody),
|
|
207
|
+
};
|
|
208
|
+
} else {
|
|
209
|
+
// Default case: when type is null, undefined, or "cat"
|
|
210
|
+
return {
|
|
211
|
+
status: 200,
|
|
212
|
+
body: json(inlineCustomCatBody),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
kind: "MockApiDefinition",
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
Scenarios.Type_Union_Discriminated_NoEnvelope_CustomDiscriminator_put = passOnSuccess({
|
|
220
|
+
uri: "/type/union/discriminated/no-envelope/custom-discriminator",
|
|
221
|
+
method: "put",
|
|
222
|
+
request: {
|
|
223
|
+
body: json(inlineCustomCatBody),
|
|
224
|
+
},
|
|
225
|
+
response: {
|
|
226
|
+
status: 200,
|
|
227
|
+
body: json(inlineCustomCatBody),
|
|
228
|
+
},
|
|
229
|
+
kind: "MockApiDefinition",
|
|
230
|
+
});
|