UNPKG

@yangcurve/actions

Version:

Server Action wrapper inspired by trpc.

113 lines (107 loc) 3.57 kB
// src/caller.ts var createCallerFactory = ({ transformer }) => { const createCaller = (actions, path = []) => new Proxy( {}, { get: (_, key) => { const action = [...path, key].reduce((acc, key2) => acc[key2], actions); if (typeof action === "function") return async (input) => { const output = await action(input); return transformer?.parse?.(output) ?? output; }; return createCaller(actions, [...path, key]); } } ); return createCaller; }; // src/client/proxy.ts import { useMutation, useQuery } from "@tanstack/react-query"; var createClientProxy = (actions, ...path) => new Proxy( {}, { get: (_, key) => key === "useQuery" ? (input, options, queryClient) => useQuery( { ...options, // @ts-expect-error ... queryFn: () => path.reduce((acc, key2) => acc[key2], actions)(input), queryKey: [...path, input] }, queryClient ) : key === "useMutation" ? (options, queryClient) => useMutation( { ...options, // @ts-expect-error ... mutationFn: (input) => path.reduce((acc, key2) => acc[key2], actions)(input), mutationKey: path }, queryClient ) : createClientProxy(actions, ...path, key) } ); // src/client/utils.ts var createClientUtils = (actions, queryClient, path = []) => new Proxy( {}, { get: (_, key) => key === "invalidate" ? (input) => queryClient.invalidateQueries({ queryKey: [...path, input].filter(Boolean) }) : key === "isMutating" ? () => queryClient.isMutating({ mutationKey: path }) : createClientUtils(actions, queryClient, [...path, key]) } ); // src/client/caller.ts import { useQueryClient } from "@tanstack/react-query"; var createClientCaller = (actions) => new Proxy( {}, { get: (_, key) => key === "useUtils" ? () => { const queryClient = useQueryClient(); return createClientUtils(actions, queryClient); } : createClientProxy(actions, key) } ); // src/procedure.ts import { z } from "zod"; var createProcedureFactory = ({ transformer }) => { const __createProcedure = ({ createContext, middlewares }) => { const getActionBuilder = (Schema) => (resolver) => async (input) => { const invokeMiddlewares = async (ctx, ...middlewares2) => middlewares2.length === 0 ? await resolver({ ctx, input: Schema.parse(input) }) : await middlewares2[0]({ ctx, // eslint-disable-next-line @typescript-eslint/no-explicit-any next: async (newCtx) => await invokeMiddlewares(newCtx ?? ctx, ...middlewares2.slice(1)) }); const output = await invokeMiddlewares(await createContext(), ...middlewares); return transformer?.stringify(output) ?? output; }; return { use: (middleware) => __createProcedure({ createContext, middlewares: [...middlewares, middleware] }), query: getActionBuilder(z.void()), mutation: getActionBuilder(z.void()), input: (Schema) => ({ query: getActionBuilder(Schema), mutation: getActionBuilder(Schema) }) }; }; const createProcedure = ({ createContext } = {}) => __createProcedure({ createContext: createContext ?? (() => new Promise(() => { })), middlewares: [] }); return createProcedure; }; // src/init.ts var initActions = ({ transformer } = {}) => ({ createProcedure: createProcedureFactory({ transformer }), createCaller: createCallerFactory({ transformer }), createClientCaller }); export { initActions };