Giter VIP home page Giter VIP logo

web-decorators's Introduction

web-decorators

Decorator based api for express or any web server framework.

Prerequisites

Tested with nodejs 0.12, built with Typescript 1.5.0-alpha (provided in the dependencies).

Installation

  • Run npm install to install the dependencies and typing definition.
  • Run npm run build to build the typescript source files to the /lib directory.

Features

  • Define classes as controllers
  • Define root path for an entier controller
  • Define routes on get, post, put, delete and patch
  • Define middlewares on routes
  • Inject data from the query string, path parameter or entire body
  • Force send Json data
  • Inject the backend object to use directly the underlying framework if needed
  • Abstract the underlying framework in front of an adapter (express is provided)

Example

The typescript example is in the /example directory, build the project with npm run build and run node lib/example/server.js

Decorator API

@Controller(adapter): Defines a class as a controller, the parameter is an adapter to a backend (express adapter is included).

@Route(path): Define a root path for a controller.

@Get(path), @Post(path), @Put(path), @Delete(path): Define a method as an handler for an HTTP verb. If @Route is used on the controller, both are concatenated. The result can be directly returned with a return statement, or a promise can be used for an asynchronous result.

@Middle(path?): Set a middleware on path. If @Route is used on the controller, both are concatenated.

@PathParam(name), @QueryParam(name), @BodyParam(), @HeaderParam(), @CookieParam(): Define a handler parameter to receive data from the request path parameters, query string or body.

@AdapterParam(): Define a handler parameter as an object defined by the backend implementation, with express, this object contains res as the http response and req as the http request.

Adapters

The API can use any class implementing the IAdapter interface, the express adapter is provided.

An adapter must provide the following methods:

  • addMiddleware: add a middleware, optionally on a specific path
  • addRoute: register an handler on a specific method and path
  • send: send raw data to the client, with status code and headers
  • sendJson: send data as json to the client, with status code and headers
  • getParameterWithConfig: extract a parameter (from query string, path param...) with the provided configuration

The backend is free to use any kind of request and response shape, so it does not assume that something like node's http request and response are used.

web-decorators's People

Contributors

cybrown avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

web-decorators's Issues

Return value

We must inject the response with @ResParam to send content to the client, it would be better to return a result with a return statement, even an asynchronous result via Promise or thunk.

Create a default adapter without express

Use https://www.npmjs.com/package/route-parser for route matching, implement the concept of routes and middlewares.

  • Create the main handler function to give to http#createServer()
  • Create the structure that takes the methods and patterns
  • Create the api to add routes to the route container
  • On request, dispatch the request to the right handler, from the pattern and method
  • Extract path params from the request
  • Extract query string params from the request
  • Provide an API to read the request body
  • Add response related tasks
  • Add middleware related tasks

Child controllers

Find something to "mount" other controllers to a current controller, so the app would be a tree of Controller linking each other.

A PropertyDecorator might be used for that purpose.

Publish on npm

How to provide the typing information through an npm package ?

  • Create .npmignore file
  • Create a dist .d.ts type definition file
  • Normalize the public interface
  • Add express adapter in the public interface

Response type handling

For the moment, the result is sent as is, it should be possible to force a return type.

Change how a controller is bootstrapped

A controller is bootstrapped (configured with the backend) when it is first encountered by the runtime.
It would be better if the @controller decorator did not contain the backend adapter, and use directly the adapter to add classes that must be parsed.

Moreover, with es6 or commonjs modules, all classes are not loaded at startup, so only adding a @controller alone would not have much effect.

@controller might be merged with @route.

Set response cookies

  • Add cookie attribute in ResponseMetadata
  • Make the express adapter to send the cookies

Error handlers

Add error handler in controllers ?

Maybe something like spring's controller advice ?

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.