Skip to content

Commit

Permalink
Add start() function to correctly setup ALS
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Apr 22, 2024
1 parent 15d8293 commit 1c2a9f4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ npm i fastify fastify-asyncforge
```js
// App.js
import fastify from 'fastify'
import { start } from 'fastify-asyncforge'
import doWork from './do-work.mjs'

const app = fastify({
logger: true
})
app.register(import('fastify-asyncforge'))

await start(app)

app.decorate('foo', 'bar')
app.decorateRequest('a')
Expand Down
5 changes: 5 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const fastify = require('fastify')
const doWork = require('./do-work')
const { logger, start } = require('../index')

const app = fastify({
logger: true
})
app.register(require('../index'))

start(app)

logger().info('hello')

app.decorate('foo', 'bar')
app.decorateRequest('a')
app.decorateReply('b')
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare namespace fastifyasyncforge {
export function request<T extends FastifyRequest>(): T;
export function reply<T extends FastifyReply>(): T;
export function logger<T extends FastifyBaseLogger>(): T;
export function start<T extends FastifyInstance>(): Promise<T>;
}

declare function fastifyasyncforge(): FastifyPluginCallback;
Expand Down
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const request = memo()
const reply = memo()
const logger = memo()

module.exports = fp(async function (fastify, opts) {
app.set(fastify)
logger.set(fastify.log)

const fastifyAsyncForge = fp(function (fastify, opts, next) {
fastify.addHook('onRequest', async function (req, res) {
setAll({
[app.key]: this,
Expand All @@ -20,8 +17,20 @@ module.exports = fp(async function (fastify, opts) {
[logger.key]: req.log
})
})
next()
})

function start (fastify) {
setAll({
[app.key]: fastify,
[logger.key]: fastify.log
})

return fastify.register(fastifyAsyncForge)
}

module.exports = fastifyAsyncForge
module.exports.start = start
module.exports.app = app
module.exports.request = request
module.exports.reply = reply
Expand Down
5 changes: 4 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectAssignable, expectError, expectType } from "tsd";
import fastifyasyncforge, { app, logger, reply, request } from ".";
import fastifyasyncforge, { app, logger, reply, request, start } from ".";
import fastify, {
type FastifyInstance,
type FastifyBaseLogger,
Expand Down Expand Up @@ -46,3 +46,6 @@ expectType<void>(logger().info({ msg: "oh!" }));
expectType<void>(logger().warn({ msg: "let's go!!!" }));
expectError<FastifyBaseLogger>(logger<object>());
expectError<FastifyBaseLogger>({});

// start
expectType<Promise<FastifyInstance>>(start());
34 changes: 31 additions & 3 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,41 @@ const fastifyAsyncForge = require('../')

const { app, request, reply, logger } = fastifyAsyncForge

test('basic helpers', async (t) => {
const p = tspl(t, { plan: 6 })
test('basic helpers with start', async (t) => {
const p = tspl(t, { plan: 7 })
const fastify = Fastify()

await fastifyAsyncForge.start(fastify)

p.strictEqual(logger(), fastify.log)
p.strictEqual(app(), fastify)

fastify.get('/', async function (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
return { hello: 'world' }
})

const res = await fastify.inject({
method: 'GET',
url: '/'
})

p.strictEqual(res.statusCode, 200)

await p.complete
})

test('basic helpers without start', async (t) => {
const p = tspl(t, { plan: 7 })
const fastify = Fastify()

await fastify.register(fastifyAsyncForge)

p.strictEqual(logger(), app.log)
p.strictEqual(logger(), undefined)
p.strictEqual(app(), undefined)

fastify.get('/', async function (_request, _reply) {
p.strictEqual(app(), this)
Expand Down

0 comments on commit 1c2a9f4

Please sign in to comment.