npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aexol/syncano-middleware-common

v0.3.0

Published

Collection of common middlewares for syncano sockets.

Downloads

10

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 |