UNPKG

@xsprtd/nuxt-api

Version:

Nuxt API Authentication and Http Client

67 lines (66 loc) 1.45 kB
import { useApiFetch } from "./useApiFetch.js"; import { useErrorBag } from "./useErrorBag.js"; import { useProcessing } from "./useProcessing.js"; export const useHttp = () => { const { processing, startProcessing, stopProcessing } = useProcessing(); const errorBag = useErrorBag(); const call = async (request, method, payload, options) => { try { startProcessing(); const callOptions = payload ? ["get", "delete"].includes(method) ? { query: payload } : { body: payload } : {}; const response = await useApiFetch( request, { method, ...callOptions, ...options } ); errorBag.reset(); return response; } catch (error) { stopProcessing(); errorBag.handle(error); throw error; } }; const get = async (request, query, options) => call( request, "get", query, options ); const post = async (request, body, options) => call( request, "post", body, options ); const put = async (request, body, options) => call( request, "put", body, options ); const patch = async (request, body, options) => call( request, "patch", body, options ); const destroy = async (request, query, options) => call( request, "delete", query, options ); return { get, post, put, patch, destroy, processing, errorBag }; };