@tahminator/sapling 1.5.28-beta.583b1882 → 1.5.28-beta.7e624925
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/index.cjs +57 -36
- package/dist/index.d.cts +24 -14
- package/dist/index.d.mts +24 -14
- package/dist/index.mjs +57 -37
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -221,6 +221,22 @@ var ResponseEntityBuilder = class {
|
|
|
221
221
|
}
|
|
222
222
|
};
|
|
223
223
|
//#endregion
|
|
224
|
+
//#region src/helper/error/responsestatus.ts
|
|
225
|
+
/**
|
|
226
|
+
* Ensure that you define a middleware that can handle this error.
|
|
227
|
+
*
|
|
228
|
+
* @see {@link Sapling.loadResponseStatusErrorMiddleware}
|
|
229
|
+
*/
|
|
230
|
+
var ResponseStatusError = class ResponseStatusError extends Error {
|
|
231
|
+
constructor(status, message) {
|
|
232
|
+
super(message ?? "Something went wrong.");
|
|
233
|
+
this.status = status;
|
|
234
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
235
|
+
this.name = `HttpError(${HttpStatus[status]})`;
|
|
236
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, ResponseStatusError);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
//#endregion
|
|
224
240
|
//#region src/helper/sapling.ts
|
|
225
241
|
const settings = {
|
|
226
242
|
serialize: JSON.stringify,
|
|
@@ -359,22 +375,6 @@ var Sapling = class Sapling {
|
|
|
359
375
|
}
|
|
360
376
|
};
|
|
361
377
|
//#endregion
|
|
362
|
-
//#region src/helper/error.ts
|
|
363
|
-
/**
|
|
364
|
-
* Ensure that you define a middleware that can handle this error.
|
|
365
|
-
*
|
|
366
|
-
* @see {@link Sapling.loadResponseStatusErrorMiddleware}
|
|
367
|
-
*/
|
|
368
|
-
var ResponseStatusError = class ResponseStatusError extends Error {
|
|
369
|
-
constructor(status, message) {
|
|
370
|
-
super(message ?? "Something went wrong.");
|
|
371
|
-
this.status = status;
|
|
372
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
373
|
-
this.name = `HttpError(${HttpStatus[status]})`;
|
|
374
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, ResponseStatusError);
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
//#endregion
|
|
378
378
|
//#region src/types.ts
|
|
379
379
|
const methodResolve = {
|
|
380
380
|
GET: "get",
|
|
@@ -507,14 +507,40 @@ function _resolve(ctor) {
|
|
|
507
507
|
return _InjectableRegistry.get(ctor);
|
|
508
508
|
}
|
|
509
509
|
//#endregion
|
|
510
|
+
//#region src/helper/error/exception.ts
|
|
511
|
+
/**
|
|
512
|
+
* This error should be thrown when some data cannot be parsed by a given schema.
|
|
513
|
+
*/
|
|
514
|
+
var ParserError = class ParserError extends ResponseStatusError {
|
|
515
|
+
constructor(location, issues, vendor) {
|
|
516
|
+
super(400, ParserError.formatMessage(location, issues, vendor));
|
|
517
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
518
|
+
}
|
|
519
|
+
static formatMessage(location, issues, vendor) {
|
|
520
|
+
const formatted = issues.map((i) => {
|
|
521
|
+
const path = Array.isArray(i.path) ? i.path.map((seg) => typeof seg === "object" && seg ? String(seg.key) : String(seg)).join(".") : "";
|
|
522
|
+
return path ? `${path}: ${i.message}` : i.message;
|
|
523
|
+
}).join("; ");
|
|
524
|
+
return `${vendor} failed to parse ${(() => {
|
|
525
|
+
switch (location) {
|
|
526
|
+
case "reqbody": return "request body";
|
|
527
|
+
case "reqparams": return "request params";
|
|
528
|
+
case "reqquery": return "request query";
|
|
529
|
+
}
|
|
530
|
+
})()}: ${formatted}`;
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
//#endregion
|
|
510
534
|
//#region src/annotation/request.ts
|
|
511
535
|
const _requestSchemaStore = /* @__PURE__ */ new WeakMap();
|
|
512
|
-
function
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
536
|
+
function _getOrCreateRequestSchemaDefinition(ctor, fnName) {
|
|
537
|
+
const byFn = (() => {
|
|
538
|
+
const fn = _requestSchemaStore.get(ctor);
|
|
539
|
+
if (fn) return fn;
|
|
540
|
+
const newFn = /* @__PURE__ */ new Map();
|
|
541
|
+
_requestSchemaStore.set(ctor, newFn);
|
|
542
|
+
return newFn;
|
|
543
|
+
})();
|
|
518
544
|
const existing = byFn.get(fnName);
|
|
519
545
|
if (existing) return existing;
|
|
520
546
|
const created = {};
|
|
@@ -529,36 +555,30 @@ function RequestBody(schema) {
|
|
|
529
555
|
return (target, propertyKey) => {
|
|
530
556
|
const ctor = target.constructor;
|
|
531
557
|
const fnName = String(propertyKey);
|
|
532
|
-
_setOnce(
|
|
558
|
+
_setOnce(_getOrCreateRequestSchemaDefinition(ctor, fnName), "body", schema, fnName);
|
|
533
559
|
};
|
|
534
560
|
}
|
|
535
561
|
function RequestParam(schema) {
|
|
536
562
|
return (target, propertyKey) => {
|
|
537
563
|
const ctor = target.constructor;
|
|
538
564
|
const fnName = String(propertyKey);
|
|
539
|
-
_setOnce(
|
|
565
|
+
_setOnce(_getOrCreateRequestSchemaDefinition(ctor, fnName), "param", schema, fnName);
|
|
540
566
|
};
|
|
541
567
|
}
|
|
542
568
|
function RequestQuery(schema) {
|
|
543
569
|
return (target, propertyKey) => {
|
|
544
570
|
const ctor = target.constructor;
|
|
545
571
|
const fnName = String(propertyKey);
|
|
546
|
-
_setOnce(
|
|
572
|
+
_setOnce(_getOrCreateRequestSchemaDefinition(ctor, fnName), "query", schema, fnName);
|
|
547
573
|
};
|
|
548
574
|
}
|
|
549
575
|
function _getRequestSchemas(ctor, fnName) {
|
|
550
576
|
return _requestSchemaStore.get(ctor)?.get(fnName);
|
|
551
577
|
}
|
|
552
|
-
function _formatIssues(issues) {
|
|
553
|
-
return issues.map((i) => {
|
|
554
|
-
const path = Array.isArray(i.path) ? i.path.map((seg) => typeof seg === "object" && seg ? String(seg.key) : String(seg)).join(".") : "";
|
|
555
|
-
return path ? `${path}: ${i.message}` : i.message;
|
|
556
|
-
}).join("; ");
|
|
557
|
-
}
|
|
558
578
|
async function _parseOrThrow(schema, input, kind) {
|
|
559
579
|
const result = await schema["~standard"].validate(input);
|
|
560
|
-
if (
|
|
561
|
-
|
|
580
|
+
if (result.issues) throw new ParserError(kind, result.issues, schema["~standard"].vendor);
|
|
581
|
+
return result.value;
|
|
562
582
|
}
|
|
563
583
|
//#endregion
|
|
564
584
|
//#region src/annotation/route.ts
|
|
@@ -669,9 +689,9 @@ function Controller({ prefix = "", deps = [] } = {}) {
|
|
|
669
689
|
router[methodName](fp, async (request, response, next) => {
|
|
670
690
|
const schemas = _getRequestSchemas(target, fnName);
|
|
671
691
|
if (schemas) {
|
|
672
|
-
if (schemas.body) request.body = await _parseOrThrow(schemas.body, request.body, "
|
|
673
|
-
if (schemas.param) request.params = await _parseOrThrow(schemas.param, request.params, "
|
|
674
|
-
if (schemas.query) request.query = await _parseOrThrow(schemas.query, request.query, "
|
|
692
|
+
if (schemas.body) request.body = await _parseOrThrow(schemas.body, request.body, "reqbody");
|
|
693
|
+
if (schemas.param) request.params = await _parseOrThrow(schemas.param, request.params, "reqparams");
|
|
694
|
+
if (schemas.query) request.query = await _parseOrThrow(schemas.query, request.query, "reqquery");
|
|
675
695
|
}
|
|
676
696
|
const result = await fn.bind(controllerInstance)(request, response, next);
|
|
677
697
|
if (method === "USE") return;
|
|
@@ -715,6 +735,7 @@ exports.OPTIONS = OPTIONS;
|
|
|
715
735
|
exports.PATCH = PATCH;
|
|
716
736
|
exports.POST = POST;
|
|
717
737
|
exports.PUT = PUT;
|
|
738
|
+
exports.ParserError = ParserError;
|
|
718
739
|
exports.RedirectView = RedirectView;
|
|
719
740
|
exports.RequestBody = RequestBody;
|
|
720
741
|
exports.RequestParam = RequestParam;
|
package/dist/index.d.cts
CHANGED
|
@@ -231,18 +231,6 @@ declare namespace StandardSchemaV1 {
|
|
|
231
231
|
}
|
|
232
232
|
/** The Standard JSON Schema interface. */
|
|
233
233
|
//#endregion
|
|
234
|
-
//#region src/annotation/request.d.ts
|
|
235
|
-
type RequestSchemaDefinition = {
|
|
236
|
-
body?: StandardSchemaV1;
|
|
237
|
-
param?: StandardSchemaV1;
|
|
238
|
-
query?: StandardSchemaV1;
|
|
239
|
-
};
|
|
240
|
-
declare function RequestBody(schema: StandardSchemaV1): MethodDecorator;
|
|
241
|
-
declare function RequestParam(schema: StandardSchemaV1): MethodDecorator;
|
|
242
|
-
declare function RequestQuery(schema: StandardSchemaV1): MethodDecorator;
|
|
243
|
-
declare function _getRequestSchemas(ctor: Function, fnName: string): RequestSchemaDefinition | undefined;
|
|
244
|
-
declare function _parseOrThrow<TSchema extends StandardSchemaV1>(schema: TSchema, input: unknown, kind: "body" | "params" | "query"): Promise<StandardSchemaV1.InferOutput<TSchema>>;
|
|
245
|
-
//#endregion
|
|
246
234
|
//#region src/helper/redirect.d.ts
|
|
247
235
|
/**
|
|
248
236
|
* Generic HTTP redirect wrapped modeled after Spring's `RedirectView`.
|
|
@@ -398,7 +386,7 @@ declare enum HttpStatus {
|
|
|
398
386
|
NETWORK_AUTHENTICATION_REQUIRED = 511
|
|
399
387
|
}
|
|
400
388
|
//#endregion
|
|
401
|
-
//#region src/helper/error.d.ts
|
|
389
|
+
//#region src/helper/error/responsestatus.d.ts
|
|
402
390
|
/**
|
|
403
391
|
* Ensure that you define a middleware that can handle this error.
|
|
404
392
|
*
|
|
@@ -508,4 +496,26 @@ declare class Sapling {
|
|
|
508
496
|
static setDeserializeFn(this: void, fn: (value: string) => any): void;
|
|
509
497
|
}
|
|
510
498
|
//#endregion
|
|
511
|
-
|
|
499
|
+
//#region src/helper/error/exception.d.ts
|
|
500
|
+
type ParserErrorLocation = "reqbody" | "reqparams" | "reqquery";
|
|
501
|
+
/**
|
|
502
|
+
* This error should be thrown when some data cannot be parsed by a given schema.
|
|
503
|
+
*/
|
|
504
|
+
declare class ParserError extends ResponseStatusError {
|
|
505
|
+
constructor(location: ParserErrorLocation, issues: readonly StandardSchemaV1.Issue[], vendor: string);
|
|
506
|
+
private static formatMessage;
|
|
507
|
+
}
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/annotation/request.d.ts
|
|
510
|
+
type RequestSchemaDefinition = {
|
|
511
|
+
body?: StandardSchemaV1;
|
|
512
|
+
param?: StandardSchemaV1;
|
|
513
|
+
query?: StandardSchemaV1;
|
|
514
|
+
};
|
|
515
|
+
declare function RequestBody(schema: StandardSchemaV1): MethodDecorator;
|
|
516
|
+
declare function RequestParam(schema: StandardSchemaV1): MethodDecorator;
|
|
517
|
+
declare function RequestQuery(schema: StandardSchemaV1): MethodDecorator;
|
|
518
|
+
declare function _getRequestSchemas(ctor: Function, fnName: string): RequestSchemaDefinition | undefined;
|
|
519
|
+
declare function _parseOrThrow<TSchema extends StandardSchemaV1>(schema: TSchema, input: unknown, kind: ParserErrorLocation): Promise<StandardSchemaV1.InferOutput<TSchema>>;
|
|
520
|
+
//#endregion
|
|
521
|
+
export { Class, Controller, DELETE, ExpressMiddlewareFn, ExpressRouterMethodKey, ExpressRouterMethods, GET, HEAD, Html404ErrorPage, HttpHeaders, HttpStatus, Injectable, Middleware, MiddlewareClass, OPTIONS, PATCH, POST, PUT, ParserError, ParserErrorLocation, RedirectView, RequestBody, RequestParam, RequestQuery, ResponseEntity, ResponseEntityBuilder, ResponseStatusError, RouteDefinition, Sapling, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _getRequestSchemas, _getRoutes, _parseOrThrow, _resolve, methodResolve };
|
package/dist/index.d.mts
CHANGED
|
@@ -231,18 +231,6 @@ declare namespace StandardSchemaV1 {
|
|
|
231
231
|
}
|
|
232
232
|
/** The Standard JSON Schema interface. */
|
|
233
233
|
//#endregion
|
|
234
|
-
//#region src/annotation/request.d.ts
|
|
235
|
-
type RequestSchemaDefinition = {
|
|
236
|
-
body?: StandardSchemaV1;
|
|
237
|
-
param?: StandardSchemaV1;
|
|
238
|
-
query?: StandardSchemaV1;
|
|
239
|
-
};
|
|
240
|
-
declare function RequestBody(schema: StandardSchemaV1): MethodDecorator;
|
|
241
|
-
declare function RequestParam(schema: StandardSchemaV1): MethodDecorator;
|
|
242
|
-
declare function RequestQuery(schema: StandardSchemaV1): MethodDecorator;
|
|
243
|
-
declare function _getRequestSchemas(ctor: Function, fnName: string): RequestSchemaDefinition | undefined;
|
|
244
|
-
declare function _parseOrThrow<TSchema extends StandardSchemaV1>(schema: TSchema, input: unknown, kind: "body" | "params" | "query"): Promise<StandardSchemaV1.InferOutput<TSchema>>;
|
|
245
|
-
//#endregion
|
|
246
234
|
//#region src/helper/redirect.d.ts
|
|
247
235
|
/**
|
|
248
236
|
* Generic HTTP redirect wrapped modeled after Spring's `RedirectView`.
|
|
@@ -398,7 +386,7 @@ declare enum HttpStatus {
|
|
|
398
386
|
NETWORK_AUTHENTICATION_REQUIRED = 511
|
|
399
387
|
}
|
|
400
388
|
//#endregion
|
|
401
|
-
//#region src/helper/error.d.ts
|
|
389
|
+
//#region src/helper/error/responsestatus.d.ts
|
|
402
390
|
/**
|
|
403
391
|
* Ensure that you define a middleware that can handle this error.
|
|
404
392
|
*
|
|
@@ -508,4 +496,26 @@ declare class Sapling {
|
|
|
508
496
|
static setDeserializeFn(this: void, fn: (value: string) => any): void;
|
|
509
497
|
}
|
|
510
498
|
//#endregion
|
|
511
|
-
|
|
499
|
+
//#region src/helper/error/exception.d.ts
|
|
500
|
+
type ParserErrorLocation = "reqbody" | "reqparams" | "reqquery";
|
|
501
|
+
/**
|
|
502
|
+
* This error should be thrown when some data cannot be parsed by a given schema.
|
|
503
|
+
*/
|
|
504
|
+
declare class ParserError extends ResponseStatusError {
|
|
505
|
+
constructor(location: ParserErrorLocation, issues: readonly StandardSchemaV1.Issue[], vendor: string);
|
|
506
|
+
private static formatMessage;
|
|
507
|
+
}
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/annotation/request.d.ts
|
|
510
|
+
type RequestSchemaDefinition = {
|
|
511
|
+
body?: StandardSchemaV1;
|
|
512
|
+
param?: StandardSchemaV1;
|
|
513
|
+
query?: StandardSchemaV1;
|
|
514
|
+
};
|
|
515
|
+
declare function RequestBody(schema: StandardSchemaV1): MethodDecorator;
|
|
516
|
+
declare function RequestParam(schema: StandardSchemaV1): MethodDecorator;
|
|
517
|
+
declare function RequestQuery(schema: StandardSchemaV1): MethodDecorator;
|
|
518
|
+
declare function _getRequestSchemas(ctor: Function, fnName: string): RequestSchemaDefinition | undefined;
|
|
519
|
+
declare function _parseOrThrow<TSchema extends StandardSchemaV1>(schema: TSchema, input: unknown, kind: ParserErrorLocation): Promise<StandardSchemaV1.InferOutput<TSchema>>;
|
|
520
|
+
//#endregion
|
|
521
|
+
export { Class, Controller, DELETE, ExpressMiddlewareFn, ExpressRouterMethodKey, ExpressRouterMethods, GET, HEAD, Html404ErrorPage, HttpHeaders, HttpStatus, Injectable, Middleware, MiddlewareClass, OPTIONS, PATCH, POST, PUT, ParserError, ParserErrorLocation, RedirectView, RequestBody, RequestParam, RequestQuery, ResponseEntity, ResponseEntityBuilder, ResponseStatusError, RouteDefinition, Sapling, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _getRequestSchemas, _getRoutes, _parseOrThrow, _resolve, methodResolve };
|
package/dist/index.mjs
CHANGED
|
@@ -197,6 +197,22 @@ var ResponseEntityBuilder = class {
|
|
|
197
197
|
}
|
|
198
198
|
};
|
|
199
199
|
//#endregion
|
|
200
|
+
//#region src/helper/error/responsestatus.ts
|
|
201
|
+
/**
|
|
202
|
+
* Ensure that you define a middleware that can handle this error.
|
|
203
|
+
*
|
|
204
|
+
* @see {@link Sapling.loadResponseStatusErrorMiddleware}
|
|
205
|
+
*/
|
|
206
|
+
var ResponseStatusError = class ResponseStatusError extends Error {
|
|
207
|
+
constructor(status, message) {
|
|
208
|
+
super(message ?? "Something went wrong.");
|
|
209
|
+
this.status = status;
|
|
210
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
211
|
+
this.name = `HttpError(${HttpStatus[status]})`;
|
|
212
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, ResponseStatusError);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
//#endregion
|
|
200
216
|
//#region src/helper/sapling.ts
|
|
201
217
|
const settings = {
|
|
202
218
|
serialize: JSON.stringify,
|
|
@@ -335,22 +351,6 @@ var Sapling = class Sapling {
|
|
|
335
351
|
}
|
|
336
352
|
};
|
|
337
353
|
//#endregion
|
|
338
|
-
//#region src/helper/error.ts
|
|
339
|
-
/**
|
|
340
|
-
* Ensure that you define a middleware that can handle this error.
|
|
341
|
-
*
|
|
342
|
-
* @see {@link Sapling.loadResponseStatusErrorMiddleware}
|
|
343
|
-
*/
|
|
344
|
-
var ResponseStatusError = class ResponseStatusError extends Error {
|
|
345
|
-
constructor(status, message) {
|
|
346
|
-
super(message ?? "Something went wrong.");
|
|
347
|
-
this.status = status;
|
|
348
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
349
|
-
this.name = `HttpError(${HttpStatus[status]})`;
|
|
350
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, ResponseStatusError);
|
|
351
|
-
}
|
|
352
|
-
};
|
|
353
|
-
//#endregion
|
|
354
354
|
//#region src/types.ts
|
|
355
355
|
const methodResolve = {
|
|
356
356
|
GET: "get",
|
|
@@ -483,14 +483,40 @@ function _resolve(ctor) {
|
|
|
483
483
|
return _InjectableRegistry.get(ctor);
|
|
484
484
|
}
|
|
485
485
|
//#endregion
|
|
486
|
+
//#region src/helper/error/exception.ts
|
|
487
|
+
/**
|
|
488
|
+
* This error should be thrown when some data cannot be parsed by a given schema.
|
|
489
|
+
*/
|
|
490
|
+
var ParserError = class ParserError extends ResponseStatusError {
|
|
491
|
+
constructor(location, issues, vendor) {
|
|
492
|
+
super(400, ParserError.formatMessage(location, issues, vendor));
|
|
493
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
494
|
+
}
|
|
495
|
+
static formatMessage(location, issues, vendor) {
|
|
496
|
+
const formatted = issues.map((i) => {
|
|
497
|
+
const path = Array.isArray(i.path) ? i.path.map((seg) => typeof seg === "object" && seg ? String(seg.key) : String(seg)).join(".") : "";
|
|
498
|
+
return path ? `${path}: ${i.message}` : i.message;
|
|
499
|
+
}).join("; ");
|
|
500
|
+
return `${vendor} failed to parse ${(() => {
|
|
501
|
+
switch (location) {
|
|
502
|
+
case "reqbody": return "request body";
|
|
503
|
+
case "reqparams": return "request params";
|
|
504
|
+
case "reqquery": return "request query";
|
|
505
|
+
}
|
|
506
|
+
})()}: ${formatted}`;
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
//#endregion
|
|
486
510
|
//#region src/annotation/request.ts
|
|
487
511
|
const _requestSchemaStore = /* @__PURE__ */ new WeakMap();
|
|
488
|
-
function
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
512
|
+
function _getOrCreateRequestSchemaDefinition(ctor, fnName) {
|
|
513
|
+
const byFn = (() => {
|
|
514
|
+
const fn = _requestSchemaStore.get(ctor);
|
|
515
|
+
if (fn) return fn;
|
|
516
|
+
const newFn = /* @__PURE__ */ new Map();
|
|
517
|
+
_requestSchemaStore.set(ctor, newFn);
|
|
518
|
+
return newFn;
|
|
519
|
+
})();
|
|
494
520
|
const existing = byFn.get(fnName);
|
|
495
521
|
if (existing) return existing;
|
|
496
522
|
const created = {};
|
|
@@ -505,36 +531,30 @@ function RequestBody(schema) {
|
|
|
505
531
|
return (target, propertyKey) => {
|
|
506
532
|
const ctor = target.constructor;
|
|
507
533
|
const fnName = String(propertyKey);
|
|
508
|
-
_setOnce(
|
|
534
|
+
_setOnce(_getOrCreateRequestSchemaDefinition(ctor, fnName), "body", schema, fnName);
|
|
509
535
|
};
|
|
510
536
|
}
|
|
511
537
|
function RequestParam(schema) {
|
|
512
538
|
return (target, propertyKey) => {
|
|
513
539
|
const ctor = target.constructor;
|
|
514
540
|
const fnName = String(propertyKey);
|
|
515
|
-
_setOnce(
|
|
541
|
+
_setOnce(_getOrCreateRequestSchemaDefinition(ctor, fnName), "param", schema, fnName);
|
|
516
542
|
};
|
|
517
543
|
}
|
|
518
544
|
function RequestQuery(schema) {
|
|
519
545
|
return (target, propertyKey) => {
|
|
520
546
|
const ctor = target.constructor;
|
|
521
547
|
const fnName = String(propertyKey);
|
|
522
|
-
_setOnce(
|
|
548
|
+
_setOnce(_getOrCreateRequestSchemaDefinition(ctor, fnName), "query", schema, fnName);
|
|
523
549
|
};
|
|
524
550
|
}
|
|
525
551
|
function _getRequestSchemas(ctor, fnName) {
|
|
526
552
|
return _requestSchemaStore.get(ctor)?.get(fnName);
|
|
527
553
|
}
|
|
528
|
-
function _formatIssues(issues) {
|
|
529
|
-
return issues.map((i) => {
|
|
530
|
-
const path = Array.isArray(i.path) ? i.path.map((seg) => typeof seg === "object" && seg ? String(seg.key) : String(seg)).join(".") : "";
|
|
531
|
-
return path ? `${path}: ${i.message}` : i.message;
|
|
532
|
-
}).join("; ");
|
|
533
|
-
}
|
|
534
554
|
async function _parseOrThrow(schema, input, kind) {
|
|
535
555
|
const result = await schema["~standard"].validate(input);
|
|
536
|
-
if (
|
|
537
|
-
|
|
556
|
+
if (result.issues) throw new ParserError(kind, result.issues, schema["~standard"].vendor);
|
|
557
|
+
return result.value;
|
|
538
558
|
}
|
|
539
559
|
//#endregion
|
|
540
560
|
//#region src/annotation/route.ts
|
|
@@ -645,9 +665,9 @@ function Controller({ prefix = "", deps = [] } = {}) {
|
|
|
645
665
|
router[methodName](fp, async (request, response, next) => {
|
|
646
666
|
const schemas = _getRequestSchemas(target, fnName);
|
|
647
667
|
if (schemas) {
|
|
648
|
-
if (schemas.body) request.body = await _parseOrThrow(schemas.body, request.body, "
|
|
649
|
-
if (schemas.param) request.params = await _parseOrThrow(schemas.param, request.params, "
|
|
650
|
-
if (schemas.query) request.query = await _parseOrThrow(schemas.query, request.query, "
|
|
668
|
+
if (schemas.body) request.body = await _parseOrThrow(schemas.body, request.body, "reqbody");
|
|
669
|
+
if (schemas.param) request.params = await _parseOrThrow(schemas.param, request.params, "reqparams");
|
|
670
|
+
if (schemas.query) request.query = await _parseOrThrow(schemas.query, request.query, "reqquery");
|
|
651
671
|
}
|
|
652
672
|
const result = await fn.bind(controllerInstance)(request, response, next);
|
|
653
673
|
if (method === "USE") return;
|
|
@@ -678,4 +698,4 @@ function MiddlewareClass(...args) {
|
|
|
678
698
|
return Controller(...args);
|
|
679
699
|
}
|
|
680
700
|
//#endregion
|
|
681
|
-
export { Controller, DELETE, GET, HEAD, Html404ErrorPage, HttpStatus, Injectable, Middleware, MiddlewareClass, OPTIONS, PATCH, POST, PUT, RedirectView, RequestBody, RequestParam, RequestQuery, ResponseEntity, ResponseEntityBuilder, ResponseStatusError, Sapling, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _getRequestSchemas, _getRoutes, _parseOrThrow, _resolve, methodResolve };
|
|
701
|
+
export { Controller, DELETE, GET, HEAD, Html404ErrorPage, HttpStatus, Injectable, Middleware, MiddlewareClass, OPTIONS, PATCH, POST, PUT, ParserError, RedirectView, RequestBody, RequestParam, RequestQuery, ResponseEntity, ResponseEntityBuilder, ResponseStatusError, Sapling, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _getRequestSchemas, _getRoutes, _parseOrThrow, _resolve, methodResolve };
|