|
1 | | -import Fastify, { FastifyReply } from 'fastify'; |
2 | | -import { expectAssignable, expectError, expectType } from 'tsd'; |
| 1 | +import Fastify, { FastifyReply } from 'fastify' |
| 2 | +import { expectAssignable, expectError, expectType } from 'tsd' |
3 | 3 | import fastifyCaching, { |
4 | 4 | AbstractCacheCompliantObject, |
5 | 5 | FastifyCachingPluginOptions, |
6 | | -} from '..'; |
| 6 | +} from '..' |
7 | 7 |
|
8 | | -const fastify = Fastify({ logger: true }); |
| 8 | +const fastify = Fastify({ logger: true }) |
9 | 9 |
|
10 | 10 | const cachingOptions: FastifyCachingPluginOptions = { |
11 | 11 | privacy: fastifyCaching.privacy.PUBLIC, |
12 | 12 | expiresIn: 300, |
13 | 13 | cacheSegment: 'fastify-caching', |
14 | | -}; |
15 | | -expectAssignable<FastifyCachingPluginOptions>(cachingOptions); |
| 14 | +} |
| 15 | +expectAssignable<FastifyCachingPluginOptions>(cachingOptions) |
16 | 16 |
|
17 | | -fastify.register(fastifyCaching, cachingOptions); |
| 17 | +fastify.register(fastifyCaching, cachingOptions) |
18 | 18 |
|
19 | | -expectType<AbstractCacheCompliantObject>(fastify.cache); |
20 | | -expectType<AbstractCacheCompliantObject['get']>(fastify.cache.get); |
21 | | -expectType<AbstractCacheCompliantObject['set']>(fastify.cache.set); |
22 | | -expectType<string>(fastify.cacheSegment); |
| 19 | +expectType<AbstractCacheCompliantObject>(fastify.cache) |
| 20 | +expectType<AbstractCacheCompliantObject['get']>(fastify.cache.get) |
| 21 | +expectType<AbstractCacheCompliantObject['set']>(fastify.cache.set) |
| 22 | +expectType<string>(fastify.cacheSegment) |
23 | 23 | // expectType<number>(fastify.etagMaxLife); |
24 | 24 |
|
25 | 25 | fastify.get('/one', async (request, reply) => { |
26 | | - expectType<(tag?: string, timeToLive?: number) => FastifyReply>(reply.etag); |
27 | | - expectType<(date?: Date) => FastifyReply>(reply.expires); |
| 26 | + expectType<(tag?: string, timeToLive?: number) => FastifyReply>(reply.etag) |
| 27 | + expectType<(date?: Date) => FastifyReply>(reply.expires) |
28 | 28 |
|
29 | | - expectType<FastifyReply>(reply.etag('hello', 6000)); |
30 | | - expectType<FastifyReply>(reply.expires(new Date(Date.now() + 6000))); |
| 29 | + expectType<FastifyReply>(reply.etag('hello', 6000)) |
| 30 | + expectType<FastifyReply>(reply.expires(new Date(Date.now() + 6000))) |
31 | 31 |
|
32 | | - return { message: 'one' }; |
33 | | -}); |
| 32 | + return { message: 'one' } |
| 33 | +}) |
34 | 34 |
|
35 | 35 | fastify.get('/two', async (request, reply) => { |
36 | 36 | expectType<FastifyReply>( |
37 | 37 | reply.etag('hello', 6000).expires(new Date(Date.now() + 6000)) |
38 | | - ); |
| 38 | + ) |
39 | 39 |
|
40 | | - return { message: 'two' }; |
41 | | -}); |
| 40 | + return { message: 'two' } |
| 41 | +}) |
42 | 42 |
|
43 | 43 | // We register a new instance that should trigger a typescript error. |
44 | | -const shouldErrorApp = Fastify({ logger: true }); |
| 44 | +const shouldErrorApp = Fastify({ logger: true }) |
45 | 45 |
|
46 | 46 | const badCachingOptions = { |
47 | 47 | privacy: fastifyCaching.privacy.PRIVATE, |
48 | 48 | expiresIn: 'a string instead of a number of second', |
49 | 49 | cacheSegment: 'fastify-caching', |
50 | | -}; |
| 50 | +} |
51 | 51 |
|
52 | | -expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions)); |
| 52 | +expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions)) |
53 | 53 |
|
54 | 54 | fastify.get('/three', async (request, reply) => { |
55 | 55 | expectAssignable<Promise<unknown>>( |
56 | 56 | fastify.cache.get('well-known') |
57 | | - ); |
| 57 | + ) |
58 | 58 | expectAssignable<Promise<{ item: string; stored: number; ttl: number; } | null>>( |
59 | 59 | fastify.cache.get<string>('well-known') |
60 | | - ); |
| 60 | + ) |
61 | 61 | expectType<void>( |
62 | 62 | fastify.cache.get<string>('well-known', (err, value) => { |
63 | | - expectType<unknown>(err); |
64 | | - expectAssignable<{ item: string; stored: number; ttl: number; } | null>(value); |
| 63 | + expectType<unknown>(err) |
| 64 | + expectAssignable<{ item: string; stored: number; ttl: number; } | null>(value) |
65 | 65 | }) |
66 | | - ); |
| 66 | + ) |
67 | 67 |
|
68 | | - return { message: 'two' }; |
69 | | -}); |
| 68 | + return { message: 'two' } |
| 69 | +}) |
0 commit comments