react-simple-api
Version:
Create and cache API requests and responses
21 lines • 950 B
JavaScript
import React, { createContext, useContext } from 'react';
import { ApiCache } from './cache';
var ApiCacheContext = createContext({
getCache: function (id) { return id; },
setCache: function (_id, data) { return data; },
});
var ApiContextProvider = function (_a) {
var children = _a.children, baseApiUrl = _a.baseApiUrl;
var cache = ApiCache.getInstance();
var handleSetCache = function (id, data, cacheExpiry) {
cache.setCachedResponse(id, data, cacheExpiry);
return data;
};
var handleGetCache = function (id) {
return cache.getCachedResponse(id);
};
return (React.createElement(ApiCacheContext.Provider, { value: { getCache: handleGetCache, setCache: handleSetCache, baseApiUrl: baseApiUrl } }, children));
};
var useApiContext = function () { return useContext(ApiCacheContext); };
export { ApiContextProvider, ApiCacheContext, useApiContext };
//# sourceMappingURL=CacheContext.js.map