An open API service indexing awesome lists of open source software.

https://github.com/devsrv/cachedfetch

the fetch api with the ability to cache response
https://github.com/devsrv/cachedfetch

fetch javascript

Last synced: 7 months ago
JSON representation

the fetch api with the ability to cache response

Awesome Lists containing this project

README

          

# cachedFetch
> A micro utility built around the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) that allows caching network query results.
> Support *`localStorage`, `sessionStorage`, `AsyncStorage` (React Native)*

## ๐Ÿ“ฅ Installation

simply include the [adapter.min.js](https://github.com/devsrv/cachedfetch/blob/master/adapter.min.js) file in your project

## ๐Ÿงช The config file :

```javascript
const config = {
mode: 'block', //support block | allow
matchIn: [
'https://jsonplaceholder.typicode.com/posts/1'
],
endsWith: [
'?postId=1',
// '/posts'
],
defaultTTL: '5 day', // day | hour | minute | second
driver: 'sessionStorage', // localStorage (default) | sessionStorage | AsyncStorage
disk: undefined // undefined | AsyncStorage
};
```

## For `React Native`
```javascript
import AsyncStorage from '@react-native-async-storage/async-storage';

const config = {
mode: 'block',
.
.
driver: 'AsyncStorage',
disk: AsyncStorage
};
```

### Initialization with common headers & config

```javascript
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('X-Requested-With', 'XMLHttpRequest');
// headers.append('X-CSRF-Token', document.querySelector('meta[name="csrf-token"]').content);
```

```javascript
import _initCachedFetch from './the-minified-adapter-file-above'; // alternative to including the bundled js as external script

const cachedFetch = _initCachedFetch(config, headers);
```

### EXAMPLES

____

__1. GET request with override cache mode__

```javascript
cachedFetch('fetch_posts', 'https://jsonplaceholder.typicode.com/posts', {
'keep-cache' : true, // override mode
cacheTTL: '1 minute'
// any additional options goes here
})
.then(r => r.json())
.then(res => console.log('result', res));
```

__2. POST request (using global cache config)__

```javascript
cachedFetch('add_user', 'https://reqres.in/api/users', {
method: 'POST',
body: JSON.stringify({name: 'sourav', job: 'engineer'}),
})
.then(r => r.json())
.then(res => console.log('result', res));
```

__3. when content-type text/*__

```javascript
cachedFetch('fetch_posts', 'https://httpbin.org/html', {
'keep-cache' : true,
cacheTTL: '1 minute'
})
.then(r => r.text())
.then(res => console.log('result', res));
```

## ๐Ÿ‘‹๐Ÿผ Say Hi!
Leave a โญ if you find this package useful ๐Ÿ‘๐Ÿผ,

also don't forget to let me know in [Twitter](https://twitter.com/srvrksh)