UNPKG

nuxt-procedures

Version:

Nuxt module to define and easily consume your backend API in a validated and type-safe way using procedures and Zod schemas.

29 lines (28 loc) 873 B
import superjson from "superjson"; import { useFetch, useRequestHeaders } from "#imports"; function deserializeSuccessResponse({ response }) { if (response && response.status >= 200 && response.status < 300) { response._data = superjson.deserialize(response._data); } } export function createCaller(url) { return { call: (input) => { return $fetch("/procedures" + url, { method: "POST", body: superjson.serialize(input), onResponse: deserializeSuccessResponse, headers: useRequestHeaders(["cookie"]) }); }, useCall: (input) => { return useFetch("/procedures" + url, { key: `${url}: ${JSON.stringify(input)}`, method: "POST", body: superjson.serialize(input), onResponse: deserializeSuccessResponse, headers: useRequestHeaders(["cookie"]) }); } }; }