@lifi/composer-sdk 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -10
- package/dist/client.cjs +1 -1
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +7 -7
- package/dist/client.d.ts +7 -7
- package/dist/client.js +1 -1
- package/dist/client.js.map +1 -1
- package/dist/errors.cjs +20 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +18 -1
- package/dist/errors.d.ts +18 -1
- package/dist/errors.js +20 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/reachability.cjs +123 -0
- package/dist/reachability.cjs.map +1 -0
- package/dist/reachability.d.cts +71 -0
- package/dist/reachability.d.ts +71 -0
- package/dist/reachability.js +99 -0
- package/dist/reachability.js.map +1 -0
- package/dist/responseSchemas.cjs +5 -1
- package/dist/responseSchemas.cjs.map +1 -1
- package/dist/responseSchemas.d.cts +4 -0
- package/dist/responseSchemas.d.ts +4 -0
- package/dist/responseSchemas.js +5 -1
- package/dist/responseSchemas.js.map +1 -1
- package/package.json +2 -2
- package/src/client.ts +7 -7
- package/src/errors.ts +22 -1
- package/src/index.ts +7 -0
- package/src/reachability.ts +203 -0
- package/src/responseSchemas.ts +11 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const DEFAULT_MAX_HOPS = 2;
|
|
2
|
+
const tokenKey = (token) => `${token.chainId}:${token.address.toLowerCase()}`;
|
|
3
|
+
const isSync = (edge) => edge.availability === void 0;
|
|
4
|
+
const isProxyHeld = (edge) => isSync(edge) && edge.recipient !== "required";
|
|
5
|
+
const visitEdge = ({
|
|
6
|
+
edge,
|
|
7
|
+
from,
|
|
8
|
+
depth,
|
|
9
|
+
sourceKey,
|
|
10
|
+
isTerminal,
|
|
11
|
+
found,
|
|
12
|
+
expanded
|
|
13
|
+
}) => {
|
|
14
|
+
const key = tokenKey(edge.out);
|
|
15
|
+
if (key === sourceKey) return void 0;
|
|
16
|
+
if (isTerminal(edge) && !found.has(key)) {
|
|
17
|
+
found.set(key, { in: from, out: edge.out, hops: depth });
|
|
18
|
+
}
|
|
19
|
+
if (!isProxyHeld(edge) || expanded.has(key)) return void 0;
|
|
20
|
+
expanded.add(key);
|
|
21
|
+
return edge.out;
|
|
22
|
+
};
|
|
23
|
+
const closureFrom = ({
|
|
24
|
+
byIn,
|
|
25
|
+
from,
|
|
26
|
+
maxHops,
|
|
27
|
+
isTerminal
|
|
28
|
+
}) => {
|
|
29
|
+
const sourceKey = tokenKey(from);
|
|
30
|
+
const found = /* @__PURE__ */ new Map();
|
|
31
|
+
const expanded = /* @__PURE__ */ new Set([sourceKey]);
|
|
32
|
+
let frontier = [from];
|
|
33
|
+
for (let depth = 1; depth <= maxHops && frontier.length > 0; depth++) {
|
|
34
|
+
const next = [];
|
|
35
|
+
for (const node of frontier) {
|
|
36
|
+
for (const edge of byIn.get(tokenKey(node)) ?? []) {
|
|
37
|
+
const out = visitEdge({
|
|
38
|
+
edge,
|
|
39
|
+
from,
|
|
40
|
+
depth,
|
|
41
|
+
sourceKey,
|
|
42
|
+
isTerminal,
|
|
43
|
+
found,
|
|
44
|
+
expanded
|
|
45
|
+
});
|
|
46
|
+
if (out) next.push(out);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
frontier = next;
|
|
50
|
+
}
|
|
51
|
+
return found;
|
|
52
|
+
};
|
|
53
|
+
const indexByIn = (packs) => {
|
|
54
|
+
const byIn = /* @__PURE__ */ new Map();
|
|
55
|
+
for (const pack of packs) {
|
|
56
|
+
for (const edge of pack.edges) {
|
|
57
|
+
const key = tokenKey(edge.in);
|
|
58
|
+
const list = byIn.get(key);
|
|
59
|
+
if (list) list.push(edge);
|
|
60
|
+
else byIn.set(key, [edge]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return byIn;
|
|
64
|
+
};
|
|
65
|
+
const buildReachability = (packs, options = {}) => {
|
|
66
|
+
const maxHops = options.maxHops ?? DEFAULT_MAX_HOPS;
|
|
67
|
+
const allowAsync = options.allowAsyncRoutes ?? false;
|
|
68
|
+
const isTerminal = (edge) => isSync(edge) || allowAsync && edge.availability === "future";
|
|
69
|
+
const byIn = indexByIn(packs);
|
|
70
|
+
const memo = /* @__PURE__ */ new Map();
|
|
71
|
+
const closure = (from) => {
|
|
72
|
+
const key = tokenKey(from);
|
|
73
|
+
const cached = memo.get(key);
|
|
74
|
+
if (cached) return cached;
|
|
75
|
+
const computed = closureFrom({ byIn, from, maxHops, isTerminal });
|
|
76
|
+
memo.set(key, computed);
|
|
77
|
+
return computed;
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
maxHops,
|
|
81
|
+
canRoute: (from, to) => closure(from).has(tokenKey(to)),
|
|
82
|
+
routableFrom: (from) => [...closure(from).values()],
|
|
83
|
+
pairs: () => {
|
|
84
|
+
const sources = /* @__PURE__ */ new Map();
|
|
85
|
+
for (const pack of packs) {
|
|
86
|
+
for (const edge of pack.edges) sources.set(tokenKey(edge.in), edge.in);
|
|
87
|
+
}
|
|
88
|
+
const all = [];
|
|
89
|
+
for (const source of sources.values()) {
|
|
90
|
+
all.push(...closure(source).values());
|
|
91
|
+
}
|
|
92
|
+
return all;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
export {
|
|
97
|
+
buildReachability
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=reachability.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/reachability.ts"],"sourcesContent":["/**\n * Client-side reachability over the zap-pack catalog.\n *\n * `GET /compose/zap-packs` lists single routing edges. `POST /compose/route`\n * chains up to the server's hop budget of them, so the set of routable pairs\n * is the closure of the catalog, not the catalog itself. This module computes\n * that closure locally with the planner's admissibility rule, so an integrator\n * can offer every pair `route` connects without a request per pair.\n *\n * The rule, mirrored from the server's route search:\n *\n * - every edge except the last must leave its output in the proxy for the\n * next edge to consume: no `availability`, no `recipient: 'required'`\n * - the last edge must be synchronous, or `availability: 'future'` when the\n * caller opts into `allowAsyncRoutes`\n * - a route never visits the same token twice\n *\n * Amount bounds (`inAmountMin` / `inAmountMax`) on an intermediate edge\n * cannot be checked without an amount, so a pair reported here is routable\n * structurally, exactly as a listed edge is. Routes that open with a\n * `lifi.swap` are not enumerable and are not reported. The server also\n * drops blacklisted tokens as intermediates, which is server-side config\n * this module cannot see, so a reported pair may still answer\n * `no_route_error`.\n */\n\nimport type { ZapPackEdge, ZapPackOverview } from './discovery.js';\n\n/** A token identified by address and chain. */\nexport interface TokenRef {\n readonly address: string;\n readonly chainId: number;\n}\n\n/** A from/to pair `POST /compose/route` can connect through the catalog. */\nexport interface RoutablePair {\n readonly in: TokenRef;\n readonly out: TokenRef;\n /** Catalog edges on the shortest admissible route; `1` is a listed edge. */\n readonly hops: number;\n}\n\nexport interface ReachabilityOptions {\n /**\n * Hop budget. The server's `no_route_error` reports its effective value in\n * `details.maxHops`; the deployment default is 2.\n */\n readonly maxHops?: number;\n /**\n * Admit a route whose last edge has `availability: 'future'`. Mirrors the\n * `allowAsyncRoutes` request option; such a pair is a `no_route_error`\n * without it.\n */\n readonly allowAsyncRoutes?: boolean;\n}\n\nexport interface Reachability {\n readonly maxHops: number;\n /** Whether `route` can connect `from` to `to` through the catalog. */\n readonly canRoute: (from: TokenRef, to: TokenRef) => boolean;\n /** Every pair starting at `from`, shortest routes first. */\n readonly routableFrom: (from: TokenRef) => readonly RoutablePair[];\n /** Every routable pair in the catalog. */\n readonly pairs: () => readonly RoutablePair[];\n}\n\nconst DEFAULT_MAX_HOPS = 2;\n\nconst tokenKey = (token: TokenRef): string =>\n `${token.chainId}:${token.address.toLowerCase()}`;\n\nconst isSync = (edge: ZapPackEdge): boolean => edge.availability === undefined;\n\n// The output stays in the proxy, so a following edge can consume it.\nconst isProxyHeld = (edge: ZapPackEdge): boolean =>\n isSync(edge) && edge.recipient !== 'required';\n\n// One edge out of a frontier token. Records the pair it completes, and returns\n// its output when a further hop may consume it (proxy-held) and no shorter\n// path already expanded that token.\nconst visitEdge = ({\n edge,\n from,\n depth,\n sourceKey,\n isTerminal,\n found,\n expanded,\n}: {\n readonly edge: ZapPackEdge;\n readonly from: TokenRef;\n readonly depth: number;\n readonly sourceKey: string;\n readonly isTerminal: (edge: ZapPackEdge) => boolean;\n readonly found: Map<string, RoutablePair>;\n readonly expanded: Set<string>;\n}): TokenRef | undefined => {\n const key = tokenKey(edge.out);\n if (key === sourceKey) return undefined;\n if (isTerminal(edge) && !found.has(key)) {\n found.set(key, { in: from, out: edge.out, hops: depth });\n }\n if (!isProxyHeld(edge) || expanded.has(key)) return undefined;\n expanded.add(key);\n return edge.out;\n};\n\n// Every pair reachable from one token within `maxHops`, breadth-first so the\n// recorded hop count is the shortest one.\nconst closureFrom = ({\n byIn,\n from,\n maxHops,\n isTerminal,\n}: {\n readonly byIn: ReadonlyMap<string, readonly ZapPackEdge[]>;\n readonly from: TokenRef;\n readonly maxHops: number;\n readonly isTerminal: (edge: ZapPackEdge) => boolean;\n}): Map<string, RoutablePair> => {\n const sourceKey = tokenKey(from);\n const found = new Map<string, RoutablePair>();\n const expanded = new Set<string>([sourceKey]);\n let frontier: readonly TokenRef[] = [from];\n for (let depth = 1; depth <= maxHops && frontier.length > 0; depth++) {\n const next: TokenRef[] = [];\n for (const node of frontier) {\n for (const edge of byIn.get(tokenKey(node)) ?? []) {\n const out = visitEdge({\n edge,\n from,\n depth,\n sourceKey,\n isTerminal,\n found,\n expanded,\n });\n if (out) next.push(out);\n }\n }\n frontier = next;\n }\n return found;\n};\n\nconst indexByIn = (\n packs: readonly ZapPackOverview[],\n): Map<string, ZapPackEdge[]> => {\n const byIn = new Map<string, ZapPackEdge[]>();\n for (const pack of packs) {\n for (const edge of pack.edges) {\n const key = tokenKey(edge.in);\n const list = byIn.get(key);\n if (list) list.push(edge);\n else byIn.set(key, [edge]);\n }\n }\n return byIn;\n};\n\n/**\n * Builds a reachability index over the packs returned by\n * {@link ComposeClient.getZapPacks}. Per-source closures are computed on\n * first use and memoised, so `canRoute` on a handful of pairs stays cheap\n * while `pairs()` enumerates everything.\n */\nexport const buildReachability = (\n packs: readonly ZapPackOverview[],\n options: ReachabilityOptions = {},\n): Reachability => {\n const maxHops = options.maxHops ?? DEFAULT_MAX_HOPS;\n const allowAsync = options.allowAsyncRoutes ?? false;\n const isTerminal = (edge: ZapPackEdge): boolean =>\n isSync(edge) || (allowAsync && edge.availability === 'future');\n\n const byIn = indexByIn(packs);\n const memo = new Map<string, Map<string, RoutablePair>>();\n const closure = (from: TokenRef): Map<string, RoutablePair> => {\n const key = tokenKey(from);\n const cached = memo.get(key);\n if (cached) return cached;\n const computed = closureFrom({ byIn, from, maxHops, isTerminal });\n memo.set(key, computed);\n return computed;\n };\n\n return {\n maxHops,\n canRoute: (from, to) => closure(from).has(tokenKey(to)),\n routableFrom: (from) => [...closure(from).values()],\n pairs: () => {\n const sources = new Map<string, TokenRef>();\n for (const pack of packs) {\n for (const edge of pack.edges) sources.set(tokenKey(edge.in), edge.in);\n }\n const all: RoutablePair[] = [];\n for (const source of sources.values()) {\n all.push(...closure(source).values());\n }\n return all;\n },\n };\n};\n"],"mappings":"AAkEA,MAAM,mBAAmB;AAEzB,MAAM,WAAW,CAAC,UAChB,GAAG,MAAM,OAAO,IAAI,MAAM,QAAQ,YAAY,CAAC;AAEjD,MAAM,SAAS,CAAC,SAA+B,KAAK,iBAAiB;AAGrE,MAAM,cAAc,CAAC,SACnB,OAAO,IAAI,KAAK,KAAK,cAAc;AAKrC,MAAM,YAAY,CAAC;AAAA,EACjB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAQ4B;AAC1B,QAAM,MAAM,SAAS,KAAK,GAA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A,SAAO;AACT;AAQO,MAAM,oBAAoB,CAC/B,OACA,UAA+B,CAAC,MACf;AACjB,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,aAAa,QAAQ,oBAAoB;AAC/C,QAAM,aAAa,CAAC,SAClB,OAAO,IAAI,KAAM,cAAc,KAAK,iBAAiB;AAEvD,QAAM,OAAO,UAAU,KAAK;AAC5B,QAAM,OAAO,oBAAI,IAAuC;AACxD,QAAM,UAAU,CAAC,SAA8C;AAC7D,UAAM,MAAM,SAAS,IAAI;AACzB,UAAM,SAAS,KAAK,IAAI,GAAG;AAC3B,QAAI,OAAQ,QAAO;AACnB,UAAM,WAAW,YAAY,EAAE,MAAM,MAAM,SAAS,WAAW,CAAC;AAChE,SAAK,IAAI,KAAK,QAAQ;AACtB,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL;AAAA,IACA,UAAU,CAAC,MAAM,OAAO,QAAQ,IAAI,EAAE,IAAI,SAAS,EAAE,CAAC;AAAA,IACtD,cAAc,CAAC,SAAS,CAAC,GAAG,QAAQ,IAAI,EAAE,OAAO,CAAC;AAAA,IAClD,OAAO,MAAM;AACX,YAAM,UAAU,oBAAI,IAAsB;AAC1C,iBAAW,QAAQ,OAAO;AACxB,mBAAW,QAAQ,KAAK,MAAO,SAAQ,IAAI,SAAS,KAAK,EAAE,GAAG,KAAK,EAAE;AAAA,MACvE;AACA,YAAM,MAAsB,CAAC;AAC7B,iBAAW,UAAU,QAAQ,OAAO,GAAG;AACrC,YAAI,KAAK,GAAG,QAAQ,MAAM,EAAE,OAAO,CAAC;AAAA,MACtC;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
|
package/dist/responseSchemas.cjs
CHANGED
|
@@ -94,13 +94,17 @@ const failedPreparedOpSchema = import_zod.default.object({
|
|
|
94
94
|
op: import_zod.default.string(),
|
|
95
95
|
kind: import_zod.default.string(),
|
|
96
96
|
message: import_zod.default.string(),
|
|
97
|
-
path: import_zod.default.string().optional()
|
|
97
|
+
path: import_zod.default.string().optional(),
|
|
98
|
+
port: import_zod.default.string().optional(),
|
|
99
|
+
fix: import_zod.default.string().optional()
|
|
98
100
|
});
|
|
99
101
|
const serverErrorBodySchema = import_zod.default.object({
|
|
100
102
|
error: import_zod.default.object({
|
|
101
103
|
kind: import_zod.default.string().optional(),
|
|
102
104
|
message: import_zod.default.string().optional(),
|
|
103
105
|
path: import_zod.default.string().optional(),
|
|
106
|
+
port: import_zod.default.string().optional(),
|
|
107
|
+
fix: import_zod.default.string().optional(),
|
|
104
108
|
sdkVersion: import_zod.default.string().optional(),
|
|
105
109
|
serverVersion: import_zod.default.string().optional(),
|
|
106
110
|
minimumSdkVersion: import_zod.default.string().optional(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/responseSchemas.ts"],"sourcesContent":["import type { SimulateResult, SimulationRevert } from '@lifi/compose-spec';\nimport z from 'zod';\n\n/**\n * Zod-backed parsers for Compose API response bodies.\n *\n * Server responses are untrusted input crossing into the SDK, so the shapes the\n * client reads back are validated here rather than asserted with `as` casts. The\n * schemas are module-private (the package builds with `--isolatedDeclarations`,\n * which forbids exporting un-annotated complex values); the public surface is\n * the typed `parse*` helpers, whose explicit return types double as the contract.\n *\n * The simulate union is the one response the SDK fully owns and parses itself\n * (it is un-enveloped, unlike `/compose`), so it gets a complete schema kept in\n * lockstep with the canonical `SimulateResult` in `@lifi/compose-spec` by the\n * compile-time assertion below — the same anchoring `@lifi/api-schemas` uses for\n * its server-side simulate schema, so the two cannot silently drift.\n */\n\nconst balanceEntrySchema = z.object({\n token: z.string(),\n owner: z.string(),\n amount: z.string(),\n});\n\nconst simulateOkSchema = z.object({\n status: z.literal('ok'),\n block: z.number(),\n timestamp: z.number(),\n balancesBefore: z.array(balanceEntrySchema),\n balancesAfter: z.array(balanceEntrySchema),\n deltas: z.array(balanceEntrySchema),\n gasUsed: z.string(),\n});\n\nconst simulateRevertDecodeResultSchema = z.object({\n errorCandidates: z\n .array(\n z.object({\n decodedErrorSignature: z.string(),\n decodedParams: z.array(z.string()),\n }),\n )\n .optional(),\n error: z.string().optional(),\n});\n\nconst simulateRevertSchema = z.object({\n status: z.literal('revert'),\n block: z.number(),\n timestamp: z.number(),\n revertReason: z.string().optional(),\n code: z.number().optional(),\n rawErrorBytes: z.string().optional(),\n decodeResult: simulateRevertDecodeResultSchema.optional(),\n});\n\nconst simulateSetupErrorSchema = z.object({\n status: z.literal('error'),\n message: z.string(),\n});\n\nconst simulateResultSchema = z.discriminatedUnion('status', [\n simulateOkSchema,\n simulateRevertSchema,\n simulateSetupErrorSchema,\n]);\n\n// The type the schema actually parses out. Kept private (referencing it from an\n// exported declaration would break `--isolatedDeclarations`) and pinned to the\n// canonical `SimulateResult` inside `parseSimulateResult` below.\ntype SimulateResultWire = z.infer<typeof simulateResultSchema>;\n\n/**\n * Parses a `POST /simulate` body into a {@link SimulateResult}. Returns `null`\n * when the body does not match the `ok`/`revert`/`error` union, letting the\n * caller raise a transport-level error.\n */\nexport const parseSimulateResult = (body: unknown): SimulateResult | null => {\n const parsed = simulateResultSchema.safeParse(body);\n if (!parsed.success) return null;\n // Pin the schema to the canonical contract in both directions, so it cannot\n // silently drift (mirroring the conformance assertion `@lifi/api-schemas` runs\n // on its server-side simulate schema). Assigning the inferred wire value to a\n // `SimulateResult` checks wire→spec; `satisfies SimulateResultWire` checks\n // spec→wire. A missing or mistyped field on either side fails to compile.\n const result: SimulateResult = parsed.data;\n return result satisfies SimulateResultWire;\n};\n\n/**\n * The validated members of an HTTP 206 partial compile envelope. `data` is\n * verified to be an object at runtime but left `unknown` here — compose-spec\n * owns its full shape (`ComposeCompilePartialData`) as a hand-authored type, so\n * the caller narrows it in one step. `error` is fully validated.\n */\nexport interface CompilePartialEnvelope {\n readonly data: unknown;\n readonly error: { readonly kind: string; readonly message: string };\n}\n\nconst compilePartialEnvelopeSchema = z.object({\n data: z.record(z.string(), z.unknown()),\n error: z.object({ kind: z.string(), message: z.string() }),\n});\n\n/** Parses an HTTP 206 partial compile envelope; `null` when it does not match. */\nexport const parseCompilePartialEnvelope = (\n body: unknown,\n): CompilePartialEnvelope | null => {\n const parsed = compilePartialEnvelopeSchema.safeParse(body);\n return parsed.success ? parsed.data : null;\n};\n\n/**\n * A single prepared-op failure inside a `preparation_error` envelope. `kind` is\n * kept as `string` here — the narrowing to `GenericComposeErrorKind` happens in\n * `errorFromHttpResponse`, where the value is lifted onto `ComposeError`.\n */\nexport interface ServerFailedPreparedOp {\n readonly callId: string;\n readonly op: string;\n readonly kind: string;\n readonly message: string;\n readonly path?: string;\n}\n\n/** Error envelope returned on non-2xx responses. */\nexport interface ServerErrorBody {\n readonly error?: {\n readonly kind?: string;\n readonly message?: string;\n readonly path?: string;\n readonly sdkVersion?: string;\n readonly serverVersion?: string;\n readonly minimumSdkVersion?: string;\n readonly details?: SimulationRevert;\n readonly failedOps?: readonly ServerFailedPreparedOp[];\n readonly succeededOps?: readonly string[];\n };\n}\n\nconst failedPreparedOpSchema = z.object({\n callId: z.string(),\n op: z.string(),\n kind: z.string(),\n message: z.string(),\n path: z.string().optional(),\n});\n\n// `details` is passed through (`z.custom`) rather than parsed: the SDK does not\n// own the `SimulationRevert` schema and forwards the field verbatim onto\n// `ComposeError`.\n//\n// `failedOps`/`succeededOps` are optional here because every error kind shares\n// this envelope; they are only populated for `preparation_error` (422). Zod\n// strips undeclared keys, so omitting them would silently drop them.\nconst serverErrorBodySchema = z.object({\n error: z\n .object({\n kind: z.string().optional(),\n message: z.string().optional(),\n path: z.string().optional(),\n sdkVersion: z.string().optional(),\n serverVersion: z.string().optional(),\n minimumSdkVersion: z.string().optional(),\n details: z.custom<SimulationRevert>().optional(),\n failedOps: z.array(failedPreparedOpSchema).readonly().optional(),\n succeededOps: z.array(z.string()).readonly().optional(),\n })\n .optional(),\n});\n\n/** Validates an already-parsed error envelope; `null` when it does not match. */\nexport const parseServerErrorBody = (json: unknown): ServerErrorBody | null => {\n const parsed = serverErrorBodySchema.safeParse(json);\n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E,OAAO,EAAE,SAAS;AAAA,EACnC,cAAc,iCAAiC,SAAS;AAC1D,CAAC;AAED,MAAM,2BAA2B,WAAAA,QAAE,OAAO;AAAA,EACxC,QAAQ,WAAAA,QAAE,QAAQ,OAAO;AAAA,EACzB,SAAS,WAAAA,QAAE,OAAO;AACpB,CAAC;AAED,MAAM,uBAAuB,WAAAA,QAAE,mBAAmB,UAAU;AAAA,EAC1D;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAYM,MAAM,sBAAsB,CAAC,SAAyC;AAC3E,QAAM,SAAS,qBAAqB,UAAU,IAAI;AAClD,MAAI,CAAC,OAAO,QAAS,QAAO;AAM5B,QAAM,SAAyB,OAAO;AACtC,SAAO;AACT;AAaA,MAAM,+BAA+B,WAAAA,QAAE,OAAO;AAAA,EAC5C,MAAM,WAAAA,QAAE,OAAO,WAAAA,QAAE,OAAO,GAAG,WAAAA,QAAE,QAAQ,CAAC;AAAA,EACtC,OAAO,WAAAA,QAAE,OAAO,EAAE,MAAM,WAAAA,QAAE,OAAO,GAAG,SAAS,WAAAA,QAAE,OAAO,EAAE,CAAC;AAC3D,CAAC;AAGM,MAAM,8BAA8B,CACzC,SACkC;AAClC,QAAM,SAAS,6BAA6B,UAAU,IAAI;AAC1D,SAAO,OAAO,UAAU,OAAO,OAAO;AACxC;
|
|
1
|
+
{"version":3,"sources":["../src/responseSchemas.ts"],"sourcesContent":["import type { SimulateResult, SimulationRevert } from '@lifi/compose-spec';\nimport z from 'zod';\n\n/**\n * Zod-backed parsers for Compose API response bodies.\n *\n * Server responses are untrusted input crossing into the SDK, so the shapes the\n * client reads back are validated here rather than asserted with `as` casts. The\n * schemas are module-private (the package builds with `--isolatedDeclarations`,\n * which forbids exporting un-annotated complex values); the public surface is\n * the typed `parse*` helpers, whose explicit return types double as the contract.\n *\n * The simulate union is the one response the SDK fully owns and parses itself\n * (it is un-enveloped, unlike `/compose`), so it gets a complete schema kept in\n * lockstep with the canonical `SimulateResult` in `@lifi/compose-spec` by the\n * compile-time assertion below — the same anchoring `@lifi/api-schemas` uses for\n * its server-side simulate schema, so the two cannot silently drift.\n */\n\nconst balanceEntrySchema = z.object({\n token: z.string(),\n owner: z.string(),\n amount: z.string(),\n});\n\nconst simulateOkSchema = z.object({\n status: z.literal('ok'),\n block: z.number(),\n timestamp: z.number(),\n balancesBefore: z.array(balanceEntrySchema),\n balancesAfter: z.array(balanceEntrySchema),\n deltas: z.array(balanceEntrySchema),\n gasUsed: z.string(),\n});\n\nconst simulateRevertDecodeResultSchema = z.object({\n errorCandidates: z\n .array(\n z.object({\n decodedErrorSignature: z.string(),\n decodedParams: z.array(z.string()),\n }),\n )\n .optional(),\n error: z.string().optional(),\n});\n\nconst simulateRevertSchema = z.object({\n status: z.literal('revert'),\n block: z.number(),\n timestamp: z.number(),\n revertReason: z.string().optional(),\n code: z.number().optional(),\n rawErrorBytes: z.string().optional(),\n decodeResult: simulateRevertDecodeResultSchema.optional(),\n});\n\nconst simulateSetupErrorSchema = z.object({\n status: z.literal('error'),\n message: z.string(),\n});\n\nconst simulateResultSchema = z.discriminatedUnion('status', [\n simulateOkSchema,\n simulateRevertSchema,\n simulateSetupErrorSchema,\n]);\n\n// The type the schema actually parses out. Kept private (referencing it from an\n// exported declaration would break `--isolatedDeclarations`) and pinned to the\n// canonical `SimulateResult` inside `parseSimulateResult` below.\ntype SimulateResultWire = z.infer<typeof simulateResultSchema>;\n\n/**\n * Parses a `POST /simulate` body into a {@link SimulateResult}. Returns `null`\n * when the body does not match the `ok`/`revert`/`error` union, letting the\n * caller raise a transport-level error.\n */\nexport const parseSimulateResult = (body: unknown): SimulateResult | null => {\n const parsed = simulateResultSchema.safeParse(body);\n if (!parsed.success) return null;\n // Pin the schema to the canonical contract in both directions, so it cannot\n // silently drift (mirroring the conformance assertion `@lifi/api-schemas` runs\n // on its server-side simulate schema). Assigning the inferred wire value to a\n // `SimulateResult` checks wire→spec; `satisfies SimulateResultWire` checks\n // spec→wire. A missing or mistyped field on either side fails to compile.\n const result: SimulateResult = parsed.data;\n return result satisfies SimulateResultWire;\n};\n\n/**\n * The validated members of an HTTP 206 partial compile envelope. `data` is\n * verified to be an object at runtime but left `unknown` here — compose-spec\n * owns its full shape (`ComposeCompilePartialData`) as a hand-authored type, so\n * the caller narrows it in one step. `error` is fully validated.\n */\nexport interface CompilePartialEnvelope {\n readonly data: unknown;\n readonly error: { readonly kind: string; readonly message: string };\n}\n\nconst compilePartialEnvelopeSchema = z.object({\n data: z.record(z.string(), z.unknown()),\n error: z.object({ kind: z.string(), message: z.string() }),\n});\n\n/** Parses an HTTP 206 partial compile envelope; `null` when it does not match. */\nexport const parseCompilePartialEnvelope = (\n body: unknown,\n): CompilePartialEnvelope | null => {\n const parsed = compilePartialEnvelopeSchema.safeParse(body);\n return parsed.success ? parsed.data : null;\n};\n\n/**\n * A single prepared-op failure inside a `preparation_error` envelope. `kind` is\n * kept as `string` here — the narrowing to `GenericComposeErrorKind` happens in\n * `errorFromHttpResponse`, where the value is lifted onto `ComposeError`.\n */\nexport interface ServerFailedPreparedOp {\n readonly callId: string;\n readonly op: string;\n readonly kind: string;\n readonly message: string;\n readonly path?: string;\n readonly port?: string;\n readonly fix?: string;\n}\n\n/** Error envelope returned on non-2xx responses. */\nexport interface ServerErrorBody {\n readonly error?: {\n readonly kind?: string;\n readonly message?: string;\n readonly path?: string;\n readonly port?: string;\n readonly fix?: string;\n readonly sdkVersion?: string;\n readonly serverVersion?: string;\n readonly minimumSdkVersion?: string;\n readonly details?: SimulationRevert;\n readonly failedOps?: readonly ServerFailedPreparedOp[];\n readonly succeededOps?: readonly string[];\n };\n}\n\nconst failedPreparedOpSchema = z.object({\n callId: z.string(),\n op: z.string(),\n kind: z.string(),\n message: z.string(),\n path: z.string().optional(),\n port: z.string().optional(),\n fix: z.string().optional(),\n});\n\n// `details` is passed through (`z.custom`) rather than parsed: the SDK does not\n// own the `SimulationRevert` schema and forwards the field verbatim onto\n// `ComposeError`.\n//\n// `failedOps`/`succeededOps` are optional here because every error kind shares\n// this envelope; they are only populated for `preparation_error` (422). Zod\n// strips undeclared keys, so omitting them would silently drop them — which is\n// also why `port` and `fix`, set on every author-caused `validation_error`,\n// have to be declared here to reach the caller.\nconst serverErrorBodySchema = z.object({\n error: z\n .object({\n kind: z.string().optional(),\n message: z.string().optional(),\n path: z.string().optional(),\n port: z.string().optional(),\n fix: z.string().optional(),\n sdkVersion: z.string().optional(),\n serverVersion: z.string().optional(),\n minimumSdkVersion: z.string().optional(),\n details: z.custom<SimulationRevert>().optional(),\n failedOps: z.array(failedPreparedOpSchema).readonly().optional(),\n succeededOps: z.array(z.string()).readonly().optional(),\n })\n .optional(),\n});\n\n/** Validates an already-parsed error envelope; `null` when it does not match. */\nexport const parseServerErrorBody = (json: unknown): ServerErrorBody | null => {\n const parsed = serverErrorBodySchema.safeParse(json);\n return parsed.success ? parsed.data : null;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,iBAAc;AAkBd,MAAM,qBAAqB,WAAAA,QAAE,OAAO;AAAA,EAClC,OAAO,WAAAA,QAAE,OAAO;AAAA,EAChB,OAAO,WAAAA,QAAE,OAAO;AAAA,EAChB,QAAQ,WAAAA,QAAE,OAAO;AACnB,CAAC;AAED,MAAM,mBAAmB,WAAAA,QAAE,OAAO;AAAA,EAChC,QAAQ,WAAAA,QAAE,QAAQ,IAAI;AAAA,EACtB,OAAO,WAAAA,QAAE,OAAO;AAAA,EAChB,WAAW,WAAAA,QAAE,OAAO;AAAA,EACpB,gBAAgB,WAAAA,QAAE,MAAM,kBAAkB;AAAA,EAC1C,eAAe,WAAAA,QAAE,MAAM,kBAAkB;AAAA,EACzC,QAAQ,WAAAA,QAAE,MAAM,kBAAkB;AAAA,EAClC,SAAS,WAAAA,QAAE,OAAO;AACpB,CAAC;AAED,MAAM,mCAAmC,WAAAA,QAAE,OAAO;AAAA,EAChD,iBAAiB,WAAAA,QACd;AAA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S,sBAAsB,UAAU,IAAI;AACnD,SAAO,OAAO,UAAU,OAAO,OAAO;AACxC;","names":["z"]}
|
|
@@ -32,6 +32,8 @@ interface ServerFailedPreparedOp {
|
|
|
32
32
|
readonly kind: string;
|
|
33
33
|
readonly message: string;
|
|
34
34
|
readonly path?: string;
|
|
35
|
+
readonly port?: string;
|
|
36
|
+
readonly fix?: string;
|
|
35
37
|
}
|
|
36
38
|
/** Error envelope returned on non-2xx responses. */
|
|
37
39
|
interface ServerErrorBody {
|
|
@@ -39,6 +41,8 @@ interface ServerErrorBody {
|
|
|
39
41
|
readonly kind?: string;
|
|
40
42
|
readonly message?: string;
|
|
41
43
|
readonly path?: string;
|
|
44
|
+
readonly port?: string;
|
|
45
|
+
readonly fix?: string;
|
|
42
46
|
readonly sdkVersion?: string;
|
|
43
47
|
readonly serverVersion?: string;
|
|
44
48
|
readonly minimumSdkVersion?: string;
|
|
@@ -32,6 +32,8 @@ interface ServerFailedPreparedOp {
|
|
|
32
32
|
readonly kind: string;
|
|
33
33
|
readonly message: string;
|
|
34
34
|
readonly path?: string;
|
|
35
|
+
readonly port?: string;
|
|
36
|
+
readonly fix?: string;
|
|
35
37
|
}
|
|
36
38
|
/** Error envelope returned on non-2xx responses. */
|
|
37
39
|
interface ServerErrorBody {
|
|
@@ -39,6 +41,8 @@ interface ServerErrorBody {
|
|
|
39
41
|
readonly kind?: string;
|
|
40
42
|
readonly message?: string;
|
|
41
43
|
readonly path?: string;
|
|
44
|
+
readonly port?: string;
|
|
45
|
+
readonly fix?: string;
|
|
42
46
|
readonly sdkVersion?: string;
|
|
43
47
|
readonly serverVersion?: string;
|
|
44
48
|
readonly minimumSdkVersion?: string;
|
package/dist/responseSchemas.js
CHANGED
|
@@ -59,13 +59,17 @@ const failedPreparedOpSchema = z.object({
|
|
|
59
59
|
op: z.string(),
|
|
60
60
|
kind: z.string(),
|
|
61
61
|
message: z.string(),
|
|
62
|
-
path: z.string().optional()
|
|
62
|
+
path: z.string().optional(),
|
|
63
|
+
port: z.string().optional(),
|
|
64
|
+
fix: z.string().optional()
|
|
63
65
|
});
|
|
64
66
|
const serverErrorBodySchema = z.object({
|
|
65
67
|
error: z.object({
|
|
66
68
|
kind: z.string().optional(),
|
|
67
69
|
message: z.string().optional(),
|
|
68
70
|
path: z.string().optional(),
|
|
71
|
+
port: z.string().optional(),
|
|
72
|
+
fix: z.string().optional(),
|
|
69
73
|
sdkVersion: z.string().optional(),
|
|
70
74
|
serverVersion: z.string().optional(),
|
|
71
75
|
minimumSdkVersion: z.string().optional(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/responseSchemas.ts"],"sourcesContent":["import type { SimulateResult, SimulationRevert } from '@lifi/compose-spec';\nimport z from 'zod';\n\n/**\n * Zod-backed parsers for Compose API response bodies.\n *\n * Server responses are untrusted input crossing into the SDK, so the shapes the\n * client reads back are validated here rather than asserted with `as` casts. The\n * schemas are module-private (the package builds with `--isolatedDeclarations`,\n * which forbids exporting un-annotated complex values); the public surface is\n * the typed `parse*` helpers, whose explicit return types double as the contract.\n *\n * The simulate union is the one response the SDK fully owns and parses itself\n * (it is un-enveloped, unlike `/compose`), so it gets a complete schema kept in\n * lockstep with the canonical `SimulateResult` in `@lifi/compose-spec` by the\n * compile-time assertion below — the same anchoring `@lifi/api-schemas` uses for\n * its server-side simulate schema, so the two cannot silently drift.\n */\n\nconst balanceEntrySchema = z.object({\n token: z.string(),\n owner: z.string(),\n amount: z.string(),\n});\n\nconst simulateOkSchema = z.object({\n status: z.literal('ok'),\n block: z.number(),\n timestamp: z.number(),\n balancesBefore: z.array(balanceEntrySchema),\n balancesAfter: z.array(balanceEntrySchema),\n deltas: z.array(balanceEntrySchema),\n gasUsed: z.string(),\n});\n\nconst simulateRevertDecodeResultSchema = z.object({\n errorCandidates: z\n .array(\n z.object({\n decodedErrorSignature: z.string(),\n decodedParams: z.array(z.string()),\n }),\n )\n .optional(),\n error: z.string().optional(),\n});\n\nconst simulateRevertSchema = z.object({\n status: z.literal('revert'),\n block: z.number(),\n timestamp: z.number(),\n revertReason: z.string().optional(),\n code: z.number().optional(),\n rawErrorBytes: z.string().optional(),\n decodeResult: simulateRevertDecodeResultSchema.optional(),\n});\n\nconst simulateSetupErrorSchema = z.object({\n status: z.literal('error'),\n message: z.string(),\n});\n\nconst simulateResultSchema = z.discriminatedUnion('status', [\n simulateOkSchema,\n simulateRevertSchema,\n simulateSetupErrorSchema,\n]);\n\n// The type the schema actually parses out. Kept private (referencing it from an\n// exported declaration would break `--isolatedDeclarations`) and pinned to the\n// canonical `SimulateResult` inside `parseSimulateResult` below.\ntype SimulateResultWire = z.infer<typeof simulateResultSchema>;\n\n/**\n * Parses a `POST /simulate` body into a {@link SimulateResult}. Returns `null`\n * when the body does not match the `ok`/`revert`/`error` union, letting the\n * caller raise a transport-level error.\n */\nexport const parseSimulateResult = (body: unknown): SimulateResult | null => {\n const parsed = simulateResultSchema.safeParse(body);\n if (!parsed.success) return null;\n // Pin the schema to the canonical contract in both directions, so it cannot\n // silently drift (mirroring the conformance assertion `@lifi/api-schemas` runs\n // on its server-side simulate schema). Assigning the inferred wire value to a\n // `SimulateResult` checks wire→spec; `satisfies SimulateResultWire` checks\n // spec→wire. A missing or mistyped field on either side fails to compile.\n const result: SimulateResult = parsed.data;\n return result satisfies SimulateResultWire;\n};\n\n/**\n * The validated members of an HTTP 206 partial compile envelope. `data` is\n * verified to be an object at runtime but left `unknown` here — compose-spec\n * owns its full shape (`ComposeCompilePartialData`) as a hand-authored type, so\n * the caller narrows it in one step. `error` is fully validated.\n */\nexport interface CompilePartialEnvelope {\n readonly data: unknown;\n readonly error: { readonly kind: string; readonly message: string };\n}\n\nconst compilePartialEnvelopeSchema = z.object({\n data: z.record(z.string(), z.unknown()),\n error: z.object({ kind: z.string(), message: z.string() }),\n});\n\n/** Parses an HTTP 206 partial compile envelope; `null` when it does not match. */\nexport const parseCompilePartialEnvelope = (\n body: unknown,\n): CompilePartialEnvelope | null => {\n const parsed = compilePartialEnvelopeSchema.safeParse(body);\n return parsed.success ? parsed.data : null;\n};\n\n/**\n * A single prepared-op failure inside a `preparation_error` envelope. `kind` is\n * kept as `string` here — the narrowing to `GenericComposeErrorKind` happens in\n * `errorFromHttpResponse`, where the value is lifted onto `ComposeError`.\n */\nexport interface ServerFailedPreparedOp {\n readonly callId: string;\n readonly op: string;\n readonly kind: string;\n readonly message: string;\n readonly path?: string;\n}\n\n/** Error envelope returned on non-2xx responses. */\nexport interface ServerErrorBody {\n readonly error?: {\n readonly kind?: string;\n readonly message?: string;\n readonly path?: string;\n readonly sdkVersion?: string;\n readonly serverVersion?: string;\n readonly minimumSdkVersion?: string;\n readonly details?: SimulationRevert;\n readonly failedOps?: readonly ServerFailedPreparedOp[];\n readonly succeededOps?: readonly string[];\n };\n}\n\nconst failedPreparedOpSchema = z.object({\n callId: z.string(),\n op: z.string(),\n kind: z.string(),\n message: z.string(),\n path: z.string().optional(),\n});\n\n// `details` is passed through (`z.custom`) rather than parsed: the SDK does not\n// own the `SimulationRevert` schema and forwards the field verbatim onto\n// `ComposeError`.\n//\n// `failedOps`/`succeededOps` are optional here because every error kind shares\n// this envelope; they are only populated for `preparation_error` (422). Zod\n// strips undeclared keys, so omitting them would silently drop them.\nconst serverErrorBodySchema = z.object({\n error: z\n .object({\n kind: z.string().optional(),\n message: z.string().optional(),\n path: z.string().optional(),\n sdkVersion: z.string().optional(),\n serverVersion: z.string().optional(),\n minimumSdkVersion: z.string().optional(),\n details: z.custom<SimulationRevert>().optional(),\n failedOps: z.array(failedPreparedOpSchema).readonly().optional(),\n succeededOps: z.array(z.string()).readonly().optional(),\n })\n .optional(),\n});\n\n/** Validates an already-parsed error envelope; `null` when it does not match. */\nexport const parseServerErrorBody = (json: unknown): ServerErrorBody | null => {\n const parsed = serverErrorBodySchema.safeParse(json);\n return parsed.success ? parsed.data : null;\n};\n"],"mappings":"AACA,OAAO,OAAO;AAkBd,MAAM,qBAAqB,EAAE,OAAO;AAAA,EAClC,O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AAO,QAAS,QAAO;AAM5B,QAAM,SAAyB,OAAO;AACtC,SAAO;AACT;AAaA,MAAM,+BAA+B,EAAE,OAAO;AAAA,EAC5C,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC;AAAA,EACtC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,SAAS,EAAE,OAAO,EAAE,CAAC;AAC3D,CAAC;AAGM,MAAM,8BAA8B,CACzC,SACkC;AAClC,QAAM,SAAS,6BAA6B,UAAU,IAAI;AAC1D,SAAO,OAAO,UAAU,OAAO,OAAO;AACxC;
|
|
1
|
+
{"version":3,"sources":["../src/responseSchemas.ts"],"sourcesContent":["import type { SimulateResult, SimulationRevert } from '@lifi/compose-spec';\nimport z from 'zod';\n\n/**\n * Zod-backed parsers for Compose API response bodies.\n *\n * Server responses are untrusted input crossing into the SDK, so the shapes the\n * client reads back are validated here rather than asserted with `as` casts. The\n * schemas are module-private (the package builds with `--isolatedDeclarations`,\n * which forbids exporting un-annotated complex values); the public surface is\n * the typed `parse*` helpers, whose explicit return types double as the contract.\n *\n * The simulate union is the one response the SDK fully owns and parses itself\n * (it is un-enveloped, unlike `/compose`), so it gets a complete schema kept in\n * lockstep with the canonical `SimulateResult` in `@lifi/compose-spec` by the\n * compile-time assertion below — the same anchoring `@lifi/api-schemas` uses for\n * its server-side simulate schema, so the two cannot silently drift.\n */\n\nconst balanceEntrySchema = z.object({\n token: z.string(),\n owner: z.string(),\n amount: z.string(),\n});\n\nconst simulateOkSchema = z.object({\n status: z.literal('ok'),\n block: z.number(),\n timestamp: z.number(),\n balancesBefore: z.array(balanceEntrySchema),\n balancesAfter: z.array(balanceEntrySchema),\n deltas: z.array(balanceEntrySchema),\n gasUsed: z.string(),\n});\n\nconst simulateRevertDecodeResultSchema = z.object({\n errorCandidates: z\n .array(\n z.object({\n decodedErrorSignature: z.string(),\n decodedParams: z.array(z.string()),\n }),\n )\n .optional(),\n error: z.string().optional(),\n});\n\nconst simulateRevertSchema = z.object({\n status: z.literal('revert'),\n block: z.number(),\n timestamp: z.number(),\n revertReason: z.string().optional(),\n code: z.number().optional(),\n rawErrorBytes: z.string().optional(),\n decodeResult: simulateRevertDecodeResultSchema.optional(),\n});\n\nconst simulateSetupErrorSchema = z.object({\n status: z.literal('error'),\n message: z.string(),\n});\n\nconst simulateResultSchema = z.discriminatedUnion('status', [\n simulateOkSchema,\n simulateRevertSchema,\n simulateSetupErrorSchema,\n]);\n\n// The type the schema actually parses out. Kept private (referencing it from an\n// exported declaration would break `--isolatedDeclarations`) and pinned to the\n// canonical `SimulateResult` inside `parseSimulateResult` below.\ntype SimulateResultWire = z.infer<typeof simulateResultSchema>;\n\n/**\n * Parses a `POST /simulate` body into a {@link SimulateResult}. Returns `null`\n * when the body does not match the `ok`/`revert`/`error` union, letting the\n * caller raise a transport-level error.\n */\nexport const parseSimulateResult = (body: unknown): SimulateResult | null => {\n const parsed = simulateResultSchema.safeParse(body);\n if (!parsed.success) return null;\n // Pin the schema to the canonical contract in both directions, so it cannot\n // silently drift (mirroring the conformance assertion `@lifi/api-schemas` runs\n // on its server-side simulate schema). Assigning the inferred wire value to a\n // `SimulateResult` checks wire→spec; `satisfies SimulateResultWire` checks\n // spec→wire. A missing or mistyped field on either side fails to compile.\n const result: SimulateResult = parsed.data;\n return result satisfies SimulateResultWire;\n};\n\n/**\n * The validated members of an HTTP 206 partial compile envelope. `data` is\n * verified to be an object at runtime but left `unknown` here — compose-spec\n * owns its full shape (`ComposeCompilePartialData`) as a hand-authored type, so\n * the caller narrows it in one step. `error` is fully validated.\n */\nexport interface CompilePartialEnvelope {\n readonly data: unknown;\n readonly error: { readonly kind: string; readonly message: string };\n}\n\nconst compilePartialEnvelopeSchema = z.object({\n data: z.record(z.string(), z.unknown()),\n error: z.object({ kind: z.string(), message: z.string() }),\n});\n\n/** Parses an HTTP 206 partial compile envelope; `null` when it does not match. */\nexport const parseCompilePartialEnvelope = (\n body: unknown,\n): CompilePartialEnvelope | null => {\n const parsed = compilePartialEnvelopeSchema.safeParse(body);\n return parsed.success ? parsed.data : null;\n};\n\n/**\n * A single prepared-op failure inside a `preparation_error` envelope. `kind` is\n * kept as `string` here — the narrowing to `GenericComposeErrorKind` happens in\n * `errorFromHttpResponse`, where the value is lifted onto `ComposeError`.\n */\nexport interface ServerFailedPreparedOp {\n readonly callId: string;\n readonly op: string;\n readonly kind: string;\n readonly message: string;\n readonly path?: string;\n readonly port?: string;\n readonly fix?: string;\n}\n\n/** Error envelope returned on non-2xx responses. */\nexport interface ServerErrorBody {\n readonly error?: {\n readonly kind?: string;\n readonly message?: string;\n readonly path?: string;\n readonly port?: string;\n readonly fix?: string;\n readonly sdkVersion?: string;\n readonly serverVersion?: string;\n readonly minimumSdkVersion?: string;\n readonly details?: SimulationRevert;\n readonly failedOps?: readonly ServerFailedPreparedOp[];\n readonly succeededOps?: readonly string[];\n };\n}\n\nconst failedPreparedOpSchema = z.object({\n callId: z.string(),\n op: z.string(),\n kind: z.string(),\n message: z.string(),\n path: z.string().optional(),\n port: z.string().optional(),\n fix: z.string().optional(),\n});\n\n// `details` is passed through (`z.custom`) rather than parsed: the SDK does not\n// own the `SimulationRevert` schema and forwards the field verbatim onto\n// `ComposeError`.\n//\n// `failedOps`/`succeededOps` are optional here because every error kind shares\n// this envelope; they are only populated for `preparation_error` (422). Zod\n// strips undeclared keys, so omitting them would silently drop them — which is\n// also why `port` and `fix`, set on every author-caused `validation_error`,\n// have to be declared here to reach the caller.\nconst serverErrorBodySchema = z.object({\n error: z\n .object({\n kind: z.string().optional(),\n message: z.string().optional(),\n path: z.string().optional(),\n port: z.string().optional(),\n fix: z.string().optional(),\n sdkVersion: z.string().optional(),\n serverVersion: z.string().optional(),\n minimumSdkVersion: z.string().optional(),\n details: z.custom<SimulationRevert>().optional(),\n failedOps: z.array(failedPreparedOpSchema).readonly().optional(),\n succeededOps: z.array(z.string()).readonly().optional(),\n })\n .optional(),\n});\n\n/** Validates an already-parsed error envelope; `null` when it does not match. */\nexport const parseServerErrorBody = (json: unknown): ServerErrorBody | null => {\n const parsed = serverErrorBodySchema.safeParse(json);\n return parsed.success ? parsed.data : null;\n};\n"],"mappings":"AACA,OAAO,OAAO;AAkBd,MAAM,qBAAqB,EAAE,OAAO;AAAA,EAClC,OAAO,EAAE,OAAO;AAAA,EAChB,OAAO,EAAE,OAAO;AAAA,EAChB,QAAQ,EAAE,OAAO;AACnB,CAAC;AAED,MAAM,mBAAmB,EAAE,OAAO;AAAA,EAChC,QAAQ,EAAE,QAAQ,IAAI;AAAA,EACtB,OAAO,EAAE,OAAO;AAAA,EAChB,WAAW,EAAE,OAAO;AAAA,EACpB,gBAAgB,EAAE,MAAM,kBAAkB;AAAA,EAC1C,eAAe,EAAE,MAAM,kBAAkB;AAAA,EACzC,QAAQ,EAAE,MAAM,kBAAkB;AAAA,EAClC,SAAS,EAAE,OAAO;AACpB,CAAC;AAED,MAAM,mCAAmC,EAAE,OAAO;AAAA,EAChD,iBAAiB,EACd;AAAA,IACC,EAAE,OAAO;AAAA,MACP,uBAAuB,EAAE,OAAO;AAAA,MAChC,eAAe,EAAE,MAAM,EAAE,OAAO,CAAC;AAAA,IACnC,CAAC;AAAA,EACH,EACC,SAAS;AAAA,EACZ,OAAO,EAAE,OAAO,EAAE,SAAS;AAC7B,CAAC;AAED,MAAM,uBAAuB,EAAE,OAAO;AAAA,EACpC,QAAQ,EAAE,QAAQ,QAAQ;AAAA,EAC1B,OAAO,EAAE,OAAO;AAAA,EAChB,WAAW,EAAE,OAAO;AAAA,EACpB,cAAc,EAAE,OAAO,EAAE,SAAS;AAAA,EAClC,MAAM,EAAE,OAAO,EAAE,SAAS;AAAA,EAC1B,eAAe,EAAE,OAAO,EAAE,SAAS;AAAA,EACnC,cAAc,iCAAiC,SAAS;AAC1D,CAAC;AAED,MAAM,2BAA2B,EAAE,OAAO;AAAA,EACxC,QAAQ,EAAE,QAAQ,OAAO;AAAA,EACzB,SAAS,EAAE,OAAO;AACpB,C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AAmB,EAAE,OAAO,EAAE,SAAS;AAAA,IACvC,SAAS,EAAE,OAAyB,EAAE,SAAS;AAAA,IAC/C,WAAW,EAAE,MAAM,sBAAsB,EAAE,SAAS,EAAE,SAAS;AAAA,IAC/D,cAAc,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,EAAE,SAAS;AAAA,EACxD,CAAC,EACA,SAAS;AACd,CAAC;AAGM,MAAM,uBAAuB,CAAC,SAA0C;AAC7E,QAAM,SAAS,sBAAsB,UAAU,IAAI;AACnD,SAAO,OAAO,UAAU,OAAO,OAAO;AACxC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lifi/composer-sdk",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Public Composer SDK for building and submitting flows",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"example:staged": "tsx src/examples/run.ts --staged"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@lifi/compose-spec": "0.6.
|
|
50
|
+
"@lifi/compose-spec": "0.6.2"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"abitype": "^1.2.3",
|
package/src/client.ts
CHANGED
|
@@ -115,12 +115,12 @@ export interface ComposeClient {
|
|
|
115
115
|
* `simulationPolicy: 'allow-revert'` yields `status: 'partial'` rather than
|
|
116
116
|
* throwing, exactly as it does for {@link compile}.
|
|
117
117
|
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
118
|
+
* The server plans a short chain of catalog edges for the pair (a position
|
|
119
|
+
* entry or exit, a wrap or unwrap, a mint or burn), optionally opened by a
|
|
120
|
+
* `lifi.swap`. {@link getZapPacks} lists the single edges; the pairs the
|
|
121
|
+
* planner connects are their closure up to the hop budget, which
|
|
122
|
+
* {@link buildReachability} computes locally. A pair the planner cannot
|
|
123
|
+
* connect is rejected with `kind: 'no_route_error'`.
|
|
124
124
|
*
|
|
125
125
|
* Only synchronous edges are considered unless the request sets
|
|
126
126
|
* `allowAsyncRoutes: true`, so by default every terminal output comes back
|
|
@@ -138,7 +138,7 @@ export interface ComposeClient {
|
|
|
138
138
|
* @param request - The from/to token pair, amount, signer, and route options.
|
|
139
139
|
* @returns A discriminated result: `status: 'success'` or `status: 'partial'`.
|
|
140
140
|
* @throws {@link ComposeError} on network, validation, or server errors —
|
|
141
|
-
* including `NOT_FOUND` (HTTP 404) when
|
|
141
|
+
* including `NOT_FOUND` (HTTP 404) when the planner cannot connect the pair.
|
|
142
142
|
*/
|
|
143
143
|
readonly route: (
|
|
144
144
|
request: ComposeRouteRequest,
|
package/src/errors.ts
CHANGED
|
@@ -79,8 +79,23 @@ export class ComposeError extends Error {
|
|
|
79
79
|
readonly url?: string;
|
|
80
80
|
/** Server-provided error kind for finer-grained classification. */
|
|
81
81
|
readonly kind?: ComposeErrorKind | ComposeRouteErrorKind;
|
|
82
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* Where a validation error points: the node id for a rejection of an
|
|
84
|
+
* authored node, or a JSON-pointer path for a rejection of the request shape.
|
|
85
|
+
*/
|
|
83
86
|
readonly path?: string;
|
|
87
|
+
/**
|
|
88
|
+
* The input port of {@link ComposeError.path}'s node whose binding caused the
|
|
89
|
+
* error. Set when the rejection is about what a port binds rather than about
|
|
90
|
+
* the node's config.
|
|
91
|
+
*/
|
|
92
|
+
readonly port?: string;
|
|
93
|
+
/**
|
|
94
|
+
* The one edit that clears the error, written for whoever authored the flow.
|
|
95
|
+
* Set on `continuation.settle` authoring rejections; may be absent on other
|
|
96
|
+
* `validation_error`s.
|
|
97
|
+
*/
|
|
98
|
+
readonly fix?: string;
|
|
84
99
|
/**
|
|
85
100
|
* Simulation revert diagnostics attached to `simulation_revert` errors.
|
|
86
101
|
* Contains the raw error bytes and decoded error candidates when the
|
|
@@ -112,6 +127,8 @@ export class ComposeError extends Error {
|
|
|
112
127
|
cause?: unknown;
|
|
113
128
|
kind?: ComposeErrorKind | ComposeRouteErrorKind;
|
|
114
129
|
path?: string;
|
|
130
|
+
port?: string;
|
|
131
|
+
fix?: string;
|
|
115
132
|
details?: SimulationRevert;
|
|
116
133
|
failedOps?: readonly FailedPreparedOp[];
|
|
117
134
|
succeededOps?: readonly string[];
|
|
@@ -125,6 +142,8 @@ export class ComposeError extends Error {
|
|
|
125
142
|
this.url = options?.url;
|
|
126
143
|
this.kind = options?.kind;
|
|
127
144
|
this.path = options?.path;
|
|
145
|
+
this.port = options?.port;
|
|
146
|
+
this.fix = options?.fix;
|
|
128
147
|
this.details = options?.details;
|
|
129
148
|
this.failedOps = options?.failedOps;
|
|
130
149
|
this.succeededOps = options?.succeededOps;
|
|
@@ -312,6 +331,8 @@ export const errorFromHttpResponse = (
|
|
|
312
331
|
kind: serverError?.kind as
|
|
313
332
|
ComposeErrorKind | ComposeRouteErrorKind | undefined,
|
|
314
333
|
path: serverError?.path,
|
|
334
|
+
port: serverError?.port,
|
|
335
|
+
fix: serverError?.fix,
|
|
315
336
|
details: serverError?.details,
|
|
316
337
|
// The wire schema types `kind` as a bare string; the server only ever
|
|
317
338
|
// reports generic kinds for individual ops.
|
package/src/index.ts
CHANGED
|
@@ -28,6 +28,13 @@ export type {
|
|
|
28
28
|
ZapPackEdge,
|
|
29
29
|
ZapPackOverview,
|
|
30
30
|
} from './discovery.js';
|
|
31
|
+
export type {
|
|
32
|
+
Reachability,
|
|
33
|
+
ReachabilityOptions,
|
|
34
|
+
RoutablePair,
|
|
35
|
+
TokenRef,
|
|
36
|
+
} from './reachability.js';
|
|
37
|
+
export { buildReachability } from './reachability.js';
|
|
31
38
|
export type {
|
|
32
39
|
ComposeError,
|
|
33
40
|
ComposeErrorCode,
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side reachability over the zap-pack catalog.
|
|
3
|
+
*
|
|
4
|
+
* `GET /compose/zap-packs` lists single routing edges. `POST /compose/route`
|
|
5
|
+
* chains up to the server's hop budget of them, so the set of routable pairs
|
|
6
|
+
* is the closure of the catalog, not the catalog itself. This module computes
|
|
7
|
+
* that closure locally with the planner's admissibility rule, so an integrator
|
|
8
|
+
* can offer every pair `route` connects without a request per pair.
|
|
9
|
+
*
|
|
10
|
+
* The rule, mirrored from the server's route search:
|
|
11
|
+
*
|
|
12
|
+
* - every edge except the last must leave its output in the proxy for the
|
|
13
|
+
* next edge to consume: no `availability`, no `recipient: 'required'`
|
|
14
|
+
* - the last edge must be synchronous, or `availability: 'future'` when the
|
|
15
|
+
* caller opts into `allowAsyncRoutes`
|
|
16
|
+
* - a route never visits the same token twice
|
|
17
|
+
*
|
|
18
|
+
* Amount bounds (`inAmountMin` / `inAmountMax`) on an intermediate edge
|
|
19
|
+
* cannot be checked without an amount, so a pair reported here is routable
|
|
20
|
+
* structurally, exactly as a listed edge is. Routes that open with a
|
|
21
|
+
* `lifi.swap` are not enumerable and are not reported. The server also
|
|
22
|
+
* drops blacklisted tokens as intermediates, which is server-side config
|
|
23
|
+
* this module cannot see, so a reported pair may still answer
|
|
24
|
+
* `no_route_error`.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { ZapPackEdge, ZapPackOverview } from './discovery.js';
|
|
28
|
+
|
|
29
|
+
/** A token identified by address and chain. */
|
|
30
|
+
export interface TokenRef {
|
|
31
|
+
readonly address: string;
|
|
32
|
+
readonly chainId: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A from/to pair `POST /compose/route` can connect through the catalog. */
|
|
36
|
+
export interface RoutablePair {
|
|
37
|
+
readonly in: TokenRef;
|
|
38
|
+
readonly out: TokenRef;
|
|
39
|
+
/** Catalog edges on the shortest admissible route; `1` is a listed edge. */
|
|
40
|
+
readonly hops: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ReachabilityOptions {
|
|
44
|
+
/**
|
|
45
|
+
* Hop budget. The server's `no_route_error` reports its effective value in
|
|
46
|
+
* `details.maxHops`; the deployment default is 2.
|
|
47
|
+
*/
|
|
48
|
+
readonly maxHops?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Admit a route whose last edge has `availability: 'future'`. Mirrors the
|
|
51
|
+
* `allowAsyncRoutes` request option; such a pair is a `no_route_error`
|
|
52
|
+
* without it.
|
|
53
|
+
*/
|
|
54
|
+
readonly allowAsyncRoutes?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface Reachability {
|
|
58
|
+
readonly maxHops: number;
|
|
59
|
+
/** Whether `route` can connect `from` to `to` through the catalog. */
|
|
60
|
+
readonly canRoute: (from: TokenRef, to: TokenRef) => boolean;
|
|
61
|
+
/** Every pair starting at `from`, shortest routes first. */
|
|
62
|
+
readonly routableFrom: (from: TokenRef) => readonly RoutablePair[];
|
|
63
|
+
/** Every routable pair in the catalog. */
|
|
64
|
+
readonly pairs: () => readonly RoutablePair[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const DEFAULT_MAX_HOPS = 2;
|
|
68
|
+
|
|
69
|
+
const tokenKey = (token: TokenRef): string =>
|
|
70
|
+
`${token.chainId}:${token.address.toLowerCase()}`;
|
|
71
|
+
|
|
72
|
+
const isSync = (edge: ZapPackEdge): boolean => edge.availability === undefined;
|
|
73
|
+
|
|
74
|
+
// The output stays in the proxy, so a following edge can consume it.
|
|
75
|
+
const isProxyHeld = (edge: ZapPackEdge): boolean =>
|
|
76
|
+
isSync(edge) && edge.recipient !== 'required';
|
|
77
|
+
|
|
78
|
+
// One edge out of a frontier token. Records the pair it completes, and returns
|
|
79
|
+
// its output when a further hop may consume it (proxy-held) and no shorter
|
|
80
|
+
// path already expanded that token.
|
|
81
|
+
const visitEdge = ({
|
|
82
|
+
edge,
|
|
83
|
+
from,
|
|
84
|
+
depth,
|
|
85
|
+
sourceKey,
|
|
86
|
+
isTerminal,
|
|
87
|
+
found,
|
|
88
|
+
expanded,
|
|
89
|
+
}: {
|
|
90
|
+
readonly edge: ZapPackEdge;
|
|
91
|
+
readonly from: TokenRef;
|
|
92
|
+
readonly depth: number;
|
|
93
|
+
readonly sourceKey: string;
|
|
94
|
+
readonly isTerminal: (edge: ZapPackEdge) => boolean;
|
|
95
|
+
readonly found: Map<string, RoutablePair>;
|
|
96
|
+
readonly expanded: Set<string>;
|
|
97
|
+
}): TokenRef | undefined => {
|
|
98
|
+
const key = tokenKey(edge.out);
|
|
99
|
+
if (key === sourceKey) return undefined;
|
|
100
|
+
if (isTerminal(edge) && !found.has(key)) {
|
|
101
|
+
found.set(key, { in: from, out: edge.out, hops: depth });
|
|
102
|
+
}
|
|
103
|
+
if (!isProxyHeld(edge) || expanded.has(key)) return undefined;
|
|
104
|
+
expanded.add(key);
|
|
105
|
+
return edge.out;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// Every pair reachable from one token within `maxHops`, breadth-first so the
|
|
109
|
+
// recorded hop count is the shortest one.
|
|
110
|
+
const closureFrom = ({
|
|
111
|
+
byIn,
|
|
112
|
+
from,
|
|
113
|
+
maxHops,
|
|
114
|
+
isTerminal,
|
|
115
|
+
}: {
|
|
116
|
+
readonly byIn: ReadonlyMap<string, readonly ZapPackEdge[]>;
|
|
117
|
+
readonly from: TokenRef;
|
|
118
|
+
readonly maxHops: number;
|
|
119
|
+
readonly isTerminal: (edge: ZapPackEdge) => boolean;
|
|
120
|
+
}): Map<string, RoutablePair> => {
|
|
121
|
+
const sourceKey = tokenKey(from);
|
|
122
|
+
const found = new Map<string, RoutablePair>();
|
|
123
|
+
const expanded = new Set<string>([sourceKey]);
|
|
124
|
+
let frontier: readonly TokenRef[] = [from];
|
|
125
|
+
for (let depth = 1; depth <= maxHops && frontier.length > 0; depth++) {
|
|
126
|
+
const next: TokenRef[] = [];
|
|
127
|
+
for (const node of frontier) {
|
|
128
|
+
for (const edge of byIn.get(tokenKey(node)) ?? []) {
|
|
129
|
+
const out = visitEdge({
|
|
130
|
+
edge,
|
|
131
|
+
from,
|
|
132
|
+
depth,
|
|
133
|
+
sourceKey,
|
|
134
|
+
isTerminal,
|
|
135
|
+
found,
|
|
136
|
+
expanded,
|
|
137
|
+
});
|
|
138
|
+
if (out) next.push(out);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
frontier = next;
|
|
142
|
+
}
|
|
143
|
+
return found;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const indexByIn = (
|
|
147
|
+
packs: readonly ZapPackOverview[],
|
|
148
|
+
): Map<string, ZapPackEdge[]> => {
|
|
149
|
+
const byIn = new Map<string, ZapPackEdge[]>();
|
|
150
|
+
for (const pack of packs) {
|
|
151
|
+
for (const edge of pack.edges) {
|
|
152
|
+
const key = tokenKey(edge.in);
|
|
153
|
+
const list = byIn.get(key);
|
|
154
|
+
if (list) list.push(edge);
|
|
155
|
+
else byIn.set(key, [edge]);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return byIn;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Builds a reachability index over the packs returned by
|
|
163
|
+
* {@link ComposeClient.getZapPacks}. Per-source closures are computed on
|
|
164
|
+
* first use and memoised, so `canRoute` on a handful of pairs stays cheap
|
|
165
|
+
* while `pairs()` enumerates everything.
|
|
166
|
+
*/
|
|
167
|
+
export const buildReachability = (
|
|
168
|
+
packs: readonly ZapPackOverview[],
|
|
169
|
+
options: ReachabilityOptions = {},
|
|
170
|
+
): Reachability => {
|
|
171
|
+
const maxHops = options.maxHops ?? DEFAULT_MAX_HOPS;
|
|
172
|
+
const allowAsync = options.allowAsyncRoutes ?? false;
|
|
173
|
+
const isTerminal = (edge: ZapPackEdge): boolean =>
|
|
174
|
+
isSync(edge) || (allowAsync && edge.availability === 'future');
|
|
175
|
+
|
|
176
|
+
const byIn = indexByIn(packs);
|
|
177
|
+
const memo = new Map<string, Map<string, RoutablePair>>();
|
|
178
|
+
const closure = (from: TokenRef): Map<string, RoutablePair> => {
|
|
179
|
+
const key = tokenKey(from);
|
|
180
|
+
const cached = memo.get(key);
|
|
181
|
+
if (cached) return cached;
|
|
182
|
+
const computed = closureFrom({ byIn, from, maxHops, isTerminal });
|
|
183
|
+
memo.set(key, computed);
|
|
184
|
+
return computed;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
maxHops,
|
|
189
|
+
canRoute: (from, to) => closure(from).has(tokenKey(to)),
|
|
190
|
+
routableFrom: (from) => [...closure(from).values()],
|
|
191
|
+
pairs: () => {
|
|
192
|
+
const sources = new Map<string, TokenRef>();
|
|
193
|
+
for (const pack of packs) {
|
|
194
|
+
for (const edge of pack.edges) sources.set(tokenKey(edge.in), edge.in);
|
|
195
|
+
}
|
|
196
|
+
const all: RoutablePair[] = [];
|
|
197
|
+
for (const source of sources.values()) {
|
|
198
|
+
all.push(...closure(source).values());
|
|
199
|
+
}
|
|
200
|
+
return all;
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
};
|
package/src/responseSchemas.ts
CHANGED
|
@@ -123,6 +123,8 @@ export interface ServerFailedPreparedOp {
|
|
|
123
123
|
readonly kind: string;
|
|
124
124
|
readonly message: string;
|
|
125
125
|
readonly path?: string;
|
|
126
|
+
readonly port?: string;
|
|
127
|
+
readonly fix?: string;
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
/** Error envelope returned on non-2xx responses. */
|
|
@@ -131,6 +133,8 @@ export interface ServerErrorBody {
|
|
|
131
133
|
readonly kind?: string;
|
|
132
134
|
readonly message?: string;
|
|
133
135
|
readonly path?: string;
|
|
136
|
+
readonly port?: string;
|
|
137
|
+
readonly fix?: string;
|
|
134
138
|
readonly sdkVersion?: string;
|
|
135
139
|
readonly serverVersion?: string;
|
|
136
140
|
readonly minimumSdkVersion?: string;
|
|
@@ -146,6 +150,8 @@ const failedPreparedOpSchema = z.object({
|
|
|
146
150
|
kind: z.string(),
|
|
147
151
|
message: z.string(),
|
|
148
152
|
path: z.string().optional(),
|
|
153
|
+
port: z.string().optional(),
|
|
154
|
+
fix: z.string().optional(),
|
|
149
155
|
});
|
|
150
156
|
|
|
151
157
|
// `details` is passed through (`z.custom`) rather than parsed: the SDK does not
|
|
@@ -154,13 +160,17 @@ const failedPreparedOpSchema = z.object({
|
|
|
154
160
|
//
|
|
155
161
|
// `failedOps`/`succeededOps` are optional here because every error kind shares
|
|
156
162
|
// this envelope; they are only populated for `preparation_error` (422). Zod
|
|
157
|
-
// strips undeclared keys, so omitting them would silently drop them
|
|
163
|
+
// strips undeclared keys, so omitting them would silently drop them — which is
|
|
164
|
+
// also why `port` and `fix`, set on every author-caused `validation_error`,
|
|
165
|
+
// have to be declared here to reach the caller.
|
|
158
166
|
const serverErrorBodySchema = z.object({
|
|
159
167
|
error: z
|
|
160
168
|
.object({
|
|
161
169
|
kind: z.string().optional(),
|
|
162
170
|
message: z.string().optional(),
|
|
163
171
|
path: z.string().optional(),
|
|
172
|
+
port: z.string().optional(),
|
|
173
|
+
fix: z.string().optional(),
|
|
164
174
|
sdkVersion: z.string().optional(),
|
|
165
175
|
serverVersion: z.string().optional(),
|
|
166
176
|
minimumSdkVersion: z.string().optional(),
|