1.5.5 • Published 2 years ago
@konghayao/request v1.5.5
request 模板请求函数
同时支持 nodejs 和 browser
request 请求主函数的使用
import {
request,
Transaction,
} from "https://cdn.jsdelivr.net/npm/@konghayao/request/dist/request.browser.js";
// import { request, Transaction } from "@konghayoa/request";
const root = "http://localhost:2021";
request({
url: root + "/post/json",
method: "post",
headers: {}, // 请求头
// get 请求的参数也是以对象格式统一写在 body 里面
body: {
a: 1,
b: 2,
},
// string(default), json, binary,
// 表示 body 使用什么格式进行传输,当为 get 请求时无效
bodyType: "json",
// json(default), text, arrayBuffer, buffer (nodejs), blob (browser)
// 数据返回的时候以什么样的格式进行解析
returnType: "json",
}).then((res) => {
// res = { result, headers(nodejs), cookies(nodejs) }
console.log(res);
});
request 请求队列
import { Transaction } from "https://cdn.jsdelivr.net/npm/@konghayao/promise-transaction@1.4.0/dist/promiseTransaction.esm.js";
// 传入请求的二维数组
const task = new Transaction([
["request1", () => request1({ url: "/" })],
[
"request2",
(collection) => {
// 在这个函数中可以通过
console.log(collection["request1"]);
},
],
]);
// 生成之后必须要手动启动队列
task.run();