Maker
Constructor Summary
Private Constructor | ||
private |
|
Method Summary
Public Methods | ||
public |
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 |
Handle DELETE HTTP method with single or many handlers |
|
public |
Handle GET HTTP method with single or many handlers |
|
public |
Handle PATCH HTTP method with single or many handlers |
|
public |
Handle POST HTTP method with single or many handlers |
|
public |
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 |
|
public |
Add scoped address, before/after handlers and simple handlers. |
since 0.2.0 |
Private Methods | ||
private |
build(): * |
|
private |
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:
Name | Type | Attribute | Description |
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
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:
Name | Type | Attribute | Description |
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
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:
Name | Type | Attribute | Description |
name | string | Name of the resource. Create route path from |
|
controller | CrudController | Object with methods |
|
options | crudOptions |
|
Options object |
creator | function(scope: Maker): void |
|
Scoped creator function |
Example:
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)
})
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'] })
})
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
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
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
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
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
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:
Name | Type | Attribute | Description |
name | string | Name of the resources. Path created from. Example: |
|
controller | ResourcesController | Object with methods |
|
options | resourcesOptions |
|
Options for resources |
creator | function(scope: Maker): void |
|
Scoped creator function |
Return:
void |
Throw:
"Resources should be named" |
|
"You can't use 'except' and 'only' options at the same time" |
|
"Controller should be object" |
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
Return:
void |
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)
})
})