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 1.0.0 to 2.0.0

2

dist/index.js

@@ -1,1 +0,1 @@

"use strict";function __awaiter(e,t,n,i){return new(n||(n=Promise))(function(r,o){function c(e){try{u(i.next(e))}catch(e){o(e)}}function a(e){try{u(i.throw(e))}catch(e){o(e)}}function u(e){e.done?r(e.value):new n(function(t){t(e.value)}).then(c,a)}u((i=i.apply(e,t||[])).next())})}Object.defineProperty(exports,"__esModule",{value:!0});const ErrCalledMoreThanOnce=new Error("A middleware is called more than once!"),emptyLambda=()=>__awaiter(void 0,void 0,void 0,function*(){});function executorOf(...e){return(t,n=emptyLambda)=>__awaiter(this,void 0,void 0,function*(){yield exec(t,[...e]),yield n()})}function exec(e,t){return __awaiter(this,void 0,void 0,function*(){0!==t.length&&(yield getNext(e,t)())})}function getNext(e,t){if(0===t.length)return emptyLambda;let n=!1;const i=t[0];return()=>__awaiter(this,void 0,void 0,function*(){if(n)throw ErrCalledMoreThanOnce;return n=!0,t.shift(),yield i(e,getNext(e,t))})}exports.executorOf=executorOf;
"use strict";function __awaiter(e,t,n,o){return new(n||(n=Promise))(function(i,r){function c(e){try{d(o.next(e))}catch(e){r(e)}}function a(e){try{d(o.throw(e))}catch(e){r(e)}}function d(e){e.done?i(e.value):new n(function(t){t(e.value)}).then(c,a)}d((o=o.apply(e,t||[])).next())})}Object.defineProperty(exports,"__esModule",{value:!0});const ErrCalledMoreThanOnce=new Error("A middleware is called more than once!"),emptyLambda=()=>__awaiter(void 0,void 0,void 0,function*(){});function compose(...e){return(t,n=emptyLambda)=>__awaiter(this,void 0,void 0,function*(){yield exec(t,[...e]),yield n()})}function exec(e,t){return __awaiter(this,void 0,void 0,function*(){0!==t.length&&(yield getNext(e,t)())})}function getNext(e,t){if(0===t.length)return emptyLambda;let n=!1;const o=t[0];return()=>__awaiter(this,void 0,void 0,function*(){if(n)throw ErrCalledMoreThanOnce;return n=!0,t.shift(),yield o(e,getNext(e,t))})}exports.compose=compose;
{
"name": "@idan-loo/middleware",
"version": "1.0.0",
"version": "2.0.0",
"description": "Create your own middlewares to divide a large logic into serveral wares",
"main": "./dist/index.js",
"types": "./types",
"types": "./types/index.d.ts",
"scripts": {

@@ -8,0 +8,0 @@ "build": "tacer-script build",

@@ -19,3 +19,3 @@ # middleware [![CircleCI](https://circleci.com/gh/IdanLoo/middleware.svg?style=svg)](https://circleci.com/gh/IdanLoo/middleware)

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

@@ -37,3 +37,7 @@ const mw1 = async (ctx, next) => {

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

@@ -72,3 +76,3 @@ ```

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

@@ -90,10 +94,14 @@ const mw1 = async (ctx: Context, next: Next) => {

// mw3 won't be executed because mw2 doesn't call the next method
const exec = executorOf(mw1, mw2, mw3)
const exec = compose(
mw1,
mw2,
mw3
)
exec({ name: 'test' })
```
### Each executor is a middleware as well, so you can combine several middlewares by simply calling the `executorOf` method
### Each calling of `compose` returns a middleware as well, so you can combine several middlewares by simply calling the `compose` method
```js
import { executorOf } from '@idan-loo/middleware'
import { compose } from '@idan-loo/middleware'

@@ -116,6 +124,12 @@ const checkAuthed = async (ctx, next) => {

*/
const getSomePrivacyIfAuthed = executorOf(checkAuthed, getSomePrivacy)
const getSomePrivacyIfAuthed = compose(
checkAuthed,
getSomePrivacy
)
const exec = executorOf(getSomePrivacyIfAuthed, handleThePrivacy)
const exec = compose(
getSomePrivacyIfAuthed,
handleThePrivacy
)
await exec(ctx)
```

@@ -1,2 +0,2 @@

export * from './types';
export { executorOf } from './middleware';
export { Next, Middleware } from './types';
export { compose } from './middleware';
import { Middleware } from './types';
export declare function executorOf<T>(...middlewares: Middleware<T>[]): Middleware<T>;
export declare function compose<T>(...middlewares: Middleware<T>[]): Middleware<T>;
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