mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 13:38:51 +00:00
38 lines
834 B
TypeScript
38 lines
834 B
TypeScript
// Constants
|
|
import { BASE_URL } from '@constant/path';
|
|
|
|
// Types
|
|
import { IResponse } from '@type/responses';
|
|
|
|
type TMethodRequest = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
|
|
/**
|
|
* The send request method to the server
|
|
* @param path The path of URL
|
|
* @param body The content will push on
|
|
* @param method HTTP method
|
|
* @returns The status code and message from server
|
|
*/
|
|
export const sendRequest = async <T>(
|
|
path: string,
|
|
method: TMethodRequest = 'GET',
|
|
body?: BodyInit
|
|
): Promise<IResponse<T>> => {
|
|
const response = await fetch(BASE_URL + path, {
|
|
method,
|
|
body,
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
const data = (await response.json()) as T;
|
|
|
|
return {
|
|
statusCode: response.status,
|
|
msg: response.statusText,
|
|
data,
|
|
};
|
|
};
|