|
| 1 | +/// <reference types='node' /> |
| 2 | + |
| 3 | +import { FastifyPluginCallback } from 'fastify'; |
| 4 | + |
| 5 | +declare module 'fastify' { |
| 6 | + interface FastifyInstance { |
| 7 | + cache: fastifyCaching.AbstractCacheCompliantObject; |
| 8 | + cacheSegment: string; |
| 9 | + etagMaxLife: number | undefined; |
| 10 | + } |
| 11 | + |
| 12 | + interface FastifyReply { |
| 13 | + /** |
| 14 | + * This method allows setting of the `expires` header. |
| 15 | + * |
| 16 | + * @link [reply.expires() documentation](https://github.com/fastify/fastify-caching#replyexpiresdate) |
| 17 | + * |
| 18 | + * @param date A regular `Date` object, or a valid date string according to [RFC 2616 section 14.21](https://datatracker.ietf.org/doc/html/rfc2616#section-14.21). |
| 19 | + */ |
| 20 | + expires(date?: Date): this; |
| 21 | + |
| 22 | + /** |
| 23 | + * This method allows setting of the `etag` header. |
| 24 | + * |
| 25 | + * @link [reply.etag() documentation](https://github.com/fastify/fastify-caching#replyetagstring-number) |
| 26 | + * |
| 27 | + * @param tag Any arbitrary string that is valid for HTTP headers. |
| 28 | + * @param timeToLive The time must be specified in milliseconds. The default lifetime, when the parameter is not specified, is `3600000`. |
| 29 | + */ |
| 30 | + etag(tag?: string, timeToLive?: number): this; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +type FastifyCaching = FastifyPluginCallback<fastifyCaching.FastifyCachingPluginOptions> & { |
| 35 | + privacy: fastifyCaching.Privacy; |
| 36 | +}; |
| 37 | + |
| 38 | +declare namespace fastifyCaching { |
| 39 | + /** |
| 40 | + * @link [`abstract-cache` protocol documentation](https://github.com/jsumners/abstract-cache#protocol) |
| 41 | + */ |
| 42 | + export interface AbstractCacheCompliantObject { |
| 43 | + get( |
| 44 | + key: string | { id: string; segment: string }, |
| 45 | + callback?: (error: unknown, result: unknown) => void |
| 46 | + ): void; |
| 47 | + set( |
| 48 | + key: string | { id: string; segment: string }, |
| 49 | + value: unknown, |
| 50 | + timeToLive: number, |
| 51 | + callback?: (error: unknown, result: unknown) => void |
| 52 | + ): void; |
| 53 | + } |
| 54 | + |
| 55 | + export interface Privacy { |
| 56 | + NOCACHE: 'no-cache'; |
| 57 | + PRIVATE: 'private'; |
| 58 | + PUBLIC: 'public'; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @link [`fastify-caching` options documentation](https://github.com/fastify/fastify-caching#options) |
| 63 | + */ |
| 64 | + export interface FastifyCachingPluginOptions { |
| 65 | + /** |
| 66 | + * An [abstract-cache](https://www.npmjs.com/package/abstract-cache) protocol compliant cache object. |
| 67 | + * Note: the plugin requires a cache instance to properly support the ETag mechanism. |
| 68 | + * Therefore, if a falsy value is supplied the default will be used. |
| 69 | + * |
| 70 | + * - Default value: `abstract-cache.memclient` |
| 71 | + */ |
| 72 | + cache?: AbstractCacheCompliantObject; |
| 73 | + |
| 74 | + /** |
| 75 | + * The segment identifier to use when communicating with the cache. |
| 76 | + * |
| 77 | + * - Default value: `fastify-caching` |
| 78 | + */ |
| 79 | + cacheSegment?: string; |
| 80 | + |
| 81 | + etagMaxLife?: number; |
| 82 | + |
| 83 | + /** |
| 84 | + * A value, in seconds, for the max-age the resource may be cached. |
| 85 | + * When this is set, and privacy is not set to no-cache, then ', max-age=<value>' |
| 86 | + * will be appended to the cache-control header. |
| 87 | + * |
| 88 | + * - Default value: `undefined` |
| 89 | + */ |
| 90 | + expiresIn?: number; |
| 91 | + |
| 92 | + /** |
| 93 | + * It can be set to any string that is valid for a cache-response-directive as |
| 94 | + * defined by [RFC 2616](https://datatracker.ietf.org/doc/html/rfc2616#section-14.9). |
| 95 | + * |
| 96 | + * - Default value: `undefined` |
| 97 | + * |
| 98 | + * @link [MDN Cache-Control - Response Directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives) |
| 99 | + * |
| 100 | + * @example |
| 101 | + * const fastifyCaching = require('fastify-caching'); |
| 102 | + * |
| 103 | + * // Disabling client side caching of all routes. |
| 104 | + * fastify.register(fastifyCaching, { privacy: fastifyCaching.privacy.NOCACHE }); |
| 105 | + */ |
| 106 | + privacy?: string; |
| 107 | + |
| 108 | + /** |
| 109 | + * A value, in seconds, for the length of time the resource is fresh and may be |
| 110 | + * held in a shared cache (e.g. a CDN). Shared caches will ignore max-age when |
| 111 | + * this is specified, though browsers will continue to use max-age. Should be |
| 112 | + * used with expiresIn, not in place of it. When this is set, and privacy is set |
| 113 | + * to public, then ', s-maxage=<value>' will be appended to the cache-control header. |
| 114 | + * |
| 115 | + * - Default value: `undefined` |
| 116 | + */ |
| 117 | + serverExpiresIn?: number; |
| 118 | + } |
| 119 | + |
| 120 | + export const privacy: { |
| 121 | + privacy: Privacy; |
| 122 | + }; |
| 123 | + export const fastifyCaching: FastifyCaching; |
| 124 | + export { fastifyCaching as default }; |
| 125 | +} |
| 126 | + |
| 127 | +declare function fastifyCaching(...params: Parameters<FastifyCaching>): ReturnType<FastifyCaching> |
| 128 | +export = fastifyCaching |
0 commit comments