Request
http request for browser based on
window.fetch
Install
npm install @cisdi/request
Usage
import Request from "@cisdi/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 body -
headers
: request headers -
query
: request query params -
params
: request path params
Res
original fetch
response, but with res.data
, which is serialized from res.body
based on Content-Type