Skip to content

Commit

Permalink
Added simplified v3 types
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Heymann committed Apr 6, 2020
1 parent 924fa7c commit c7fc869
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 71 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.6.1",
"description": "Plugin helper for Fastify",
"main": "plugin.js",
"typings": "plugin.d.ts",
"types": "plugin.d.ts",
"scripts": {
"typescript": "tsc --project ./tsconfig.json",
"typescript": "tsd",
"test": "standard && tap test/*.js && npm run typescript"
},
"repository": {
Expand All @@ -29,6 +29,7 @@
"proxyquire": "^2.1.3",
"standard": "^14.0.0",
"tap": "^12.6.5",
"tsd": "^0.11.0",
"typescript": "^3.8.2"
},
"dependencies": {
Expand Down
21 changes: 8 additions & 13 deletions plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ import {
* 3. Pass some custom metadata of the plugin to Fastify
* @param fn Fastify plugin function
* @param options Optional plugin options
* @param next The `next` callback is not available when using `async`/`await`. If you do invoke a `next` callback in this situation unexpected behavior may occur.
*/
declare function fastifyPlugin<
Options extends FastifyPluginOptions,
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>
>(
fn: FastifyPlugin<Options, RawServer, RawRequest, RawReply>,
options?: PluginOptions | string,
next?: NextCallback
): FastifyPlugin<Options, RawServer, RawRequest, RawReply>;
export default function fp<Options>(
fn: FastifyPlugin<Options>,
options?: string,
): FastifyPlugin<Options>;
export default function fp<Options>(
fn: FastifyPlugin<Options>,
options?: Options & PluginOptions,
): FastifyPlugin<Options>;

export interface PluginOptions {
/** Bare-minimum version of Fastify for your plugin, just add the semver range that you need. */
Expand All @@ -44,5 +41,3 @@ export interface PluginOptions {
}

export type NextCallback = (err?: Error) => void;

export default fastifyPlugin
53 changes: 53 additions & 0 deletions plugin.test-d.ts
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
55 changes: 0 additions & 55 deletions test/types.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"module": "commonjs",
"noEmit": true,
"strict": true,
"noImplicitAny": true,
},
"files": [
"./test/types.test.ts"
Expand Down

0 comments on commit c7fc869

Please sign in to comment.