{"version":3,"file":"index.mjs","names":[],"sources":["../../src/http/index.ts"],"sourcesContent":["/**\n * HTTP method accepted by {@link HttpInvocationConfig}. Distinct from the core\n * `builtin_triggers` HTTP method enum, which also covers HEAD/OPTIONS.\n */\nexport type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'\n\n/**\n * Authentication configuration for HTTP-invoked functions.\n *\n * - `hmac`: HMAC signature verification using a shared secret.\n * - `bearer`: Bearer token authentication.\n * - `api_key`: API key sent via a custom header.\n */\nexport type HttpAuthConfig =\n  | { type: 'hmac'; secret_key: string }\n  | { type: 'bearer'; token_key: string }\n  | { type: 'api_key'; header: string; value_key: string }\n\n/**\n * Configuration for an HTTP-invoked function (Lambda, Cloudflare Workers, etc.).\n */\nexport type HttpInvocationConfig = {\n  /** URL to invoke. */\n  url: string\n  /** HTTP method. Defaults to `POST`. */\n  method?: HttpMethod\n  /** Timeout in milliseconds. */\n  timeout_ms?: number\n  /** Custom headers to send with the request. */\n  headers?: Record<string, string>\n  /** Authentication configuration. */\n  auth?: HttpAuthConfig\n}\n\n/**\n * Incoming buffered HTTP request received by a function handler.\n *\n * @typeParam TBody - Type of the parsed request body.\n */\nexport type HttpRequest<TBody = unknown> = {\n  /** Path parameters extracted from the matched route. */\n  path_params: Record<string, string>\n  /** Query-string parameters from the request URL. */\n  query_params: Record<string, string | string[]>\n  /** Parsed request body. */\n  body: TBody\n  /** Request headers. */\n  headers: Record<string, string | string[]>\n  /** HTTP method of the request (e.g. `GET`, `POST`). */\n  method: string\n  /** Streaming reader for the raw request body. */\n  request_body: HttpStreamReader\n}\n\n/**\n * Structured buffered HTTP response returned from function handlers.\n *\n * @typeParam TStatus - HTTP status code literal type.\n * @typeParam TBody - Type of the response body.\n *\n * @example\n * ```typescript\n * const response: HttpResponse = {\n *   status_code: 200,\n *   headers: { 'content-type': 'application/json' },\n *   body: { message: 'ok' },\n * }\n * ```\n */\nexport type HttpResponse<\n  TStatus extends number = number,\n  TBody = string | Buffer | Record<string, unknown>,\n> = {\n  /** HTTP status code. */\n  status_code: TStatus\n  /** Response headers. */\n  headers?: Record<string, string>\n  /** Response body. */\n  body?: TBody\n}\n\n/**\n * Structural shape of the reader end of a stream channel. Declared locally so\n * the helpers package does not runtime-depend on the core SDK.\n */\ntype HttpStreamReader = {\n  stream: NodeJS.ReadableStream\n  readAll: () => Promise<Buffer>\n  onMessage: (callback: (msg: string) => void) => void\n  close: () => void\n}\n\n/** Structural shape of the writer end of a stream channel. */\ntype HttpStreamWriter = {\n  sendMessage: (message: string) => unknown\n  stream: NodeJS.WritableStream\n  close: () => unknown\n}\n\n/** Structural shape of a streaming request passed to the {@link http} handler. */\ntype HttpStreamingRequest = {\n  path_params: Record<string, string>\n  query_params: Record<string, string | string[]>\n  body: unknown\n  headers: Record<string, string | string[]>\n  method: string\n  request_body: HttpStreamReader\n}\n\n/** Structural shape of the streaming response passed to the {@link http} handler. */\ntype HttpStreamingResponse = {\n  status: (statusCode: number) => void\n  headers: (headers: Record<string, string>) => void\n  stream: NodeJS.WritableStream\n  close: () => void\n}\n\n/** Structural shape of the internal request delivered by the runtime. */\ntype HttpInternalRequest = HttpStreamingRequest & { response: HttpStreamWriter }\n\n/**\n * Helper that wraps an HTTP-style handler (with separate `req`/`res` arguments)\n * into the function handler format expected by the SDK.\n *\n * @param callback - Async handler receiving a streaming request and response.\n * @returns A function handler compatible with `IIIClient.registerFunction`.\n *\n * @example\n * ```typescript\n * import { http } from '@iii-dev/helpers/http'\n *\n * worker.registerFunction(\n *   'my-api',\n *   http(async (req, res) => {\n *     res.status(200)\n *     res.headers({ 'content-type': 'application/json' })\n *     res.stream.end(JSON.stringify({ hello: 'world' }))\n *     res.close()\n *   }),\n * )\n * ```\n */\nexport const http = (\n  // biome-ignore lint/suspicious/noConfusingVoidType: void is necessary here\n  callback: (req: HttpStreamingRequest, res: HttpStreamingResponse) => Promise<void | HttpResponse>,\n) => {\n  return async (req: HttpInternalRequest) => {\n    const { response, ...request } = req\n\n    const httpResponse: HttpStreamingResponse = {\n      status: (status_code: number) =>\n        response.sendMessage(JSON.stringify({ type: 'set_status', status_code })),\n      headers: (headers: Record<string, string>) =>\n        response.sendMessage(JSON.stringify({ type: 'set_headers', headers })),\n      stream: response.stream,\n      close: () => response.close(),\n    }\n\n    return callback(request, httpResponse)\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA8IA,MAAa,QAEX,aACG;AACH,QAAO,OAAO,QAA6B;EACzC,MAAM,EAAE,UAAU,GAAG,YAAY;AAWjC,SAAO,SAAS,SAAS;GARvB,SAAS,gBACP,SAAS,YAAY,KAAK,UAAU;IAAE,MAAM;IAAc;IAAa,CAAC,CAAC;GAC3E,UAAU,YACR,SAAS,YAAY,KAAK,UAAU;IAAE,MAAM;IAAe;IAAS,CAAC,CAAC;GACxE,QAAQ,SAAS;GACjB,aAAa,SAAS,OAAO;GAGM,CAAC"}