@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
112 lines (111 loc) • 4.03 kB
JavaScript
import { ShortfallZod } from "@lifi/compose-spec";
import z from "zod";
// 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 = z.object({
token: z.string(),
owner: z.string(),
amount: z.string()
});
const simulateOkSchema = z.object({
status: z.literal("ok"),
block: z.number().optional(),
timestamp: z.number().optional(),
balancesBefore: z.array(balanceEntrySchema),
balancesAfter: z.array(balanceEntrySchema),
deltas: z.array(balanceEntrySchema),
gasUsed: z.string()
});
const simulateRevertDecodeResultSchema = z.object({
errorCandidates: z.array(z.object({
decodedErrorSignature: z.string(),
decodedParams: z.array(z.string()),
description: z.string().optional()
})).optional(),
error: z.string().optional()
});
const simulateRevertSchema = z.object({
status: z.literal("revert"),
block: z.number().optional(),
timestamp: z.number().optional(),
revertReason: z.string().optional(),
code: z.number().optional(),
rawErrorBytes: z.string().optional(),
decodeResult: simulateRevertDecodeResultSchema.optional()
});
const simulateSetupErrorSchema = z.object({
status: z.literal("error"),
message: z.string()
});
const simulateResultSchema = z.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 = z.object({
data: z.record(z.string(), z.unknown()),
error: z.object({
kind: z.string(),
message: z.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 = z.object({
callId: z.string(),
op: z.string(),
kind: z.string(),
message: z.string(),
path: z.string().optional(),
port: z.string().optional(),
fix: z.string().optional(),
shortfall: ShortfallZod.optional()
});
const serverErrorBodySchema = z.object({ error: z.object({
kind: z.string().optional(),
message: z.string().optional(),
path: z.string().optional(),
port: z.string().optional(),
fix: z.string().optional(),
shortfall: ShortfallZod.optional(),
sdkVersion: z.string().optional(),
serverVersion: z.string().optional(),
minimumSdkVersion: z.string().optional(),
details: z.custom().optional(),
failedOps: z.array(failedPreparedOpSchema).readonly().optional(),
succeededOps: z.array(z.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;
};
export { parseCompilePartialEnvelope, parseServerErrorBody, parseSimulateResult };
//# sourceMappingURL=responseSchemas.js.map