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