Skip to content

Commit

Permalink
feat: change log middleware to terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Apr 7, 2022
1 parent 310e495 commit 7b3b6ae
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "1.1.5",
"version": "1.1.6",
"description": "The Athenna Http server. Built on top of fastify",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ export interface TerminateContextContract {
data?: any
params: any
queries: any
body: any
headers: any
status: number
responseTime: number
next: NextContract
}
10 changes: 7 additions & 3 deletions src/Utils/FastifyHandler.ts → src/Handlers/FastifyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare module 'fastify' {

export class FastifyHandler {
static createOnSendHandler(handler: InterceptHandlerContract) {
return async (req, _res, payload) => {
return async (req: FastifyRequest, _res, payload) => {
const request = new Request(req)

if (!req.data) req.data = {}
Expand Down Expand Up @@ -56,7 +56,7 @@ export class FastifyHandler {
}

static createDoneHandler(handler: HandleHandlerContract) {
return (req, res, done) => {
return (req: FastifyRequest, res: FastifyReply, done: any) => {
const request = new Request(req)
const response = new Response(res)

Expand All @@ -76,7 +76,7 @@ export class FastifyHandler {
}

static createResponseHandler(handler: TerminateHandlerContract) {
return (req, res: FastifyReply, done) => {
return (req: FastifyRequest, res: FastifyReply, done) => {
const request = new Request(req)
const response = new Response(res)

Expand All @@ -90,6 +90,10 @@ export class FastifyHandler {
params: req.params,
queries: req.query,
data: req.data,
body: req.body,
headers: res.getHeaders(),
status: res.statusCode,
responseTime: res.getResponseTime(),
next: done,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import fastify, {
PrintRoutesOptions,
} from 'fastify'

import { FastifyHandler } from './Utils/FastifyHandler'
import { FastifyHandler } from './Handlers/FastifyHandler'
import { HttpMethodTypes } from './Contracts/HttpMethodTypes'
import { HandlerContract } from './Contracts/Context/HandlerContract'
import { MiddlewareTypesContract } from './Contracts/MiddlewareTypesContract'
Expand Down
4 changes: 2 additions & 2 deletions src/Kernels/HttpKernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export abstract class HttpKernel {

if (Config.get<boolean>('http.log')) {
httpServer.use(async (ctx: InterceptContextContract) => {
await new Logger().channel('requests').log(ctx)
await new Logger().channel('request').log(ctx)

return ctx.body
}, 'intercept')
}, 'terminate')
}
}

Expand Down

0 comments on commit 7b3b6ae

Please sign in to comment.