import Fastify, { FastifyReply } from 'fastify' import { expect } from 'tstyche' import fastifyCaching, { AbstractCacheCompliantObject, FastifyCachingPluginOptions, } from '..' const fastify = Fastify({ logger: true }) const cachingOptions: FastifyCachingPluginOptions = { privacy: fastifyCaching.privacy.PUBLIC, expiresIn: 300, cacheSegment: 'fastify-caching', } expect(cachingOptions).type.toBeAssignableTo() fastify.register(fastifyCaching, cachingOptions) expect(fastify.cache).type.toBe() expect(fastify.cache.get).type.toBe() expect(fastify.cache.set).type.toBe() expect(fastify.cacheSegment).type.toBe() fastify.get('/one', async (_request, reply) => { expect(reply.etag).type.toBe<(tag?: string, timeToLive?: number) => FastifyReply>() expect(reply.expires).type.toBe<(date?: Date) => FastifyReply>() expect(reply.etag('hello', 6000)).type.toBe() expect(reply.expires(new Date(Date.now() + 6000))).type.toBe() return { message: 'one' } }) fastify.get('/two', async (_request, reply) => { expect( reply.etag('hello', 6000).expires(new Date(Date.now() + 6000)) ).type.toBe() return { message: 'two' } }) // We register a new instance that should trigger a typescript error. const shouldErrorApp = Fastify({ logger: true }) const badCachingOptions = { privacy: fastifyCaching.privacy.PRIVATE, expiresIn: 'a string instead of a number of second', cacheSegment: 'fastify-caching', } expect(shouldErrorApp.register).type.not.toBeCallableWith(fastifyCaching, badCachingOptions) fastify.get('/three', async () => { expect(fastify.cache.get('well-known')).type.toBeAssignableTo>() expect(fastify.cache.get('well-known')).type.toBe< Promise<{ item: string; stored: number; ttl: number; } | null> >() const result = fastify.cache.get('well-known', (err, value) => { expect(err).type.toBe() expect(value).type.toBe<{ item: string; stored: number; ttl: number; } | null>() }) expect(result).type.toBe() return { message: 'two' } })