Skip to content

Latest commit

 

History

History
 
 

simm-fetch

simple-fetch

npm version npm downloads

💡 A simple fetch API Utils.

Implement from ofetch

🚀 Quick Start

Install:

# npm
npm i @techbase/simm-fetch

✔️ Usage

Import the required methods and constants:

import { get, post, put, del } from "@techbase/simm-fetch";

✔️ GET Request

const fetchData = async () => {
  try {
    const response = await get("/endpoint");
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
};

fetchData();

✔️ POST Request

const postData = async () => {
  try {
    const response = await post("/endpoint", { key: "value" });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
};

postData();

✔️ PUT Request

const updateData = async () => {
  try {
    const response = await put("/endpoint", { key: "value" });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
};

updateData();

✔️ DELETE Request

const deleteData = async () => {
  try {
    const response = await del("/endpoint");
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
};

deleteData();

✔️ Retry Option

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();

✔️ Timeout Option

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);
  }
};

✔️ Build Package

npm run build

✔️ Publish Package

npm login
npm publish

TODO

  • 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..

License

UIT. Made with 💖

New line