Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcollina/fastify-asyncforge
https://github.com/mcollina/fastify-asyncforge
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/mcollina/fastify-asyncforge
- Owner: mcollina
- License: mit
- Created: 2024-04-19T14:28:32.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-04-24T10:38:03.000Z (8 months ago)
- Last Synced: 2024-05-02T01:05:44.550Z (8 months ago)
- Language: JavaScript
- Size: 83 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-asyncforge
Provide easy [AsyncLocalStorage](https://nodejs.org/api/async_context.html) magic to your
[Fastify](https://fastify.dev) apps. It's based on [asyncforge](http://npm.im/asyncforge).## Install
```sh
npm i fastify fastify-asyncforge
```## Usage
```js
// app.mjs
import fastify from 'fastify'
import doWork from './do-work.mjs'
import asyncforge, { logger } from 'fastify-asyncforge'const app = fastify({
logger: true
})
await app.register(asyncforge)app.runInAsyncScope(() => {
logger().info('hello')
})app.decorate('foo', 'bar')
app.decorateRequest('a')
app.decorateReply('b')app.addHook('onRequest', async function (req, reply) {
req.a = 'a'
reply.b = 'b'
})app.get('/', async function (request, reply) {
doWork()
return { hello: 'world' }
})app.listen({ port: 3000 })
// do-work.mjs
import { logger, app, request, reply } from 'fastify-asyncforge'export default function doWork () {
const log = logger()
log.info({
foo: app().foo,
a: request().a,
b: reply().b
}, 'doing work')
}
```## `.enterWith()`
In case `.runInAsyncScope()` is incovenient, you can use `.enterWith()`
```js
import fastify from 'fastify'
import asyncforge from 'fastify-asyncforge'const fastify = Fastify()
// Calling .enterWith() is necessary or `asyncforge.app()` will throw
fastify.enterWith()
asyncforge.app()
```### Notice
Note that you cannot wrap `.enterWith()` inside an async function, as it will not work.
If you are interested in knowing more, read https://github.com/nodejs/node/issues/53037.## License
MIT