{"version":3,"file":"hydration-CLZgCNpJ.mjs","names":[],"sources":["../src/adapters/next/hydration.ts"],"sourcesContent":["import type {\n\tDehydrateOptions,\n\tHydrateOptions,\n\tHydrationSchema,\n\tHydrationSnapshot,\n\tSerializable,\n} from \"./types\";\n\n/**\n * Reads serializable snapshots from hydratable entities declared in a schema.\n *\n * Use this on the server side before crossing into a Client Component. Only the\n * returned snapshot should cross the boundary, not the container or service\n * instances themselves.\n *\n * @example\n * ```ts\n * const hydration = { user: USER_STATE };\n * const snapshot = dehydrate({\n *   container: getRequestContainer(),\n *   schema: hydration,\n * });\n * ```\n */\nexport const dehydrate = <const TSchema extends HydrationSchema>(\n\toptions: DehydrateOptions<TSchema>,\n): HydrationSnapshot<TSchema> => {\n\tconst snapshot: Record<string, Serializable> = {};\n\n\tfor (const key of Object.keys(options.schema) as Array<keyof TSchema>) {\n\t\tconst token = options.schema[key];\n\t\tconst isMissingToken = token === undefined;\n\t\tconst snapshotKey = String(key);\n\n\t\tif (isMissingToken) {\n\t\t\tthrow new Error(`Missing hydration token for key \"${snapshotKey}\".`);\n\t\t}\n\n\t\tsnapshot[snapshotKey] = options.container.get(token).dehydrate();\n\t}\n\n\treturn snapshot as HydrationSnapshot<TSchema>;\n};\n\n/**\n * Restores serializable snapshots into hydratable entities declared in a schema.\n *\n * Use this with a client-safe container and the snapshot received through a\n * Client Component prop.\n *\n * @example\n * ```ts\n * hydrate({\n *   container: clientContainer,\n *   schema: hydration,\n *   snapshot,\n * });\n * ```\n */\nexport const hydrate = <const TSchema extends HydrationSchema>(\n\toptions: HydrateOptions<TSchema>,\n): void => {\n\tconst snapshotRecord = options.snapshot as Record<\n\t\tstring,\n\t\tSerializable | undefined\n\t>;\n\n\tfor (const key of Object.keys(options.schema) as Array<keyof TSchema>) {\n\t\tconst token = options.schema[key];\n\t\tconst snapshotKey = String(key);\n\t\tconst value = snapshotRecord[snapshotKey];\n\n\t\tconst isMissingToken = token === undefined;\n\t\tconst isMissingSnapshot = value === undefined;\n\n\t\tif (isMissingToken) {\n\t\t\tthrow new Error(`Missing hydration token for key \"${snapshotKey}\".`);\n\t\t}\n\n\t\tif (isMissingSnapshot) {\n\t\t\tthrow new Error(`Missing hydration snapshot for key \"${snapshotKey}\".`);\n\t\t}\n\n\t\toptions.container.get(token).hydrate(value);\n\t}\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAwBA,MAAa,aACZ,YACgC;CAChC,MAAM,WAAyC,CAAC;CAEhD,KAAK,MAAM,OAAO,OAAO,KAAK,QAAQ,MAAM,GAA2B;EACtE,MAAM,QAAQ,QAAQ,OAAO;EAC7B,MAAM,iBAAiB,UAAU;EACjC,MAAM,cAAc,OAAO,GAAG;EAE9B,IAAI,gBACH,MAAM,IAAI,MAAM,oCAAoC,YAAY,GAAG;EAGpE,SAAS,eAAe,QAAQ,UAAU,IAAI,KAAK,CAAC,CAAC,UAAU;CAChE;CAEA,OAAO;AACR;;;;;;;;;;;;;;;;AAiBA,MAAa,WACZ,YACU;CACV,MAAM,iBAAiB,QAAQ;CAK/B,KAAK,MAAM,OAAO,OAAO,KAAK,QAAQ,MAAM,GAA2B;EACtE,MAAM,QAAQ,QAAQ,OAAO;EAC7B,MAAM,cAAc,OAAO,GAAG;EAC9B,MAAM,QAAQ,eAAe;EAE7B,MAAM,iBAAiB,UAAU;EACjC,MAAM,oBAAoB,UAAU;EAEpC,IAAI,gBACH,MAAM,IAAI,MAAM,oCAAoC,YAAY,GAAG;EAGpE,IAAI,mBACH,MAAM,IAAI,MAAM,uCAAuC,YAAY,GAAG;EAGvE,QAAQ,UAAU,IAAI,KAAK,CAAC,CAAC,QAAQ,KAAK;CAC3C;AACD"}