/** * `buildTransforms()` — compile a pair of declarative mapping objects into typed * `(toCommon, fromCommon)` callables. * * Each direction is author-provided — this utility never inverts one into the * other, because many-to-one handlers like `switch` are not reversible. * * @module @common-grants/sdk/extensions */ import { z } from "zod"; import { type Handler, type TransformResult } from "./types"; /** * Return shape of {@link buildTransforms}. */ export interface BuiltTransforms { toCommon: (source: TSource) => TransformResult; fromCommon: (common: TCommon) => TransformResult; } /** * Compile a pair of declarative mapping objects into typed * `(toCommon, fromCommon)` callables. * * @internal Plugin authors should use `definePlugin({ schemas: { [Name]: { mappings } } })` * instead of calling this directly. `definePlugin()` invokes `buildTransforms()` * automatically from the `mappings` entry and wraps the result with schema validation. * This function remains exported for unit testing and advanced use cases. * * @example * ```ts * const { toCommon, fromCommon } = buildTransforms( * { * id: { field: "data.opportunity_uuid" }, * title: { field: "data.opportunity_title" }, * status: { * value: { * match: { * field: "data.opportunity_status", * case: { posted: "open", archived: "closed" }, * default: "custom", * }, * }, * }, * }, * { data: { opportunity_title: { field: "title" } } }, * ); * * const result = toCommon(sourceData); * if (result.errors.length === 0) use(result.result); * ``` * * @remarks * Error aggregation is asymmetric across the two failure modes: * * - Handler failures (a registered handler throws): the mapping walk * short-circuits on the first failure, so `errors` carries exactly one * `TransformError` even when several fields would have failed. * - Zod-validation failures (`commonSchema` or `sourceSchema` provided): every * `ZodIssue` is flattened into a separate `TransformError`, so `errors` carries * the full set. * * Callers writing strict-mode handling should treat any non-empty `errors` * as failure regardless of length. * * @param toCommonMapping - Declarative mapping from source system format → CommonGrants. * @param fromCommonMapping - Declarative mapping from CommonGrants → source system format. * @param handlers - Optional custom handlers registered for this call only. * Name collisions with {@link DEFAULT_HANDLERS} raise a `TypeError` at call * time rather than silently shadowing the default. * @param commonSchema - Optional Zod schema to validate `toCommon` output against. * Must be the fully extended schema (e.g. result of `withCustomFields(...)`) — * not the base schema. Passing the base schema silently weakens validation of * typed custom fields. When provided, `safeParse()` runs on the transform * result and Zod issues are flattened into `TransformResult.errors`. * `TransformError.path` for Zod-flattened issues uses dot notation including * numeric indices (e.g. `"customFields.items.0.value"`). * @param sourceSchema - Optional Zod schema to validate `fromCommon` output * against. Without this, `fromCommon` casts its result to `TSource` without * any runtime check, so `TSource` provides no real safety guarantee. When * provided, `safeParse()` runs on the transform result and Zod issues are * flattened into `TransformResult.errors` using the same format as * `commonSchema`. * * @throws TypeError when custom handler names collide with built-in defaults. * @throws Error when either mapping is structurally malformed (sibling keys * on a handler-dispatch node). */ export declare function buildTransforms(toCommonMapping: Record, fromCommonMapping: Record, handlers?: Map, commonSchema?: z.ZodType, sourceSchema?: z.ZodType): BuiltTransforms; //# sourceMappingURL=transforms.d.ts.map