@travic/fetch
v1.1.0
Published
Fetch Lib with Caching Support
Downloads
4
Maintainers
Readme
Fetch
Fetch Lib with Caching Support
npm i @travic/fetch
# or
yarn add @travic/fetch
import { Fetch } from '@travic/fetch';
// or
const { Fetch } = require('@travic/fetch');
//
interface Fetch {
getUrl: () => string;
setUrl: (url: string) => void;
getCacheInterval: () => number;
setCacheInterval: (interval: number) => void;
getCache: () => object;
clearCache: () => void;
get: (url: string, headers?: any) => Promise<ResultHandlerResponse>;
post: (
url: string,
body?: any,
headers?: any
) => Promise<ResultHandlerResponse>;
put: (
url: string,
body?: any,
headers?: any
) => Promise<ResultHandlerResponse>;
delete: (
url: string,
body?: any,
headers?: any
) => Promise<ResultHandlerResponse>;
fetch: (url: string, fetchOptions?: any) => Promise<ResultHandlerResponse>;
}
interface ResultHandlerResponse {
response: null | Response; // the response;
data: any; // the response.json()
error: any; // any caught errors
fromCache: boolean; // if response came from cache
}
//
// example use
const url = '{URL}';
// fetch field requires window.fetch or node-fetch
// tested using "[email protected]"
const fetchObject = Fetch({ fetch: node_fetch, cacheInterval: 1000 });
const { response, data, error, fromCache } = await fetchObject.get(url);
const { response, data, error, fromCache } = await fetchObject.get(url, {
'Content-Type': 'application/json',
});
- POST, PUT, DELETE are supported but caching is still being worked on.
- Update Coming Soon