Created
June 15, 2024 08:40
-
-
Save zax4r0/38036362ad2f956360f6d152e5c3de20 to your computer and use it in GitHub Desktop.
axiosclinet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @format */ | |
import { useCookies } from "next-client-cookies"; | |
import axios, { AxiosError, AxiosInstance, AxiosResponse } from "axios"; | |
import { getCookies } from "next-client-cookies/server"; | |
const TOKEN_KEY = "token"; | |
const isServer = typeof window === "undefined"; | |
export type ParamsType = { | |
type?: string; | |
text?: string; | |
category?: string; | |
status?: string; | |
is_active?: string; | |
shop_id?: string; | |
limit?: number; | |
}; | |
export function getBaseUrl() { | |
if (typeof window !== "undefined") return window.location.origin; | |
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; | |
return `http://localhost:${process.env.PORT ?? 3000}`; | |
} | |
/** | |
* This is an Axios instance created using the axios.create() method. | |
* It has a base URL and headers that are set to accept and send JSON data. | |
* @type {Object} - An instance of the Axios library. | |
* @property {Object} headers - The default headers for all requests made with this instance. | |
* @property {string} headers.Accept - The accept header, set to 'application/json'. | |
* @property {string} headers.Content-Type - The content-type header, set to 'application/json'. | |
*/ | |
const Axios: AxiosInstance = axios.create({ | |
baseURL: "https://v2.jokeapi.dev/joke", | |
timeout: 5000, | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
}); | |
// Change request data/error here | |
Axios.interceptors.request.use( | |
(config: any) => { | |
const token = isServer | |
? getCookies().get(TOKEN_KEY) | |
: window?.document?.cookie[TOKEN_KEY]; | |
console.log("token", token); | |
config.headers = { | |
...config.headers, | |
Authorization: `Bearer ${token ? token : ""}`, | |
}; | |
return config; | |
}, | |
(error: AxiosError) => { | |
return Promise.reject(error); | |
} | |
); | |
Axios.interceptors.response.use( | |
(response: AxiosResponse) => response, | |
(error) => { | |
if ( | |
(error.response && error.response.status === 401) || | |
(error.response && error.response.status === 403) || | |
(error.response && | |
error.response.data.message === "PIXER_ERROR.NOT_AUTHORIZED") | |
) { | |
} | |
return Promise.reject(error); | |
} | |
); | |
/** | |
* The HttpClient class provides methods for making HTTP requests using the AxiosC instance. | |
* The methods are: | |
* | |
* - `get(url: string, params?: any): Promise<any>` | |
* | |
* - `post(url: string, data: any, options?: AxiosRequestConfig): Promise<any>` | |
* | |
* - `put(url: string, data: any): Promise<any>` | |
* | |
* - `patch(url: string, data: any): Promise<any>` | |
* | |
* - `delete(url: string): Promise<any>` | |
* | |
* They all return the response data from the server. | |
*/ | |
export class HttpClient { | |
static async get<T>(url: string, params?: unknown) { | |
const response = await Axios.get<T>(url, { params }); | |
return response.data; | |
} | |
static async post<T>(url: string, data: unknown, options?: any) { | |
const response = await Axios.post<T>(url, data, options); | |
return response.data; | |
} | |
static async put<T>(url: string, data: unknown) { | |
const response = await Axios.put<T>(url, data); | |
return response.data; | |
} | |
static async patch<T>(url: string, data: unknown) { | |
const response = await Axios.patch<T>(url, data); | |
return response.data; | |
} | |
static async delete<T>(url: string) { | |
const response = await Axios.delete<T>(url); | |
return response.data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment