fetch-lifecycle

0.0.4 • Public • Published

Fetch Lifecycle

Wrap fetch with lifecycle hooks, before, success, error, after.

const fetchLifecycle = new FetchLifecycle(
  "https://jsonplaceholder.typicode.com",
  { headers: { "Content-Type": "application/json" } },
  {
    before: [
      function (req, res, error) {
        console.log("before", req);
      },
    ],
    success: [
      async function (req, res) {
        console.log("success", req);
      },
    ],
    error: [
      (req, res, error) => {
        console.log("error", req, res, error);
      },
    ],
    after: [
      (req, res, error) => {
        console.log("after");
      },
    ],
  }
);
const lifecycle = await fetchLifecycle.request("/todos/1"); //(url[, options]) same as fetch
// lifecycle = {req,res,error}
fetchLifecycle.success([
  async function (req, res) {
    this.data = await res.json();
  },
]);
const lifecycle = await fetchLifecycle.request("/todos/1"); //(url[, options]) same as fetch
// lifecycle = {req,res,error,data}

Readme

Keywords

Package Sidebar

Install

npm i fetch-lifecycle

Weekly Downloads

0

Version

0.0.4

License

MIT

Unpacked Size

6.12 kB

Total Files

5

Last publish

Collaborators

  • niradler55