💡 A simple fetch API Utils.
Implement from ofetch
Install:
# npm
npm i @techbase/simm-fetch
Import the required methods and constants:
import { get, post, put, del } from "@techbase/simm-fetch";
const fetchData = async () => {
try {
const response = await get("/endpoint");
console.log(response.data);
} catch (error) {
console.error(error);
}
};
fetchData();
const postData = async () => {
try {
const response = await post("/endpoint", { key: "value" });
console.log(response.data);
} catch (error) {
console.error(error);
}
};
postData();
const updateData = async () => {
try {
const response = await put("/endpoint", { key: "value" });
console.log(response.data);
} catch (error) {
console.error(error);
}
};
updateData();
const deleteData = async () => {
try {
const response = await del("/endpoint");
console.log(response.data);
} catch (error) {
console.error(error);
}
};
deleteData();
Retry a request up to a specified number of times if it fails:
const fetchDataWithRetry = async () => {
try {
const response = await get("/endpoint", { retry: 3 });
console.log(response.data);
} catch (error) {
console.error(error);
}
};
fetchDataWithRetry();
Set a timeout for a request. If the request takes longer than the specified time, it will throw a timeout error:
const fetchDataWithTimeout = async () => {
try {
const response = await get("/endpoint", { timeout: 1000 });
console.log(response.data);
} catch (error) {
console.error(error);
}
};
npm run build
npm login
npm publish
- Authen Token/ RefreshToken
- Retry Request
- Timeout Request
- Request Config
- Merge Header
- Set/Get Header
- Handle Response Success
- Handle Response Error
- Error Type
- decode JWT token Util
- Flattern Object Payload
- Validate Payload/Body/Params
- Cache
- v..v..
UIT. Made with 💖
New line