UNPKG

@orpc/vue-query

Version:

<div align="center"> <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" /> </div>

127 lines (121 loc) 4.45 kB
import { generateOperationKey } from '@orpc/tanstack-query'; export { generateOperationKey as buildKey } from '@orpc/tanstack-query'; import { isObject, isAsyncIteratorObject } from '@orpc/shared'; import { skipToken, experimental_streamedQuery } from '@tanstack/vue-query'; import { isRef, computed } from 'vue'; function createGeneralUtils(path) { return { key(options) { return generateOperationKey(path, options); } }; } function unrefDeep(value) { if (isRef(value)) { return unrefDeep(value.value); } if (Array.isArray(value)) { return value.map(unrefDeep); } if (isObject(value)) { return Object.keys(value).reduce((acc, key) => { acc[key] = unrefDeep(value[key]); return acc; }, {}); } return value; } function createProcedureUtils(client, options) { return { call: client, queryOptions(...[optionsIn = {}]) { return { queryKey: computed(() => generateOperationKey(options.path, { type: "query", input: unrefDeep(optionsIn.input) })), queryFn: ({ signal }) => { const input = unrefDeep(optionsIn.input); if (input === skipToken) { throw new Error("queryFn should not be called with skipToken used as input"); } return client(input, { signal, context: unrefDeep(optionsIn.context) }); }, enabled: computed(() => unrefDeep(optionsIn.input) === skipToken ? false : void 0), ...optionsIn }; }, experimental_streamedOptions(...[optionsIn = {}]) { return { enabled: computed(() => unrefDeep(optionsIn.input) === skipToken ? false : void 0), queryKey: computed(() => generateOperationKey(options.path, { type: "streamed", input: unrefDeep(optionsIn.input), fnOptions: optionsIn.queryFnOptions })), queryFn: experimental_streamedQuery({ queryFn: async ({ signal }) => { const input = unrefDeep(optionsIn.input); if (input === skipToken) { throw new Error("queryFn should not be called with skipToken used as input"); } const output = await client(input, { signal, context: unrefDeep(optionsIn.context) }); if (!isAsyncIteratorObject(output)) { throw new Error("streamedQuery requires an event iterator output"); } return output; }, ...optionsIn.queryFnOptions }), ...optionsIn }; }, infiniteOptions(optionsIn) { return { queryKey: computed(() => { const input = unrefDeep(optionsIn.input); return generateOperationKey(options.path, { type: "infinite", input: input === skipToken ? skipToken : unrefDeep(input(unrefDeep(optionsIn.initialPageParam))) }); }), queryFn: ({ pageParam, signal }) => { const input = unrefDeep(optionsIn.input); if (input === skipToken) { throw new Error("queryFn should not be called with skipToken used as input"); } return client(unrefDeep(input(pageParam)), { signal, context: unrefDeep(optionsIn.context) }); }, enabled: computed(() => unrefDeep(optionsIn.input) === skipToken ? false : void 0), ...optionsIn }; }, mutationOptions(...[optionsIn = {}]) { return { mutationKey: generateOperationKey(options.path, { type: "mutation" }), mutationFn: (input) => client(input, { context: unrefDeep(optionsIn.context) }), ...optionsIn }; } }; } function createRouterUtils(client, options = {}) { const path = options.path ?? []; const generalUtils = createGeneralUtils(path); const procedureUtils = createProcedureUtils(client, { path }); const recursive = new Proxy({ ...generalUtils, ...procedureUtils }, { get(target, prop) { const value = Reflect.get(target, prop); if (typeof prop !== "string") { return value; } const nextUtils = createRouterUtils(client[prop], { ...options, path: [...path, prop] }); if (typeof value !== "function") { return nextUtils; } return new Proxy(value, { get(_, prop2) { return Reflect.get(nextUtils, prop2); } }); } }); return recursive; } export { createGeneralUtils, createRouterUtils as createORPCVueQueryUtils, createProcedureUtils, createRouterUtils, unrefDeep };