domain compelete api attachment
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import axios from "axios";
|
||||
import { getToken } from "./func";
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_BASE_URL,
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
axiosInstance.interceptors.request.use(async function (config) {
|
||||
const token = getToken();
|
||||
|
||||
config.headers.Authorization = "Bearer " + token;
|
||||
config.headers["x-business-id"] = localStorage.getItem(
|
||||
import.meta.env.VITE_WORKSPACE_ID
|
||||
);
|
||||
return config;
|
||||
});
|
||||
|
||||
export default axiosInstance;
|
||||
@@ -0,0 +1,22 @@
|
||||
import axios from "axios";
|
||||
import { getToken } from "./func";
|
||||
|
||||
const axiosDanak = axios.create({
|
||||
baseURL: import.meta.env.VITE_DANAK_BASE_URL,
|
||||
});
|
||||
|
||||
axiosDanak.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
axiosDanak.interceptors.request.use(async function (config) {
|
||||
const token = getToken();
|
||||
|
||||
config.headers.Authorization = "Bearer " + token;
|
||||
return config;
|
||||
});
|
||||
|
||||
export default axiosDanak;
|
||||
@@ -0,0 +1,61 @@
|
||||
import Cookie from "js-cookie";
|
||||
|
||||
export function isEmail(input: string): boolean {
|
||||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||
return emailRegex.test(input);
|
||||
}
|
||||
|
||||
export function NumberFormat(number: number): string {
|
||||
return Intl.NumberFormat("fa-IR").format(number);
|
||||
}
|
||||
|
||||
export const getToken = () => {
|
||||
return Cookie.get(import.meta.env.VITE_TOKEN_NAME);
|
||||
};
|
||||
|
||||
export const getRefreshToken = () => {
|
||||
return Cookie.get(import.meta.env.VITE_REFRESH_TOKEN_NAME);
|
||||
};
|
||||
|
||||
const isProduction = import.meta.env.MODE === "production";
|
||||
const domain = isProduction ? ".danakcorp.com" : undefined;
|
||||
|
||||
export const setToken = async (token: string) => {
|
||||
Cookie.set(import.meta.env.VITE_TOKEN_NAME, token, { domain });
|
||||
};
|
||||
|
||||
export const setRefreshToken = async (refreshToken: string) => {
|
||||
Cookie.set(import.meta.env.VITE_REFRESH_TOKEN_NAME, refreshToken, {
|
||||
domain,
|
||||
});
|
||||
};
|
||||
|
||||
export const removeToken = async () => {
|
||||
Cookie.remove(import.meta.env.VITE_TOKEN_NAME, { domain, path: "/" });
|
||||
};
|
||||
|
||||
export const removeRefreshToken = async () => {
|
||||
Cookie.remove(import.meta.env.VITE_REFRESH_TOKEN_NAME, { domain, path: "/" });
|
||||
};
|
||||
|
||||
export const timeAgo = (date: string | Date): string => {
|
||||
const now = new Date();
|
||||
const past = new Date(date);
|
||||
const diff = Math.floor((now.getTime() - past.getTime()) / 1000); // اختلاف بر حسب ثانیه
|
||||
|
||||
const units = [
|
||||
{ name: "سال", value: 60 * 60 * 24 * 365 },
|
||||
{ name: "ماه", value: 60 * 60 * 24 * 30 },
|
||||
{ name: "روز", value: 60 * 60 * 24 },
|
||||
{ name: "ساعت", value: 60 * 60 },
|
||||
{ name: "دقیقه", value: 60 },
|
||||
{ name: "ثانیه", value: 1 },
|
||||
];
|
||||
|
||||
for (const unit of units) {
|
||||
const amount = Math.floor(diff / unit.value);
|
||||
if (amount >= 1) return `${amount} ${unit.name} پیش`;
|
||||
}
|
||||
|
||||
return "همین الان";
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import { getToken } from "./func";
|
||||
|
||||
const orvalAxiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_BASE_URL,
|
||||
});
|
||||
|
||||
orvalAxiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
orvalAxiosInstance.interceptors.request.use(async (config) => {
|
||||
const token = getToken();
|
||||
|
||||
config.headers["Content-Type"] = "application/json";
|
||||
config.headers["Accept"] = "application/json";
|
||||
|
||||
if (token) {
|
||||
config.headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const businessId = localStorage.getItem(import.meta.env.VITE_WORKSPACE_ID);
|
||||
if (businessId) {
|
||||
config.headers["x-business-id"] = businessId;
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
export const customAxiosInstance = <T = unknown>(
|
||||
config: AxiosRequestConfig,
|
||||
options?: AxiosRequestConfig
|
||||
): Promise<T> => {
|
||||
const source = axios.CancelToken.source();
|
||||
const promise = orvalAxiosInstance({
|
||||
...config,
|
||||
...options,
|
||||
cancelToken: source.token,
|
||||
}).then(({ data }) => data);
|
||||
|
||||
// @ts-expect-error - Adding cancel method to promise
|
||||
promise.cancel = () => {
|
||||
source.cancel("Query was cancelled");
|
||||
};
|
||||
|
||||
return promise;
|
||||
};
|
||||
Reference in New Issue
Block a user