UNPKG

@lifi/composer-sdk

Version:

Public Composer SDK for building and submitting flows

140 lines 5.08 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var client_exports = {}; __export(client_exports, { createComposeClient: () => createComposeClient }); module.exports = __toCommonJS(client_exports); var import_errors = require("./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 import_errors.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 import_errors.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 import_errors.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 import_errors.ComposeError("NETWORK_ERROR", message, { cause: err }); } if (!res.ok) { const body = await res.text(); throw (0, import_errors.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 import_errors.ComposeError("NETWORK_ERROR", message, { cause: err }); } if (res.status === 206) { return await parsePartialBody(res, url); } if (!res.ok) { const body = await res.text(); throw (0, import_errors.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 import_errors.ComposeError("NETWORK_ERROR", message, { cause: err }); } if (!res.ok) { const body = await res.text(); throw (0, import_errors.errorFromHttpResponse)(res.status, body, url); } return await parseBody(res, url); }; return { getManifest, compile, getZapPacks }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createComposeClient }); //# sourceMappingURL=client.cjs.map