Home Reference Source
import {Maker} from 'createrest/lib'
protected class | source

Maker

Constructor Summary

Private Constructor
private

Method Summary

Public Methods
public

afterEach(list: ...function)

Add middlewares after request handler to current scope and all child scope

public

beforeEach(list: ...function)

Add middlewares before request handler to current scope and all child scope

public

crud(name: string, controller: CrudController, options: crudOptions, creator: function(scope: Maker): void)

Add CRUD methods for single resource.

since 0.9.0
public

delete(name: string, handlers: ...function)

Handle DELETE HTTP method with single or many handlers

public

get(name: string, handlers: ...function)

Handle GET HTTP method with single or many handlers

public

patch(name: string, handlers: ...function)

Handle PATCH HTTP method with single or many handlers

public

post(name: string, handlers: ...function)

Handle POST HTTP method with single or many handlers

public

put(name: string, handlers: ...function)

Handle PUT HTTP method with single or many handlers

public

resources(name: string, controller: ResourcesController, options: resourcesOptions, creator: function(scope: Maker): void): void

Add index, create, read, update, patch, remove methods to manage resource
See ResourcesController and resourcesOptions

public

scope(name: string, creator: function(scope: Maker): void): void

Add scoped address, before/after handlers and simple handlers.

since 0.2.0
Private Methods
private

build(): *

private

local(method: string, listeners: function[])

Add HTTP method listeners to local

Private Constructors

private constructor() source

Public Methods

public afterEach(list: ...function) source

Add middlewares after request handler to current scope and all child scope

Params:

NameTypeAttributeDescription
list ...function

List of the middlewares

Example:

const getHandler = () => console.log('get')
const afterHandler = () => console.log('after')

createRest(root => {
  root.afterEach(afterHandler)
  // GET /demo    getHandler(); afterHandler()
  root.get('/demo', getHandler)
})
// Output:
// > get
// > after
You can combine beforeEach and afterEach
const getHandler = () => console.log('get foo')
const deleteHandler = () => console.log('DELETE requested')
const before1 = () => console.log('That is before 1')
const before2 = () => console.log('Second before')
const after = () => console.log('Just after request')

const routes = createRest(root => {
  root.beforeEach(before1, before2)
  root.afterEach(after)

  // GET /foo    before1(); before2(); getHandler(); after()
  root.get('/foo', getHandler)

  root.scope('bar', bar => {
    // DELETE /bar/baz    before1(); before2(); deleteHandler(); after()
    bar.delete('/baz', deleteHandler)
  })
})
// Output on GET /foo request:
// > That is before 1
// > Second before
// > get foo
// > Just after request

// Output on DELETE /bar/baz
// > That is before 1
// > Second before
// > DELETE requested
// > Just after request

public beforeEach(list: ...function) source

Add middlewares before request handler to current scope and all child scope

Params:

NameTypeAttributeDescription
list ...function

List of the middlewares

Example:

const beforeHandler = () => console.log('before')
const getHandler = () => console.log('get')

createRest(root => {
  root.beforeEach(beforeHandler)
  // GET /demo    beforeHandler(); getHandler()
  root.get('/demo', getHandler)
})
// Output:
// > before
// > get
With variable count
const handle1 = () => console.log('handle1')
const handle2 = () => console.log('handle2')

createRest(root => {
  root.beforeEach(handle1, handle2)
  // same as
  root.beforeEach(handle1)
  root.beforeEach(handle2)
})

public crud(name: string, controller: CrudController, options: crudOptions, creator: function(scope: Maker): void) since 0.9.0 source

Add CRUD methods for single resource.
CRUD methods not merging. Use only one crud for path.

Params:

NameTypeAttributeDescription
name string

Name of the resource. Create route path from

controller CrudController

Object with methods

options crudOptions
  • optional
  • default: {}

Options object

creator function(scope: Maker): void
  • optional
  • default: null

Scoped creator function

Example:

Simple
const Controller = {
  read() {},
  create() {},
  update() {},
  destroy() {},
  beforeEach() {},
  afterEach() {},
}
const routes = createRest(root => {
  // GET /example     read()
  // POST /example    create()
  // PUT /example     update()
  // DELETE /example  destroy()
  root.crud('example', Controller)
})
Only/Except option
const Controller = {
  read() {},
  create() {},
  update() {},
  destroy() {},
  beforeEach() {},
  afterEach() {},
}
const routes = createRest(root => {
  // GET /demo    read()
  // POST /demo   create()
  root.crud('demo', Controller, { only: ['create', 'read'] })

  // GET /single     read()
  // PUT /single     update()
  root.crud('single', Controller, { except: ['destroy', 'create'] })
})
With scope
const routes = createRest(root => {
  // GET /example
  // POST /example
  // PUT /example
  // DELETE /example
  root.crud('example', Controller, {}, example => {
    // GET /example/demo
    example.get('/demo', () => {})
  })
})

public delete(name: string, handlers: ...function) source

Handle DELETE HTTP method with single or many handlers

Params:

NameTypeAttributeDescription
name string
  • optional

Route path. Default is current scope

handlers ...function

List of http-handlers

Throw:

Error

Path should not be deep

TypeError

Maybe you forget to add listener?

Example:

createRest(root => {
  root.delete('name', () => console.log('Handled delete /name request'))
  root.delete(() => console.log('Handled delete / request'))
  root.delete('create',
    (req, res, next) => next(),
    authorize('user'),
    () => console.log('Handled delete /create with middlewares')
  )
})

public get(name: string, handlers: ...function) source

Handle GET HTTP method with single or many handlers

Params:

NameTypeAttributeDescription
name string
  • optional

Route path. Default is current scope

handlers ...function

List of http-handlers

Throw:

Error

Path should not be deep

TypeError

Maybe you forget to add listener?

Example:

createRest(root => {
  root.get('name', () => console.log('Handled get /name request'))
  root.get(() => console.log('Handled get / request'))
  root.get('create',
    (req, res, next) => next(),
    authorize('user'),
    () => console.log('Handled get /create with middlewares')
  )
})

public patch(name: string, handlers: ...function) source

Handle PATCH HTTP method with single or many handlers

Params:

NameTypeAttributeDescription
name string
  • optional

Route path. Default is current scope

handlers ...function

List of http-handlers

Throw:

Error

Path should not be deep

TypeError

Maybe you forget to add listener?

Example:

createRest(root => {
  root.patch('name', () => console.log('Handled patch /name request'))
  root.patch(() => console.log('Handled patch / request'))
  root.patch('create',
    (req, res, next) => next(),
    authorize('user'),
    () => console.log('Handled patch /create with middlewares')
  )
})

public post(name: string, handlers: ...function) source

Handle POST HTTP method with single or many handlers

Params:

NameTypeAttributeDescription
name string
  • optional

Route path. Default is current scope

handlers ...function

List of http-handlers

Throw:

Error

Path should not be deep

TypeError

Maybe you forget to add listener?

Example:

createRest(root => {
  root.post('name', () => console.log('Handled post /name request'))
  root.post(() => console.log('Handled post / request'))
  root.post('create',
    (req, res, next) => next(),
    authorize('user'),
    () => console.log('Handled post /create with middlewares')
  )
})

public put(name: string, handlers: ...function) source

Handle PUT HTTP method with single or many handlers

Params:

NameTypeAttributeDescription
name string
  • optional

Route path. Default is current scope

handlers ...function

List of http-handlers

Throw:

Error

Path should not be deep

TypeError

Maybe you forget to add listener?

Example:

createRest(root => {
  root.put('name', () => console.log('Handled put /name request'))
  root.put(() => console.log('Handled put / request'))
  root.put('create',
    (req, res, next) => next(),
    authorize('user'),
    () => console.log('Handled put /create with middlewares')
  )
})

public resources(name: string, controller: ResourcesController, options: resourcesOptions, creator: function(scope: Maker): void): void source

Add index, create, read, update, patch, remove methods to manage resource
See ResourcesController and resourcesOptions

Params:

NameTypeAttributeDescription
name string

Name of the resources. Path created from. Example: books

controller ResourcesController

Object with methods

options resourcesOptions
  • optional
  • default: {}

Options for resources

creator function(scope: Maker): void
  • optional
  • default: null

Scoped creator function

Return:

void

Throw:

Error

"Resources should be named"

Error

"You can't use 'except' and 'only' options at the same time"

TypeError

"Controller should be object"

Example:

Full example
createRest(root => {
  // GET /users             -> index()
  // POST /users            -> create()
  // GET /users/:userId     -> read()
  // PUT /users/:userId     -> update()
  // PATCH /users/:userId   -> patch()
  // DELETE /users/:userId  -> destroy()
  root.resources('users', UsersController)
})

public scope(name: string, creator: function(scope: Maker): void): void since 0.2.0 source

Add scoped address, before/after handlers and simple handlers.
Before/After handlers is inherits from parent scope.
Scopes with the same name will be merged

Params:

NameTypeAttributeDescription
name string

Name of the scope

creator function(scope: Maker): void

Return:

void

Throw:

Error

"Name of the scope should be a word"

TypeError

"Name of the scope should be string!"

Example:

const before1 = () => console.log('before1')
const before2 = () => console.log('before2')
const after1 = () => console.log('after1')
const after2 = () => console.log('after2')
const bazHandler = () => console.log('baz')
const barHandler = () => console.log('bar')

createRest(root => {
  root.beforeEach(before1)
  root.afterEach(after1)

  // POST /baz   before1(); bazHandler(); after1()
  root.post('baz', bazHandler)

  root.scope('foo', foo => {
    foo.beforeEach(before2)
    foo.afterEach(after2)

    // GET /foo/bar   before1(); before2(); barHandler(); after2(); after1()
    foo.get('bar', barHandler)
  })
})

Private Methods

private build(): * source

Return:

*

private local(method: string, listeners: function[]) source

Add HTTP method listeners to local

Params:

NameTypeAttributeDescription
method string
listeners function[]