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

koa-rester

v0.1.10

Published

A simple Koa library that makes REST quite easy

Downloads

17

Readme

koa-rester

Build Status Coverage Status dependencies Status

Koa based framework for deploying RESTful APIs easily

  • One line to deploy a REST API from a Model
  • Tested with mongoose and ORM models
  • Tested with koa-router but it should work with almost any router that provides get|post|put|delete operations.
  • Tested with koa-bodyparser
  • Todo features are listed in #1

Installation

$ npm install koa-rester

Usage

const router = new Router();
const base = 'test';

router.use(bodyParser());
rester = new Rester({ router, base });

// Expose GET, POST /test/resource 
//        GET, PATCH, DELETE /test/resource/:id
rester.add(model, 'resource').rest({
  after: async (ctx, next) => {},
  // afterPost overwrites after for POST requests
  afterPost: async (ctx, next) => {},
});

// Expose GET /test/resource1 
//        GET /test/resource1/:id
rester.add(model1, 'resource1').list({
  before: async (ctx, next) => {},
}).get();

new Koa()
  .use(r.routes())
  .use(r.allowedMethods())
  .listen(30001);

More complex examples, with model definitions included, are located in the wiki.

API Reference

koa-rester

Rester ⏏

Kind: Exported class

new Rester(options)

Create a new Rester.

| Param | Type | Description | | --- | --- | --- | | options | Object | Configuration object. Property router is compulsory. | | options.errorHandler | function | A function that receive the DB error and returns an JSON object with at least two properties status and message. Status will be the HTTP response status and the whole object will be sent as body. | | options.router | Object | The koa express style router. Any router that supports get, post, put, patch and delete operations. | | options.model | Object | The persistence layer model. It can be included here or by using the add() function. Use add function if the rester itself is going to be used to export multiple resources. | | options.base | String | The resource base url. It can be included here or by using the add() function. Use add function if the rester itself is going to be used to export multiple resources. | | options.before | function | A koa middleware to be executed before each single rest request. It can be added in get, post, put, delete, list and rest options. | | options.after | function | A koa middleware to be executed after each single rest request. It can be added in get, post, put, delete, list and rest options. |

rester.add(model, name) ⇒ Rester

Kind: instance method of Rester
Returns: Rester - A new Rester instance configured with the given model and base.

| Param | Type | Description | | --- | --- | --- | | model | Object | The persistence layer Model object. | | name | String | The resource name used to build the resource URI |

rester.rest(options) ⇒ Rester

Build the endpoints /resource and /resource/:id with the methods GET, POST PUT, PATCH and DELETE.

Kind: instance method of Rester
Returns: Rester - The Rester itself.

| Param | Type | Description | | --- | --- | --- | | options | Object | The resource specific options. | | options.before | function | beforeList, beforePost, beforeGet, beforePut and/or beforeDelte: A koa middleware to be executed before the selected operation. | | options.after | function | afterList, afterPost, afterGet, afterPut and/or afterDelte: A koa middleware to be executed after the selected operation. |

rester.list(options) ⇒ Rester

Build the endpoint /resource allowing GET requests. It will respond with all the resources available in the persistence layer.

Kind: instance method of Rester
Returns: Rester - The Rester itself.

| Param | Type | Description | | --- | --- | --- | | options | Object | The endpoint specific options. |

rester.post(options) ⇒ Rester

Build the endpoint /resource allowing POST requests. It will save the resource received in the persistence layer.

Kind: instance method of Rester
Returns: Rester - The Rester itself.

| Param | Type | Description | | --- | --- | --- | | options | Object | The endpoint specific options. |

rester.get(options) ⇒ Rester

Build the endpoint /resource/:id allowing GET requests. It will return the resource with the id given in the url.

Kind: instance method of Rester
Returns: Rester - The Rester itself.

| Param | Type | Description | | --- | --- | --- | | options | Object | The endpoint specific options. |

rester.patch(options) ⇒ Rester

Build the endpoint /resource/:id allowing PATCH requests. It will modify the resource with the id given in the url.

Kind: instance method of Rester
Returns: Rester - The Rester itself.

| Param | Type | Description | | --- | --- | --- | | options | Object | The endpoint specific options. |

rester.delete(options) ⇒ Rester

Build the endpoint /resource/:id allowing DELETE requests. It will remove the resource with the id given in the url.

Kind: instance method of Rester
Returns: Rester - The Rester itself.

| Param | Type | Description | | --- | --- | --- | | options | Object | The endpoint specific options. |

Rester.errorHandler(error) ⇒ Object

Converts a persistence layer error into a JSON error. JSON errors must have at least 2 properties 'status' and 'message'. Status will be the http status code of the response so it must be a valid one. This handler supports only mongoose and orm2 errors. If any other DBMS is required it can be overwritten via Rester's option errorHandler.

Kind: static method of Rester
Returns: Object - The JSON that should be returned via http.

| Param | Type | Description | | --- | --- | --- | | error | Object | The error object thrown from the persistence layer. |