1.1.5 • Published 2 years ago
@foodism/http v1.1.5
ts版本http 请求库
安装
npm install @foodism/http
在项目中使用
import Service from "@foodism/http";
class Http extends Service {
constructor(props?: any) {
super();
this.init();
}
init() {
this.baseURL = "https://jsonplaceholder.typicode.com/";
this.timeout = 1000;
this.withCredentials = false;
this.headers = {
"Content-Type": "application/json",
};
super.setup();
}
}
const http = new Http();
interface Post {
id: number;
title: string;
}
async function getUsers() {
// post 请求
const data = await http.request<Post[]>({
url: "/posts",
method: "post",
data: {
userId: 10001,
id: 100,
title: "yagn-=titi",
body: "body-yang",
},
headers: {
"Content-Type": "application/json",
},
});
// get 请求
// 1. 把get请求的参数 拼接在url后面
const data2 = await http.request({
url: "/post?name=xx",
});
// 2. 把get请求的参数 放在data里面,自动拼接在url后面
const data3 = await http.request({
url: "/post",
data: {
name: "xx"
}
});
console.log("最终的值得");
console.log(data);
}
getUsers();