|
|
@ -1,17 +1,9 @@
|
|
|
|
import { getAuthorization, getStringParams, handleResponse, hostMap, createFormBody } from '@/request/client/utils';
|
|
|
|
import { getAuthorization, getStringParams, handleResponse, hostMap, createFormBody } from '@/request/client/utils';
|
|
|
|
import { IAuthType, IResponse } from '@/request/type';
|
|
|
|
import { IAuthType, IResponse } from '@/request/type';
|
|
|
|
|
|
|
|
|
|
|
|
export const post = async <T>(
|
|
|
|
export const post = async <T>(url: string, data: any, revalidate = 20): Promise<IResponse<T>> => {
|
|
|
|
url: string,
|
|
|
|
const token = await getAuthorization();
|
|
|
|
data: any,
|
|
|
|
const response = await fetch(url, {
|
|
|
|
authType: IAuthType = 'default',
|
|
|
|
|
|
|
|
revalidate = 20,
|
|
|
|
|
|
|
|
): Promise<IResponse<T>> => {
|
|
|
|
|
|
|
|
const token = await getAuthorization(authType);
|
|
|
|
|
|
|
|
// const host = hostMap[authType];
|
|
|
|
|
|
|
|
// const finallyUrl = `${host}${url}`;
|
|
|
|
|
|
|
|
const finallyUrl = `${url}`;
|
|
|
|
|
|
|
|
const response = await fetch(finallyUrl, {
|
|
|
|
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
|
Authorization: token || '',
|
|
|
|
Authorization: token || '',
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
|
@ -27,19 +19,11 @@ export const post = async <T>(
|
|
|
|
return await handleResponse(response);
|
|
|
|
return await handleResponse(response);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const get = async <T>(
|
|
|
|
export const get = async <T>(url: string, data: any = null, revalidate = 20): Promise<IResponse<T>> => {
|
|
|
|
url: string,
|
|
|
|
const token = await getAuthorization();
|
|
|
|
data: any = null,
|
|
|
|
const response = await fetch(url, {
|
|
|
|
authType: IAuthType = 'noToken',
|
|
|
|
|
|
|
|
revalidate = 20,
|
|
|
|
|
|
|
|
): Promise<IResponse<T>> => {
|
|
|
|
|
|
|
|
const token = await getAuthorization(authType);
|
|
|
|
|
|
|
|
const host = hostMap[authType];
|
|
|
|
|
|
|
|
const formatUrl = data ? `${url}?${getStringParams(data)}` : url;
|
|
|
|
|
|
|
|
const finallyUrl = `${host}${formatUrl}`;
|
|
|
|
|
|
|
|
const response = await fetch(finallyUrl, {
|
|
|
|
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
|
Authorization: token,
|
|
|
|
Authorization: token || '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
method: 'GET',
|
|
|
|
next: {
|
|
|
|
next: {
|
|
|
@ -49,19 +33,11 @@ export const get = async <T>(
|
|
|
|
return await handleResponse(response);
|
|
|
|
return await handleResponse(response);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const remove = async <T>(
|
|
|
|
export const remove = async <T>(url: string, data: any = null, revalidate = 20): Promise<IResponse<T>> => {
|
|
|
|
url: string,
|
|
|
|
const token = await getAuthorization();
|
|
|
|
data: any = null,
|
|
|
|
const response = await fetch(url, {
|
|
|
|
authType: IAuthType = 'default',
|
|
|
|
|
|
|
|
revalidate = 20,
|
|
|
|
|
|
|
|
): Promise<IResponse<T>> => {
|
|
|
|
|
|
|
|
const token = await getAuthorization(authType);
|
|
|
|
|
|
|
|
const host = hostMap[authType];
|
|
|
|
|
|
|
|
const formatUrl = data ? `${url}?${getStringParams(data)}` : url;
|
|
|
|
|
|
|
|
const finallyUrl = `${host}${formatUrl}`;
|
|
|
|
|
|
|
|
const response = await fetch(finallyUrl, {
|
|
|
|
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
|
Authorization: token,
|
|
|
|
Authorization: token || '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
method: 'DELETE',
|
|
|
|
method: 'DELETE',
|
|
|
|
next: {
|
|
|
|
next: {
|
|
|
@ -72,12 +48,12 @@ export const remove = async <T>(
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const uploadFile = async <T>(url: string, file: File): Promise<IResponse<T>> => {
|
|
|
|
export const uploadFile = async <T>(url: string, file: File): Promise<IResponse<T>> => {
|
|
|
|
const token = await getAuthorization('default');
|
|
|
|
const token = await getAuthorization();
|
|
|
|
const formData = new FormData();
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', file);
|
|
|
|
formData.append('file', file);
|
|
|
|
const response = await fetch(url, {
|
|
|
|
const response = await fetch(url, {
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
|
authorization: token,
|
|
|
|
authorization: token || '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
method: 'POST',
|
|
|
|
body: formData,
|
|
|
|
body: formData,
|
|
|
|