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 - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

.circleci/config.yml

2

package.json
{
"name": "@idan-loo/middleware",
"version": "0.2.0",
"version": "0.3.0",
"description": "Create your own middlewares to divide a large logic into serveral wares",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -1,3 +0,89 @@

# middleware
# middleware [![CircleCI](https://circleci.com/gh/IdanLoo/middleware.svg?style=svg)](https://circleci.com/gh/IdanLoo/middleware)
create your own middlewares to divide a large logic into serveral wares
Create your own middlewares to divide a large logic into serveral wares.
It is inspired by the middleware system of [Koa](https://koajs.com/)
## Install
```bash
# with npm
npm install --save @idan-loo/middleware
# with yarn
yarn add @idan-loo/middleware
```
## Usage
```js
import { executorOf } 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 = executorOf({ name: 'test' })
await exec(mw1, mw2, mw3)
```
## 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()`.
```js
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.
```js
import { executorOf } 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()
}
const exec = executorOf({ name: 'test' })
// mw3 won't be executed because mw2 doesn't call the next method
exec(mw1, mw2, mw3)
```
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