0.1.0 • Published 7 years ago

routing-controllers-multiparam v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

routing-controllers-multiparam

npm version Dependency Status devDependency Status peerDependency Status

A simple plugin for routing-controller which allows to inject param from multiple sources.

Installation

Module installation

npm install routing-controllers-multiparam --save

(or the short way with NPM v5):

npm i routing-controllers-multiparam

Peer dependencies

This package is only a simple plugin, so you have to install the routing-controllers package because it can't work without them.

Usage

The usage of this module is very simple. All you need is:

import { JsonController, Post, createExpressServer } from "routing-controllers";
// import the `@MultiParam` decorator and `ParamType` enum from the module
import { MultiParam, ParamType } from "routing-controllers-multiparam";

// declare the controller class using routing-controller decorators
@JsonController()
class SampleController {
    @Post("/sample")
    multipleObjects(
        // use the `@MultiParam` decorator to define the sources of the param to inject
        @MultiParam({ allow: {
            [ParamType.QueryParam]: "roleQuery",
            [ParamType.BodyParam]: "roleBody",
        }})
        role: string,
    ) {
        return {
            role,
        };
    }
}

// start the server
createExpressServer({ controllers: [SampleController]}).listen(3000);

And that's it! This will lead to inject the first non-undefined value from the list of sources, so when you specify roleBody param in body but not roleQuery inside path (query string) it will be injected. It works just like switch-case!

API reference

Function signatures

The @MultiParam decorator has two overloads:

export function MultiParam(options: NamedParamOptions): ParameterDecorator;
export function MultiParam(paramName: string, options: UnamedParamOptions): ParameterDecorator;

Parameters and types

  • NamedParamOptions - a type of object that property allow can be a dictionary of allowed types:
export type MultiParamDecoratorNamedOptions = {
    required?: boolean;
    allow: { 
        [P in ParamType]?: string | string[];
    };
};

So the usage is just like in the example:

@MultiParam({ allow: {
    [ParamType.QueryParam]: ["role", "roleFromQuery"],
    [ParamType.BodyParam]: "roleFromBody",
}})
  • UnamedParamOptions - a type of object that property allow can be ParamType or array of ParamType
export type MultiParamDecoratorUnnamedOptions = {
    required?: boolean;
    allow: ParamType | ParamType[];
};

It can be used only with paramName parameter when you want to get the param from multiple source but which is avaible on the same name:

@MultiParam("api", { allow: [ParamType.QueryParam, ParamType.HeaderParam] })

More info

If you need more examples of usage, go to the sources and check unit tests file - /src/decorators/MultiParam.ts. If you have questions or new features/ideas, feel free to open an issue on GitHub repository.

Release notes

0.1.0

  • initial version with basic @MultiParam decorator support