Giter Club home page Giter Club logo

serverless-request-handler's Introduction

serverless-request-handler

A request handler that uses class-transformer and class-validator to create a type-safe serverless handler

What is serverless-request-handler

Serverless request handler is a micro-framework that's here to make your life easier. You give it an options object with what you expect the request to look like; and it does the object request transformation, request validation, response validation and error handdling.

A typical handler will look like this.

class CreateUserDto {
    @IsEmail()
    userName: string;

    @IsString()
    @MinLength(7)
    password: string;
}

class UserPathParams {
    @IsNumber()
    userId: number;
}

const options = {
    body: {
        classType: CreateUserDto
    },
    pathParameters: {
        classType: UserPathParams
    }
};


const createUser = handler(options, async (event) => {
    return Result.Ok(201, userService.create(event.pathParameters.userId, event.body));
});

Installation

  1. Install module:

    npm install serverless-request-handler --save

  2. reflect-metadata shim is required, install it too:

    npm install reflect-metadata --save

    and make sure to import it in a global place, like server.ts:

    import "reflect-metadata";
  3. class-validator and class-transfrormer are required peer dependencies

    npm install class-validator class-transfrormer --save

  4. ES6 features are used, if you are using old version of node.js you may need to install es6-shim:

    npm install es6-shim --save

    and import it in a global place like app.ts:

    import "es6-shim";

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.