Skip to content

Commit

Permalink
remove header interpretation, improve API
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigg committed Dec 2, 2024
1 parent 4b0c421 commit d9b5c79
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
20 changes: 8 additions & 12 deletions src/api/request-with-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function requestWithParameters<T = any>({
cache: REQUEST_CACHE,
canReadCache,
canStoreInCache,
} = getCacheSettings(localCache, customHeaders);
} = getCacheSettings(localCache);

if (canReadCache && REQUEST_CACHE.has(key)) {
return REQUEST_CACHE.get(key) as Promise<T>;
Expand Down Expand Up @@ -95,17 +95,13 @@ export async function requestWithParameters<T = any>({
return jsonPromise;
}

function getCacheSettings(
localCache: LocalCacheOptions | undefined,
headers: Record<string, string>
) {
const cacheControl = headers['Cache-Control'];
const canReadCache = localCache
? localCache.canReadCache
: !cacheControl?.includes('no-cache');
const canStoreInCache = localCache
? localCache.canStoreInCache
: !cacheControl?.includes('no-store');
function getCacheSettings(localCache: LocalCacheOptions | undefined) {
const canReadCache = localCache?.cacheControl?.includes('no-cache')
? false
: true;
const canStoreInCache = localCache?.cacheControl?.includes('no-store')
? false
: true;
const cache = localCache?.cache || DEFAULT_REQUEST_CACHE;

return {
Expand Down
24 changes: 14 additions & 10 deletions src/sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,26 @@ export type SourceOptionalOptions = {
maxLengthURL?: number;

/**
* Local cache options.
* * `canReadCache`: If `true`, the source will try to read from the local memory cache.
* * `canStoreInCache`: If `true`, the source will store the response in the local memory cache.
* * `cache`: A map of promises that are used to store the responses.
*
* If not provided, source will try to detect `CacheControl: no-cache or no-store` headers in the response and disable respective caching modes.
*
* By default, local in-memory caching is enabled
* By default, local in-memory caching is enabled.
*/
localCache?: LocalCacheOptions;
};

export type LocalCacheOptions = {
canReadCache?: boolean;
canStoreInCache?: boolean;
/**
* Map that stores requests and their responses.
*/
cache?: Map<string, Promise<unknown>>;

/**
* Cache control
* * `no-cache`: If present, the source will always fetch from original source.
* * `no-store`: If present, source will not store result in cache (for later reuse).
*
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#directives
*/

cacheControl?: ('no-cache' | 'no-store')[];
};

export type SourceOptions = SourceRequiredOptions &
Expand Down

0 comments on commit d9b5c79

Please sign in to comment.