{"version":3,"file":"registry.cjs","names":[],"sources":["../../src/registry.ts"],"sourcesContent":["import { $ZodRegistry, type $ZodType } from 'zod/v4/core'\n\n/**\n * NOTE: This module reaches into the `_map` and `_idmap` fields of `$ZodRegistry`.\n * Zod does not expose a public way to iterate a registry or to seed a registry with\n * a fallback lookup, so we depend on these internals. All such access is kept in this\n * file so an upgrade that changes the registry representation only has to be fixed here.\n */\n\nexport type SchemaRegistryMeta = {\n  id?: string | undefined\n  [key: string]: unknown\n}\n\nexport type IORegistries = {\n  inputRegistry: $ZodRegistry<SchemaRegistryMeta>\n  outputRegistry: $ZodRegistry<SchemaRegistryMeta>\n}\n\nconst getSchemaId = (id: string, io: 'input' | 'output'): string => {\n  return io === 'input' ? `${id}Input` : id\n}\n\n// A WeakMap that falls back to another WeakMap when a key is not found, this is to ensure nested metadata is properly resolved\nclass WeakMapWithFallback extends WeakMap<$ZodType, SchemaRegistryMeta> {\n  constructor(private fallback: WeakMap<$ZodType, SchemaRegistryMeta>) {\n    super()\n  }\n\n  get(key: $ZodType): SchemaRegistryMeta | undefined {\n    return super.get(key) ?? this.fallback.get(key)\n  }\n\n  has(key: $ZodType): boolean {\n    return super.has(key) || this.fallback.has(key)\n  }\n}\n\nconst copyRegistry = (\n  baseRegistry: $ZodRegistry<SchemaRegistryMeta>,\n  idReplaceFn: (id: string) => string,\n): $ZodRegistry<SchemaRegistryMeta> => {\n  const copy = new $ZodRegistry<SchemaRegistryMeta>()\n\n  copy._map = new WeakMapWithFallback(baseRegistry._map)\n\n  baseRegistry._idmap.forEach((schema, id) => {\n    copy.add(schema, {\n      ...baseRegistry._map.get(schema),\n      id: idReplaceFn(id),\n    })\n  })\n\n  return copy\n}\n\nexport const generateIORegistries = (\n  baseRegistry: $ZodRegistry<SchemaRegistryMeta>,\n): IORegistries => {\n  const inputRegistry = copyRegistry(baseRegistry, (id) => getSchemaId(id, 'input'))\n  const outputRegistry = copyRegistry(baseRegistry, (id) => getSchemaId(id, 'output'))\n\n  // Detect colliding schemas (an input id colliding with an output id)\n  inputRegistry._idmap.forEach((_, id) => {\n    if (outputRegistry._idmap.has(id)) {\n      throw new Error(\n        `Collision detected for schema \"${id}\". An input and an output schema resolve to the same component name.`,\n      )\n    }\n  })\n\n  return { inputRegistry, outputRegistry }\n}\n\n/**\n * Returns a memoized accessor for the derived input/output registries.\n *\n * The per-route transform runs once for every documented route, but the derived\n * registries only depend on the contents of the base registry. Rebuilding them on\n * every call is wasted work, so we cache the result and only recompute when the\n * number of registered schemas changes (e.g. schemas added before a later\n * `app.swagger()` call).\n */\nexport const createIORegistriesProvider = (\n  baseRegistry: $ZodRegistry<SchemaRegistryMeta>,\n): (() => IORegistries) => {\n  let cache: { size: number; registries: IORegistries } | undefined\n\n  return () => {\n    const size = baseRegistry._idmap.size\n    if (!cache || cache.size !== size) {\n      cache = { size, registries: generateIORegistries(baseRegistry) }\n    }\n    return cache.registries\n  }\n}\n"],"mappings":";;AAmBA,IAAM,eAAe,IAAY,OAAmC;CAClE,OAAO,OAAO,UAAU,GAAG,GAAG,SAAS;AACzC;AAGA,IAAM,sBAAN,cAAkC,QAAsC;CAClD;CAApB,YAAY,UAAyD;EACnE,MAAM;EADY,KAAA,WAAA;CAEpB;CAEA,IAAI,KAA+C;EACjD,OAAO,MAAM,IAAI,GAAG,KAAK,KAAK,SAAS,IAAI,GAAG;CAChD;CAEA,IAAI,KAAwB;EAC1B,OAAO,MAAM,IAAI,GAAG,KAAK,KAAK,SAAS,IAAI,GAAG;CAChD;AACF;AAEA,IAAM,gBACJ,cACA,gBACqC;CACrC,MAAM,OAAO,IAAI,YAAA,aAAiC;CAElD,KAAK,OAAO,IAAI,oBAAoB,aAAa,IAAI;CAErD,aAAa,OAAO,SAAS,QAAQ,OAAO;EAC1C,KAAK,IAAI,QAAQ;GACf,GAAG,aAAa,KAAK,IAAI,MAAM;GAC/B,IAAI,YAAY,EAAE;EACpB,CAAC;CACH,CAAC;CAED,OAAO;AACT;AAEA,IAAa,wBACX,iBACiB;CACjB,MAAM,gBAAgB,aAAa,eAAe,OAAO,YAAY,IAAI,OAAO,CAAC;CACjF,MAAM,iBAAiB,aAAa,eAAe,OAAO,YAAY,IAAI,QAAQ,CAAC;CAGnF,cAAc,OAAO,SAAS,GAAG,OAAO;EACtC,IAAI,eAAe,OAAO,IAAI,EAAE,GAC9B,MAAM,IAAI,MACR,kCAAkC,GAAG,qEACvC;CAEJ,CAAC;CAED,OAAO;EAAE;EAAe;CAAe;AACzC;;;;;;;;;;AAWA,IAAa,8BACX,iBACyB;CACzB,IAAI;CAEJ,aAAa;EACX,MAAM,OAAO,aAAa,OAAO;EACjC,IAAI,CAAC,SAAS,MAAM,SAAS,MAC3B,QAAQ;GAAE;GAAM,YAAY,qBAAqB,YAAY;EAAE;EAEjE,OAAO,MAAM;CACf;AACF"}