-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
87 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,91 @@ | ||
import Axios, { InternalAxiosRequestConfig } from 'axios'; | ||
|
||
import { useNotifications } from '@/components/ui/notifications'; | ||
import { env } from '@/config/env'; | ||
|
||
function authRequestInterceptor(config: InternalAxiosRequestConfig) { | ||
if (config.headers) { | ||
config.headers.Accept = 'application/json'; | ||
} | ||
type RequestOptions = { | ||
method?: string; | ||
headers?: Record<string, string>; | ||
body?: any; | ||
cookie?: string; | ||
params?: Record<string, string | number | boolean | undefined | null>; | ||
cache?: RequestCache; | ||
next?: NextFetchRequestConfig; | ||
}; | ||
|
||
config.withCredentials = true; | ||
return config; | ||
function buildUrlWithParams( | ||
url: string, | ||
params?: RequestOptions['params'], | ||
): string { | ||
if (!params) return url; | ||
const filteredParams = Object.fromEntries( | ||
Object.entries(params).filter( | ||
([, value]) => value !== undefined && value !== null, | ||
), | ||
); | ||
if (Object.keys(filteredParams).length === 0) return url; | ||
const queryString = new URLSearchParams( | ||
filteredParams as Record<string, string>, | ||
).toString(); | ||
return `${url}?${queryString}`; | ||
} | ||
|
||
export const api = Axios.create({ | ||
baseURL: env.API_URL, | ||
}); | ||
|
||
api.interceptors.request.use(authRequestInterceptor); | ||
api.interceptors.response.use( | ||
(response) => { | ||
return response.data; | ||
}, | ||
(error) => { | ||
const message = error.response?.data?.message || error.message; | ||
useNotifications.getState().addNotification({ | ||
type: 'error', | ||
title: 'Error', | ||
message, | ||
}); | ||
async function fetchApi<T>( | ||
url: string, | ||
options: RequestOptions = {}, | ||
): Promise<T> { | ||
const { | ||
method = 'GET', | ||
headers = {}, | ||
body, | ||
cookie, | ||
params, | ||
cache = 'no-store', | ||
next, | ||
} = options; | ||
const fullUrl = buildUrlWithParams(`${env.API_URL}${url}`, params); | ||
|
||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
// if the endpoint requires the visiting user to be authenticated, | ||
// attaching cookies is required for requests made on the server side | ||
export const attachCookie = ( | ||
cookie?: string, | ||
headers?: Record<string, string>, | ||
) => { | ||
return { | ||
const response = await fetch(fullUrl, { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
...headers, | ||
...(cookie ? { Cookie: cookie } : {}), | ||
}, | ||
}; | ||
body: body ? JSON.stringify(body) : undefined, | ||
credentials: 'include', | ||
cache, | ||
next, | ||
}); | ||
|
||
if (!response.ok) { | ||
const message = (await response.json()).message || response.statusText; | ||
if (typeof window !== 'undefined') { | ||
useNotifications.getState().addNotification({ | ||
type: 'error', | ||
title: 'Error', | ||
message, | ||
}); | ||
} | ||
throw new Error(message); | ||
} | ||
|
||
return response.json(); | ||
} | ||
|
||
export const api = { | ||
get<T>(url: string, options?: RequestOptions): Promise<T> { | ||
return fetchApi<T>(url, { ...options, method: 'GET' }); | ||
}, | ||
post<T>(url: string, body?: any, options?: RequestOptions): Promise<T> { | ||
return fetchApi<T>(url, { ...options, method: 'POST', body }); | ||
}, | ||
put<T>(url: string, body?: any, options?: RequestOptions): Promise<T> { | ||
return fetchApi<T>(url, { ...options, method: 'PUT', body }); | ||
}, | ||
patch<T>(url: string, body?: any, options?: RequestOptions): Promise<T> { | ||
return fetchApi<T>(url, { ...options, method: 'PATCH', body }); | ||
}, | ||
delete<T>(url: string, options?: RequestOptions): Promise<T> { | ||
return fetchApi<T>(url, { ...options, method: 'DELETE' }); | ||
}, | ||
}; |
This file contains 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
This file contains 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