Create a RESTful service with ES7 decorators for your node.js application.
Make sure you have a shim for es7 reflect. For example core-js or reflect-metadat.
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;
}
}
import { ServiceDescription, ServiceParser } from 'js-restful';
let serviceDescription = ServiceParser.parse(bookService);
{
"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}]
}
]
}
This is a base project that different implementations can use to integrate this information in a concrete node js framework.