{"version":3,"file":"request-DNOy04EM.mjs","sources":["../../../libs/frontend-modules/state/src/helpers/request.ts"],"sourcesContent":["/**\n * The shape every failed request resolves with, on top of whatever the body carried.\n *\n * `status` is the HTTP status, so a caller can tell \"the server said no\" from \"the server did not\n * answer\" — the distinction CTAP-1817 needed and could not make, because this helper used to\n * discard both the status and the body on anything other than a 200.\n */\nexport type RequestFailure = {\n  message?: string;\n  success: false;\n  status: number;\n};\n\n/**\n * Parse a JSON body, or null when there isn't one.\n *\n * An error response is not guaranteed to be JSON — a gateway 502 is usually HTML — so this never\n * throws for a body it cannot read.\n */\nasync function parseJson(\n  response: Response,\n): Promise<Record<string, unknown> | null> {\n  try {\n    return (await response.json()) as Record<string, unknown>;\n  } catch {\n    return null;\n  }\n}\n\nexport async function request<T>(url: string, options: object): Promise<T> {\n  const response = await fetch(url, options);\n  const body = await parseJson(response);\n\n  // An explicit 2xx range rather than `response.ok`: a Response is always constructed with `ok`,\n  // but the fetch doubles in this package's specs are `{ status, json }`, and reading a property\n  // they do not set would make every mocked success look like a failure.\n  if (response.status >= 200 && response.status < 300) {\n    return (body ?? {}) as T;\n  }\n\n  // The body FIRST, then the status: an error response's own `message` and structured `code` are\n  // what the caller wants, and before CTAP-1817 they were thrown away in favour of the bare status\n  // text — so a 401 carrying `ACCOUNT_LOCKED` lost the code that makes it actionable.\n  return {\n    message: response.statusText,\n    ...(body ?? {}),\n    success: false,\n    status: response.status,\n  } as T;\n}\n\nexport default request;\n"],"names":["parseJson","response","request","url","options","body"],"mappings":"AAmBA,eAAeA,EACbC,GACyC;AACzC,MAAI;AACF,WAAQ,MAAMA,EAAS,KAAA;AAAA,EACzB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,eAAsBC,EAAWC,GAAaC,GAA6B;AACzE,QAAMH,IAAW,MAAM,MAAME,GAAKC,CAAO,GACnCC,IAAO,MAAML,EAAUC,CAAQ;AAKrC,SAAIA,EAAS,UAAU,OAAOA,EAAS,SAAS,MACtCI,KAAQ,CAAA,IAMX;AAAA,IACL,SAASJ,EAAS;AAAA,IAClB,GAAII,KAAQ,CAAA;AAAA,IACZ,SAAS;AAAA,IACT,QAAQJ,EAAS;AAAA,EAAA;AAErB;"}