UNPKG

vormiaqueryjs

Version:

Vormia Query Js - A npm package for query management with VormiaPHP laravel Backend application

135 lines (134 loc) 3.98 kB
import axios from "axios"; const DEFAULT_CONFIG = { VORMIA_API_URL: process.env.VORMIA_API_URL || "", VORMIA_AUTH_TOKEN_KEY: process.env.VORMIA_AUTH_TOKEN_KEY || "auth_token", VORMIA_TIMEOUT: process.env.VORMIA_TIMEOUT ? parseInt(process.env.VORMIA_TIMEOUT, 10) : 3e4, VORMIA_WITH_CREDENTIALS: process.env.VORMIA_WITH_CREDENTIALS === "true" || false }; class VormiaError extends Error { constructor(message, status, response, code) { super(message); this.name = "VormiaError"; this.status = status; this.response = response; this.code = code; } } class VormiaClient { constructor(config = {}) { const defaultConfig = { baseURL: DEFAULT_CONFIG.VORMIA_API_URL || "", authTokenKey: DEFAULT_CONFIG.VORMIA_AUTH_TOKEN_KEY, withCredentials: DEFAULT_CONFIG.VORMIA_WITH_CREDENTIALS, timeout: DEFAULT_CONFIG.VORMIA_TIMEOUT }; this.config = { ...defaultConfig, ...config }; if (!this.config.baseURL) { console.warn( "VormiaClient: No baseURL provided. Please set VORMIA_API_URL in your .env file or pass baseURL in the config." ); } this.axiosInstance = axios.create({ baseURL: this.config.baseURL, timeout: this.config.timeout, headers: { "Content-Type": "application/json", ...this.config.headers }, withCredentials: this.config.withCredentials }); this.setupInterceptors(); } setupInterceptors() { this.axiosInstance.interceptors.request.use( (config) => { const token = this.getAuthToken(); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }, (error) => Promise.reject(error) ); this.axiosInstance.interceptors.response.use( (response) => response, (error) => { if (error.response && error.response.status === 401) { if (this.config.onUnauthenticated) { this.config.onUnauthenticated(); } } return Promise.reject(error); } ); } // Core HTTP methods async request(config) { var _a, _b; try { const response = await this.axiosInstance.request(config); return response; } catch (error) { const status = (_a = error.response) == null ? void 0 : _a.status; const response = (_b = error.response) == null ? void 0 : _b.data; const message = error.message || "Request failed"; throw new VormiaError(message, status, response, error.code); } } get(url, config) { return this.request({ ...config, method: "GET", url }); } post(url, data, config) { return this.request({ ...config, method: "POST", url, data }); } put(url, data, config) { return this.request({ ...config, method: "PUT", url, data }); } delete(url, config) { return this.request({ ...config, method: "DELETE", url }); } patch(url, data, config) { return this.request({ ...config, method: "PATCH", url, data }); } // Auth methods setAuthToken(token) { if (typeof window !== "undefined") { localStorage.setItem(this.config.authTokenKey, token); } } clearAuthToken() { if (typeof window !== "undefined") { localStorage.removeItem(this.config.authTokenKey); } } getAuthToken() { if (typeof window !== "undefined") { return localStorage.getItem(this.config.authTokenKey); } return null; } } let globalClient = null; function createVormiaClient(config) { globalClient = new VormiaClient(config); return globalClient; } function getGlobalVormiaClient() { if (!globalClient) { throw new Error( "VormiaClient has not been initialized. Call createVormiaClient first." ); } return globalClient; } function setGlobalVormiaClient(client) { globalClient = client; } export { VormiaClient, VormiaError, createVormiaClient, getGlobalVormiaClient, setGlobalVormiaClient }; //# sourceMappingURL=VormiaClient.mjs.map