diff --git a/index.js b/index.js index 531cd68..f3a707f 100644 --- a/index.js +++ b/index.js @@ -38,7 +38,7 @@ function cachingLoadByEtag (req, res, next) { }) } -function cachingStoreByEtag (req, res, payload, next) { +function cachingStoreByEtag (_req, res, payload, next) { const etag = res.getHeader('etag') if (!etag || !res._etagLife) return next() this.cache.set( @@ -70,7 +70,7 @@ function fastifyCaching (instance, options, next) { value += `, s-maxage=${_options.serverExpiresIn}` } - instance.addHook('onRequest', function cachingSetCacheControlHeader (req, res, next) { + instance.addHook('onRequest', function cachingSetCacheControlHeader (_req, res, next) { if (!res.hasHeader('Cache-control')) { res.header('Cache-control', value) } diff --git a/test/cache.test.js b/test/cache.test.js index 8441b4e..4d74c3c 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -21,25 +21,25 @@ test('cache is usable', async (t) => { t.plan(4) const fastify = Fastify() - await fastify.register(async (instance, options) => { - instance.addHook('onRequest', async function checkCachingRegistered (req, reply) { + await fastify.register(async (instance) => { + instance.addHook('onRequest', async function checkCachingRegistered () { t.assert.ifError(instance[Symbol.for('fastify-caching.registered')]) }) }) await fastify.register(plugin) - fastify.addHook('onRequest', async function checkCachingRegistered (req, reply) { + fastify.addHook('onRequest', async function checkCachingRegistered () { t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true) }) - fastify.get('/one', (req, reply) => { + fastify.get('/one', (_req, reply) => { fastify.cache.set('one', { one: true }, 1000, (err) => { if (err) return reply.send(err) reply.redirect('/two') }) }) - fastify.get('/two', (req, reply) => { + fastify.get('/two', (_req, reply) => { fastify.cache.get('one', (err, obj) => { t.assert.ifError(err) t.assert.strictEqual(obj.item, { one: true }) @@ -70,25 +70,25 @@ test('cache is usable with function as plugin default options input', async (t) t.plan(4) const fastify = Fastify() - await fastify.register(async (instance, options) => { - instance.addHook('onRequest', async function checkCachingNotRegistered (req, reply) { + await fastify.register(async (instance) => { + instance.addHook('onRequest', async function checkCachingNotRegistered () { t.assert.failure(instance[Symbol.for('fastify-caching.registered')]) }) }) await fastify.register(plugin, () => () => {}) - fastify.addHook('onRequest', async function checkCachingRegistered (req, reply) { + fastify.addHook('onRequest', async function checkCachingRegistered () { t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true) }) - fastify.get('/one', (req, reply) => { + fastify.get('/one', (_req, reply) => { fastify.cache.set('one', { one: true }, 1000, (err) => { if (err) return reply.send(err) reply.redirect('/two') }) }) - fastify.get('/two', (req, reply) => { + fastify.get('/two', (_req, reply) => { fastify.cache.get('one', (err, obj) => { t.assert.ifError(err) t.assert.strictEqual(obj.item, { one: true }) @@ -120,21 +120,21 @@ test('getting cache item with error returns error', async (t) => { t.plan(1) const mockCache = { - get: (info, callback) => callback(new Error('cache.get always errors')), - set: (key, value, ttl, callback) => callback() + get: (_info, callback) => callback(new Error('cache.get always errors')), + set: (_key, _value, _ttl, callback) => callback() } const fastify = Fastify() await fastify.register(plugin, { cache: mockCache }) - fastify.get('/one', (req, reply) => { + fastify.get('/one', (_req, reply) => { fastify.cache.set('one', { one: true }, 1000, (err) => { if (err) return reply.send(err) return reply.etag('123456').send({ hello: 'world' }) }) }) - fastify.get('/two', (req, reply) => { + fastify.get('/two', () => { fastify.cache.get('one', (err, obj) => { t.assert.failure(err) t.assert.failure(obj) @@ -164,7 +164,7 @@ test('etags get stored in cache', async (t) => { const fastify = Fastify() await fastify.register(plugin) - fastify.get('/one', (req, reply) => { + fastify.get('/one', (_req, reply) => { reply.etag('123456').send({ hello: 'world' }) }) @@ -191,7 +191,7 @@ test('etag cache life is customizable', async (t) => { const fastify = Fastify() await fastify.register(plugin) - fastify.get('/one', function (req, reply) { + fastify.get('/one', function (_req, reply) { reply // We set a cache lifetime of 50 milliseconds .etag('123456', 50) @@ -237,7 +237,7 @@ test('returns response payload', async (t) => { const fastify = Fastify() await fastify.register(plugin) - fastify.get('/one', (req, reply) => { + fastify.get('/one', (_req, reply) => { reply.etag('123456', 300).send({ hello: 'world' }) }) diff --git a/test/headers.test.js b/test/headers.test.js index 015a2eb..1739b3e 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -10,7 +10,7 @@ test('decorators get added', async (t) => { const fastify = Fastify() await fastify.register(plugin) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { t.assert.ok(reply.etag) reply.send() }) @@ -31,7 +31,7 @@ test('decorators add headers', async (t) => { const fastify = Fastify() await fastify.register(plugin) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.etag(tag).send() }) @@ -51,7 +51,7 @@ test('sets etag header for falsy argument', async (t) => { const fastify = Fastify() await fastify.register(plugin) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.etag().send() }) @@ -70,7 +70,7 @@ test('sets no-cache header', async (t) => { const fastify = Fastify() await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE }) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.send({ hello: 'world' }) }) @@ -95,7 +95,7 @@ test('sets private with max-age header', async (t) => { const fastify = Fastify() await fastify.register(plugin, opts) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.send({ hello: 'world' }) }) @@ -124,7 +124,7 @@ test('sets public with max-age and s-maxage header', async (t) => { const fastify = Fastify() await fastify.register(plugin, opts) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.send({ hello: 'world' }) }) @@ -151,12 +151,12 @@ test('do not set headers if another upstream plugin already sets it', async (t) } const fastify = Fastify() - fastify.addHook('onRequest', async function checkCachingDoesNotOverrideCacheControlHeader (req, reply) { + fastify.addHook('onRequest', async function checkCachingDoesNotOverrideCacheControlHeader (_req, reply) { reply.header('cache-control', 'do not override') }) await fastify.register(plugin, opts) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.send({ hello: 'world' }) }) @@ -182,7 +182,7 @@ test('only sets max-age and ignores s-maxage with private header', async (t) => const fastify = Fastify() await fastify.register(plugin, opts) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.send({ hello: 'world' }) }) @@ -210,7 +210,7 @@ test('s-maxage is optional with public header', async (t) => { const fastify = Fastify() await fastify.register(plugin, opts) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.send({ hello: 'world' }) }) @@ -230,7 +230,7 @@ test('sets no-store with max-age header', async (t) => { const fastify = Fastify() await fastify.register(plugin, { privacy: 'no-store', expiresIn: 300 }) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.send({ hello: 'world' }) }) @@ -255,7 +255,7 @@ test('sets the expires header', async (t) => { const fastify = Fastify() await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE }) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.expires(now).send({ hello: 'world' }) }) @@ -275,7 +275,7 @@ test('sets the expires header to a falsy value', async (t) => { const fastify = Fastify() await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE }) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.expires().send({ hello: 'world' }) }) @@ -294,7 +294,7 @@ test('sets the expires header to a custom value', async (t) => { const fastify = Fastify() await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE }) - fastify.get('/', (req, reply) => { + fastify.get('/', (_req, reply) => { reply.expires('foobar').send({ hello: 'world' }) }) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 805541e..d68be47 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -22,7 +22,7 @@ expectType(fastify.cache.set) expectType(fastify.cacheSegment) // expectType(fastify.etagMaxLife); -fastify.get('/one', async (request, reply) => { +fastify.get('/one', async (_request, reply) => { expectType<(tag?: string, timeToLive?: number) => FastifyReply>(reply.etag) expectType<(date?: Date) => FastifyReply>(reply.expires) @@ -32,7 +32,7 @@ fastify.get('/one', async (request, reply) => { return { message: 'one' } }) -fastify.get('/two', async (request, reply) => { +fastify.get('/two', async (_request, reply) => { expectType( reply.etag('hello', 6000).expires(new Date(Date.now() + 6000)) ) @@ -51,7 +51,7 @@ const badCachingOptions = { expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions)) -fastify.get('/three', async (request, reply) => { +fastify.get('/three', async () => { expectAssignable>( fastify.cache.get('well-known') )