Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: fastify/fastify-plugin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.5.0
Choose a base ref
...
head repository: fastify/fastify-plugin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.5.1
Choose a head ref
  • 9 commits
  • 8 files changed
  • 5 contributors

Commits on Jan 13, 2023

  1. Copy the full SHA
    a763060 View commit details

Commits on Mar 5, 2023

  1. Copy the full SHA
    606e2e9 View commit details

Commits on Mar 13, 2023

  1. build(deps-dev): bump tsd from 0.25.0 to 0.27.0 (#212)

    Bumps [tsd](https://github.com/SamVerschueren/tsd) from 0.25.0 to 0.27.0.
    - [Release notes](https://github.com/SamVerschueren/tsd/releases)
    - [Commits](tsdjs/tsd@v0.25.0...v0.27.0)
    
    ---
    updated-dependencies:
    - dependency-name: tsd
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Mar 13, 2023
    Copy the full SHA
    e309fcf View commit details

Commits on Mar 20, 2023

  1. build(deps-dev): bump tsd from 0.27.0 to 0.28.0 (#213)

    Bumps [tsd](https://github.com/SamVerschueren/tsd) from 0.27.0 to 0.28.0.
    - [Release notes](https://github.com/SamVerschueren/tsd/releases)
    - [Commits](tsdjs/tsd@v0.27.0...v0.28.0)
    
    ---
    updated-dependencies:
    - dependency-name: tsd
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Mar 20, 2023
    Copy the full SHA
    e866c12 View commit details

Commits on Apr 3, 2023

  1. build(deps-dev): bump @fastify/type-provider-typebox from 2.4.0 to 3.…

    …0.0 (#214)
    
    Bumps [@fastify/type-provider-typebox](https://github.com/fastify/fastify-type-provider-typebox) from 2.4.0 to 3.0.0.
    - [Release notes](https://github.com/fastify/fastify-type-provider-typebox/releases)
    - [Commits](fastify/fastify-type-provider-typebox@v2.4.0...v3.0.0)
    
    ---
    updated-dependencies:
    - dependency-name: "@fastify/type-provider-typebox"
      dependency-type: direct:development
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Apr 3, 2023
    Copy the full SHA
    782ea77 View commit details

Commits on Apr 28, 2023

  1. Copy the full SHA
    a8173f7 View commit details

Commits on May 8, 2023

  1. build(deps-dev): bump @types/node from 18.16.5 to 20.1.0 (#216)

    Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 18.16.5 to 20.1.0.
    - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
    - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)
    
    ---
    updated-dependencies:
    - dependency-name: "@types/node"
      dependency-type: direct:development
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored May 8, 2023
    Copy the full SHA
    9bfed09 View commit details

Commits on Jul 18, 2023

  1. fix inferring the correct fn type (#218)

    * simplify and make the issue aware by using expectType instead of expectAssignable
    
    * fix narrowing partially
    
    * fix typings
    
    * fix typings
    
    * fix
    
    * Apply suggestions from code review
    Uzlopak authored Jul 18, 2023
    Copy the full SHA
    17f1593 View commit details
  2. Bumped v4.5.1

    Signed-off-by: Matteo Collina <hello@matteocollina.com>
    mcollina committed Jul 18, 2023
    Copy the full SHA
    457c767 View commit details
Showing with 167 additions and 81 deletions.
  1. +5 −0 .github/workflows/ci.yml
  2. +1 −0 .gitignore
  3. +15 −15 README.md
  4. +4 −4 package.json
  5. +19 −0 types/example-async.test-d.ts
  6. +19 −0 types/example-callback.test-d.ts
  7. +10 −32 types/plugin.d.ts
  8. +94 −30 types/plugin.test-d.ts
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -2,6 +2,11 @@ name: CI

on:
push:
branches:
- main
- master
- next
- 'v*'
paths-ignore:
- 'docs/**'
- '*.md'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -139,6 +139,7 @@ dist
.clinic

# lock files
bun.lockb
package-lock.json
pnpm-lock.yaml
yarn.lock
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -23,9 +23,9 @@ Example using a callback:
```js
const fp = require('fastify-plugin')

module.exports = fp(function (fastify, opts, next) {
module.exports = fp(function (fastify, opts, done) {
// your plugin code
next()
done()
})
```

@@ -48,9 +48,9 @@ If you need to set a bare-minimum version of Fastify for your plugin, just add t
```js
const fp = require('fastify-plugin')

module.exports = fp(function (fastify, opts, next) {
module.exports = fp(function (fastify, opts, done) {
// your plugin code
next()
done()
}, { fastify: '4.x' })
```

@@ -64,9 +64,9 @@ Fastify uses this option to validate the dependency graph, allowing it to ensure
```js
const fp = require('fastify-plugin')

function plugin (fastify, opts, next) {
function plugin (fastify, opts, done) {
// your plugin code
next()
done()
}

module.exports = fp(plugin, {
@@ -81,9 +81,9 @@ You can also check if the `plugins` and `decorators` that your plugin intend to
```js
const fp = require('fastify-plugin')

function plugin (fastify, opts, next) {
function plugin (fastify, opts, done) {
// your plugin code
next()
done()
}

module.exports = fp(plugin, {
@@ -103,10 +103,10 @@ This allows you to set the plugin's name and validate its dependencies without m
```js
const fp = require('fastify-plugin')

function plugin (fastify, opts, next) {
function plugin (fastify, opts, done) {
// the decorator is not accessible outside this plugin
fastify.decorate('util', function() {})
next()
done()
}

module.exports = fp(plugin, {
@@ -132,13 +132,13 @@ The type definition would have to be updated to leverage this.
It is common for developers to inline their plugin with fastify-plugin such as:

```js
fp((fastify, opts, next) => { next() })
fp((fastify, opts, done) => { done() })
fp(async (fastify, opts) => { return })
```

TypeScript can sometimes infer the types of the arguments for these functions. Plugins in fastify are recommended to be typed using either `FastifyPluginCallback` or `FastifyPluginAsync`. These two definitions only differ in two ways:

1. The third argument `next` (the callback part)
1. The third argument `done` (the callback part)
2. The return type `FastifyPluginCallback` or `FastifyPluginAsync`

At this time, TypeScript inference is not smart enough to differentiate by definition argument length alone.
@@ -149,14 +149,14 @@ Thus, if you are a TypeScript developer please use on the following patterns ins
// Callback

// 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 => { })

// Async

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-plugin",
"version": "4.5.0",
"version": "4.5.1",
"description": "Plugin helper for Fastify",
"main": "plugin.js",
"types": "types/plugin.d.ts",
@@ -26,12 +26,12 @@
},
"homepage": "https://github.com/fastify/fastify-plugin#readme",
"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",
"proxyquire": "^2.1.3",
"standard": "^17.0.0",
"tap": "^16.0.1",
"tsd": "^0.25.0"
"tsd": "^0.28.0"
}
}
19 changes: 19 additions & 0 deletions types/example-async.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FastifyPluginAsync } from "fastify";

type FastifyExampleAsync = FastifyPluginAsync<fastifyExampleAsync.FastifyExampleAsyncOptions>;

declare namespace fastifyExampleAsync {

export interface FastifyExampleAsyncOptions {
foo?: 'bar'
}

export interface FastifyExampleAsyncPluginOptions extends FastifyExampleAsyncOptions {
}
export const fastifyExampleAsync: FastifyExampleAsync
export { fastifyExampleAsync as default }
}

declare function fastifyExampleAsync(...params: Parameters<FastifyExampleAsync>): ReturnType<FastifyExampleAsync>

export default fastifyExampleAsync
19 changes: 19 additions & 0 deletions types/example-callback.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FastifyPluginCallback } from "fastify";

type FastifyExampleCallback = FastifyPluginCallback<fastifyExampleCallback.FastifyExampleCallbackOptions>;

declare namespace fastifyExampleCallback {

export interface FastifyExampleCallbackOptions {
foo?: 'bar'
}

export interface FastifyExampleCallbackPluginOptions extends FastifyExampleCallbackOptions {
}
export const fastifyExampleCallback: FastifyExampleCallback
export { fastifyExampleCallback as default }
}

declare function fastifyExampleCallback(...params: Parameters<FastifyExampleCallback>): ReturnType<FastifyExampleCallback>

export default fastifyExampleCallback
42 changes: 10 additions & 32 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,9 @@ import {
RawServerDefault,
FastifyTypeProvider,
FastifyTypeProviderDefault,
FastifyBaseLogger,
} from 'fastify'
import { IncomingMessage, Server, ServerResponse } from 'http'

type FastifyPlugin = typeof fastifyPlugin

@@ -45,40 +47,16 @@ declare namespace fastifyPlugin {
* @param fn Fastify plugin function
* @param options Optional plugin options
*/
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
>(
fn: FastifyPluginAsync<Options, RawServer, TypeProvider>,
options?: string
): FastifyPluginAsync<Options, RawServer, TypeProvider>;

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
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: FastifyPluginCallback<Options>,
options?: string
): FastifyPluginCallback<Options>;
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;

export = fastifyPlugin
Loading