import { decodeHttpRequestBody, type HttpRequestBody, type HttpRequestBodyType, } from "@automate.ax/codec/http" import type { StandardSchemaV1 } from "@standard-schema/spec" import { validateStandardSchemaSync } from "../../automation/standard-schema" export type HttpRequestPayload = { headers: Record method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" path: string query: Record rawBody: Uint8Array url: URL } export type DecodedHttpRequest = { body: TBody bodyType: HttpRequestBodyType headers: Record method: HttpRequestPayload["method"] path: string query: Record rawBody: Uint8Array url: string } /** * Decodes a stored HTTP event and optionally validates its body. * * @param event - Stored request payload. * @param bodySchema - Optional synchronous Standard Schema validator. */ export function decodeHttpRequestEvent( event: HttpRequestPayload, bodySchema: TSchema, ): DecodedHttpRequest> /** @inheritdoc */ export function decodeHttpRequestEvent( event: HttpRequestPayload, bodySchema?: undefined, ): DecodedHttpRequest /** @inheritdoc */ export function decodeHttpRequestEvent( event: HttpRequestPayload, bodySchema?: StandardSchemaV1, ): DecodedHttpRequest export function decodeHttpRequestEvent( event: HttpRequestPayload, bodySchema?: StandardSchemaV1, ): DecodedHttpRequest { const decoded = decodeHttpRequestBody( event.rawBody, event.headers["content-type"], ) return { ...event, ...decoded, body: bodySchema ? validateStandardSchemaSync( bodySchema, decoded.body, "HTTP request body", ) : decoded.body, url: event.url.href, } }