@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
209 lines (208 loc) • 8.79 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_errors = require("./errors.cjs");
const require_responseSchemas = require("./responseSchemas.cjs");
let _lifi_compose_spec = require("@lifi/compose-spec");
// src/client.ts
const SDK_VERSION = "0.18.0";
const SDK_VERSION_TRIPLE = (0, _lifi_compose_spec.parseVersion)(SDK_VERSION);
/**
* `JSON.stringify` replacer that renders `bigint` amounts as decimal strings.
*
* The compose wire carries token amounts as decimal strings, and `bigint` has
* no JSON representation, so every payload this client sends goes through it.
* Exported so the example harness prints a request exactly as it is sent.
*/
const bigintReplacer = (_key, value) => typeof value === "bigint" ? value.toString() : value;
const isNonNullObject = (v) => typeof v === "object" && v !== null;
const INTEGER_STRING = /^\d+$/;
const decodeAmount = (value, path, url) => {
if (typeof value !== "string" || !INTEGER_STRING.test(value)) throw new require_errors.ComposeError("UNKNOWN_ERROR", `Malformed compose response: ${path} is ${value === void 0 ? "absent" : "not an integer string"}`, { url });
return BigInt(value);
};
const malformed = (path, what, url) => new require_errors.ComposeError("UNKNOWN_ERROR", `Malformed compose response: ${path} ${what}`, { url });
const decodeAmountLeaf = (amount, field, path, url) => {
const leaf = amount[field];
if (leaf === void 0) return;
if (!isNonNullObject(leaf)) throw malformed(`${path}.${field}`, "is not an object", url);
leaf.value = decodeAmount(leaf.value, `${path}.${field}.value`, url);
};
const decodeOutputs = (data, url) => {
const outputs = data.outputs;
if (!isNonNullObject(outputs)) throw malformed("outputs", outputs === void 0 ? "is absent" : "is not an object", url);
for (const key of Object.keys(outputs)) {
const entry = outputs[key];
if (!isNonNullObject(entry)) throw malformed(`outputs.${key}`, "is not an object", url);
const amount = entry.amount;
if (!isNonNullObject(amount)) throw malformed(`outputs.${key}.amount`, amount === void 0 ? "is absent" : "is not an object", url);
decodeAmountLeaf(amount, "estimate", `outputs.${key}.amount`, url);
decodeAmountLeaf(amount, "minimum", `outputs.${key}.amount`, url);
}
};
const decodeEpilogue = (data, url) => {
const epilogue = data.epilogue;
if (epilogue === void 0) return;
if (!isNonNullObject(epilogue) || !Array.isArray(epilogue.forwards)) throw malformed("epilogue", "is not an object with forwards", url);
epilogue.forwards.forEach((forward, index) => {
if (!isNonNullObject(forward)) throw malformed(`epilogue.forwards[${index}]`, "is not an object", url);
forward.amount = decodeAmount(forward.amount, `epilogue.forwards[${index}].amount`, url);
});
};
const decodeWireAmounts = (data, url) => {
if (!isNonNullObject(data)) return;
decodeOutputs(data, url);
decodeEpilogue(data, url);
};
const parseBody = async (res, url) => {
const body = await res.json().catch((_) => null);
if (!isNonNullObject(body) || !("data" in body)) throw new require_errors.ComposeError("UNKNOWN_ERROR", "Unexpected response format", { url });
return body.data;
};
const parseCompileSuccessBody = async (res, url) => {
const data = await parseBody(res, url);
decodeWireAmounts(data, url);
return {
...data,
status: "success"
};
};
const parsePartialBody = async (res, url) => {
const body = await res.json().catch((_) => null);
const envelope = require_responseSchemas.parseCompilePartialEnvelope(body);
if (envelope === null) throw new require_errors.ComposeError("UNKNOWN_ERROR", "Unexpected partial response format", { url });
const data = envelope.data;
decodeWireAmounts(data, url);
return {
...data,
status: "partial",
error: envelope.error
};
};
const parseSimulateBody = async (res, url) => {
const body = await res.json().catch((_) => null);
const result = require_responseSchemas.parseSimulateResult(body);
if (result === null) throw new require_errors.ComposeError("UNKNOWN_ERROR", "Unexpected simulate response format", { url });
return result;
};
const assertServerVersion = (res, url) => {
const serverVersion = res.headers.get(_lifi_compose_spec.COMPOSE_VERSION_HEADER);
if (serverVersion === null || SDK_VERSION_TRIPLE === void 0) return;
const serverTriple = (0, _lifi_compose_spec.parseVersion)(serverVersion);
if (serverTriple === void 0) return;
const compatibility = (0, _lifi_compose_spec.checkCompatibility)(SDK_VERSION_TRIPLE, serverTriple);
if (compatibility === "compatible") return;
const versions = {
sdkVersion: SDK_VERSION,
serverVersion
};
if (compatibility === "sdk_outdated") throw new require_errors.ComposeError("VALIDATION_ERROR", `composer-sdk ${SDK_VERSION} is older than the compose contract served at ${url} (${serverVersion}); upgrade @lifi/composer-sdk and @lifi/compose-spec`, {
status: res.status,
url,
kind: "sdk_outdated",
sdkOutdated: {
...versions,
minimumSdkVersion: `${serverTriple.major}.${serverTriple.minor}.0`
}
});
throw new require_errors.ComposeError("VALIDATION_ERROR", `composer-sdk ${SDK_VERSION} targets a newer compose contract than the server at ${url} serves (${serverVersion}); pin @lifi/composer-sdk and @lifi/compose-spec to ${serverVersion} or wait for the server rollout`, {
status: res.status,
url,
kind: "server_outdated",
serverOutdated: versions
});
};
const requestInit = (baseHeaders, init) => init.method === "GET" ? {
method: "GET",
headers: { ...baseHeaders }
} : {
method: "POST",
headers: {
...baseHeaders,
"Content-Type": "application/json"
},
body: JSON.stringify(init.body, bigintReplacer)
};
const send = async ({ fetchFn, baseHeaders }, url, init) => {
try {
return await fetchFn(url, requestInit(baseHeaders, init));
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new require_errors.ComposeError("NETWORK_ERROR", message, { cause: err });
}
};
const postCompile = async (transport, url, request) => {
const res = await send(transport, url, {
method: "POST",
body: request
});
assertServerVersion(res, url);
if (res.status === 206) return await parsePartialBody(res, url);
if (!res.ok) {
const body = await res.text();
throw require_errors.errorFromHttpResponse(res.status, body, url);
}
return await parseCompileSuccessBody(res, url);
};
const getJson = async (transport, url) => {
const res = await send(transport, url, { method: "GET" });
if (!res.ok) {
const body = await res.text();
throw require_errors.errorFromHttpResponse(res.status, body, url);
}
return await parseBody(res, url);
};
/**
* Creates a low-level Compose API client.
*
* @param options - Client configuration including the API base URL.
* @returns A {@link ComposeClient} instance.
*/
const createComposeClient = (options) => {
if (!options.baseUrl || !/^https?:\/\//i.test(options.baseUrl)) throw new require_errors.ComposeError("VALIDATION_ERROR", `Invalid baseUrl: expected an HTTP(S) URL, got "${options.baseUrl}"`);
const trimmedApiKey = options.apiKey?.trim() || void 0;
if (!trimmedApiKey) throw new require_errors.ComposeError("VALIDATION_ERROR", "apiKey is required: pass a LI.FI API key to createComposeSdk().");
const fetchFn = options.fetch ?? globalThis.fetch;
const base = options.baseUrl.replace(/\/$/, "");
const transport = {
fetchFn,
baseHeaders: {
Accept: "application/json",
[_lifi_compose_spec.COMPOSER_SDK_VERSION_HEADER]: SDK_VERSION,
"x-lifi-api-key": trimmedApiKey
}
};
const getManifest = async () => getJson(transport, `${base}/compose/manifest`);
const getChains = async () => getJson(transport, `${base}/chains`);
const compile = async (request) => postCompile(transport, `${base}/compose`, request);
const route = async (request) => postCompile(transport, `${base}/compose/route`, request);
const getZapPacks = async (options) => {
const params = new URLSearchParams();
if (options?.protocols !== void 0) {
const raw = options.protocols;
const list = typeof raw === "string" ? raw : raw.join(",");
params.set("protocols", list);
}
const qs = params.toString();
return getJson(transport, `${base}/compose/zap-packs${qs ? `?${qs}` : ""}`);
};
const simulate = async (request) => {
const url = `${base}/simulate`;
const res = await send(transport, url, {
method: "POST",
body: request
});
if (res.status === 200 || res.status === 422) return await parseSimulateBody(res, url);
const body = await res.text();
throw require_errors.errorFromHttpResponse(res.status, body, url);
};
return {
getManifest,
getChains,
compile,
route,
getZapPacks,
simulate
};
};
exports.bigintReplacer = bigintReplacer;
exports.createComposeClient = createComposeClient;
//# sourceMappingURL=client.cjs.map