diff --git a/src/index.ts b/src/index.ts index bef267a..3a22b96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) @@ -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}'` + ) }) })