{"version":3,"file":"index.mjs","names":[],"sources":["../../src/http/http.ts"],"sourcesContent":["/* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD */\n/* SPDX-License-Identifier: Apache-2.0 */\n\n/**\n * HTTP service module for JS apps — wraps `brookesia_service_http` RPC.\n *\n * @example\n * import http from '@brookesia/service/http';\n * if (http.available()) {\n *   const response = http.request({ url: 'https://example.com', method: 'Get' });\n *   brookesia.print(response);\n *   http.close();\n * }\n */\n\nimport {\n  cancelRequest,\n  closeHttp,\n  getRequestState,\n  isHttpAvailable,\n  openHttp,\n  request,\n  requestAsync,\n  resetHttpStatistics,\n  type HttpClientOptions,\n  type HttpHandle,\n} from './http-client.ts';\nimport type {\n  HttpErrorCode,\n  HttpMethod,\n  HttpRequest,\n  HttpRequestState,\n  HttpRequestStateInfo,\n  HttpResponse,\n  HttpStatistics,\n  HttpTlsVerifyMode,\n} from './http-types.ts';\n\nexport type {\n  HttpErrorCode,\n  HttpMethod,\n  HttpRequest,\n  HttpRequestState,\n  HttpRequestStateInfo,\n  HttpResponse,\n  HttpStatistics,\n  HttpTlsVerifyMode,\n};\n\nexport type HttpModuleOptions = HttpClientOptions;\n\ntype State = {\n  handle: HttpHandle | null;\n  options: HttpModuleOptions;\n};\n\nconst state: State = { handle: null, options: {} };\n\nfunction handle(): HttpHandle {\n  if (state.handle !== null) {\n    return state.handle;\n  }\n  if (!isHttpAvailable(state.options)) {\n    throw new Error('HTTP service is not available');\n  }\n  state.handle = openHttp(state.options);\n  return state.handle;\n}\n\n/**\n * Inject a mock RPC host and reset the cached service handle (vitest / Node only).\n *\n * @param options - Client options, typically `{ host }` from `@brookesia/service/hosts/http`\n */\nexport function configure(options: HttpModuleOptions): void {\n  if (state.handle !== null) {\n    closeHttp(state.handle, state.options);\n  }\n  state.handle = null;\n  state.options = options;\n}\n\nconst http = {\n  /** @returns `true` when the HTTP service is registered */\n  available(): boolean {\n    return isHttpAvailable(state.options);\n  },\n\n  /** Release the HTTP service binding. */\n  close(): void {\n    if (state.handle === null) {\n      return;\n    }\n    closeHttp(state.handle, state.options);\n    state.handle = null;\n  },\n\n  /**\n   * Perform a blocking HTTP request (maps to C++ `Request`).\n   *\n   * @param req - `{ url, method, headers?, body?, timeout_ms? }`\n   * @returns Response with `status_code`, `body`, headers\n   *\n   * @example\n   * const res = http.request({ url: 'https://example.com', method: 'Get' });\n   */\n  request(req: HttpRequest): HttpResponse {\n    return request(handle(), req, state.options);\n  },\n\n  /**\n   * Start an asynchronous HTTP request (maps to C++ `RequestAsync`).\n   *\n   * @param req - Request parameters (same as {@link request})\n   * @returns Request ID for polling via {@link getRequestState}\n   */\n  requestAsync(req: HttpRequest): number {\n    return requestAsync(handle(), req, state.options);\n  },\n\n  /**\n   * Cancel an in-flight async request.\n   *\n   * @param requestId - ID from {@link requestAsync}\n   */\n  cancelRequest(requestId: number): void {\n    cancelRequest(handle(), requestId, state.options);\n  },\n\n  /**\n   * Poll async request progress / result.\n   *\n   * @param requestId - ID from {@link requestAsync}\n   * @returns `{ state, status_code, body, ... }` — `state` is `'Completed'` when done\n   */\n  getRequestState(requestId: number): HttpRequestStateInfo {\n    return getRequestState(handle(), requestId, state.options);\n  },\n\n  /** Reset HTTP client statistics counters. */\n  resetStatistics(): void {\n    resetHttpStatistics(handle(), state.options);\n  },\n};\n\nexport default http;\n"],"mappings":";;;;;;;;;;;;;AAwDA,MAAM,QAAe;CAAE,QAAQ;CAAM,SAAS,CAAC;AAAE;AAEjD,SAAS,SAAqB;CAC5B,IAAI,MAAM,WAAW,MACnB,OAAO,MAAM;CAEf,IAAI,CAAC,gBAAgB,MAAM,OAAO,GAChC,MAAM,IAAI,MAAM,+BAA+B;CAEjD,MAAM,SAAS,SAAS,MAAM,OAAO;CACrC,OAAO,MAAM;AACf;;;;;;AAOA,SAAgB,UAAU,SAAkC;CAC1D,IAAI,MAAM,WAAW,MACnB,UAAU,MAAM,QAAQ,MAAM,OAAO;CAEvC,MAAM,SAAS;CACf,MAAM,UAAU;AAClB;AAEA,MAAM,OAAO;;CAEX,YAAqB;EACnB,OAAO,gBAAgB,MAAM,OAAO;CACtC;;CAGA,QAAc;EACZ,IAAI,MAAM,WAAW,MACnB;EAEF,UAAU,MAAM,QAAQ,MAAM,OAAO;EACrC,MAAM,SAAS;CACjB;;;;;;;;;;CAWA,QAAQ,KAAgC;EACtC,OAAO,QAAQ,OAAO,GAAG,KAAK,MAAM,OAAO;CAC7C;;;;;;;CAQA,aAAa,KAA0B;EACrC,OAAO,aAAa,OAAO,GAAG,KAAK,MAAM,OAAO;CAClD;;;;;;CAOA,cAAc,WAAyB;EACrC,cAAc,OAAO,GAAG,WAAW,MAAM,OAAO;CAClD;;;;;;;CAQA,gBAAgB,WAAyC;EACvD,OAAO,gBAAgB,OAAO,GAAG,WAAW,MAAM,OAAO;CAC3D;;CAGA,kBAAwB;EACtB,oBAAoB,OAAO,GAAG,MAAM,OAAO;CAC7C;AACF"}