UNPKG

browser-cache-async

Version:

The browser-cache-async package provides a highly customizable, asynchronous caching system for client-side data management. Leveraging the power of the browser's IndexedDB, it enables efficient storage and retrieval of API responses, significantly reduci

1 lines 1.07 kB
import ms from"ms";import{encodeError}from"error-message-utils";import{ERRORS}from"../shared/errors.js";const __ms=e=>{try{return ms(e)}catch(e){return}},calculateRevalidateTime=(e="1 day")=>{let r;if("number"==typeof e&&e>0&&(r=e),"string"==typeof e&&(r=__ms(e)),"number"!=typeof r)throw new Error(encodeError(`The provided revalidate value '${e}' is invalid. Please provide either a numeric value representing milliseconds (e.g., 1000) or a valid time duration string (e.g., "1 hour" or "35 s").`,ERRORS.INVALID_REVALIDATE_VALUE));return r},buildQueryOptions=e=>{if("function"!=typeof e.query)throw new Error(encodeError("The query function provided in the functions is invalid.",ERRORS.INVALID_QUERY_FUNCTION));return{...e,revalidate:calculateRevalidateTime(e.revalidate)}},canQueryBeCached=async(e,r,a)=>"function"==typeof a?a(e,r):"boolean"!=typeof a||a,wrapData=(e,r)=>({data:e,staleAt:Date.now()+r}),unwrapData=e=>Boolean(e)&&"object"==typeof e&&e.staleAt>Date.now()?e.data:void 0;export{calculateRevalidateTime,buildQueryOptions,canQueryBeCached,wrapData,unwrapData};