@guseyn/cutie-rest

Cutie extension for using REST (based on cutie-http) in Node.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@guseyn/cutie-rest
5191.0.74 years ago4 years agoMinified + gzip package size for @guseyn/cutie-rest in KB

Readme

cutie-rest
Cutie extension for using REST (based on cutie-http) in Node.
!NPM Versionnpm-imagenpm-url
Cutie extension for fs module in Node. It's based on the Async Tree Pattern.
Usage
const {
  // Needed async objects here from the table below
} = require('@guseyn/cutie-rest');

This library provides following objects: Backend, RestApi, ServingFiles, CachedServingFiles and Method, NotFoundMethod interfaces.
| Object | Parameters | Description | | ------ | -----------| ----------- | | Backend | port (number), host (string), api (RestApi)| Declares backend server(just http for now) on specified port and host, also it provides declared api (REST).| | RestApi | ...methods(classes that extend Method) | Declares methods of api. | | Method | regexp (RegExp), method (string) | Declares a method(in api) with url that matches regexp and specified method('GET', 'POST', etc.). This class has a method invoke(request, response) that needs to be overridden.| | ServingFiles | regexp (RegExp), mapper (function(url)), notFoundMethod (Method) | Extends Method and serves files on url that mathes regexp with mapper function that gets location of a file on a disk by the url. Also it's required to declare notFoundMethod that handles the cases when a file is not found. | | CachedServingFiles | regexp (RegExp), mapper (function(url)), notFoundMethod (Method) | Does the same that ServingFiles does and caches files for increasing speed of serving them. | | NotFoundMethod | regexp (RegExp) | Method is used in RestApi, ServingFiles, CachedServingFiles for declaring method on 404(NOTFOUND) status. |
Example
'use strict'

const path = require('path');
const {
  Backend,
  RestApi,
  CachedServingFiles
} = require('@guseyn/cutie-rest');
const GeneratedResponse = require('./GeneratedResponse');
const CustomNotFoundMethod = require('./CustomNotFoundMethod');

const notFoundMethod = new CustomNotFoundMethod(new RegExp(/\/not-found/));

const mapper = (url) => {
  let paths = url.split('/').filter(path => path !== '');
  return path.join(...paths);
}

new Backend(8080, '127.0.0.1', new RestApi(
  new GeneratedResponse(new RegExp(/\/response/), 'GET'),
  new CachedServingFiles(new RegExp(/\/files/), mapper, notFoundMethod),
  notFoundMethod
)).run();

CustomNotFoundMethod

'use strict'

const { NotFoundMethod } = require('./NotFoundMethod');

class CustomNotFoundMethod extends NotFoundMethod {

  constructor(regexpUrl) {
    super(regexpUrl);
  }

}

module.exports = CustomNotFoundMethod;

GeneratedResponse

This class also uses cutie-http

'use strict'

const { Method } = require('@guseyn/cutie-rest');

const {
  EndedResponse,
  WrittenResponse,
  ResponseWithWrittenHead
} = require('@guseyn/cutie-http');

class GeneratedResponse extends Method {

  constructor(regexpUrl, type) {
    super(regexpUrl, type);
  }

  invoke(request, response) {
    new EndedResponse(
      new WrittenResponse(
        new ResponseWithWrittenHead(
          response, 200, 'ok',  {
            'Content-Type': 'text/plain' 
          }
        ), 'content ... '
      ), `is delivered`
    ).call();
  }

}

module.exports = GeneratedResponse;