Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

routing-controllers-multiparam

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

routing-controllers-multiparam

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

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 07 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc