1.0.4 • Published 2 years ago

@stjohnd777/easyrest v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

##EasyRest

REST framework for implementing REST service.

Install

npm install @stjohnd777/easyrest

Overview

  • uses express
  • will create express instance if not provided
  • will piggyback on express instance if provided
  • provides heart beat service /ping
  • supports http and https
  • stand up a dozen services in a day

Just define the object literal

let ep = {
  method: method,
  path: path,
  provider: async (req,res) => { ...}
}

Or Use object constructor

let ep = new RestEndpoint(method, path, provider)

Example Hello World

GET Request at /hello

const {RestServiceFactory } = require('@stjohnd777/easyrest')

const port = 3000
const aProvider =  (req, res) =>{
    res.json({
        success:true,
        message : "Hello Easy Rest"
    });
}

let service = new RestServiceFactory(
    serviceName
    ,[ {
        method:'GET',
        path:"/hello",
        provider:aProvider
    } ]

)
service.start(port)

Path Parameters are as usual with express

  • Path parameters are part of the url itself that usually occurs after the name of the service.
  • Example /thing/:thingId

Query Parameters

  • The query string portion of a URL is the part of the URL after the question mark ?. For example: ?answer=42
  • Each key=value pair is called a query parameter.
  • If your query string has multiple query parameters, they're separated by &. For example, the below string has 2 query parameters, a and b.

?a=1&b=2

GET request to path parameters

const {RestServiceFactory } = require('@stjohnd777/easyrest')
 
let service = new RestServiceFactory(
    serviceName
    ,[ {
        method:'GET',
        path:"/widget/:prop1/:prop2",
        provider:async ()=>{
          let prop1 = req.params.props1
          let prop3 = req.params.props2
          const qps = req.query
          // do stuff ...
          res.json({ ...

          });
        }
    } ]

)
service.start(port)

Review

HTTP methodDescription
GETRetrieve an existing resource.
POSTCreate a new resource.
PUTUpdate an existing resource.
PATCHPartially update an existing resource.
DELETEDelete a resource.

Review Code Ranges

CodeCategory
2xxSuccessful operation
3xxRedirection
4xxClient error
5xxServer error

Review Some Codes

CodeMeaning Description
200OK The requested action was successful.
201Created A new resource was created.
202Accepted The request was received, but no modification has been made yet.
204No Content The request was successful, but the response has no content.
400Bad Request The request was malformed.
401Unauthorized The client is not authorized to perform the requested action.
404Not Found The requested resource was not found.
415Unsupported Media Type The request data format is not supported by the server.
422Unprocessable Entity The request data was properly formatted but contained invalid or missing data.
500Internal Server Error The server threw an error when processing the request.

Review API endpoint in

HTTPAPI endpointDescription
GET/NOUNGet a list of NOUN.
GET/NOUN/<noun_id>Get a single customer.
POST/NOUNCreate a new customer.
PUT/NOUN/<noun_id>Update a customer.
PATCH/NOUN/<noun_id>Partially update a customer.
DELETE/NOUN/<noun_id>Delete a customer.

Let use Task as the noun:

task = {
    id,
    assigned_to,
    name,
    description,
    due_date,
    status,
}

let epFetchTasks = {'GET', '/task/:task_id', async(req,res)=>{
    let id = req.params.task_id
    // fetch all
    let tasks = ...
    req.json {
        success: true
        tasks: tasks
    }    
}}
let epAddTask = { 'POST', '/task', async (res,res)=>{
    let body = req.body
    
}}
let epUpdateTask = { 'PUT', '/task/:task_id',async (res,res)=>{
    let body = req.body
}}
let ep = { 'PATCH', '/task/:task_id',async (res,res)=>{
    let body = req.body
    
}}
let epDeleteTask = { 'DELETE', '/task/:task_id',async (res,res)=>{
    let id = req.params.task_id
}}

const endpoints = [epFetchTasks,epAddTask,epUpdateTask,epDeleteTask]


let service = new RestServiceFactory(
    'task_services'
    ,endpoints
)
service.start(port)
1.0.2

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago