Created
March 16, 2022 18:52
-
-
Save ajparrah/7bf2740446f01eeac7b7949c0a2b9abe to your computer and use it in GitHub Desktop.
How to type decorateReply of fastify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FastifyInstance, FastifyReply } from 'fastify'; | |
import fp from 'fastify-plugin'; | |
// To use it thisway: reply.noCache().code(200).send({}); | |
declare module 'fastify' { | |
interface FastifyReply { | |
noCache(): FastifyReply; | |
} | |
} | |
const headersToPreventHTTPCaching = { | |
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', | |
Pragma: 'no-cache', | |
Expires: 'Fri, 01 Jan 1990 00:00:00 GMT', | |
}; | |
const noCacheReply = fp(async (fastify: FastifyInstance) => { | |
function noCache(this: FastifyReply): FastifyReply { | |
const reply = this; | |
reply.headers(headersToPreventHTTPCaching); | |
return reply; | |
} | |
fastify.decorateReply<() => FastifyReply>('noCache', noCache); | |
}); | |
export default noCacheReply; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment