jcwallet/src/lib/Http.ts
2023-08-17 18:59:58 +08:00

104 lines
2.3 KiB
TypeScript

import { fetch } from 'whatwg-fetch';
import { WalletEnv } from '../config/WalletEnv';
import { VERSION_CODE } from '../config/constants';
export function request(url, option) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let walletEnv = new WalletEnv();
if (walletEnv.token) {
headers.append('Authorization', `Bearer ${walletEnv.token}`);
}
headers.append('api_version', VERSION_CODE + '');
headers.append('api_env', jc.wallet.env);
headers.append('api_platform', jc.wallet.platform);
let optionInt: any = {
method: 'GET',
mode: 'cors',
headers,
cache: 'no-cache',
};
Object.assign(optionInt, option);
// console.log("request option", JSON.stringify(optionInt));
return fetch(url, optionInt);
}
export function GET(url: string) {
return request(url, {});
}
export function GET_JSON(url: string) {
return GET(url).then((res) => {
return res.json();
});
}
export function POST(url, data) {
let option = {
method: 'POST',
body: JSON.stringify(data),
};
return request(url, option);
}
export function DELETE(url, data) {
let option = {
method: 'DELETE',
body: JSON.stringify(data),
};
return request(url, option);
}
export function PUT(url, data) {
let option = {
method: 'PUT',
body: JSON.stringify(data),
};
return request(url, option);
}
export function POST_JSON(url, data) {
return POST(url, data).then((res) => {
return res.json();
});
}
export function DELETE_JSON(url, data) {
return DELETE(url, data).then((res) => {
return res.json();
});
}
export function PUT_JSON(url, data) {
return PUT(url, data).then((res) => {
return res.json();
});
}
/**
* var headers = new Headers();
headers.append("Content-Type", "application/json");
if (window.chain.logined) {
formData.token = window.chain.token;
}
try {
let data = await fetch(
FORM_URL,
{
method: 'POST',
headers,
mode: 'cors',
cache: 'no-cache',
body: JSON.stringify(formData)
},
)
.then(res => {
return res.json()
})
if (!data.errcode) {
showSuccess();
}
} catch (err) {
console.log('error post data', err);
}
*/