Skip to content

Commit

Permalink
feat(platform-params): make RawPathParams configurable like is it for…
Browse files Browse the repository at this point in the history
… PathParams (useValidation, useType, etc...)

Closes: #2515
  • Loading branch information
Romakita committed Nov 8, 2023
1 parent d664514 commit ad6861a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {JsonParameterStore} from "@tsed/schema";
import {ParamTypes} from "../domain/ParamTypes";
import {PathParams, RawPathParams} from "./pathParams";
import {JsonParameterStore} from "@tsed/schema";

describe("@PathParams", () => {
it("should call ParamFilter.useParam method with the correct parameters", () => {
Expand All @@ -14,6 +14,20 @@ describe("@PathParams", () => {
expect(param.expression).toEqual("expression");
expect(param.paramType).toEqual(ParamTypes.PATH);
expect(param.type).toEqual(Test);
expect(param.pipes).toHaveLength(2);
});
it("should call ParamFilter.useParam method with the correct parameters (+options)", () => {
class Test {}

class Ctrl {
test(@PathParams({expression: "expression", useType: Test, useValidation: false}) param: Test) {}
}

const param = JsonParameterStore.get(Ctrl, "test", 0);
expect(param.expression).toEqual("expression");
expect(param.paramType).toEqual(ParamTypes.PATH);
expect(param.type).toEqual(Test);
expect(param.pipes).toHaveLength(1);
});
it("should call ParamFilter.useParam method with the correct parameters (raw)", () => {
class Ctrl {
Expand All @@ -23,5 +37,18 @@ describe("@PathParams", () => {
const param = JsonParameterStore.get(Ctrl, "test", 0);
expect(param.expression).toEqual("expression");
expect(param.paramType).toEqual(ParamTypes.PATH);
expect(param.type).toEqual(String);
expect(param.pipes).toHaveLength(0);
});
it("should call ParamFilter.useParam method with the correct parameters (raw + options)", () => {
class Ctrl {
test(@RawPathParams({expression: "expression", useValidation: true, useType: String}) param: Date) {}
}

const param = JsonParameterStore.get(Ctrl, "test", 0);
expect(param.expression).toEqual("expression");
expect(param.paramType).toEqual(ParamTypes.PATH);
expect(param.type).toEqual(String);
expect(param.pipes).toHaveLength(1);
});
});
11 changes: 9 additions & 2 deletions packages/platform/platform-params/src/decorators/pathParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,17 @@ export function PathParams(...args: any[]): ParameterDecorator {
* @operation
* @input
*/
export function RawPathParams(expression: string) {
export function RawPathParams(expression: string): ParameterDecorator;
export function RawPathParams(options: Partial<ParamOptions>): ParameterDecorator;
export function RawPathParams(): ParameterDecorator;
export function RawPathParams(...args: any[]): ParameterDecorator {
const {expression, useType, useMapper = false, useValidation = false} = mapParamsOptions(args);
return UseParam({
paramType: ParamTypes.PATH,
dataPath: "$ctx.request.params",
expression
expression,
useValidation,
useMapper,
useType
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Constant, Injectable} from "@tsed/di";
import {Injectable} from "@tsed/di";
import {deserialize} from "@tsed/json-mapper";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";

Expand Down

0 comments on commit ad6861a

Please sign in to comment.