Skip to content

Commit 470ec5f

Browse files
authored
Nodenext compatibility (#116)
1 parent da57e1a commit 470ec5f

File tree

7 files changed

+137
-134
lines changed

7 files changed

+137
-134
lines changed

plugin.js renamed to index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function etagOnSend (req, res, payload, next) {
5353
)
5454
}
5555

56-
function fastifyCachingPlugin (instance, options, next) {
56+
function fastifyCaching (instance, options, next) {
5757
let _options
5858
if (Function.prototype.isPrototypeOf(options)) {
5959
_options = Object.assign({}, defaultOptions)
@@ -92,10 +92,12 @@ function fastifyCachingPlugin (instance, options, next) {
9292
next()
9393
}
9494

95-
module.exports = fp(fastifyCachingPlugin, {
95+
module.exports = fp(fastifyCaching, {
9696
fastify: '4.x',
9797
name: '@fastify/caching'
9898
})
99+
module.exports.default = fastifyCaching
100+
module.exports.fastifyCaching = fastifyCaching
99101

100102
module.exports.privacy = {
101103
NOCACHE: 'no-cache',

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "@fastify/caching",
33
"version": "8.0.1",
44
"description": "A plugin for Fastify to enable management of cache control headers",
5-
"main": "plugin.js",
6-
"types": "plugin.d.ts",
5+
"main": "index.js",
6+
"types": "types/index.d.ts",
77
"scripts": {
88
"lint": "standard --verbose | snazzy",
99
"test": "npm run test:unit && npm run test:typescript",
@@ -45,9 +45,6 @@
4545
"fastify-plugin": "^4.0.0",
4646
"uid-safe": "^2.1.5"
4747
},
48-
"tsd": {
49-
"directory": "test/types"
50-
},
5148
"publishConfig": {
5249
"access": "public"
5350
},

plugin.d.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

test/cache.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { test } = require('tap')
4-
const plugin = require('../plugin')
4+
const plugin = require('..')
55

66
const Fastify = require('fastify')
77

test/headers.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { test } = require('tap')
44
const Fastify = require('fastify')
5-
const plugin = require('../plugin')
5+
const plugin = require('..')
66

77
test('decorators get added', async (t) => {
88
t.plan(1)

types/index.d.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

test/types/plugin.test-d.ts renamed to types/index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expectAssignable, expectError, expectType } from 'tsd';
33
import fastifyCaching, {
44
AbstractCacheCompliantObject,
55
FastifyCachingPluginOptions,
6-
} from '../..';
6+
} from '..';
77

88
const fastify = Fastify({ logger: true });
99

0 commit comments

Comments
 (0)