@otools/request
v0.4.0
Published
http request util based on Fetch API
Downloads
3
Readme
Request
http request for browser based on
window.fetch
Install
npm install @otools/request
Usage
import Request from "@otools/request";
const request = new Request({
baseURL: "https://example.com",
});
request.get("/posts/{id}", {
params: { id: 1 },
});
request.post("/posts", {
body: { content: "content" },
});
request.put("/posts/{id}", {
params: { id: 1 },
body: { content: "content" },
});
request.patch("/posts/{id}", {
params: { id: 1 },
body: { content: "content" },
});
request.del("/posts/{id}", {
params: { id: 1 },
});
API
Base API
new Request(options?)
request.get(url, Req)
: send http request useGET
request.post(url, Req)
: send http request usePOST
request.put(url, Req)
: send http request usePUT
request.patch(url, Req)
: send http request usePATCH
request.del(url, Req)
: send http request useDELETE
beforeRequest
- type:
(req: Req) => Promise<Req>
afterRequest
- type:
(res: Res) => Promise<Res>
Req
request config
body
: request bodyheaders
: request headersquery
: request query paramsparams
: request path params
Res
original fetch
response, but with res.data
, which is serialized from res.body
based on Content-Type