@aexol/syncano-middleware-common
v0.3.0
Published
Collection of common middlewares for syncano sockets.
Downloads
9
Readme
Syncano Middleware Common
About
Collection of common utilities for sockets in syncano that can be used in conjunction with @aexol/syncano-middleware lib.
Installation
npm install @aexol/syncano-middleware-common
Usage
API Reference
Modules
Functions
@aexol/syncano-middleware-common
Common middlewares for syncano.
allowedMethods(fn, allowed) ⇒ function
Checks if request is allowed based on request method.
Kind: global function
Access: public
| Param | Type | Description |
| --- | --- | --- |
| fn | function | Object | Either next function in chain or object with key: value
pairs of method and handler function for method type. |
| allowed | Array | List of allowed methods in case of fn being function. Optional. |
Example
import serve, {response} from '@aexol/syncano-middleware'
import {allowedMethods} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, allowedMethods(hello, ['GET']))
Example
import serve, {response} from '@aexol/syncano-middleware'
import {allowedMethods} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, allowedMethods({
GET: hello,
POST: hello
}))
loggedIn(fn, opts) ⇒ Object
Checks if user is logged in, returning response with 403 and message if not.
Kind: global function
Access: public
| Param | Type | Description | | --- | --- | --- | | fn | function | Next function in request chain | | opts | Object | Additional options. Optional | | opts.message | String | Alternative message if user is not logged in. |
Example
import serve, {response} from '@aexol/syncano-middleware'
import {loggedIn} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, loggedIn(hello))
parseGETFields(fn) ⇒ function
Parses args in GET request as json if possible. If not, leaves them unchanged.
Kind: global function
Access: public
| Param | Type | Description | | --- | --- | --- | | fn | function | Next function in request chain |
Example
import serve, {response} from '@aexol/syncano-middleware'
import {parseGETFields} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, parseGETFields(hello))
replaceBuffers(fn, opts) ⇒ Object
Replace all buffers in socket args.
Kind: global function
Access: public
| Param | Type | Description | | --- | --- | --- | | fn | function | Handler function | | opts | Object | Aditional opts. Optional | | opts.replaceFn | function | If set, will be used to replace buffer contents instead of default behaviour. | | opts.exclude | Array | List of args to skip from replacing. | | opts.encoding | String | Input encoding of buffer in args. | | opts.inputEncoding | String | Output encoding of buffer in args. Unless, replaceFn was set, this middleware replaces all buffers with it's string content in place. Modifies ctx.args. |
Example
import serve, {response} from '@aexol/syncano-middleware'
import {replaceBuffers} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: 'ok'})
}
export default ctx => serve(ctx, replaceBuffers(hello))
rootAccount(fn, opts) ⇒ Object
Root account check middleware.
Kind: global function
Access: public
| Param | Type | Description | | --- | --- | --- | | fn | function | Next function in chain. | | opts | Object | Additional options. Optional. | | opts.message | String | Custom error message if not root account. | | opts.condFn | function | Check for root only if function evaluates to true. |
Example
import serve, {response} from '@aexol/syncano-middleware'
import {rootAccount} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.admin.email}`)
}
export default ctx => serve(ctx, rootAccount(hello))
toBool(fn, fields)
Attempts to cast certain fields in request to bool. Can be useful to handling both GET and POST input on endpoint as GET endpoints will always have a string.
Fields that are either true
or 'true'
will evaluate to true.
Everything else will be considered false.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | fn | function | Next function in chain | | fields | Array | fields to cast to bool |
toNumber(fn, fields)
Attempts to cast certain fields in request to number. Can be useful to handling both GET and POST input on endpoint as GET endpoints will always have a string.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | fn | function | Next function in chain | | fields | Array | fields to cast to number |