@tsed/platform-params 8.11.1 → 8.11.2-rc.1
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/lib/esm/builder/PlatformParams.js +0 -1
- package/lib/esm/decorators/bodyParams.js +0 -1
- package/lib/esm/decorators/context.js +0 -1
- package/lib/esm/decorators/cookies.js +0 -1
- package/lib/esm/decorators/headerParams.js +0 -1
- package/lib/esm/decorators/locals.js +0 -1
- package/lib/esm/decorators/paramFn.js +0 -1
- package/lib/esm/decorators/pathParams.js +0 -1
- package/lib/esm/decorators/queryParams.js +0 -1
- package/lib/esm/decorators/session.js +0 -1
- package/lib/esm/decorators/useDeserialization.js +0 -1
- package/lib/esm/decorators/useParam.js +0 -1
- package/lib/esm/decorators/useParamExpression.js +0 -1
- package/lib/esm/decorators/useParamType.js +0 -1
- package/lib/esm/decorators/usePipe.js +0 -1
- package/lib/esm/decorators/useType.js +0 -1
- package/lib/esm/decorators/useValidation.js +0 -1
- package/lib/esm/domain/ParamMetadata.js +0 -1
- package/lib/esm/domain/ParamOptions.js +0 -1
- package/lib/esm/domain/ParamTypes.js +0 -1
- package/lib/esm/errors/ParamValidationError.js +0 -1
- package/lib/esm/errors/RequiredValidationError.js +0 -1
- package/lib/esm/errors/ValidationError.js +0 -1
- package/lib/esm/exports.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/pipes/DeserializerPipe.js +0 -1
- package/lib/esm/pipes/ParseExpressionPipe.js +0 -1
- package/lib/esm/pipes/ValidationPipe.js +0 -1
- package/lib/esm/utils/mapParamsOptions.js +0 -1
- package/package.json +12 -9
- package/lib/esm/builder/PlatformParams.js.map +0 -1
- package/lib/esm/decorators/bodyParams.js.map +0 -1
- package/lib/esm/decorators/context.js.map +0 -1
- package/lib/esm/decorators/cookies.js.map +0 -1
- package/lib/esm/decorators/headerParams.js.map +0 -1
- package/lib/esm/decorators/locals.js.map +0 -1
- package/lib/esm/decorators/paramFn.js.map +0 -1
- package/lib/esm/decorators/pathParams.js.map +0 -1
- package/lib/esm/decorators/queryParams.js.map +0 -1
- package/lib/esm/decorators/session.js.map +0 -1
- package/lib/esm/decorators/useDeserialization.js.map +0 -1
- package/lib/esm/decorators/useParam.js.map +0 -1
- package/lib/esm/decorators/useParamExpression.js.map +0 -1
- package/lib/esm/decorators/useParamType.js.map +0 -1
- package/lib/esm/decorators/usePipe.js.map +0 -1
- package/lib/esm/decorators/useType.js.map +0 -1
- package/lib/esm/decorators/useValidation.js.map +0 -1
- package/lib/esm/domain/ParamMetadata.js.map +0 -1
- package/lib/esm/domain/ParamOptions.js.map +0 -1
- package/lib/esm/domain/ParamTypes.js.map +0 -1
- package/lib/esm/errors/ParamValidationError.js.map +0 -1
- package/lib/esm/errors/RequiredValidationError.js.map +0 -1
- package/lib/esm/errors/ValidationError.js.map +0 -1
- package/lib/esm/exports.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/pipes/DeserializerPipe.js.map +0 -1
- package/lib/esm/pipes/ParseExpressionPipe.js.map +0 -1
- package/lib/esm/pipes/ValidationPipe.js.map +0 -1
- package/lib/esm/utils/mapParamsOptions.js.map +0 -1
- package/src/builder/PlatformParams.spec.ts +0 -431
- package/src/builder/PlatformParams.ts +0 -92
- package/src/decorators/__snapshots__/bodyParams.spec.ts.snap +0 -105
- package/src/decorators/bodyParams.spec.ts +0 -432
- package/src/decorators/bodyParams.ts +0 -93
- package/src/decorators/context.spec.ts +0 -16
- package/src/decorators/context.ts +0 -70
- package/src/decorators/cookies.spec.ts +0 -19
- package/src/decorators/cookies.ts +0 -102
- package/src/decorators/headerParams.spec.ts +0 -16
- package/src/decorators/headerParams.ts +0 -49
- package/src/decorators/locals.spec.ts +0 -16
- package/src/decorators/locals.ts +0 -57
- package/src/decorators/paramFn.ts +0 -14
- package/src/decorators/pathParams.spec.ts +0 -58
- package/src/decorators/pathParams.ts +0 -97
- package/src/decorators/queryParams.spec.ts +0 -58
- package/src/decorators/queryParams.ts +0 -102
- package/src/decorators/session.spec.ts +0 -47
- package/src/decorators/session.ts +0 -51
- package/src/decorators/useDeserialization.ts +0 -14
- package/src/decorators/useParam.spec.ts +0 -82
- package/src/decorators/useParam.ts +0 -38
- package/src/decorators/useParamExpression.ts +0 -16
- package/src/decorators/useParamType.ts +0 -36
- package/src/decorators/usePipe.ts +0 -21
- package/src/decorators/useType.ts +0 -35
- package/src/decorators/useValidation.ts +0 -18
- package/src/domain/ParamMetadata.ts +0 -1
- package/src/domain/ParamOptions.ts +0 -12
- package/src/domain/ParamTypes.ts +0 -37
- package/src/errors/ParamValidationError.spec.ts +0 -152
- package/src/errors/ParamValidationError.ts +0 -38
- package/src/errors/RequiredValidationError.spec.ts +0 -49
- package/src/errors/RequiredValidationError.ts +0 -30
- package/src/errors/ValidationError.spec.ts +0 -18
- package/src/errors/ValidationError.ts +0 -11
- package/src/exports.ts +0 -1
- package/src/index.ts +0 -31
- package/src/pipes/DeserializerPipe.spec.ts +0 -39
- package/src/pipes/DeserializerPipe.ts +0 -15
- package/src/pipes/ParseExpressionPipe.spec.ts +0 -242
- package/src/pipes/ParseExpressionPipe.ts +0 -33
- package/src/pipes/ValidationPipe.spec.ts +0 -645
- package/src/pipes/ValidationPipe.ts +0 -112
- package/src/utils/mapParamsOptions.spec.ts +0 -28
- package/src/utils/mapParamsOptions.ts +0 -34
- package/vitest.config.mts +0 -21
|
@@ -1,431 +0,0 @@
|
|
|
1
|
-
import {Injectable, ProviderScope} from "@tsed/di";
|
|
2
|
-
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
-
|
|
4
|
-
import {buildPlatformParams, invokePlatformParams} from "../../test/helpers/buildPlatformParams.js";
|
|
5
|
-
import {PathParams} from "../decorators/pathParams.js";
|
|
6
|
-
import {QueryParams} from "../decorators/queryParams.js";
|
|
7
|
-
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
8
|
-
|
|
9
|
-
describe("PlatformParams", () => {
|
|
10
|
-
beforeEach(() => PlatformTest.create());
|
|
11
|
-
afterEach(() => PlatformTest.reset());
|
|
12
|
-
describe("getArg()", () => {
|
|
13
|
-
it("should return REQUEST (node)", async () => {
|
|
14
|
-
// GIVEN
|
|
15
|
-
const {param, $ctx, h, platformParams} = await buildPlatformParams({
|
|
16
|
-
paramType: ParamTypes.NODE_REQUEST,
|
|
17
|
-
dataPath: "$ctx.request.req"
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// WHEN
|
|
21
|
-
const pipes = await platformParams.getPipes(param);
|
|
22
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
23
|
-
|
|
24
|
-
// THEN
|
|
25
|
-
expect(value).toEqual($ctx.getReq());
|
|
26
|
-
});
|
|
27
|
-
it("should return REQUEST (framework)", async () => {
|
|
28
|
-
// GIVEN
|
|
29
|
-
const {param, $ctx, h, platformParams} = await buildPlatformParams({
|
|
30
|
-
paramType: ParamTypes.REQUEST,
|
|
31
|
-
dataPath: "$ctx.request.request"
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
// WHEN
|
|
35
|
-
const pipes = await platformParams.getPipes(param);
|
|
36
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
37
|
-
|
|
38
|
-
// THEN
|
|
39
|
-
expect(value).toEqual($ctx.getRequest());
|
|
40
|
-
});
|
|
41
|
-
it("should return REQUEST (platform)", async () => {
|
|
42
|
-
// GIVEN
|
|
43
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
44
|
-
paramType: ParamTypes.PLATFORM_REQUEST,
|
|
45
|
-
dataPath: "$ctx.request"
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// WHEN
|
|
49
|
-
const pipes = await platformParams.getPipes(param);
|
|
50
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
51
|
-
|
|
52
|
-
// THEN
|
|
53
|
-
expect(value).toEqual($ctx.request);
|
|
54
|
-
});
|
|
55
|
-
it("should return RESPONSE (node)", async () => {
|
|
56
|
-
// GIVEN
|
|
57
|
-
const {param, $ctx, h, platformParams} = await buildPlatformParams({
|
|
58
|
-
paramType: ParamTypes.NODE_RESPONSE,
|
|
59
|
-
dataPath: "$ctx.response.res"
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// WHEN
|
|
63
|
-
const pipes = await platformParams.getPipes(param);
|
|
64
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
65
|
-
|
|
66
|
-
// THEN
|
|
67
|
-
expect(value).toEqual($ctx.getRes());
|
|
68
|
-
});
|
|
69
|
-
it("should return RESPONSE (framework)", async () => {
|
|
70
|
-
// GIVEN
|
|
71
|
-
const {param, $ctx, h, platformParams} = await buildPlatformParams({
|
|
72
|
-
paramType: ParamTypes.RESPONSE,
|
|
73
|
-
dataPath: "$ctx.response.response"
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// WHEN
|
|
77
|
-
const pipes = await platformParams.getPipes(param);
|
|
78
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
79
|
-
|
|
80
|
-
// THEN
|
|
81
|
-
expect(value).toEqual($ctx.getResponse());
|
|
82
|
-
});
|
|
83
|
-
it("should return RESPONSE (platform)", async () => {
|
|
84
|
-
// GIVEN
|
|
85
|
-
const {param, $ctx, h, platformParams} = await buildPlatformParams({
|
|
86
|
-
paramType: ParamTypes.PLATFORM_RESPONSE,
|
|
87
|
-
dataPath: "$ctx.response"
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
// WHEN
|
|
91
|
-
const pipes = await platformParams.getPipes(param);
|
|
92
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
93
|
-
|
|
94
|
-
// THEN
|
|
95
|
-
expect(value).toEqual($ctx.response);
|
|
96
|
-
});
|
|
97
|
-
it("should return NEXT", async () => {
|
|
98
|
-
// GIVEN
|
|
99
|
-
const {param, h, platformParams} = await buildPlatformParams({
|
|
100
|
-
paramType: ParamTypes.NEXT_FN,
|
|
101
|
-
dataPath: "next"
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// WHEN
|
|
105
|
-
const pipes = await platformParams.getPipes(param);
|
|
106
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
107
|
-
|
|
108
|
-
// THEN
|
|
109
|
-
expect(value).toBeInstanceOf(Function);
|
|
110
|
-
});
|
|
111
|
-
it("should return ERR", async () => {
|
|
112
|
-
// GIVEN
|
|
113
|
-
const {param, h, platformParams} = await buildPlatformParams({
|
|
114
|
-
paramType: ParamTypes.ERR,
|
|
115
|
-
dataPath: "$ctx.error"
|
|
116
|
-
});
|
|
117
|
-
h.$ctx.error = new Error();
|
|
118
|
-
|
|
119
|
-
// WHEN
|
|
120
|
-
const pipes = await platformParams.getPipes(param);
|
|
121
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
122
|
-
|
|
123
|
-
// THEN
|
|
124
|
-
expect(value).toEqual(h.$ctx.error);
|
|
125
|
-
});
|
|
126
|
-
it("should return $CTX", async () => {
|
|
127
|
-
// GIVEN
|
|
128
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
129
|
-
paramType: ParamTypes.$CTX,
|
|
130
|
-
dataPath: "$ctx"
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// WHEN
|
|
134
|
-
// @ts-ignore
|
|
135
|
-
const pipes = await platformParams.getPipes(param);
|
|
136
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
137
|
-
|
|
138
|
-
// THEN
|
|
139
|
-
expect(value).toEqual($ctx);
|
|
140
|
-
});
|
|
141
|
-
it("should return RESPONSE_DATA", async () => {
|
|
142
|
-
// GIVEN
|
|
143
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
144
|
-
paramType: ParamTypes.RESPONSE_DATA,
|
|
145
|
-
dataPath: "$ctx.data"
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// WHEN
|
|
149
|
-
const pipes = await platformParams.getPipes(param);
|
|
150
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
151
|
-
|
|
152
|
-
// THEN
|
|
153
|
-
expect(value).toEqual($ctx.data);
|
|
154
|
-
});
|
|
155
|
-
it("should return ENDPOINT_INFO", async () => {
|
|
156
|
-
// GIVEN
|
|
157
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
158
|
-
paramType: ParamTypes.ENDPOINT_INFO,
|
|
159
|
-
dataPath: "$ctx.endpoint"
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// @ts-ignore
|
|
163
|
-
$ctx.endpoint = "endpoint";
|
|
164
|
-
|
|
165
|
-
// WHEN
|
|
166
|
-
const pipes = await platformParams.getPipes(param);
|
|
167
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
168
|
-
|
|
169
|
-
// THEN
|
|
170
|
-
expect(value).toEqual($ctx.endpoint);
|
|
171
|
-
});
|
|
172
|
-
it("should return BODY", async () => {
|
|
173
|
-
// GIVEN
|
|
174
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
175
|
-
paramType: ParamTypes.BODY,
|
|
176
|
-
dataPath: "$ctx.request.body"
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// WHEN
|
|
180
|
-
const pipes = await platformParams.getPipes(param);
|
|
181
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
182
|
-
|
|
183
|
-
// THEN
|
|
184
|
-
expect(value).toEqual($ctx.getRequest().body);
|
|
185
|
-
});
|
|
186
|
-
it("should return PATH", async () => {
|
|
187
|
-
// GIVEN
|
|
188
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
189
|
-
paramType: ParamTypes.PATH,
|
|
190
|
-
dataPath: "$ctx.request.params"
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
// WHEN
|
|
194
|
-
const pipes = await platformParams.getPipes(param);
|
|
195
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
196
|
-
|
|
197
|
-
// THEN
|
|
198
|
-
expect(value).toEqual($ctx.getRequest().params);
|
|
199
|
-
});
|
|
200
|
-
it("should return QUERY", async () => {
|
|
201
|
-
// GIVEN
|
|
202
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
203
|
-
paramType: ParamTypes.QUERY,
|
|
204
|
-
dataPath: "$ctx.request.query"
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
// WHEN
|
|
208
|
-
const pipes = await platformParams.getPipes(param);
|
|
209
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
210
|
-
|
|
211
|
-
// THEN
|
|
212
|
-
expect(value).toEqual($ctx.getRequest().query);
|
|
213
|
-
});
|
|
214
|
-
it("should return HEADER", async () => {
|
|
215
|
-
// GIVEN
|
|
216
|
-
const {param, h, platformParams, ctx} = await buildPlatformParams({
|
|
217
|
-
paramType: ParamTypes.HEADER,
|
|
218
|
-
dataPath: "$ctx.request.headers"
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
ctx.request.raw.headers["accept"] = "application/json";
|
|
222
|
-
ctx.request.raw.headers["content-type"] = "application/json";
|
|
223
|
-
|
|
224
|
-
// WHEN
|
|
225
|
-
const pipes = await platformParams.getPipes(param);
|
|
226
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
227
|
-
|
|
228
|
-
// THEN
|
|
229
|
-
expect(value).toEqual({
|
|
230
|
-
accept: "application/json",
|
|
231
|
-
"content-type": "application/json"
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
it("should return COOKIES", async () => {
|
|
235
|
-
// GIVEN
|
|
236
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
237
|
-
paramType: ParamTypes.COOKIES,
|
|
238
|
-
dataPath: "$ctx.request.cookies"
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
// WHEN
|
|
242
|
-
const pipes = await platformParams.getPipes(param);
|
|
243
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
244
|
-
|
|
245
|
-
// THEN
|
|
246
|
-
expect(value).toEqual($ctx.getRequest().cookies);
|
|
247
|
-
});
|
|
248
|
-
it("should return SESSION", async () => {
|
|
249
|
-
// GIVEN
|
|
250
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
251
|
-
paramType: ParamTypes.SESSION,
|
|
252
|
-
dataPath: "$ctx.request.session"
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
// WHEN
|
|
256
|
-
const pipes = await platformParams.getPipes(param);
|
|
257
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
258
|
-
|
|
259
|
-
// THEN
|
|
260
|
-
expect(value).toEqual($ctx.request.session);
|
|
261
|
-
});
|
|
262
|
-
it("should return LOCALS", async () => {
|
|
263
|
-
// GIVEN
|
|
264
|
-
const {param, h, $ctx, platformParams} = await buildPlatformParams({
|
|
265
|
-
paramType: ParamTypes.LOCALS,
|
|
266
|
-
dataPath: "$ctx.response.locals"
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
h.$ctx.error = new Error();
|
|
270
|
-
|
|
271
|
-
// WHEN
|
|
272
|
-
const pipes = await platformParams.getPipes(param);
|
|
273
|
-
const value = await platformParams.getArg(h, pipes, param);
|
|
274
|
-
|
|
275
|
-
// THEN
|
|
276
|
-
expect(value).toEqual($ctx.getResponse().locals);
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
describe("compileHandler()", () => {
|
|
280
|
-
it("should build all params and return value (SINGLETON)", async () => {
|
|
281
|
-
const platformParams = await invokePlatformParams();
|
|
282
|
-
|
|
283
|
-
@Injectable()
|
|
284
|
-
class MyCtrTest {
|
|
285
|
-
get(@PathParams() params: any, @QueryParams() query: any) {
|
|
286
|
-
return {params, query};
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
PlatformTest.injector.addProvider(MyCtrTest);
|
|
291
|
-
|
|
292
|
-
const handler = await platformParams.compileHandler({
|
|
293
|
-
token: MyCtrTest,
|
|
294
|
-
propertyKey: "get"
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
const $ctx = PlatformTest.createRequestContext({
|
|
298
|
-
event: {
|
|
299
|
-
request: {
|
|
300
|
-
query: {
|
|
301
|
-
test: "test"
|
|
302
|
-
},
|
|
303
|
-
params: {
|
|
304
|
-
s: "s"
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
const result = await handler({
|
|
311
|
-
$ctx
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
expect(result).toEqual({
|
|
315
|
-
query: {
|
|
316
|
-
test: "test"
|
|
317
|
-
},
|
|
318
|
-
params: {
|
|
319
|
-
s: "s"
|
|
320
|
-
}
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
it("should build all params and return value (REQUEST)", async () => {
|
|
324
|
-
const platformParams = await invokePlatformParams();
|
|
325
|
-
|
|
326
|
-
@Injectable({
|
|
327
|
-
scope: ProviderScope.REQUEST
|
|
328
|
-
})
|
|
329
|
-
class MyCtrTest {
|
|
330
|
-
get(@PathParams() params: any, @QueryParams() query: any) {
|
|
331
|
-
return {params, query};
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
const $ctx = PlatformTest.createRequestContext({
|
|
336
|
-
event: {
|
|
337
|
-
request: {
|
|
338
|
-
query: {
|
|
339
|
-
test: "test"
|
|
340
|
-
},
|
|
341
|
-
params: {
|
|
342
|
-
s: "s"
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
PlatformTest.injector.addProvider(MyCtrTest);
|
|
349
|
-
|
|
350
|
-
const handler = await platformParams.compileHandler({
|
|
351
|
-
token: MyCtrTest,
|
|
352
|
-
propertyKey: "get"
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
const result = await handler({
|
|
356
|
-
$ctx
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
expect(result).toEqual({
|
|
360
|
-
query: {
|
|
361
|
-
test: "test"
|
|
362
|
-
},
|
|
363
|
-
params: {
|
|
364
|
-
s: "s"
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
});
|
|
368
|
-
it("should call without args", async () => {
|
|
369
|
-
const platformParams = await invokePlatformParams();
|
|
370
|
-
|
|
371
|
-
@Injectable()
|
|
372
|
-
class MyCtrTest {
|
|
373
|
-
get() {
|
|
374
|
-
return "test";
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
PlatformTest.injector.addProvider(MyCtrTest);
|
|
379
|
-
|
|
380
|
-
const $ctx = PlatformTest.createRequestContext({
|
|
381
|
-
event: {
|
|
382
|
-
request: {
|
|
383
|
-
query: {
|
|
384
|
-
test: "test"
|
|
385
|
-
},
|
|
386
|
-
params: {
|
|
387
|
-
s: "s"
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
|
|
393
|
-
const handler = await platformParams.compileHandler({
|
|
394
|
-
token: MyCtrTest,
|
|
395
|
-
propertyKey: "get"
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
const result = await handler({
|
|
399
|
-
$ctx
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
expect(result).toEqual("test");
|
|
403
|
-
});
|
|
404
|
-
it("should return handler", async () => {
|
|
405
|
-
const platformParams = await invokePlatformParams();
|
|
406
|
-
|
|
407
|
-
const $ctx = PlatformTest.createRequestContext({
|
|
408
|
-
event: {
|
|
409
|
-
request: {
|
|
410
|
-
query: {
|
|
411
|
-
test: "test"
|
|
412
|
-
},
|
|
413
|
-
params: {
|
|
414
|
-
s: "s"
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
const handler = await platformParams.compileHandler({
|
|
421
|
-
handler: ($ctx: any) => $ctx.request.query.test
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
const result = await handler({
|
|
425
|
-
$ctx
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
expect(result).toEqual("test");
|
|
429
|
-
});
|
|
430
|
-
});
|
|
431
|
-
});
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import {DIContext, Inject, Injectable, injectable, injector, InjectorService, ProviderScope, TokenProvider} from "@tsed/di";
|
|
2
|
-
import {JsonMethodStore, JsonParameterStore, PipeMethods} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {ParamValidationError} from "../errors/ParamValidationError.js";
|
|
5
|
-
import {ParseExpressionPipe} from "../pipes/ParseExpressionPipe.js";
|
|
6
|
-
|
|
7
|
-
export type PlatformParamsScope<Context extends DIContext = DIContext> = {$ctx: Context} & Record<string, any>;
|
|
8
|
-
export type PlatformParamsCallback<Context extends DIContext = DIContext> = (scope: PlatformParamsScope<Context>) => Promise<any>;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Platform Params abstraction layer.
|
|
12
|
-
* @platform
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
export class PlatformParams {
|
|
16
|
-
getPipes(param: JsonParameterStore) {
|
|
17
|
-
const get = (pipe: TokenProvider) => {
|
|
18
|
-
return injector().getProvider(pipe)!.priority || 0;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const sort = (p1: TokenProvider, p2: TokenProvider) => (get(p1) < get(p2) ? -1 : get(p1) > get(p2) ? 1 : 0);
|
|
22
|
-
const map = (token: TokenProvider) => injector().get<PipeMethods>(token)!;
|
|
23
|
-
|
|
24
|
-
return [ParseExpressionPipe, ...param.pipes.sort(sort)].map(map).filter(Boolean);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Return a handler with injectable parameters
|
|
29
|
-
* @param handlerMetadata
|
|
30
|
-
*/
|
|
31
|
-
compileHandler<Context extends DIContext = DIContext>({
|
|
32
|
-
propertyKey,
|
|
33
|
-
token,
|
|
34
|
-
handler
|
|
35
|
-
}: {
|
|
36
|
-
propertyKey?: string | symbol;
|
|
37
|
-
token?: any;
|
|
38
|
-
handler?: any;
|
|
39
|
-
}): PlatformParamsCallback<Context> {
|
|
40
|
-
if (!token || !propertyKey) {
|
|
41
|
-
return (scope: PlatformParamsScope<Context>) => handler(scope.$ctx);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const inj = injector();
|
|
45
|
-
const store = JsonMethodStore.fromMethod(token, propertyKey);
|
|
46
|
-
const getArguments = this.compile<Context>(store);
|
|
47
|
-
const provider = inj.getProvider(token)!;
|
|
48
|
-
|
|
49
|
-
return async (scope: PlatformParamsScope<Context>) => {
|
|
50
|
-
const container = provider.scope === ProviderScope.REQUEST ? scope.$ctx.container : undefined;
|
|
51
|
-
|
|
52
|
-
const [instance, args] = await Promise.all([inj.invoke<any>(token, {locals: container}), getArguments(scope)]);
|
|
53
|
-
|
|
54
|
-
return instance[propertyKey].call(instance, ...args, scope.$ctx);
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
compile<Context extends DIContext = DIContext>(entity: JsonMethodStore): PlatformParamsCallback<Context> {
|
|
59
|
-
const params = JsonParameterStore.getParams(entity.target, entity.propertyKey);
|
|
60
|
-
const argsPipes = params.map((param) => {
|
|
61
|
-
return {
|
|
62
|
-
param,
|
|
63
|
-
pipes: this.getPipes(param)
|
|
64
|
-
};
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return (scope: PlatformParamsScope<Context>) => {
|
|
68
|
-
const promises = argsPipes.map(({param, pipes}) => this.getArg<Context>(scope, pipes, param));
|
|
69
|
-
return Promise.all(promises);
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async getArg<Context extends DIContext = DIContext>(
|
|
74
|
-
scope: PlatformParamsScope<Context>,
|
|
75
|
-
pipes: PipeMethods[],
|
|
76
|
-
param: JsonParameterStore
|
|
77
|
-
): Promise<any> {
|
|
78
|
-
try {
|
|
79
|
-
let value = scope;
|
|
80
|
-
|
|
81
|
-
for await (const pipe of pipes) {
|
|
82
|
-
value = await pipe.transform(value, param);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return value;
|
|
86
|
-
} catch (er) {
|
|
87
|
-
throw ParamValidationError.from(param, er);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
injectable(PlatformParams).imports([ParseExpressionPipe]);
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
-
|
|
3
|
-
exports[`@BodyParams > should generate the correct spec for BodyParams from given arg 1`] = `
|
|
4
|
-
{
|
|
5
|
-
"components": {
|
|
6
|
-
"schemas": {
|
|
7
|
-
"TestModel": {
|
|
8
|
-
"properties": {
|
|
9
|
-
"name": {
|
|
10
|
-
"minLength": 1,
|
|
11
|
-
"type": "string",
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
"required": [
|
|
15
|
-
"name",
|
|
16
|
-
],
|
|
17
|
-
"type": "object",
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
"paths": {
|
|
22
|
-
"/": {
|
|
23
|
-
"post": {
|
|
24
|
-
"operationId": "myCtrlTest",
|
|
25
|
-
"parameters": [],
|
|
26
|
-
"requestBody": {
|
|
27
|
-
"content": {
|
|
28
|
-
"application/json": {
|
|
29
|
-
"schema": {
|
|
30
|
-
"$ref": "#/components/schemas/TestModel",
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
"required": false,
|
|
35
|
-
},
|
|
36
|
-
"responses": {
|
|
37
|
-
"200": {
|
|
38
|
-
"description": "Success",
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
"tags": [
|
|
42
|
-
"MyCtrl",
|
|
43
|
-
],
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
"tags": [
|
|
48
|
-
{
|
|
49
|
-
"name": "MyCtrl",
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
}
|
|
53
|
-
`;
|
|
54
|
-
|
|
55
|
-
exports[`@BodyParams > should generate the correct spec for BodyParams from metadata 1`] = `
|
|
56
|
-
{
|
|
57
|
-
"components": {
|
|
58
|
-
"schemas": {
|
|
59
|
-
"TestModel": {
|
|
60
|
-
"properties": {
|
|
61
|
-
"name": {
|
|
62
|
-
"minLength": 1,
|
|
63
|
-
"type": "string",
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
"required": [
|
|
67
|
-
"name",
|
|
68
|
-
],
|
|
69
|
-
"type": "object",
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
"paths": {
|
|
74
|
-
"/": {
|
|
75
|
-
"post": {
|
|
76
|
-
"operationId": "myCtrlTest",
|
|
77
|
-
"parameters": [],
|
|
78
|
-
"requestBody": {
|
|
79
|
-
"content": {
|
|
80
|
-
"application/json": {
|
|
81
|
-
"schema": {
|
|
82
|
-
"$ref": "#/components/schemas/TestModel",
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
"required": false,
|
|
87
|
-
},
|
|
88
|
-
"responses": {
|
|
89
|
-
"200": {
|
|
90
|
-
"description": "Success",
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
"tags": [
|
|
94
|
-
"MyCtrl",
|
|
95
|
-
],
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
"tags": [
|
|
100
|
-
{
|
|
101
|
-
"name": "MyCtrl",
|
|
102
|
-
},
|
|
103
|
-
],
|
|
104
|
-
}
|
|
105
|
-
`;
|