@trpc/next
Version:
38 lines (35 loc) • 1.33 kB
JavaScript
import { createRootHooks, createReactDecoration, createReactQueryUtils } from '@trpc/react-query/shared';
import { createFlatProxy } from '@trpc/server/unstable-core-do-not-import';
import { useMemo } from 'react';
import { withTRPC } from './withTRPC.mjs';
/* istanbul ignore file -- @preserve */ // We're testing this through E2E-testing
function createTRPCNext(opts) {
const hooks = createRootHooks(opts);
// TODO: maybe set TSSRContext to `never` when using `WithTRPCNoSSROptions`
const _withTRPC = withTRPC(opts);
const proxy = createReactDecoration(hooks);
return createFlatProxy((key)=>{
if (key === 'useContext' || key === 'useUtils') {
return ()=>{
const context = hooks.useUtils();
// create a stable reference of the utils context
return useMemo(()=>{
return createReactQueryUtils(context);
}, [
context
]);
};
}
if (key === 'useQueries') {
return hooks.useQueries;
}
if (key === 'useSuspenseQueries') {
return hooks.useSuspenseQueries;
}
if (key === 'withTRPC') {
return _withTRPC;
}
return proxy[key];
});
}
export { createTRPCNext };