Create a RESTful service with ES7 decorators for your node.js application.
TypeScript JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config
src
.codeclimate.yml
.eslintignore
.eslintrc
.gitignore
.npmignore
.travis.yml fix build Nov 21, 2016
LICENSE
README.md remove direct dependency for reflect-metadat Sep 29, 2016
karma.conf.js
package.json
tsconfig.json
yarn.lock

README.md

js-restful

Create a RESTful service with ES7 decorators for your node.js application.

CI Status npm version Coverage Status Code Climate Issue Count Test Coverage Issue Stats

Installation

npm install js-restful --save

Make sure you have a shim for es7 reflect. For example core-js or reflect-metadat.

Usage Extend your node js service class with decorators (this is TypeScript with decorators!):

import { GET, POST, PUT, DELETE, Path, PathParam, HeaderParam, QueryParam } from 'js-restful';

class Book {
    id:number;
    name: string;
}

@Path('/books')
class BookService {

    @GET()
    allBooks() : Book[]{
       return [];
    }

    @Path('/:name')
    @POST()
    createBook(@PathParam('name') name:string, @HeaderParam('token') token:string) :Book {
        return {id:1, name:name};
    }

    @Path('/:id/:name')
    @PUT()
    updateBook(@PathParam('id') id:number, @PathParam('name') name:string) : Book {
        return {id:id, name:name};
    }

    @Path('/:id')
    @DELETE()
    deleteBook(@PathParam('id') id:number, @QueryParam('time') time:number): boolean {
        return true;
    }
}

Later (at runtime) you can use the parser to get back all of these information:

import { ServiceDescription, ServiceParser } from 'js-restful';

let serviceDescription = ServiceParser.parse(bookService);

The result is a complete description of your Service:

{
  "basePath":"/books",
  "methods":[
    {
      "methodName":"allBooks",
      "httpMethod":0,
      "path":null,
      "pathParams":[],
      "headerParams":[],
      "queryParams":[]
    },
    {
      "methodName":"createBook",
      "httpMethod":1,
      "path":"/:name",
      "pathParams":[{"paramName":"name","index":0}],
      "headerParams":[{"paramName":"token","index":1}],
      "queryParams":[]
    },
    {
      "methodName":"updateBook",
      "httpMethod":2,
      "path":"/:id/:name",
      "pathParams":[{"paramName":"name","index":1},{"paramName":"id","index":0}],
      "headerParams":[],
      "queryParams":[]
    },
    {
      "methodName":"deleteBook",
      "httpMethod":3,
      "path":"/:id",
      "pathParams":[{"paramName":"id","index":0}],
      "headerParams":[],
      "queryParams":[{"paramName":"time","index":1}]
    }
  ]
}

Ok! But what can I do with this? Have a look at the github project js-restful-express.

This is a base project that different implementations can use to integrate this information in a concrete node js framework.