43 lines
2.5 KiB
Plaintext
43 lines
2.5 KiB
Plaintext
import { StatusCode } from "../../utils/http-status.js";
|
|
import { MiddlewareHandler } from "../../types.js";
|
|
import { Context } from "../../context.js";
|
|
//#region src/middleware/cache/index.d.ts
|
|
/**
|
|
* Cache Middleware for Hono.
|
|
*
|
|
* @see {@link https://hono.dev/docs/middleware/builtin/cache}
|
|
*
|
|
* @param {Object} options - The options for the cache middleware.
|
|
* @param {string | Function} options.cacheName - The name of the cache. Can be used to store multiple caches with different identifiers.
|
|
* @param {boolean} [options.wait=false] - A boolean indicating if Hono should wait for the Promise of the `cache.put` function to resolve before continuing with the request. Required to be true for the Deno environment.
|
|
* @param {string} [options.cacheControl] - A string of directives for the `Cache-Control` header.
|
|
* @param {string | string[]} [options.vary] - Adds the configured request headers to the cache key variants and sets the `Vary` header in the response. If the original response header already contains a `Vary` header, the values are merged, removing any duplicates.
|
|
* @param {Function} [options.keyGenerator] - Generates keys for every request in the `cacheName` store. This can be used to cache data based on request parameters or context parameters. QUERY keys additionally include a digest of the request content and its representation metadata.
|
|
* @param {number} [options.maxQueryBodySize=65536] - The maximum QUERY request body size in bytes that can be cached. Larger QUERY requests bypass the cache.
|
|
* @param {number[]} [options.cacheableStatusCodes=[200]] - An array of status codes that can be cached.
|
|
* @param {Function | false} [options.onCacheNotAvailable] - A callback invoked with the reason when `globalThis.caches` is not available or QUERY caching cannot use Web Crypto. By default, the reason is logged to the console. Set to `false` to suppress the log, or provide a custom function.
|
|
* @returns {MiddlewareHandler} The middleware handler function.
|
|
* @throws {Error} If the `vary` option includes "*".
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* app.use(
|
|
* '*',
|
|
* cache({
|
|
* cacheName: 'my-app',
|
|
* cacheControl: 'max-age=3600',
|
|
* })
|
|
* )
|
|
* ```
|
|
*/
|
|
export declare const cache: (options: {
|
|
cacheName: string | ((c: Context) => Promise<string> | string);
|
|
wait?: boolean;
|
|
cacheControl?: string;
|
|
vary?: string | string[];
|
|
keyGenerator?: (c: Context) => Promise<string> | string;
|
|
maxQueryBodySize?: number;
|
|
cacheableStatusCodes?: StatusCode[];
|
|
onCacheNotAvailable?: ((reason: string) => void) | false;
|
|
}) => MiddlewareHandler;
|
|
//#endregion |