@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
115 lines • 3.79 kB
JavaScript
;
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 FlowBuilderCore_exports = {};
__export(FlowBuilderCore_exports, {
createFlowBuilderCore: () => createFlowBuilderCore
});
module.exports = __toCommonJS(FlowBuilderCore_exports);
var import_handles = require("./handles.js");
var import_raw = require("./raw.js");
const toRef = (bindable) => {
if ("_tag" in bindable) return (0, import_handles.handleToRef)(bindable);
return bindable;
};
const isResource = (decl) => typeof decl === "object" && decl !== null && "kind" in decl;
const createFlowBuilderCore = (chainId, options) => {
const id = options.name ?? crypto.randomUUID();
const flowInputs = [];
const nodes = [];
const nodeIds = /* @__PURE__ */ new Set();
const inputHandles = {};
for (const [name, decl] of Object.entries(options.inputs)) {
if (isResource(decl)) {
if (decl.chainId !== chainId) {
throw new Error(
`Input "${name}" has chainId ${decl.chainId} but flow targets chain ${chainId}`
);
}
flowInputs.push({ name, resource: decl });
inputHandles[name] = { _tag: "input", inputName: name, resource: decl };
} else {
flowInputs.push({ name, type: decl });
inputHandles[name] = { _tag: "input", inputName: name };
}
}
const call = (nodeId, op, args, outputs) => {
if (nodeIds.has(nodeId)) throw new Error(`Duplicate node id: "${nodeId}"`);
nodeIds.add(nodeId);
const bind = {};
for (const [key, val] of Object.entries(args.bind)) {
bind[key] = toRef(val);
}
const node = {
id: nodeId,
op,
bind,
config: args.config ?? {},
...args.guards && args.guards.length > 0 && { guards: args.guards }
};
nodes.push(node);
return new Proxy({}, {
get: (_target, prop) => {
if (typeof prop !== "string" || prop === "then") return void 0;
if (!(prop in outputs)) {
throw new Error(
`Op "${op}" has no output port "${prop}". Valid ports: ${Object.keys(
outputs
).join(", ")}`
);
}
return { _tag: "output", nodeId, portName: prop };
}
});
};
const untypedOp = (nodeId, op, args) => {
if (nodeIds.has(nodeId)) throw new Error(`Duplicate node id: "${nodeId}"`);
nodeIds.add(nodeId);
const node = {
id: nodeId,
op,
bind: args.bind,
config: args.config,
...args.guards && args.guards.length > 0 && { guards: args.guards }
};
nodes.push(node);
};
const build = () => ({
version: 1,
id,
chainId,
inputs: [...flowInputs],
nodes: [...nodes]
});
const context = {
sender: (0, import_raw.ref)("context.sender"),
executionAddress: (0, import_raw.ref)("context.executionAddress")
};
return {
context,
inputs: inputHandles,
call,
untypedOp,
build
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createFlowBuilderCore
});
//# sourceMappingURL=FlowBuilderCore.cjs.map