@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
117 lines (116 loc) • 4.87 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
let _lifi_compose_spec = require("@lifi/compose-spec");
let zod = require("zod");
zod = require_runtime.__toESM(zod, 1);
//#region src/responseSchemas.ts
/**
* Zod-backed parsers for Compose API response bodies.
*
* Server responses are untrusted input crossing into the SDK, so the shapes the
* client reads back are validated here rather than asserted with `as` casts. The
* schemas are module-private (the package builds with `--isolatedDeclarations`,
* which forbids exporting un-annotated complex values); the public surface is
* the typed `parse*` helpers, whose explicit return types double as the contract.
*
* The simulate union is the one response the SDK fully owns and parses itself
* (it is un-enveloped, unlike `/compose`), so it gets a complete schema kept in
* lockstep with the canonical `SimulateResult` in `@lifi/compose-spec` by the
* compile-time assertion below — the same anchoring `@lifi/api-schemas` uses for
* its server-side simulate schema, so the two cannot silently drift.
*/
const balanceEntrySchema = zod.default.object({
token: zod.default.string(),
owner: zod.default.string(),
amount: zod.default.string()
});
const simulateOkSchema = zod.default.object({
status: zod.default.literal("ok"),
block: zod.default.number(),
timestamp: zod.default.number(),
balancesBefore: zod.default.array(balanceEntrySchema),
balancesAfter: zod.default.array(balanceEntrySchema),
deltas: zod.default.array(balanceEntrySchema),
gasUsed: zod.default.string()
});
const simulateRevertDecodeResultSchema = zod.default.object({
errorCandidates: zod.default.array(zod.default.object({
decodedErrorSignature: zod.default.string(),
decodedParams: zod.default.array(zod.default.string())
})).optional(),
error: zod.default.string().optional()
});
const simulateRevertSchema = zod.default.object({
status: zod.default.literal("revert"),
block: zod.default.number(),
timestamp: zod.default.number(),
revertReason: zod.default.string().optional(),
code: zod.default.number().optional(),
rawErrorBytes: zod.default.string().optional(),
decodeResult: simulateRevertDecodeResultSchema.optional()
});
const simulateSetupErrorSchema = zod.default.object({
status: zod.default.literal("error"),
message: zod.default.string()
});
const simulateResultSchema = zod.default.discriminatedUnion("status", [
simulateOkSchema,
simulateRevertSchema,
simulateSetupErrorSchema
]);
/**
* Parses a `POST /simulate` body into a {@link SimulateResult}. Returns `null`
* when the body does not match the `ok`/`revert`/`error` union, letting the
* caller raise a transport-level error.
*/
const parseSimulateResult = (body) => {
const parsed = simulateResultSchema.safeParse(body);
if (!parsed.success) return null;
return parsed.data;
};
const compilePartialEnvelopeSchema = zod.default.object({
data: zod.default.record(zod.default.string(), zod.default.unknown()),
error: zod.default.object({
kind: zod.default.string(),
message: zod.default.string()
})
});
/** Parses an HTTP 206 partial compile envelope; `null` when it does not match. */
const parseCompilePartialEnvelope = (body) => {
const parsed = compilePartialEnvelopeSchema.safeParse(body);
return parsed.success ? parsed.data : null;
};
const failedPreparedOpSchema = zod.default.object({
callId: zod.default.string(),
op: zod.default.string(),
kind: zod.default.string(),
message: zod.default.string(),
path: zod.default.string().optional(),
port: zod.default.string().optional(),
fix: zod.default.string().optional(),
shortfall: _lifi_compose_spec.ShortfallZod.optional()
});
const serverErrorBodySchema = zod.default.object({ error: zod.default.object({
kind: zod.default.string().optional(),
message: zod.default.string().optional(),
path: zod.default.string().optional(),
port: zod.default.string().optional(),
fix: zod.default.string().optional(),
shortfall: _lifi_compose_spec.ShortfallZod.optional(),
sdkVersion: zod.default.string().optional(),
serverVersion: zod.default.string().optional(),
minimumSdkVersion: zod.default.string().optional(),
details: zod.default.custom().optional(),
failedOps: zod.default.array(failedPreparedOpSchema).readonly().optional(),
succeededOps: zod.default.array(zod.default.string()).readonly().optional()
}).optional() });
/** Validates an already-parsed error envelope; `null` when it does not match. */
const parseServerErrorBody = (json) => {
const parsed = serverErrorBodySchema.safeParse(json);
return parsed.success ? parsed.data : null;
};
//#endregion
exports.parseCompilePartialEnvelope = parseCompilePartialEnvelope;
exports.parseServerErrorBody = parseServerErrorBody;
exports.parseSimulateResult = parseSimulateResult;
//# sourceMappingURL=responseSchemas.cjs.map