@falcondev-oss/trpc-vue-query
Version:
A tRPC wrapper around '@tanstack/vue-query'
92 lines (90 loc) • 3.49 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
createTRPCVueQueryClient: () => createTRPCVueQueryClient
});
module.exports = __toCommonJS(src_exports);
var import_vue_query = require("@tanstack/vue-query");
var import_client = require("@trpc/client");
var import_shared = require("@trpc/server/shared");
var import_core = require("@vueuse/core");
var import_vue = require("vue");
function getQueryKey(path, input) {
return input === void 0 ? path : [...path, input];
}
function maybeToRefs(obj) {
return (0, import_vue.isReactive)(obj) ? (0, import_core.toRefs)(obj) : (0, import_core.toRefs)((0, import_core.toRef)(obj));
}
function createVueQueryProxyDecoration(name, client, queryClient) {
return (0, import_shared.createRecursiveProxy)((opts) => {
const args = opts.args;
const path = [name, ...opts.path];
const lastProperty = path.pop();
const joinedPath = path.join(".");
const [firstParam, secondParam] = args;
if (lastProperty === "_def") {
return { path };
}
if (lastProperty === "useQuery") {
const { trpc, ...queryOptions } = secondParam || {};
return (0, import_vue_query.useQuery)({
queryKey: (0, import_vue.computed)(() => getQueryKey(path, (0, import_core.toValue)(firstParam))),
queryFn: ({ queryKey, signal }) => client[joinedPath].query(queryKey.at(-1), {
signal,
...trpc
}),
...maybeToRefs(queryOptions)
});
}
if (lastProperty === "invalidate") {
return queryClient.invalidateQueries({
queryKey: getQueryKey(path, (0, import_core.toValue)(firstParam))
});
}
if (lastProperty === "setQueryData") {
return queryClient.setQueryData(getQueryKey(path, (0, import_core.toValue)(secondParam)), firstParam);
}
if (lastProperty === "key") {
return getQueryKey(path, (0, import_core.toValue)(firstParam));
}
if (lastProperty === "useMutation") {
const { trpc, ...mutationOptions } = firstParam || {};
return (0, import_vue_query.useMutation)({
mutationFn: (payload) => client[joinedPath].mutate(payload, {
...trpc
}),
...maybeToRefs(mutationOptions)
});
}
return client[joinedPath][lastProperty](...args);
});
}
function createTRPCVueQueryClient(opts) {
const client = (0, import_client.createTRPCProxyClient)(opts.trpc);
const decoratedClient = (0, import_shared.createFlatProxy)((key) => {
return createVueQueryProxyDecoration(key, client, opts.queryClient);
});
return decoratedClient;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createTRPCVueQueryClient
});
;