feat: add onRequest logging hook to capture method and URL per request#66
Open
SdSarthak wants to merge 2 commits into
Open
feat: add onRequest logging hook to capture method and URL per request#66SdSarthak wants to merge 2 commits into
SdSarthak wants to merge 2 commits into
Conversation
- Add app.addHook('onRequest') in app.ts that logs request.method and
request.url via pino (app.log.info) for every incoming request
- Add apps/backend/src/__tests__/app.test.ts with two vitest tests:
one asserting the log entry is created for GET /health, one asserting
existing health check behavior is unchanged (200 + status ok)
Closes Dev-Card#8
There was a problem hiding this comment.
Pull request overview
Adds a Fastify onRequest hook to log each incoming request’s method and URL, and introduces a small Vitest suite to verify the hook runs and /health remains unchanged.
Changes:
- Added an
onRequesthook inbuildApp()that logs{ method, url }for every request. - Added Vitest tests to assert the log entry is emitted and the health endpoint still returns
{ status: 'ok' }. - (Unrelated) Introduced an
nfcRoutesimport inapp.ts.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| apps/backend/src/app.ts | Adds onRequest logging hook; also adds an nfcRoutes import. |
| apps/backend/src/tests/app.test.ts | Adds tests for request logging and /health response. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import { followRoutes } from './routes/follow.js'; | ||
| import { connectRoutes } from './routes/connect.js'; | ||
| import { analyticsRoutes } from './routes/analytics.js'; | ||
| import { nfcRoutes } from './routes/nfc.js'; |
|
|
||
| // ─── Request Logger ─── | ||
| app.addHook('onRequest', async (request) => { | ||
| app.log.info({ method: request.method, url: request.url }, 'incoming request'); |
Comment on lines
+7
to
+23
| const logSpy = vi.spyOn(app.log, 'info'); | ||
|
|
||
| await app.inject({ method: 'GET', url: '/health' }); | ||
|
|
||
| const calls = logSpy.mock.calls.map((c) => c[0]); | ||
| const loggedRequest = calls.find( | ||
| (c: any) => typeof c === 'object' && c?.method === 'GET' && c?.url === '/health' | ||
| ); | ||
| expect(loggedRequest).toBeDefined(); | ||
| }); | ||
|
|
||
| it('returns 200 for health check (existing behavior unchanged)', async () => { | ||
| const app = await buildApp(); | ||
| const res = await app.inject({ method: 'GET', url: '/health' }); | ||
| expect(res.statusCode).toBe(200); | ||
| const body = JSON.parse(res.body); | ||
| expect(body.status).toBe('ok'); |
Comment on lines
+6
to
+9
| const app = await buildApp(); | ||
| const logSpy = vi.spyOn(app.log, 'info'); | ||
|
|
||
| await app.inject({ method: 'GET', url: '/health' }); |
Author
|
Fixed in the follow-up commit:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
app.addHook('onRequest', ...)inapps/backend/src/app.tsthat logs each incoming request'smethodandurlvia pino (app.log.info)apps/backend/src/__tests__/app.test.tswith two vitest tests:{ method, url }is written for an injected GET /health request{ status: 'ok' }(no regression)Closes #8
Test plan
cd apps/backend && npm test— both new tests pass