0.3.0 • Published 6 years ago

@aexol/syncano-middleware-common v0.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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

ParamTypeDescription
fnfunction | ObjectEither next function in chain or object with key: value pairs of method and handler function for method type.
allowedArrayList 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

ParamTypeDescription
fnfunctionNext function in request chain
optsObjectAdditional options. Optional
opts.messageStringAlternative 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

ParamTypeDescription
fnfunctionNext 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

ParamTypeDescription
fnfunctionHandler function
optsObjectAditional opts. Optional
opts.replaceFnfunctionIf set, will be used to replace buffer contents instead of default behaviour.
opts.excludeArrayList of args to skip from replacing.
opts.encodingStringInput encoding of buffer in args.
opts.inputEncodingStringOutput 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

ParamTypeDescription
fnfunctionNext function in chain.
optsObjectAdditional options. Optional.
opts.messageStringCustom error message if not root account.
opts.condFnfunctionCheck 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

ParamTypeDescription
fnfunctionNext function in chain
fieldsArrayfields 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

ParamTypeDescription
fnfunctionNext function in chain
fieldsArrayfields to cast to number