@travic/fetch
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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 "node-fetch@2.6.1"
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

Package Sidebar

Install

npm i @travic/fetch

Weekly Downloads

3

Version

1.1.0

License

ISC

Unpacked Size

18.9 kB

Total Files

6

Last publish

Collaborators

  • travic