Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@idan-loo/middleware

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@idan-loo/middleware

Create your own middlewares to divide a large logic into serveral wares

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

middleware CircleCI

Create your own middlewares to divide a large logic into serveral wares.

It is inspired by the middleware system of Koa

Install

# with npm
npm install --save @idan-loo/middleware
# with yarn
yarn add @idan-loo/middleware

Usage

import { compose } from '@idan-loo/middleware'

const mw1 = async (ctx, next) => {
  ctx.name = 'mw1'
  await next()
}

const mw2 = async (ctx, next) => {
  ctx.name = 'mw2'
  await next()
}

const mw3 = async (ctx, next) => {
  ctx.name = 'mw3'
  await next()
}

const exec = compose(
  mw1,
  mw2,
  mw3
)
await exec({ name: 'test' })

Middleware

Each middleware should return a Promise object. You can simply add an async keyword in front of the function declation, or just return next().

import { Next } from '@idan-loo/middleware'

type Context = {
  name: string,
}

const mw1 = async (ctx: Context, next: Next) => {
  ctx.name = 'mw1'
  return next()
}

const mw2 = async (ctx: Context, next: Next) => {
  ctx.name = await anAsyncAction()
  await next()
}

const mw3 = async (ctx: Context, next: Next) => {
  await next()
  ctx.name = 'after next'
}

If you want to go on executing the next middlewares, you should call next() manually.

import { compose } from '@idan-loo/middleware'

const mw1 = async (ctx: Context, next: Next) => {
  ctx.name = 'mw1'
  return next()
}

const mw2 = async (ctx: Context) => {
  ctx.name = 'mw2'
}

const mw3 = async (ctx: Context, next: Next) => {
  ctx.name = 'mw3'
  return next()
}

// mw3 won't be executed because mw2 doesn't call the next method
const exec = compose(
  mw1,
  mw2,
  mw3
)
exec({ name: 'test' })

Each calling of compose returns a middleware as well, so you can combine several middlewares by simply calling the compose method

import { compose } from '@idan-loo/middleware'

const checkAuthed = async (ctx, next) => {
  // Go next only if the `ctx.isAuthed` is true
  if (ctx.isAuthed) {
    return next()
  }
}

const getSomePrivacy = async (ctx, next) => {
  /* Do some things */
  return next()
}

/* 
   Combine the `getSomePrivacy` with the `checkAuthed`.
   `checkAuthed` can be reused in everywhere you need.
 */
const getSomePrivacyIfAuthed = compose(
  checkAuthed,
  getSomePrivacy
)

const exec = compose(
  getSomePrivacyIfAuthed,
  handleThePrivacy
)
await exec(ctx)

Keywords

FAQs

Package last updated on 12 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc