Skip to content
This repository has been archived by the owner on Jul 20, 2018. It is now read-only.
Diego Ceresuela edited this page Oct 11, 2017 · 7 revisions

koa-rester

Installation

$ npm install koa-rester

Usage

const koa = new Koa()
const rester = new Rester({
  persistence: require('kr-persistence-inmemory'),
  routerOptions: { prefix: '/api' } // base path is /api
})

koa.use(bodyParser())

const authentication = async (ctx, next) => .....
const log = (ctx, next) => ....

const nameResource = rester.add(model, 'name').rest({
  before: authentication, // Every endpoint needs auth
  afterPost: log // Post requests are logged
})

const otherResource = rester.add(otherModel, 'otherName').get({
  after: log // Get requests are logged
}).list().post({
  before: authentication // Only post needs auth
})

koa
  .use(nameResource.routes())
  .use(nameResource.allowedMethods())
  .use(otherResource.routes())
  .use(otherResource.allowedMethods())

API Reference

Classes

Rester
Resource

Rester

Kind: global class

new Rester(options)

Create a rester.

Param Type Description
options Object Configuration object.
options.router Router The router to be used, by default koa-router, change this property can break the package.
options.routerOptions Object The options that will be passed to koa-router constructor. If options.router is overwritten with any other router this options must be changed according to the new router.
options.log function The function used to log the events
options.persistence KoaResterPersistence An instance of KoaResterPersistence, such as kr-presistence-sequelize, kr-persistence-inmemory or kr-presistence-mongoose. This property is compulsory, an error will be thrown if it is not present.

rester.add(model, name) ⇒ Resource

Create a Resource configured on top of the rester, this Resource instance has it own Router and KoaResterPersistence instances.

Kind: instance method of Rester
Returns: Resource - A Resource instance

Param Type Description
model Object A native instance of the supported ORM. If persitence is kr-presistence-mongoose it should be a Mongoose model.
name String The resource name used to build the resource URI without slashes i.e. 'resourceName'.

Resource

Kind: global class

new Resource(options)

Should not be used directly. Build Resource through Rester.add.

Param Type Description
options Object Configuration options

resource.list(options) ⇒ Resource

Create the GET /resource endpoint in the Resource router.

Kind: instance method of Resource

Param Type Description
options Object
options.before function The middleware or array of middlewares that will be executed before the list method.
options.after function The middleware or array of middlewares that will be executed after the list method.

resource.get(options) ⇒ Resource

Create the GET /resource/:id endpoint in the Resource router.

Kind: instance method of Resource

Param Type Description
options Object
options.before function The middleware or array of middlewares that will be executed before the list method.
options.after function The middleware or array of middlewares that will be executed after the list method.

resource.post(options) ⇒ Resource

Create the POST /resource endpoint in the Resource router.

Kind: instance method of Resource

Param Type Description
options Object
options.before function The middleware or array of middlewares that will be executed before the create method.
options.after function The middleware or array of middlewares that will be executed after the create method.

resource.patch(options) ⇒ Resource

Create the PATCH /resource/:id endpoint in the Resource router.

Kind: instance method of Resource

Param Type Description
options Object
options.before function The middleware or array of middlewares that will be executed before the update method.
options.after function The middleware or array of middlewares that will be executed after the update method.

resource.put(options) ⇒ Resource

Create the PUT /resource/:id endpoint in the Resource router.

Kind: instance method of Resource

Param Type Description
options Object
options.before function The middleware or array of middlewares that will be executed before the replace method.
options.after function The middleware or array of middlewares that will be executed after the replace method.

resource.delete(options) ⇒ Resource

Create the DELETE /resource/:id endpoint in the Resource router.

Kind: instance method of Resource

Param Type Description
options Object
options.before function The middleware or array of middlewares that will be executed before the delete method.
options.after function The middleware or array of middlewares that will be executed after the delete method.

resource.rest(options) ⇒ Resource

Create all the endpoints

Kind: instance method of Resource

Param Type Description
options Object The configuration for all the endpoints
options.before function The middleware or array of middlewares that will be executed before any method.
options.after function The middleware or array of middlewares that will be executed after all the methods.
options.afterList function options.after for list()
options.beforeList function options.before for list()
options.afterGet function options.after for get()
options.beforeGet function options.before for get()
options.afterPost function options.after for post()
options.beforePost function options.before for post()
options.afterPut function options.after for put()
options.beforePut function options.before for put()
options.afterDelete function options.after for delete()
options.beforeDelete function options.before for delete()
options.afterPatch function options.after for patch()
options.beforePatch function options.before for patch()

resource.routes() ⇒ function

Sugar syntax that returns resource.router.routes()

Kind: instance method of Resource

resource.allowedMethods() ⇒ function

Sugar syntax that returns resource.router.allowedMethods()

Kind: instance method of Resource