fetch-with-timeout-and-cache
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

fetch-with-timeout-and-cache

Extending fetch api to include timeouts, a client side cache and tries to avoid multiples requests and create resilients apps.

NPM

Table of Contents

Install

npm install --save fetch-with-timeout-and-cache

Usage

import React, { useState, useEffect } from 'react';
import fetchTC from 'fetch-with-timeout-and-cache'

interface Post {
  id: number;
  title: string;
  body: string;
}

function App() {
  const [posts, setPosts] = useState<Post[]>([]);

  useEffect(() => {
    fetchTC('https://jsonplaceholder.typicode.com/posts',
      {
        timeout: 1300,
      },
      {
        key: 'posts',
        ms: 30000,
      })
      .then(response => response.json())
      .then((data: Post[]) => setPosts(data));
  }, []);

  return (
    <div>
      <h1>Posts</h1>
      <ul>
        {posts.map(post => (
          <li key={post.id}>
            <h2>{post.title}</h2>
            <p>{post.body}</p>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default App;

Example with retry

import React, { useState, useEffect } from "react";
import fetchTC from "../../src/index";

interface Post {
  id: number;
  title: string;
  body: string;
}

function App() {
  const [posts, setPosts] = useState<Post[]>([]);

  useEffect(() => {
    fetchTC({
      resource: "https://mock.codes/500",
      cacheOptions: {
        key: "mocks",
        ms: 30000,
      },
      retryOptions: {
        delay: 2000,
        retries: 2,
      },
    })
      .then((response) => response.json())
      .then((data: Post[]) => setPosts(data));
  }, []);

  return (
    <div>
      <h1>Posts</h1>
      <ul>
        {posts.map((post) => (
          <li key={post.id}>
            <h2>{post.title}</h2>
            <p>{post.body}</p>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default App;

  return (
    <div>
      <h1>Posts</h1>
      <ul>
        {posts.map(post => (
          <li key={post.id}>
            <h2>{post.title}</h2>
            <p>{post.body}</p>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default App;

Properties

Name Description Default
resource This defines the resource that you wish to fetch
options An object containing any custom settings that you want to apply to the request. Use the property timeout to define a custom timeout undefined
cacheOptions Define the cache options if you want it undefined
retryOptions Define the retry options in case of internal server erros undefined

License

MIT © rafaelguinho

Readme

Keywords

none

Package Sidebar

Install

npm i fetch-with-timeout-and-cache

Weekly Downloads

1

Version

1.1.0

License

none

Unpacked Size

12.7 kB

Total Files

11

Last publish

Collaborators

  • rafaelguinho4