https://github.com/samislam/stack-player
stack-player is a small powerful express middleware that allows for executing middlewares switching between them conditionally and handling their errors separately
https://github.com/samislam/stack-player
Last synced: 10 months ago
JSON representation
stack-player is a small powerful express middleware that allows for executing middlewares switching between them conditionally and handling their errors separately
- Host: GitHub
- URL: https://github.com/samislam/stack-player
- Owner: samislam
- Created: 2022-09-10T20:42:58.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-12T18:53:51.000Z (about 2 years ago)
- Last Synced: 2024-12-18T07:09:03.515Z (about 1 year ago)
- Language: TypeScript
- Size: 32.2 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# stack-player
**stack-player** is a small, powerful Express middleware that allows for executing middlewares, switching between them conditionally, and handling their errors separately.
For example, you can use stack-player to execute Express middlewares as follows:
```typescript
userRouter.get('/:userId', stackplayer((req, res, sNext) => {
/* your code */
}))
userRouter.get('/:userId', stackplayer((req, res, sNext) => (req, res, sNext) => {
/* your code */
}))
userRouter.get('/:userId', stackplayer((req, res, sNext) => [
(req, res, sNext) => { /* your code */ },
(req, res, sNext) => { /* your code */ },
(req, res, sNext) => { /* your code */ },
], { autoCallNext: true, callNext: true })
userRouter.get('/:userId', stackplayer([
(req, res, sNext) => { /* your code */ },
(req, res, sNext) => { /* your code */ },
(req, res, sNext) => { /* your code */ },
], { autoCallNext: true, callNext: true }))
```
# How this is useful?
This opens up the doors to new express applications design patterns, for example, the following code sample demonstrates using stack-player with fancy-object to set the request within the corresponding middleware stack to the logged in user role:
```ts
userRouter.use((req, res, next) => {
req.$loggedInUser = { role: 'admin' }
next()
})
userRouter.get(
'/:userId',
stackplayer(
(req) =>
({
superadmin: [(req, res, next) => {}, (req, res, next) => {}],
admin: [(req, res, next) => {}, (req, res, next) => {}],
user: [(req, res, next) => {}, (req, res, next) => {}],
}[req.$loggedInUser.role])
)
)
```