Comparing version 3.0.0 to 4.0.0
/** | ||
* Next function supports optional `ctx` replacement for following middleware. | ||
*/ | ||
export declare type Next<T, U> = (ctx?: T) => Promise<U>; | ||
export declare type Next<T> = () => Promise<T>; | ||
/** | ||
* Middleware function pattern. | ||
*/ | ||
export declare type Middleware<T, U> = (ctx: T, next: Next<T, U>) => U | Promise<U>; | ||
export declare type Middleware<T, U> = (ctx: T, next: Next<U>) => U | Promise<U>; | ||
/** | ||
@@ -10,0 +10,0 @@ * Final function has no `next()`. |
@@ -19,31 +19,27 @@ "use strict"; | ||
} | ||
return function debugComposed(ctx, done) { | ||
return function composedDebug(ctx, done) { | ||
if (typeof done !== 'function') { // tslint:disable-line | ||
throw new TypeError(`Expected the last argument to be \`done(ctx)\`, but got ${typeof done}`); | ||
} | ||
function dispatcher(startIndex, ctx) { | ||
let index = startIndex; | ||
function dispatch(pos) { | ||
const fn = middleware[pos] || done; | ||
if (pos > middleware.length) { | ||
throw new TypeError('Composed `done(ctx)` function should not call `next()`'); | ||
} | ||
index = pos; | ||
return new Promise(resolve => { | ||
const result = fn(ctx, function next(newCtx) { | ||
if (index > pos) | ||
throw new TypeError('`next()` called multiple times'); | ||
if (newCtx === undefined) | ||
return dispatch(pos + 1); | ||
return dispatcher(pos + 1, newCtx); | ||
}); | ||
if (result === undefined) { // tslint:disable-line | ||
throw new TypeError('Expected middleware to return `next()` or a value'); | ||
let index = 0; | ||
function dispatch(pos) { | ||
const fn = middleware[pos] || done; | ||
index = pos; | ||
return new Promise(resolve => { | ||
const result = fn(ctx, function next() { | ||
if (pos < index) { | ||
throw new TypeError('`next()` called multiple times'); | ||
} | ||
return resolve(result); | ||
if (pos > middleware.length) { | ||
throw new TypeError('Composed `done(ctx)` function should not call `next()`'); | ||
} | ||
return dispatch(pos + 1); | ||
}); | ||
} | ||
return dispatch(startIndex); | ||
if (result === undefined) { // tslint:disable-line | ||
throw new TypeError('Expected middleware to return `next()` or a value'); | ||
} | ||
return resolve(result); | ||
}); | ||
} | ||
return dispatcher(0, ctx); | ||
return dispatch(index); | ||
}; | ||
@@ -60,4 +56,4 @@ } | ||
return new Promise(resolve => { | ||
return resolve(fn(ctx, function next(newCtx) { | ||
return dispatch(pos + 1, newCtx === undefined ? ctx : newCtx, done); | ||
return resolve(fn(ctx, function next() { | ||
return dispatch(pos + 1, ctx, done); | ||
})); | ||
@@ -64,0 +60,0 @@ }); |
@@ -19,3 +19,3 @@ "use strict"; | ||
const fn = index_1.compose([]); | ||
const expectedName = process.env.NODE_ENV !== 'production' ? 'debugComposed' : 'composed'; | ||
const expectedName = process.env.NODE_ENV !== 'production' ? 'composedDebug' : 'composed'; | ||
expect(fn.name).toEqual(expectedName); | ||
@@ -109,43 +109,4 @@ }); | ||
})); | ||
it('should compose multiple layers', () => __awaiter(this, void 0, void 0, function* () { | ||
const arr = []; | ||
function middleware(n, next) { | ||
arr.push(n); | ||
return next(n + 1); | ||
} | ||
const fn = index_1.compose([ | ||
middleware, | ||
index_1.compose([ | ||
index_1.compose([ | ||
middleware, | ||
middleware | ||
], debugMode), | ||
middleware | ||
], debugMode), | ||
index_1.compose([ | ||
middleware | ||
], debugMode) | ||
], debugMode); | ||
const res = yield fn(0, ctx => ctx); | ||
expect(res).toEqual(5); | ||
expect(arr).toEqual([0, 1, 2, 3, 4]); | ||
})); | ||
it('should replace context object', () => __awaiter(this, void 0, void 0, function* () { | ||
const fn = index_1.compose([ | ||
function (ctx, next) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
expect(ctx.original).toBe(true); | ||
const res = yield next(); | ||
expect(ctx.original).toBe(true); | ||
return res; | ||
}); | ||
}, | ||
function (ctx, next) { | ||
return next({ original: false }); | ||
} | ||
], debugMode); | ||
expect(yield fn({ original: true }, ctx => ctx.original)).toEqual(false); | ||
})); | ||
}); | ||
} | ||
//# sourceMappingURL=index.spec.js.map |
{ | ||
"name": "throwback", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Simple asynchronous middleware pattern", | ||
@@ -57,11 +57,11 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^22.2.3", | ||
"@types/jest": "^23.3.2", | ||
"@types/node": "^10.1.2", | ||
"jest": "^22.4.4", | ||
"jest": "^23.6.0", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^22.4.6", | ||
"ts-jest": "^23.10.0", | ||
"tslint": "^5.10.0", | ||
"tslint-config-standard": "^7.0.0", | ||
"typescript": "^2.8.3" | ||
"tslint-config-standard": "^8.0.1", | ||
"typescript": "^3.0.3" | ||
} | ||
} |
@@ -73,24 +73,2 @@ # Throwback | ||
### Advanced | ||
Did you know `next(ctx?)` accepts an optional `ctx` argument which will override `ctx` for all following middleware functions? This enables advanced functionality such as `Request` cloning and retries in [`popsicle`](https://github.com/serviejs/popsicle). | ||
```js | ||
async function retryRequest (req, next) { | ||
let retries = 5 | ||
while (retries--) { | ||
try { | ||
const res = await next(req.clone()) | ||
return res | ||
} catch (e) { | ||
continue | ||
} | ||
} | ||
throw new Error('Retry limit exceeded') | ||
} | ||
``` | ||
## Use Cases | ||
@@ -97,0 +75,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
25762
200
86