From 16b804ee1d892dad9a0b5bf2da28b7cc4cd4d119 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 20 Sep 2024 16:56:05 +0700 Subject: [PATCH] fix: headers cannot be set (#12) Co-authored-by: TinsFox --- src/lib/api-fetch.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib/api-fetch.ts b/src/lib/api-fetch.ts index 931a378..2bbf365 100644 --- a/src/lib/api-fetch.ts +++ b/src/lib/api-fetch.ts @@ -3,6 +3,23 @@ import { FetchError, ofetch } from "ofetch" export const apiFetch = ofetch.create({ credentials: "include", retry: false, + onRequest: async ({ options }) => { + const header = new Headers(options.headers) + + options.headers = header + + if (options.method && options.method.toLowerCase() !== "get") { + if (typeof options.body === "string") { + options.body = JSON.parse(options.body) + } + if (!options.body) { + options.body = {} + } + } + }, + onResponse() { + // TODO: response interceptor + }, onResponseError(context) { if (context.response.status === 401) { return redirectToLogin()