npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@guseyn/cutie-rest

v1.0.7

Published

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

Downloads

10

Readme

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.

| 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(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;