fastify-plugin
Advanced tools
Comparing version 4.5.0 to 4.5.1
{ | ||
"name": "fastify-plugin", | ||
"version": "4.5.0", | ||
"version": "4.5.1", | ||
"description": "Plugin helper for Fastify", | ||
@@ -29,4 +29,4 @@ "main": "plugin.js", | ||
"devDependencies": { | ||
"@fastify/type-provider-typebox": "^2.3.0", | ||
"@types/node": "^18.0.0", | ||
"@fastify/type-provider-typebox": "^3.0.0", | ||
"@types/node": "^20.1.0", | ||
"fastify": "^4.0.1", | ||
@@ -36,4 +36,4 @@ "proxyquire": "^2.1.3", | ||
"tap": "^16.0.1", | ||
"tsd": "^0.25.0" | ||
"tsd": "^0.28.0" | ||
} | ||
} |
@@ -26,5 +26,5 @@ # fastify-plugin | ||
module.exports = fp(function (fastify, opts, next) { | ||
module.exports = fp(function (fastify, opts, done) { | ||
// your plugin code | ||
next() | ||
done() | ||
}) | ||
@@ -52,5 +52,5 @@ ``` | ||
module.exports = fp(function (fastify, opts, next) { | ||
module.exports = fp(function (fastify, opts, done) { | ||
// your plugin code | ||
next() | ||
done() | ||
}, { fastify: '4.x' }) | ||
@@ -69,5 +69,5 @@ ``` | ||
function plugin (fastify, opts, next) { | ||
function plugin (fastify, opts, done) { | ||
// your plugin code | ||
next() | ||
done() | ||
} | ||
@@ -87,5 +87,5 @@ | ||
function plugin (fastify, opts, next) { | ||
function plugin (fastify, opts, done) { | ||
// your plugin code | ||
next() | ||
done() | ||
} | ||
@@ -110,6 +110,6 @@ | ||
function plugin (fastify, opts, next) { | ||
function plugin (fastify, opts, done) { | ||
// the decorator is not accessible outside this plugin | ||
fastify.decorate('util', function() {}) | ||
next() | ||
done() | ||
} | ||
@@ -140,3 +140,3 @@ | ||
```js | ||
fp((fastify, opts, next) => { next() }) | ||
fp((fastify, opts, done) => { done() }) | ||
fp(async (fastify, opts) => { return }) | ||
@@ -147,3 +147,3 @@ ``` | ||
1. The third argument `next` (the callback part) | ||
1. The third argument `done` (the callback part) | ||
2. The return type `FastifyPluginCallback` or `FastifyPluginAsync` | ||
@@ -159,10 +159,10 @@ | ||
// Assign type directly | ||
const pluginCallback: FastifyPluginCallback = (fastify, options, next) => { } | ||
const pluginCallback: FastifyPluginCallback = (fastify, options, done) => { } | ||
fp(pluginCallback) | ||
// or define your own function declaration that satisfies the existing definitions | ||
const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { } | ||
const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void => { } | ||
fp(pluginCallbackWithTypes) | ||
// or inline | ||
fp((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }) | ||
fp((fastify: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void => { }) | ||
@@ -169,0 +169,0 @@ // Async |
@@ -11,3 +11,5 @@ /// <reference types="fastify" /> | ||
FastifyTypeProviderDefault, | ||
FastifyBaseLogger, | ||
} from 'fastify' | ||
import { IncomingMessage, Server, ServerResponse } from 'http' | ||
@@ -49,38 +51,14 @@ type FastifyPlugin = typeof fastifyPlugin | ||
*/ | ||
declare function fastifyPlugin< | ||
Options extends FastifyPluginOptions, | ||
RawServer extends RawServerBase = RawServerDefault, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
>( | ||
fn: FastifyPluginAsync<Options, RawServer, TypeProvider>, | ||
options?: fastifyPlugin.PluginMetadata | ||
): FastifyPluginAsync<Options, RawServer, TypeProvider>; | ||
declare function fastifyPlugin< | ||
Options extends FastifyPluginOptions, | ||
RawServer extends RawServerBase = RawServerDefault, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
Options extends FastifyPluginOptions = Record<never, never>, | ||
RawServer extends RawServerBase = RawServerDefault, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
Fn extends FastifyPluginCallback<Options, RawServer, TypeProvider, Logger> | FastifyPluginAsync<Options, RawServer, TypeProvider, Logger> = FastifyPluginCallback<Options, RawServer, TypeProvider, Logger> | ||
>( | ||
fn: FastifyPluginAsync<Options, RawServer, TypeProvider>, | ||
options?: string | ||
): FastifyPluginAsync<Options, RawServer, TypeProvider>; | ||
fn: Fn extends unknown ? Fn extends (...args: any) => Promise<any> ? FastifyPluginAsync<Options, RawServer, TypeProvider, Logger> : FastifyPluginCallback<Options, RawServer, TypeProvider, Logger> : Fn, | ||
options?: fastifyPlugin.PluginMetadata | string | ||
): Fn; | ||
declare function fastifyPlugin< | ||
Options extends FastifyPluginOptions, | ||
RawServer extends RawServerBase = RawServerDefault, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
>( | ||
fn: FastifyPluginCallback<Options, RawServer, TypeProvider>, | ||
options?: fastifyPlugin.PluginMetadata | ||
): FastifyPluginCallback<Options, RawServer, TypeProvider>; | ||
declare function fastifyPlugin< | ||
Options extends FastifyPluginOptions, | ||
RawServer extends RawServerBase = RawServerDefault, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault | ||
>( | ||
fn: FastifyPluginCallback<Options>, | ||
options?: string | ||
): FastifyPluginCallback<Options>; | ||
export = fastifyPlugin |
import fastifyPlugin from '..'; | ||
import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify'; | ||
import { expectAssignable } from 'tsd' | ||
import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger } from 'fastify'; | ||
import { expectAssignable, expectError, expectNotType, expectType } from 'tsd' | ||
import { Server } from "node:https" | ||
import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox" | ||
import fastifyExampleCallback from './example-callback.test-d'; | ||
import fastifyExampleAsync from './example-async.test-d'; | ||
@@ -16,19 +18,21 @@ interface Options { | ||
const pluginCallback: FastifyPluginCallback = (fastify, options, next) => { } | ||
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback)) | ||
expectType<FastifyPluginCallback>(fastifyPlugin(pluginCallback)) | ||
const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { } | ||
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallbackWithTypes)) | ||
expectNotType<any>(fastifyPlugin(pluginCallbackWithTypes)) | ||
expectAssignable<FastifyPluginCallback>(fastifyPlugin((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { })) | ||
expectNotType<any>(fastifyPlugin((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { })) | ||
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback, '' )) | ||
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback, { | ||
expectType<FastifyPluginCallback>(fastifyPlugin(pluginCallback, '')) | ||
expectType<FastifyPluginCallback>(fastifyPlugin(pluginCallback, { | ||
fastify: '', | ||
name: '', | ||
decorators: { | ||
fastify: [ '', testSymbol ], | ||
reply: [ '', testSymbol ], | ||
request: [ '', testSymbol ] | ||
fastify: ['', testSymbol], | ||
reply: ['', testSymbol], | ||
request: ['', testSymbol] | ||
}, | ||
dependencies: [ '' ], | ||
dependencies: [''], | ||
encapsulate: true | ||
@@ -38,16 +42,16 @@ })) | ||
const pluginCallbackWithOptions: FastifyPluginCallback<Options> = (fastify, options, next) => { | ||
expectAssignable<string>(options.foo) | ||
expectType<string>(options.foo) | ||
} | ||
expectAssignable<FastifyPluginCallback<Options>>(fastifyPlugin(pluginCallbackWithOptions)) | ||
expectType<FastifyPluginCallback<Options>>(fastifyPlugin(pluginCallbackWithOptions)) | ||
const pluginCallbackWithServer: FastifyPluginCallback<Options, Server> = (fastify, options, next) => { | ||
expectAssignable<Server>(fastify.server) | ||
expectType<Server>(fastify.server) | ||
} | ||
expectAssignable<FastifyPluginCallback<Options, Server>>(fastifyPlugin(pluginCallbackWithServer)) | ||
expectType<FastifyPluginCallback<Options, Server>>(fastifyPlugin(pluginCallbackWithServer)) | ||
const pluginCallbackWithTypeProvider: FastifyPluginCallback<Options, Server, TypeBoxTypeProvider> = (fastify, options, next) => {} | ||
const pluginCallbackWithTypeProvider: FastifyPluginCallback<Options, Server, TypeBoxTypeProvider> = (fastify, options, next) => { } | ||
expectAssignable<FastifyPluginCallback<Options, Server, TypeBoxTypeProvider>>(fastifyPlugin(pluginCallbackWithTypeProvider)) | ||
expectType<FastifyPluginCallback<Options, Server, TypeBoxTypeProvider>>(fastifyPlugin(pluginCallbackWithTypeProvider)) | ||
@@ -57,18 +61,18 @@ // Async | ||
const pluginAsync: FastifyPluginAsync = async (fastify, options) => { } | ||
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync)) | ||
expectType<FastifyPluginAsync>(fastifyPlugin(pluginAsync)) | ||
const pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { } | ||
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsyncWithTypes)) | ||
expectType<FastifyPluginAsync<FastifyPluginOptions, RawServerDefault, FastifyTypeProviderDefault>>(fastifyPlugin(pluginAsyncWithTypes)) | ||
expectAssignable<FastifyPluginAsync>(fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { })) | ||
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync, '' )) | ||
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync, { | ||
expectType<FastifyPluginAsync<FastifyPluginOptions, RawServerDefault, FastifyTypeProviderDefault>>(fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { })) | ||
expectType<FastifyPluginAsync>(fastifyPlugin(pluginAsync, '')) | ||
expectType<FastifyPluginAsync>(fastifyPlugin(pluginAsync, { | ||
fastify: '', | ||
name: '', | ||
decorators: { | ||
fastify: [ '', testSymbol ], | ||
reply: [ '', testSymbol ], | ||
request: [ '', testSymbol ] | ||
fastify: ['', testSymbol], | ||
reply: ['', testSymbol], | ||
request: ['', testSymbol] | ||
}, | ||
dependencies: [ '' ], | ||
dependencies: [''], | ||
encapsulate: true | ||
@@ -78,16 +82,16 @@ })) | ||
const pluginAsyncWithOptions: FastifyPluginAsync<Options> = async (fastify, options) => { | ||
expectAssignable<string>(options.foo) | ||
expectType<string>(options.foo) | ||
} | ||
expectAssignable<FastifyPluginAsync<Options>>(fastifyPlugin(pluginAsyncWithOptions)) | ||
expectType<FastifyPluginAsync<Options>>(fastifyPlugin(pluginAsyncWithOptions)) | ||
const pluginAsyncWithServer: FastifyPluginAsync<Options, Server> = async (fastify, options) => { | ||
expectAssignable<Server>(fastify.server) | ||
expectType<Server>(fastify.server) | ||
} | ||
expectAssignable<FastifyPluginAsync<Options, Server>>(fastifyPlugin(pluginAsyncWithServer)) | ||
expectType<FastifyPluginAsync<Options, Server>>(fastifyPlugin(pluginAsyncWithServer)) | ||
const pluginAsyncWithTypeProvider: FastifyPluginAsync<Options, Server, TypeBoxTypeProvider> = async (fastify, options) => {} | ||
const pluginAsyncWithTypeProvider: FastifyPluginAsync<Options, Server, TypeBoxTypeProvider> = async (fastify, options) => { } | ||
expectAssignable<FastifyPluginAsync<Options, Server, TypeBoxTypeProvider>>(fastifyPlugin(pluginAsyncWithTypeProvider)) | ||
expectType<FastifyPluginAsync<Options, Server, TypeBoxTypeProvider>>(fastifyPlugin(pluginAsyncWithTypeProvider)) | ||
@@ -107,1 +111,61 @@ // Fastify register | ||
server.register(fastifyPlugin(pluginAsyncWithTypeProvider)) | ||
// properly handling callback and async | ||
fastifyPlugin(function (fastify, options, next) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Record<never, never>>(options) | ||
expectType<(err?: Error) => void>(next) | ||
}) | ||
fastifyPlugin<Options>(function (fastify, options, next) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Options>(options) | ||
expectType<(err?: Error) => void>(next) | ||
}) | ||
fastifyPlugin<Options>(async function (fastify, options) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Options>(options) | ||
}) | ||
expectAssignable<FastifyPluginAsync<Options, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger>>(fastifyPlugin(async function (fastify: FastifyInstance, options: Options) { })) | ||
expectNotType<any>(fastifyPlugin(async function (fastify: FastifyInstance, options: Options) { })) | ||
fastifyPlugin(async function (fastify, options: Options) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Options>(options) | ||
}) | ||
fastifyPlugin(async function (fastify, options) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Record<never, never>>(options) | ||
}) | ||
expectError( | ||
fastifyPlugin(async function (fastify, options: Options, next) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Options>(options) | ||
}) | ||
) | ||
expectAssignable<FastifyPluginCallback<Options>>(fastifyPlugin(function (fastify, options, next) { })) | ||
expectNotType<any>(fastifyPlugin(function (fastify, options, next) { })) | ||
fastifyPlugin(function (fastify, options: Options, next) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Options>(options) | ||
expectType<(err?: Error) => void>(next) | ||
}) | ||
expectError( | ||
fastifyPlugin(function (fastify, options: Options, next) { | ||
expectType<FastifyInstance>(fastify) | ||
expectType<Options>(options) | ||
return Promise.resolve() | ||
}) | ||
) | ||
server.register(fastifyExampleCallback, { foo: 'bar' }) | ||
expectError(server.register(fastifyExampleCallback, { foo: 'baz' })) | ||
server.register(fastifyExampleAsync, { foo: 'bar' }) | ||
expectError(server.register(fastifyExampleAsync, { foo: 'baz' })) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40252
23
866