UNPKG

@trpc/next

Version:

The tRPC Next.js library

48 lines (47 loc) 2.16 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_shared = require("../../shared-BcvoW6jT.cjs"); let _trpc_client_unstable_internals = require("@trpc/client/unstable-internals"); let _trpc_server_unstable_core_do_not_import = require("@trpc/server/unstable-core-do-not-import"); let _trpc_client = require("@trpc/client"); let next_cache = require("next/cache"); let _trpc_server_observable = require("@trpc/server/observable"); //#region src/app-dir/links/nextCache.ts function experimental_nextCacheLink(opts) { const transformer = (0, _trpc_client_unstable_internals.getTransformer)(opts.transformer); return () => ({ op }) => (0, _trpc_server_observable.observable)((observer) => { var _ref; const { path, input, type, context } = op; const cacheTag = require_shared.generateCacheTag(path, input); const requestRevalidate = typeof context["revalidate"] === "number" || context["revalidate"] === false ? context["revalidate"] : void 0; const revalidate = (_ref = requestRevalidate !== null && requestRevalidate !== void 0 ? requestRevalidate : opts.revalidate) !== null && _ref !== void 0 ? _ref : false; opts.createContext().then(async (ctx) => { const callProc = async (_cachebuster) => { const procedureResult = await (0, _trpc_server_unstable_core_do_not_import.callProcedure)({ router: opts.router, path, getRawInput: async () => input, ctx, type, signal: void 0, batchIndex: 0 }); return transformer.input.serialize(procedureResult); }; if (type === "query") return (0, next_cache.unstable_cache)(callProc, path.split("."), { revalidate, tags: [cacheTag] })(cacheTag); return callProc(cacheTag); }).catch((cause) => { observer.error(_trpc_client.TRPCClientError.from(cause)); }).then((data) => { const transformedResult = transformer.output.deserialize(data); observer.next({ result: { data: transformedResult } }); observer.complete(); }).catch((cause) => { observer.error(_trpc_client.TRPCClientError.from(cause)); }); }); } //#endregion exports.experimental_nextCacheLink = experimental_nextCacheLink;