GithubHelp home page GithubHelp logo

bluelovers / js-restful Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mseemann/js-restful

0.0 2.0 0.0 149 KB

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

License: MIT License

JavaScript 9.61% TypeScript 90.39%

js-restful's Introduction

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.

js-restful's People

Contributors

mseemann avatar greenkeeperio-bot avatar greenkeeper[bot] avatar

Watchers

James Cloos avatar  avatar

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.