1.1.0 • Published 3 years ago

@travic/fetch v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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
1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago