122 lines
3.3 KiB
TypeScript
122 lines
3.3 KiB
TypeScript
import { useStorage } from "@vueuse/core";
|
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
// ======================
|
|
// Runtime Config (Public)
|
|
// ======================
|
|
const config = useRuntimeConfig();
|
|
|
|
const BASE_URL = (config.public.NUXT_PUBLIC_BASE_URL as string) || "";
|
|
const API_NAME = (config.public.NUXT_PUBLIC_API_NAME as string) || "";
|
|
|
|
// ======================
|
|
// Safe URL Joiner
|
|
// ======================
|
|
const joinURL = (base: string, path: string) => {
|
|
if (!base) return path;
|
|
if (!path) return base;
|
|
return `${base.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
|
|
};
|
|
|
|
const baseURL = joinURL(BASE_URL, API_NAME);
|
|
|
|
// ======================
|
|
// Reactive Token
|
|
// ======================
|
|
const token = useStorage<string>("id_token", "");
|
|
|
|
// ======================
|
|
// Fetch Instance
|
|
// ======================
|
|
const api = $fetch.create({
|
|
baseURL,
|
|
|
|
// ======================
|
|
// Request Interceptor
|
|
// ======================
|
|
onRequest({ options }) {
|
|
const auth = token.value;
|
|
if (!auth) return;
|
|
const headers = (options.headers ||= {});
|
|
if (headers instanceof Headers) {
|
|
headers.set("Authorization", `Bearer ${auth}`);
|
|
} else if (Array.isArray(headers)) {
|
|
headers.push(["Authorization", `Bearer ${auth}`]);
|
|
} else {
|
|
(headers as Record<string, string>).Authorization = `Bearer ${auth}`;
|
|
}
|
|
},
|
|
|
|
// ======================
|
|
// Response Handler
|
|
// ======================
|
|
onResponse({ response }) {
|
|
return response._data;
|
|
},
|
|
|
|
// ======================
|
|
// Error Handler
|
|
// ======================
|
|
async onResponseError({ response }) {
|
|
const status = response?.status;
|
|
|
|
if (status === 401) {
|
|
token.value = "";
|
|
if (import.meta.client) {
|
|
await nuxtApp.runWithContext(() => navigateTo("/login"));
|
|
}
|
|
}
|
|
|
|
throw {
|
|
status,
|
|
message:
|
|
response?._data?.message || response?.statusText || "خطای نامشخص در ارتباط با سرور",
|
|
data: response?._data || null,
|
|
};
|
|
},
|
|
});
|
|
|
|
// ======================
|
|
// HTTP Service (Public API)
|
|
// ======================
|
|
const http = {
|
|
getRequest: <T = any>(url: string, options: any = {}) =>
|
|
api<T>(url, { method: "GET", ...options }),
|
|
|
|
postRequest: <T = any>(url: string, body?: any, options: any = {}) =>
|
|
api<T>(url, { method: "POST", body, ...options }),
|
|
|
|
putRequest: <T = any>(url: string, body?: any, options: any = {}) =>
|
|
api<T>(url, { method: "PUT", body, ...options }),
|
|
|
|
patchRequest: <T = any>(url: string, body?: any, options: any = {}) =>
|
|
api<T>(url, { method: "PATCH", body, ...options }),
|
|
|
|
deleteRequest: <T = any>(url: string, options: any = {}) =>
|
|
api<T>(url, { method: "DELETE", ...options }),
|
|
|
|
// متدهای مدیریت توکن که در authStore استفاده میشوند
|
|
setToken: (newToken: string) => {
|
|
token.value = newToken;
|
|
},
|
|
|
|
clearToken: () => {
|
|
token.value = "";
|
|
},
|
|
|
|
getToken: () => token.value,
|
|
|
|
// دسترسی مستقیم به کلاینت خام Fetch
|
|
raw: api,
|
|
};
|
|
|
|
// ======================
|
|
// Provide to Nuxt
|
|
// ======================
|
|
return {
|
|
provide: {
|
|
http,
|
|
},
|
|
};
|
|
});
|