Simple Fluent Interface to call fetch
npm install @web-atoms/fetch-builderjavascript
const text = await FetchBuilder.get("https://someurl.com")
.asText();
`
Post Json body and retrieve Json
`javascript
const result = await FetchBuilder.post("https://someapi.com")
.header("x-api-key", someKey)
.jsonBody({
name: "bla bla bla",
email: "bla@bla.bla"
})
.asJson();
`
Reuse builder
This is important as you can configure builder once with default headers.
`javascript
const client = FetchBuilder
.url("https://somewhere.com/")
.header("x-api-key", someKey);
const result = await client.post("/api/orders/create")
.jsonBody({
productId: 3
})
.asJson();
`
Log Failed Requests
This will log only failed requests.
`javascript
const client = FetchBuilder
.url("https://somewhere.com/")
.header("x-api-key", someKey)
.logWhenFailed(console.error);
const result = await client.post("/api/orders/create")
.jsonBody({
productId: 3
})
.asJson();
`
Query String
`javascript
const client = FetchBuilder
.url("https://somewhere.com/")
.header("x-api-key", someKey)
.logWhenFailed(console.error);
const result = await client.get("/api/orders/reports")
.query("year-start", 2015)
.query("year-end", 2022)
.query("search-text", "social mail") // <-- this will be encoded
.asJson();
``