@trpc/next
Version:
45 lines (42 loc) • 1.56 kB
JavaScript
import { createTRPCUntypedClient, clientCallTypeToProcedureType } from '@trpc/client';
import { createRecursiveProxy } from '@trpc/server/unstable-core-do-not-import';
export { experimental_createActionHook, experimental_serverActionLink } from './create-action-hook.mjs';
// ts-prune-ignore-next
function experimental_createTRPCNextAppDirClient(opts) {
const client = createTRPCUntypedClient(opts.config());
// const useProxy = createUseProxy<TRouter>(client);
const cache = new Map();
return createRecursiveProxy(({ path, args })=>{
// const pathCopy = [key, ...path];
const pathCopy = [
...path
];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const procedureType = clientCallTypeToProcedureType(pathCopy.pop());
if (procedureType === 'query') {
const queryCacheKey = JSON.stringify([
path,
args[0]
]);
const cached = cache.get(queryCacheKey);
if (cached?.promise) {
return cached.promise;
}
}
const fullPath = pathCopy.join('.');
const promise = client[procedureType](fullPath, ...args);
if (procedureType !== 'query') {
return promise;
}
const queryCacheKey = JSON.stringify([
path,
args[0]
]);
cache.set(queryCacheKey, {
promise
});
return promise;
});
// });
}
export { experimental_createTRPCNextAppDirClient };