From 982db016840336591ae31a0bdbd38c309b0d5191 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 19 Feb 2024 17:17:54 +0800 Subject: [PATCH 1/7] feat: support custom param decorator to throw error --- .../core/src/decorator/web/paramMapping.ts | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/core/src/decorator/web/paramMapping.ts b/packages/core/src/decorator/web/paramMapping.ts index 4ebd7491b360..f81c903d459a 100644 --- a/packages/core/src/decorator/web/paramMapping.ts +++ b/packages/core/src/decorator/web/paramMapping.ts @@ -28,10 +28,10 @@ export interface RouterParamValue { } const createParamMapping = function (type: RouteParamTypes) { - return (propertyOrPipes?: any, pipes?: Array) => { + return (propertyOrPipes?: any, options: ParamDecoratorOptions = {}) => { let propertyData = propertyOrPipes; - if (Array.isArray(propertyOrPipes) && pipes === undefined) { - pipes = propertyOrPipes; + if (Array.isArray(propertyOrPipes) && options.pipes === undefined) { + options.pipes = propertyOrPipes; propertyData = undefined; } return createCustomParamDecorator( @@ -40,9 +40,7 @@ const createParamMapping = function (type: RouteParamTypes) { type, propertyData, }, - { - pipes, - } + options ); }; }; @@ -70,45 +68,42 @@ export const createRequestParamDecorator = function ( pipes: pipesOrOptions as Array, }; } - return createParamMapping(RouteParamTypes.CUSTOM)( - transform, - pipesOrOptions.pipes - ); + return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipesOrOptions); }; export const Session = ( propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[] -) => createParamMapping(RouteParamTypes.SESSION)(propertyOrPipes, pipes); +) => createParamMapping(RouteParamTypes.SESSION)(propertyOrPipes, { pipes }); export const Body = ( propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[] -) => createParamMapping(RouteParamTypes.BODY)(propertyOrPipes, pipes); +) => createParamMapping(RouteParamTypes.BODY)(propertyOrPipes, { pipes }); export const Query = ( propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[] -) => createParamMapping(RouteParamTypes.QUERY)(propertyOrPipes, pipes); +) => createParamMapping(RouteParamTypes.QUERY)(propertyOrPipes, { pipes }); export const Param = ( propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[] -) => createParamMapping(RouteParamTypes.PARAM)(propertyOrPipes, pipes); +) => createParamMapping(RouteParamTypes.PARAM)(propertyOrPipes, { pipes }); export const Headers = ( propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[] -) => createParamMapping(RouteParamTypes.HEADERS)(propertyOrPipes, pipes); +) => createParamMapping(RouteParamTypes.HEADERS)(propertyOrPipes, { pipes }); export const File = (propertyOrPipes?: any, pipes?: PipeUnionTransform[]) => - createParamMapping(RouteParamTypes.FILESTREAM)(propertyOrPipes, pipes); + createParamMapping(RouteParamTypes.FILESTREAM)(propertyOrPipes, { pipes }); export const Files = (propertyOrPipes?: any, pipes?: PipeUnionTransform[]) => - createParamMapping(RouteParamTypes.FILESSTREAM)(propertyOrPipes, pipes); + createParamMapping(RouteParamTypes.FILESSTREAM)(propertyOrPipes, { pipes }); export const RequestPath = (pipes?: PipeUnionTransform[]) => - createParamMapping(RouteParamTypes.REQUEST_PATH)(undefined, pipes); + createParamMapping(RouteParamTypes.REQUEST_PATH)(undefined, { pipes }); export const RequestIP = (pipes?: PipeUnionTransform[]) => - createParamMapping(RouteParamTypes.REQUEST_IP)(undefined, pipes); + createParamMapping(RouteParamTypes.REQUEST_IP)(undefined, { pipes }); export const Queries = ( propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[] -) => createParamMapping(RouteParamTypes.QUERIES)(propertyOrPipes, pipes); +) => createParamMapping(RouteParamTypes.QUERIES)(propertyOrPipes, { pipes }); export const Fields = ( propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[] -) => createParamMapping(RouteParamTypes.FIELDS)(propertyOrPipes, pipes); +) => createParamMapping(RouteParamTypes.FIELDS)(propertyOrPipes, { pipes }); From c56da052859968b0c395daf0d57c0e5a09c9396e Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 19 Feb 2024 17:19:20 +0800 Subject: [PATCH 2/7] feat: support custom param decorator to throw error --- packages/core/src/decorator/web/paramMapping.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/decorator/web/paramMapping.ts b/packages/core/src/decorator/web/paramMapping.ts index f81c903d459a..10503d841b57 100644 --- a/packages/core/src/decorator/web/paramMapping.ts +++ b/packages/core/src/decorator/web/paramMapping.ts @@ -28,7 +28,7 @@ export interface RouterParamValue { } const createParamMapping = function (type: RouteParamTypes) { - return (propertyOrPipes?: any, options: ParamDecoratorOptions = {}) => { + return (propertyOrPipes: any, options: ParamDecoratorOptions = {}) => { let propertyData = propertyOrPipes; if (Array.isArray(propertyOrPipes) && options.pipes === undefined) { options.pipes = propertyOrPipes; From fde15552162753654ebe153e0685cc2e793ae28f Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 19 Feb 2024 17:31:55 +0800 Subject: [PATCH 3/7] chore: update snapshot --- .../test/decorator/web/__snapshots__/paramMapping.test.ts.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap b/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap index 71d3f2f4c60f..a7255da9f37a 100644 --- a/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap +++ b/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap @@ -162,6 +162,7 @@ exports[`/test/web/paramMapping.test.ts paramMapping decorator should be ok 1`] "pipes": [ [Function], ], + "throwError": true, }, "parameterIndex": 0, "propertyName": "dogetError", From cf15290f4938b637d17694340e91583dbe6db756 Mon Sep 17 00:00:00 2001 From: 142vip Date: Mon, 19 Feb 2024 18:02:08 +0800 Subject: [PATCH 4/7] feat(core): createRequestParamDecorator default support throw error --- packages/core/src/decorator/web/paramMapping.ts | 3 +++ .../test/decorator/web/__snapshots__/paramMapping.test.ts.snap | 1 + 2 files changed, 4 insertions(+) diff --git a/packages/core/src/decorator/web/paramMapping.ts b/packages/core/src/decorator/web/paramMapping.ts index 10503d841b57..684ffd445e2f 100644 --- a/packages/core/src/decorator/web/paramMapping.ts +++ b/packages/core/src/decorator/web/paramMapping.ts @@ -68,6 +68,9 @@ export const createRequestParamDecorator = function ( pipes: pipesOrOptions as Array, }; } + if(pipesOrOptions.throwError==null) + pipesOrOptions.throwError=true + return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipesOrOptions); }; diff --git a/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap b/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap index a7255da9f37a..3d65f88ab324 100644 --- a/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap +++ b/packages/core/test/decorator/web/__snapshots__/paramMapping.test.ts.snap @@ -14,6 +14,7 @@ exports[`/test/web/paramMapping.test.ts paramMapping decorator should be ok 1`] "pipes": [ [Function], ], + "throwError": true, }, "parameterIndex": 9, "propertyName": "doget", From 666695f00195a119e5c30b76868ae98f6875b852 Mon Sep 17 00:00:00 2001 From: 142vip Date: Mon, 19 Feb 2024 21:25:50 +0800 Subject: [PATCH 5/7] chore: update --- packages/core/src/decorator/web/paramMapping.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/decorator/web/paramMapping.ts b/packages/core/src/decorator/web/paramMapping.ts index 684ffd445e2f..01a920e01ac8 100644 --- a/packages/core/src/decorator/web/paramMapping.ts +++ b/packages/core/src/decorator/web/paramMapping.ts @@ -68,7 +68,7 @@ export const createRequestParamDecorator = function ( pipes: pipesOrOptions as Array, }; } - if(pipesOrOptions.throwError==null) + if(pipesOrOptions.throwError===undefined) pipesOrOptions.throwError=true return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipesOrOptions); From 656a7093977bb6bef12b3bb0787eb045dd4c78a5 Mon Sep 17 00:00:00 2001 From: 142vip Date: Mon, 19 Feb 2024 21:27:33 +0800 Subject: [PATCH 6/7] chore: update --- packages/core/src/decorator/web/paramMapping.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/decorator/web/paramMapping.ts b/packages/core/src/decorator/web/paramMapping.ts index 01a920e01ac8..4326e6d3510d 100644 --- a/packages/core/src/decorator/web/paramMapping.ts +++ b/packages/core/src/decorator/web/paramMapping.ts @@ -68,7 +68,7 @@ export const createRequestParamDecorator = function ( pipes: pipesOrOptions as Array, }; } - if(pipesOrOptions.throwError===undefined) + if(pipesOrOptions.throwError == null) pipesOrOptions.throwError=true return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipesOrOptions); From 573a05922400ecd70fee8a200570e009593a755b Mon Sep 17 00:00:00 2001 From: 142vip Date: Mon, 19 Feb 2024 21:30:28 +0800 Subject: [PATCH 7/7] chore: update --- packages/core/src/decorator/web/paramMapping.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core/src/decorator/web/paramMapping.ts b/packages/core/src/decorator/web/paramMapping.ts index 4326e6d3510d..0aee57e0b93e 100644 --- a/packages/core/src/decorator/web/paramMapping.ts +++ b/packages/core/src/decorator/web/paramMapping.ts @@ -68,8 +68,7 @@ export const createRequestParamDecorator = function ( pipes: pipesOrOptions as Array, }; } - if(pipesOrOptions.throwError == null) - pipesOrOptions.throwError=true + if (pipesOrOptions.throwError == null) pipesOrOptions.throwError = true; return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipesOrOptions); };