From 1c2a9f49a6d4b652a6bf191b9be4d89a4e4cc7e8 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 22 Apr 2024 17:09:59 +0200 Subject: [PATCH] Add start() function to correctly setup ALS Signed-off-by: Matteo Collina --- README.md | 4 +++- example/app.js | 5 +++++ index.d.ts | 1 + index.js | 17 +++++++++++++---- index.test-d.ts | 5 ++++- test/basic.test.js | 34 +++++++++++++++++++++++++++++++--- 6 files changed, 57 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2a89a77..ca3cb6e 100644 --- a/README.md +++ b/README.md @@ -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') diff --git a/example/app.js b/example/app.js index 1e984da..90232ef 100644 --- a/example/app.js +++ b/example/app.js @@ -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') diff --git a/index.d.ts b/index.d.ts index 96398b6..512bf6f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,6 +11,7 @@ declare namespace fastifyasyncforge { export function request(): T; export function reply(): T; export function logger(): T; + export function start(): Promise; } declare function fastifyasyncforge(): FastifyPluginCallback; diff --git a/index.js b/index.js index 4761446..503dda0 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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 diff --git a/index.test-d.ts b/index.test-d.ts index 58dfd0a..313ff56 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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, @@ -46,3 +46,6 @@ expectType(logger().info({ msg: "oh!" })); expectType(logger().warn({ msg: "let's go!!!" })); expectError(logger()); expectError({}); + +// start +expectType>(start()); diff --git a/test/basic.test.js b/test/basic.test.js index ae14130..3a3bbae 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -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)