import { PassThrough, type Transform } from 'node:stream'; import qs from 'qs'; import { type JsonValue } from 'type-fest'; import { type WhookRequestBodySpec } from './body.js'; import { NodeEnv } from 'application-services'; export type WhookParser = ( content: string, bodySpec?: WhookRequestBodySpec, ) => JsonValue; export type WhookParsers = Record; export type WhookStringifyer = (content: string) => string; export type WhookStringifiers = Record; export type WhookEncoder = () => T; export type WhookEncoders = Record< string, WhookEncoder >; export type WhookDecoder = () => T; export type WhookDecoders = Record< string, WhookDecoder >; export const DEFAULT_DEBUG_NODE_ENVS = [NodeEnv.Test, NodeEnv.Development]; export const DEFAULT_BUFFER_LIMIT = '500kB'; export const DEFAULT_PARSERS = { 'text/plain': (content: string): JsonValue => content, 'application/json': (content: string): JsonValue => JSON.parse(content), 'application/x-www-form-urlencoded': (content: string): JsonValue => qs.parse(content) as JsonValue, } satisfies WhookParsers; export const DEFAULT_STRINGIFIERS = { 'text/plain': ensureString, 'application/json': (content: JsonValue): string => JSON.stringify(content), 'application/x-www-form-urlencoded': (content: JsonValue): string => qs.stringify(content), } satisfies WhookStringifiers; export const DEFAULT_DECODERS = { 'utf-8': () => new PassThrough(), } satisfies WhookDecoders; export const DEFAULT_ENCODERS = { 'utf-8': () => new PassThrough(), } satisfies WhookEncoders; function ensureString(maybeString: unknown): string { return 'undefined' === typeof maybeString ? '' : 'string' === typeof maybeString ? maybeString : JSON.stringify(maybeString); }