UNPKG

rsc-superjson

Version:

A superjson wrapper for React Server Components (RSC) and Next.js

36 lines 1.64 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx } from "react/jsx-runtime"; import hoistNonReactStatics from "hoist-non-react-statics"; import SuperJSON from "superjson"; export function deserializeProps(serializedProps) { const { _superjson } = serializedProps, props = __rest(serializedProps, ["_superjson"]); return SuperJSON.deserialize({ json: props, meta: _superjson }); } export function withSuperJsonDeserializedProps(Component) { function _withSuperJsonDeserialized(serializedProps) { try { const deserializedProps = deserializeProps(serializedProps); return _jsx(Component, Object.assign({}, deserializedProps)); } catch (e) { throw new Error(`SuperJson deserialization failed for ${Component.name}. Details: ${e instanceof Error ? e.message : e}`); } } hoistNonReactStatics(_withSuperJsonDeserialized, Component); return _withSuperJsonDeserialized; } export function serialize(props) { const { json, meta: _superjson } = SuperJSON.serialize(props); return Object.assign(Object.assign({}, json), { _superjson }); } //# sourceMappingURL=superjson-deserialize.js.map