@idan-loo/middleware
Advanced tools
Comparing version 0.3.0 to 1.0.0
@@ -1,1 +0,1 @@ | ||
"use strict";function __awaiter(e,t,n,r){return new(n||(n=Promise))(function(o,i){function c(e){try{a(r.next(e))}catch(e){i(e)}}function u(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){e.done?o(e.value):new n(function(t){t(e.value)}).then(c,u)}a((r=r.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)=>exec(e,t)}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 r=t[0];return()=>__awaiter(this,void 0,void 0,function*(){if(n)throw ErrCalledMoreThanOnce;return n=!0,t.shift(),yield r(e,getNext(e,t))})}exports.executorOf=executorOf; | ||
"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; |
{ | ||
"name": "@idan-loo/middleware", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"description": "Create your own middlewares to divide a large logic into serveral wares", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -36,4 +36,4 @@ # middleware [![CircleCI](https://circleci.com/gh/IdanLoo/middleware.svg?style=svg)](https://circleci.com/gh/IdanLoo/middleware) | ||
const exec = executorOf({ name: 'test' }) | ||
await exec(mw1, mw2, mw3) | ||
const exec = executorOf(mw1, mw2, mw3) | ||
await exec({ name: 'test' }) | ||
``` | ||
@@ -43,3 +43,3 @@ | ||
Each middleware should return a Promise object. You can simply add an `async` keyword in front of the function declation, or just return `next()`. | ||
### Each middleware should return a Promise object. You can simply add an `async` keyword in front of the function declation, or just return `next()`. | ||
@@ -69,3 +69,3 @@ ```js | ||
If you want to go on executing the next middlewares, you should call `next()` manually. | ||
### If you want to go on executing the next middlewares, you should call `next()` manually. | ||
@@ -89,5 +89,32 @@ ```js | ||
const exec = executorOf({ name: 'test' }) | ||
// mw3 won't be executed because mw2 doesn't call the next method | ||
exec(mw1, mw2, mw3) | ||
const exec = executorOf(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 | ||
```js | ||
import { executorOf } 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 = executorOf(checkAuthed, getSomePrivacy) | ||
const exec = executorOf(getSomePrivacyIfAuthed, handleThePrivacy) | ||
await exec(ctx) | ||
``` |
import { Middleware } from './types'; | ||
export declare function executorOf<T>(ctx: T): (...middlewares: Middleware<T>[]) => Promise<void>; | ||
export declare function executorOf<T>(...middlewares: Middleware<T>[]): Middleware<T>; |
export declare type Next = () => Promise<void>; | ||
export declare type Middleware<T = any> = (ctx: T, next: Next) => Promise<void>; | ||
export declare type Middleware<T = any> = (ctx: T, next?: Next) => Promise<void>; |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4724
1
117
8