UNPKG

2.54 kBPlain TextView Raw
1import fp from './plugin';
2import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify';
3import { expectAssignable } from 'tsd'
4
5interface Options {
6 foo: string
7}
8
9const testSymbol = Symbol('foobar')
10
11// Callback
12
13const pluginCallback: FastifyPluginCallback = (fastify, options, next) => { }
14expectAssignable<FastifyPluginCallback>(fp(pluginCallback))
15
16const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }
17expectAssignable<FastifyPluginCallback>(fp(pluginCallbackWithTypes))
18
19expectAssignable<FastifyPluginCallback>(fp((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }))
20
21expectAssignable<FastifyPluginCallback>(fp(pluginCallback, '' ))
22expectAssignable<FastifyPluginCallback>(fp(pluginCallback, {
23 fastify: '',
24 name: '',
25 decorators: {
26 fastify: [ '', testSymbol ],
27 reply: [ '', testSymbol ],
28 request: [ '', testSymbol ]
29 },
30 dependencies: [ '' ]
31}))
32
33const pluginCallbackWithOptions: FastifyPluginCallback<Options> = (fastify, options, next) => {
34 expectAssignable<string>(options.foo)
35}
36
37expectAssignable<FastifyPluginCallback<Options>>(fp(pluginCallbackWithOptions))
38
39// Async
40
41const pluginAsync: FastifyPluginAsync = async (fastify, options) => { }
42expectAssignable<FastifyPluginAsync>(fp(pluginAsync))
43
44const pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }
45expectAssignable<FastifyPluginAsync>(fp(pluginAsyncWithTypes))
46
47expectAssignable<FastifyPluginAsync>(fp(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }))
48expectAssignable<FastifyPluginAsync>(fp(pluginAsync, '' ))
49expectAssignable<FastifyPluginAsync>(fp(pluginAsync, {
50 fastify: '',
51 name: '',
52 decorators: {
53 fastify: [ '', testSymbol ],
54 reply: [ '', testSymbol ],
55 request: [ '', testSymbol ]
56 },
57 dependencies: [ '' ]
58}))
59
60const pluginAsyncWithOptions: FastifyPluginAsync<Options> = async (fastify, options) => {
61 expectAssignable<string>(options.foo)
62}
63
64expectAssignable<FastifyPluginAsync<Options>>(fp(pluginAsyncWithOptions))
65
66// Fastify register
67
68const server = fastify()
69server.register(fp(pluginCallback))
70server.register(fp(pluginCallbackWithTypes))
71server.register(fp(pluginCallbackWithOptions))
72server.register(fp(pluginAsync))
73server.register(fp(pluginAsyncWithTypes))
74server.register(fp(pluginAsyncWithOptions))