1 | import fp from './plugin';
|
2 | import fastify, { FastifyPlugin } from 'fastify';
|
3 | import { expectError } from 'tsd'
|
4 |
|
5 | export interface TestOptions {
|
6 | customNumber: number
|
7 | }
|
8 |
|
9 | export const testPluginWithOptions: FastifyPlugin<TestOptions> = fp(
|
10 | function (fastify, options, _next) {
|
11 | fastify.decorate('utility', () => options.customNumber)
|
12 | },
|
13 | '>=1'
|
14 | );
|
15 |
|
16 | export const testPluginWithCallback: FastifyPlugin<TestOptions> = fp(
|
17 | function (fastify, _options, next) {
|
18 | fastify.decorate('utility', () => { })
|
19 | next();
|
20 | return;
|
21 | }
|
22 | )
|
23 |
|
24 | export const testPluginWithAsync = fp<TestOptions>(
|
25 | async function (fastify, _options) {
|
26 | fastify.decorate('utility', () => { })
|
27 | },
|
28 | {
|
29 | fastify: '>=1',
|
30 | name: 'TestPlugin',
|
31 | decorators: {
|
32 | request: ['log']
|
33 | }
|
34 | }
|
35 | );
|
36 |
|
37 |
|
38 | const server = fastify()
|
39 |
|
40 | server.register(testPluginWithOptions)
|
41 | server.register(testPluginWithCallback, { customNumber: 2 })
|
42 | server.register(testPluginWithAsync, { customNumber: 3 })
|
43 | expectError(server.register(testPluginWithCallback, { }))
|
44 | expectError(server.register(testPluginWithAsync, { customNumber: '2' }))
|
45 |
|
46 |
|
47 |
|
48 | const serverWithHttp2 = fastify({ http2: true });
|
49 |
|
50 | serverWithHttp2.register(testPluginWithOptions)
|
51 | serverWithHttp2.register(testPluginWithCallback)
|
52 | expectError(serverWithHttp2.register({ no: 'plugin' }))
|
53 | expectError(serverWithHttp2.register(testPluginWithAsync, { logLevel: 'invalid-log-level' }))
|
54 | expectError(serverWithHttp2.register(testPluginWithAsync, { customNumber: 'not-a-number' }))
|