Skip to content

Instantly share code, notes, and snippets.

@ajparrah
Created March 16, 2022 18:52
Show Gist options
  • Save ajparrah/7bf2740446f01eeac7b7949c0a2b9abe to your computer and use it in GitHub Desktop.
Save ajparrah/7bf2740446f01eeac7b7949c0a2b9abe to your computer and use it in GitHub Desktop.
How to type decorateReply of fastify
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