middleware-async
Advanced tools
Comparing version
{ | ||
"name": "middleware-async", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "A handy tool to work with async/promise express middleware", | ||
@@ -10,2 +10,6 @@ "main": "index.js", | ||
"private": false, | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"scripts": { | ||
@@ -15,3 +19,4 @@ "build": "yarn tsc -d index.ts", | ||
"coverage": "yarn jest --coverage", | ||
"lint": "yarn eslint **/*.ts; yarn tslint -p tsconfig.json" | ||
"clean": "rm -rf index.d.ts index.js", | ||
"lint": "yarn eslint index.ts" | ||
}, | ||
@@ -38,5 +43,7 @@ "keywords": [ | ||
"@types/node": "^13.13.4", | ||
"@typescript-eslint/eslint-plugin": "^3.8.0", | ||
"@typescript-eslint/parser": "^3.8.0", | ||
"babel-eslint": "^10.1.0", | ||
"babel-jest": "^25.2.4", | ||
"eslint": "^6.8.0", | ||
"eslint": "^7.6.0", | ||
"eslint-config-airbnb-base": "^14.1.0", | ||
@@ -48,3 +55,2 @@ "eslint-plugin-babel": "^5.3.0", | ||
"jest": "^25.2.4", | ||
"tslint": "^6.1.1", | ||
"typescript": "^3.8.3" | ||
@@ -51,0 +57,0 @@ }, |
@@ -8,3 +8,3 @@ # Async Middleware [](https://travis-ci.org/tranvansang/middleware-async) | ||
## Why this tool is needed? | ||
## Why is this tool needed? | ||
@@ -20,8 +20,8 @@ Lets check at this code | ||
The `next()` will be executed after `User.findById(...).exec()` is fulfilled because express allow handler returning Promise. | ||
The `next()` will be executed after `User.findById(...).exec()` is fulfilled because express allow middlewares returning a promise. | ||
However, express does not support if promise returned by the handler is rejected. | ||
The following handlers will never be called. | ||
However, express does not support if the promise returned by the middleware is rejected. | ||
The following middlewares will never be called, and the response will never be returned to the client. | ||
Solution is simple by wrapping the handler with | ||
The solution is simple by wrapping the middleware with | ||
@@ -73,8 +73,23 @@ ```javascript | ||
- `asyncMiddleware(middlware)`: returns a handler that covers error thrown or error that is rejected by handler via the `next` function. The next function is called at most once. | ||
- `combineMiddlewares(list of handlers or list of list of handlers with any depth)`: combine many handlers into one handler. Very useful for testing | ||
You can combine your handlers like `combineMiddlewares([mdw1, mdw2], [[mdw3], [mdw4, [mdw5, mdw6]], mdw7], mdw8)`. The function will take care of expanding parameters. | ||
- `middlewareToPromise`: convert express-style handler into Promise by appending the next handler to the input handler. | ||
- `asyncMiddleware(middlware)`: returns a middleware that covers the error thrown (`throw err`) or rejected (`next(err)`) by middlewares. The next parameter of the returned middleware is called at most once. | ||
Sample usage: | ||
``` | ||
app.use(asyncMiddleware(async (req, res, next) => {/*middleware code*/})) | ||
``` | ||
- `combineMiddlewares(middleware, list of middlewares, or list of middlewares with any depth)`: combine one or many middlewares into one middlware. Very useful for testing. | ||
You can use this API like `combineMiddlewares(mdw)` or `combineMiddlewares([mdw1, mdw2], [[mdw3], [mdw4, [mdw5, mdw6]], mdw7], mdw8)`. The function will take care of expanding parameters. | ||
Note that this function does not wrap the middelware with `asyncMiddleware`. If the middleware returns a promise, you need to wrap the middleware manually otherwise the error will never be caught. | ||
- `middlewareToPromise`: convert express-style middlware to a function which returns a promise. | ||
`await middlewareToPromise(mdw)(req, res)` is rejected if the middleware `mdw` throws error (in **express/connect-like** style), otherwise the promise is resolved normally. | ||
- `combineToAsync`: combination of `middleewareToPromise` and `combineMiddlewares` | ||
Example: `await combineToAsync(mdw)(req, res)` | ||
## Sample usages | ||
@@ -81,0 +96,0 @@ |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
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
117
14.71%6287
-78.51%21
5%3
-81.25%0
-100%2
Infinity%