0.2.8 • Published 3 years ago
ts-req v0.2.8
ts-req
TypeScript
로 작성된 XMLHttpRequest 기반 request 라이브러리입니다.
기본적으로 Promise를 반환합니다.
🛠 Insatll
npm install --save ts-req
📌 Import
import TsReq from "ts-req";
💅 Basic Usage
const req = new TsReq(url);
const res1 = await req.get(pathname, headers, callback);
const res2 = await req.post(pathname, body, headers, callback);
url은 필수입니다.
headers 는 참고를 key, value로 가지는 object입니다. default 는 "Content-Type": "application/json", "Cache-Control": "no-cache", Accept: "*" 입니다.
callback은 각 request의 state가 변경될 때 실행됩니다.
resolve를 반환하는 status는 0, 100, 101, 200, 201, 202, 203, 204, 205, 206 가 있습니다. status 0은 요청을 취소했을 때 반환합니다.
Example
📝 GET (JSON)
(async () => {
const url = "https://httpbin.org";
const req = new TsReq(url);
const res = await req.get("/get");
console.log(res);
})();
📝 GET (Other Types)
(async () => {
const url = "https://httpbin.org";
const req = new TsReq(url);
const res = await req.get("/image/png");
console.log(res);
})();
📝 POST (JSON)
(async () => {
const url = "https://httpbin.org";
const req = new TsReq(url);
const res = await req.post("/post", {
title: "title",
body: "body",
userId: 1,
});
console.log(res);
})();
📝 POST (FormData)
(async () => {
const url = "https://httpbin.org";
const req = new TsReq(url);
const formData = new FormData();
formData.append("test", "test");
const res = await req.post("/post", formData);
console.log(res);
})();
📝 Callback
(async () => {
const url = "https://httpbin.org";
const req = new TsReq(url);
const res1 = await req.get("/get", null, (req) => {
// ... do something
console.log(req);
});
const res2 = await req.post(
"/post",
{
title: "title",
body: "body",
userId: 1,
},
null,
(req) => {
// ... do something
console.log(req);
}
);
console.log(res1, res2);
})();
callback의 첫 번째 인자인 req는 해당 요청의 XMLHttpRequest 인스턴스 입니다. type은 XMLHttpRequest입니다.
📝 Cancel
(async () => {
const url = "https://httpbin.org";
const req = new TsReq(url);
const needToCancel = true;
const res = await req.get("/get", null, (req) => {
if (needToCancel) req.abort();
});
console.log(res);
})();
취소된 응답은 status 0을 반환합니다.
📝 Set Common Headers
req.defaultHeader["KEY"] = "VALUE";
📝 Set Resolve Status
resolve를 반환할 status를 지정합니다. 아래 예제는 status 200이 아닌 경우 모두 reject를 반환합니다.
req.resolveStatus = [200];