-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Heymann
committed
Apr 6, 2020
1 parent
924fa7c
commit c7fc869
Showing
5 changed files
with
64 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import fp from './plugin'; | ||
import fastify, { FastifyPlugin } from 'fastify'; | ||
import { expectError } from 'tsd' | ||
|
||
export interface TestOptions { | ||
customNumber: number | ||
} | ||
|
||
export const testPluginWithOptions: FastifyPlugin<TestOptions> = fp( | ||
function (fastify, options, _next) { | ||
fastify.decorate('utility', () => options.customNumber) | ||
}, | ||
'>=1' | ||
); | ||
|
||
export const testPluginWithCallback: FastifyPlugin<TestOptions> = fp( | ||
function (fastify, _options, next) { | ||
fastify.decorate('utility', () => { }) | ||
next(); | ||
return; | ||
}, | ||
{ customNumber: 1 }, | ||
) | ||
|
||
export const testPluginWithAsync = fp<TestOptions>( | ||
async function (fastify, _options) { | ||
fastify.decorate('utility', () => { }) | ||
}, | ||
{ | ||
customNumber: 2, | ||
fastify: '>=1', | ||
name: 'TestPlugin', | ||
decorators: { | ||
request: ['log'] | ||
} | ||
} | ||
); | ||
|
||
// Register with HTTP | ||
const server = fastify() | ||
|
||
server.register(testPluginWithOptions) // register expects a FastifyPlugin | ||
server.register(testPluginWithCallback) | ||
server.register(testPluginWithAsync) | ||
|
||
// Register with HTTP2 | ||
const serverWithHttp2 = fastify({ http2: true }); | ||
|
||
serverWithHttp2.register(testPluginWithOptions) // register expects a FastifyPlugin | ||
serverWithHttp2.register(testPluginWithCallback) | ||
expectError(serverWithHttp2.register({ no: 'plugin' })) // register only accept valid plugins | ||
expectError(serverWithHttp2.register(testPluginWithAsync, { logLevel: 'invalid-log-level' })) // register options need to be valid built in fastify options | ||
expectError(serverWithHttp2.register(testPluginWithAsync, { customNumber: 'not-a-number' })) // or valid custom options defined by plugin itself |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters