ra

    0.9.7 • Public • Published

    Emerap RA ☀️

    Build Status

    Synopsis

    Customizing Rest API builder

    NPM

    API reference

    call(req, res, [method])

    • req: required Instance of http.IncomingMessage
    • res: required Instance of http.ServerResponse
    • method: Method name, (default value from req.params.method)

    Return object as Promise.

    definition(obj)

    • obj: required Definition reference
    {
      name: 'methodName',
      args: {
        paramName: {
          dataType: 'dataTypeName',
        },
      },
      callback: (args) => {
        return promise or value
      }
    }

    datatype(obj)

    • obj: required Datatype reference
    {
      type: 'datatypeName',
      callback: (value) => {
        return promise or value
      }
    }

    Quick start

    Open terminal and run command

    mkdir ra-test && cd ra-test && npm init -y && touch app.js && npm install express ra --save
    

    Paste code into app.js and save changes

    const express = require('express');
    const app = express();
     
    const ra = require('ra');
    const api = new ra();
     
    api.datatype({
      type: 'fooBar',
      callback: (value) => {
        return `foo bar ${value}`;
      }
    });
     
    api.definition({
      name: 'ra.fooBar',
      args: {
        user: {
          dataType: 'fooBar',
        },
      },
      callback: (args) => {
        return Promise.resolve(args);
      }
    });
     
    app.all('/ra/method/:method', (req, res) => {
      api.call(req, res).then((response) => {
        res.status(response.code).json(response.data);
      });
     
    });
     
    app.listen(3000, () => {
      console.log('=====================================================');
      console.log('Starting app http://lvh.me:3000/ra/method/ra.version');
      console.log('=====================================================');
    });

    Start app in terminal

    node app.js
    

    © Alexander Pokhodyun (Karbunkul) 2017

    Install

    npm i ra

    DownloadsWeekly Downloads

    4

    Version

    0.9.7

    License

    MIT

    Last publish

    Collaborators

    • karbunkul