{"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 but kept untyped — compose-spec owns its full shape\n * (`ComposeCompilePartialData`) as a hand-authored type, so the caller narrows\n * it. `error` is fully validated.\n */\nexport interface CompilePartialEnvelope {\n  readonly data: Record<string, 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 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      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,EAC1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