UNPKG

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