UNPKG

@lifi/composer-sdk

Version:

Public Composer SDK for building and submitting flows

116 lines 3.93 kB
import { ComposeError, errorFromHttpResponse } from "./errors.js"; const SDK_VERSION = true ? "0.1.0" : "dev"; const bigintReplacer = (_key, value) => typeof value === "bigint" ? value.toString() : value; const isNonNullObject = (v) => typeof v === "object" && v !== null; const parseBody = async (res, url) => { const body = await res.json().catch((_) => null); if (!isNonNullObject(body) || !("data" in body)) { throw new ComposeError("UNKNOWN_ERROR", "Unexpected response format", { url }); } return body.data; }; const parseCompileSuccessBody = async (res, url) => { const data = await parseBody(res, url); return { ...data, status: "success" }; }; const parsePartialBody = async (res, url) => { const body = await res.json().catch((_) => null); if (!isNonNullObject(body) || !("data" in body) || !isNonNullObject(body.data) || !("error" in body) || !isNonNullObject(body.error)) { throw new ComposeError( "UNKNOWN_ERROR", "Unexpected partial response format", { url } ); } const data = body.data; const error = body.error; return { ...data, status: "partial", error }; }; const createComposeClient = (options) => { if (!options.baseUrl || !/^https?:\/\//i.test(options.baseUrl)) { throw new ComposeError( "VALIDATION_ERROR", `Invalid baseUrl: expected an HTTP(S) URL, got "${options.baseUrl}"` ); } const fetchFn = options.fetch ?? globalThis.fetch; const base = options.baseUrl.replace(/\/$/, ""); const trimmedApiKey = options.apiKey?.trim() || void 0; const baseHeaders = { Accept: "application/json", "x-lifi-composer-sdk": SDK_VERSION, ...trimmedApiKey ? { "x-lifi-api-key": trimmedApiKey } : {} }; const getManifest = async () => { const url = `${base}/compose/manifest`; let res; try { res = await fetchFn(url, { method: "GET", headers: { ...baseHeaders } }); } catch (err) { const message = err instanceof Error ? err.message : String(err); throw new ComposeError("NETWORK_ERROR", message, { cause: err }); } if (!res.ok) { const body = await res.text(); throw errorFromHttpResponse(res.status, body, url); } return await parseBody(res, url); }; const compile = async (request) => { const url = `${base}/compose`; let res; try { res = await fetchFn(url, { method: "POST", headers: { ...baseHeaders, "Content-Type": "application/json" }, body: JSON.stringify(request, bigintReplacer) }); } catch (err) { const message = err instanceof Error ? err.message : String(err); throw new ComposeError("NETWORK_ERROR", message, { cause: err }); } if (res.status === 206) { return await parsePartialBody(res, url); } if (!res.ok) { const body = await res.text(); throw errorFromHttpResponse(res.status, body, url); } return await parseCompileSuccessBody(res, url); }; const getZapPacks = async (options2) => { const params = new URLSearchParams(); if (options2?.protocols !== void 0) { const raw = options2.protocols; const list = typeof raw === "string" ? raw : raw.join(","); params.set("protocols", list); } const qs = params.toString(); const url = `${base}/compose/zap-packs${qs ? `?${qs}` : ""}`; let res; try { res = await fetchFn(url, { method: "GET", headers: { ...baseHeaders } }); } catch (err) { const message = err instanceof Error ? err.message : String(err); throw new ComposeError("NETWORK_ERROR", message, { cause: err }); } if (!res.ok) { const body = await res.text(); throw errorFromHttpResponse(res.status, body, url); } return await parseBody(res, url); }; return { getManifest, compile, getZapPacks }; }; export { createComposeClient }; //# sourceMappingURL=client.js.map