1.0.7 • Published 6 years ago

@guseyn/cutie-rest v1.0.7

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

cutie-rest

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

NPM Version

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.

ObjectParametersDescription
Backendport (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.
Methodregexp (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.
ServingFilesregexp (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.
CachedServingFilesregexp (RegExp), mapper (function(url)), notFoundMethod (Method)Does the same that ServingFiles does and caches files for increasing speed of serving them.
NotFoundMethodregexp (RegExp)Method is used in RestApi, ServingFiles, CachedServingFiles for declaring method on 404(NOT_FOUND) 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;