Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Logging improvements #1

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ import { defineHook } from '@directus/extensions-sdk'
import express, { Request } from 'express'
import { IncomingMessage, ServerResponse } from 'http'

const EXTENSION_NAME = 'directus-extension-raw-body'

export type RequestWithRawBody = IncomingMessage & Request & { rawBody: string }

export default defineHook(({ init }, { env, logger }) => {
export default defineHook(({ init }, { env, logger: rootLogger }) => {
const logger = rootLogger.child({ extension: EXTENSION_NAME })

let raw_body_endpoint_pattern: RegExp

init('middlewares.before', async function ({ app }) {
logger.info(
`Starting directus-extension-raw-body for URLs matching: ${env.RAW_BODY_ENDPOINT_PATTERN}`
)
if (!env.RAW_BODY_ENDPOINT_PATTERN) {
logger.warn(
`${EXTENSION_NAME} 'RAW_BODY_ENDPOINT_PATTERN' not configured`
)
return
}

raw_body_endpoint_pattern = new RegExp(env.RAW_BODY_ENDPOINT_PATTERN)

Expand All @@ -22,17 +29,18 @@ export default defineHook(({ init }, { env, logger }) => {
buf: Buffer,
_encoding: string
) => {
if (
raw_body_endpoint_pattern &&
req.originalUrl.match(raw_body_endpoint_pattern)
) {
if (req.originalUrl.match(raw_body_endpoint_pattern)) {
logger.debug(
`Setting up 'rawBody' in request object for ${req.originalUrl}`
`${EXTENSION_NAME} Setting up 'rawBody' in request object for ${req.originalUrl}`
)
req.rawBody = buf.toString()
}
},
})
)

logger.info(
`${EXTENSION_NAME} loaded matching '${env.RAW_BODY_ENDPOINT_PATTERN}'`
)
})
})