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.3.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.4.0
Choose a head ref
  • 6 commits
  • 14 files changed
  • 2 contributors

Commits on Nov 30, 2022

  1. fix unit test (#202)

    remove semver
    improve npm scripts
    activate linting on ci
    Uzlopak authored Nov 30, 2022
    Copy the full SHA
    61c2e70 View commit details

Commits on Dec 1, 2022

  1. rename stackParser.js to extactPluginName.js (#201)

    rename stackParser.js to extractPluginName.js and move it to lib-folder
    Uzlopak authored Dec 1, 2022
    Copy the full SHA
    2474543 View commit details
  2. Copy the full SHA
    f2be4e1 View commit details
  3. Copy the full SHA
    5cb68c5 View commit details

Commits on Dec 3, 2022

  1. nodenext compatibility (#204)

    * nodenext compatibility
    
    * add missing newline
    Uzlopak authored Dec 3, 2022
    Copy the full SHA
    1c20b66 View commit details
  2. Bumped v4.4.0

    Signed-off-by: Matteo Collina <hello@matteocollina.com>
    mcollina committed Dec 3, 2022
    Copy the full SHA
    c22f021 View commit details
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -14,4 +14,5 @@ jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
with:
lint: true
license-check: true
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 3 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -2,3 +2,6 @@ ts: false
jsx: false
flow: false
coverage: true

files:
- test/**/*.js
13 changes: 12 additions & 1 deletion stackParser.js → lib/getPluginName.js
Original file line number Diff line number Diff line change
@@ -3,9 +3,20 @@
const fpStackTracePattern = /at\s{1}(?:.*\.)?plugin\s{1}.*\n\s*(.*)/
const fileNamePattern = /(\w*(\.\w*)*)\..*/

module.exports = function extractPluginName (stack) {
module.exports = function getPluginName (fn) {
if (fn.name.length > 0) return fn.name

try {
throw new Error('anonymous function')
} catch (e) {
return extractPluginName(e.stack)
}
}

function extractPluginName (stack) {
const m = stack.match(fpStackTracePattern)

// get last section of path and match for filename
return m ? m[1].split(/[/\\]/).slice(-1)[0].match(fileNamePattern)[1] : 'anonymous'
}
module.exports.extractPluginName = extractPluginName
11 changes: 11 additions & 0 deletions lib/toCamelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

module.exports = function toCamelCase (name) {
if (name[0] === '@') {
name = name.slice(1).replace('/', '-')
}
const newName = name.replace(/-(.)/g, function (match, g1) {
return g1.toUpperCase()
})
return newName
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "fastify-plugin",
"version": "4.3.0",
"version": "4.4.0",
"description": "Plugin helper for Fastify",
"main": "plugin.js",
"types": "plugin.d.ts",
"types": "types/plugin.d.ts",
"scripts": {
"test": "standard && tap test/*.js test/esm/*.js --no-check-coverage && npm run typescript",
"test:ci": "standard && tap test/*.js test/esm/*.js --no-check-coverage --coverage-report=lcovonly && npm run typescript",
"typescript": "tsd"
"lint": "standard",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
},
"repository": {
"type": "git",
@@ -29,7 +30,6 @@
"@types/node": "^18.0.0",
"fastify": "^4.0.1",
"proxyquire": "^2.1.3",
"semver": "^7.3.7",
"standard": "^17.0.0",
"tap": "^16.0.1",
"tsd": "^0.24.1"
73 changes: 0 additions & 73 deletions plugin.d.ts

This file was deleted.

28 changes: 5 additions & 23 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const extractPluginName = require('./stackParser')
const getPluginName = require('./lib/getPluginName')
const toCamelCase = require('./lib/toCamelCase')

let count = 0

@@ -38,7 +39,7 @@ function plugin (fn, options = {}) {

if (!options.name) {
autoName = true
options.name = checkName(fn) + '-auto-' + count++
options.name = getPluginName(fn) + '-auto-' + count++
}

fn[Symbol.for('skip-override')] = options.encapsulate !== true
@@ -61,25 +62,6 @@ function plugin (fn, options = {}) {
return fn
}

function checkName (fn) {
if (fn.name.length > 0) return fn.name

try {
throw new Error('anonymous function')
} catch (e) {
return extractPluginName(e.stack)
}
}

function toCamelCase (name) {
if (name[0] === '@') {
name = name.slice(1).replace('/', '-')
}
const newName = name.replace(/-(.)/g, function (match, g1) {
return g1.toUpperCase()
})
return newName
}

plugin.default = plugin
module.exports = plugin
module.exports.default = plugin
module.exports.fastifyPlugin = plugin
24 changes: 8 additions & 16 deletions test/esm/index.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
'use strict'

const t = require('tap')
const semver = require('semver')

if (semver.lt(process.versions.node, '13.3.0')) {
t.skip('Skip because Node version <= 13.3.0')
t.end()
} else {
// Node v8 throw a `SyntaxError: Unexpected token import`
// even if this branch is never touch in the code,
// by using `eval` we can avoid this issue.
// eslint-disable-next-line
new Function('module', 'return import(module)')('./esm.mjs').catch((err) => {
process.nextTick(() => {
throw err
})
// Node v8 throw a `SyntaxError: Unexpected token import`
// even if this branch is never touch in the code,
// by using `eval` we can avoid this issue.
// eslint-disable-next-line
new Function('module', 'return import(module)')('./esm.mjs').catch((err) => {
process.nextTick(() => {
throw err
})
}
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const t = require('tap')
const extractPluginName = require('../stackParser')
const extractPluginName = require('../lib/getPluginName').extractPluginName

const winStack = `Error: anonymous function
at checkName (C:\\Users\\leonardo.davinci\\Desktop\\fastify-plugin\\index.js:43:11)
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -299,7 +299,7 @@ test('should check version when encapsulated', t => {
}))

fastify.ready(err => {
t.match(err.message, /fastify-plugin: test - expected '<=2.10.0' fastify version, '\d.\d.\d' is installed/)
t.match(err.message, /fastify-plugin: test - expected '<=2.10.0' fastify version, '\d.\d+.\d+' is installed/)
})
})

24 changes: 24 additions & 0 deletions test/toCamelCase.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const { test } = require('tap')
const toCamelCase = require('../lib/toCamelCase')

test('from kebab-case to camelCase', (t) => {
t.plan(1)
t.equal(toCamelCase('hello-world'), 'helloWorld')
})

test('from @-prefixed named imports', (t) => {
t.plan(1)
t.equal(toCamelCase('@hello/world'), 'helloWorld')
})

test('from @-prefixed named kebab-case to camelCase', (t) => {
t.plan(1)
t.equal(toCamelCase('@hello/my-world'), 'helloMyWorld')
})

test('from kebab-case to camelCase multiple words', (t) => {
t.plan(1)
t.equal(toCamelCase('hello-long-world'), 'helloLongWorld')
})
84 changes: 84 additions & 0 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/// <reference types="fastify" />

import {
FastifyPluginCallback,
FastifyPluginAsync,
FastifyPluginOptions,
RawServerBase,
RawServerDefault,
FastifyTypeProvider,
FastifyTypeProviderDefault,
} from 'fastify'

type FastifyPlugin = typeof fastifyPlugin

declare namespace fastifyPlugin {
export interface PluginMetadata {
/** Bare-minimum version of Fastify for your plugin, just add the semver range that you need. */
fastify?: string,
name?: string,
/** Decorator dependencies for this plugin */
decorators?: {
fastify?: (string | symbol)[],
reply?: (string | symbol)[],
request?: (string | symbol)[]
},
/** The plugin dependencies */
dependencies?: string[],
encapsulate?: boolean
}
// Exporting PluginOptions for backward compatibility after renaming it to PluginMetadata
/**
* @deprecated Use PluginMetadata instead
*/
export interface PluginOptions extends PluginMetadata {}

export const fastifyPlugin: FastifyPlugin
export { fastifyPlugin as default }
}

/**
* This function does three things for you:
* 1. Add the `skip-override` hidden property
* 2. Check bare-minimum version of Fastify
* 3. Pass some custom metadata of the plugin to Fastify
* @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
>(
fn: FastifyPluginCallback<Options>,
options?: string
): FastifyPluginCallback<Options>;

export = fastifyPlugin
54 changes: 27 additions & 27 deletions plugin.test-d.ts → types/plugin.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fp from './plugin';
import fastifyPlugin from '..';
import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify';
import { expectAssignable } from 'tsd'
import { Server } from "node:https"
@@ -13,15 +13,15 @@ const testSymbol = Symbol('foobar')
// Callback

const pluginCallback: FastifyPluginCallback = (fastify, options, next) => { }
expectAssignable<FastifyPluginCallback>(fp(pluginCallback))
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback))

const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }
expectAssignable<FastifyPluginCallback>(fp(pluginCallbackWithTypes))
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallbackWithTypes))

expectAssignable<FastifyPluginCallback>(fp((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }))
expectAssignable<FastifyPluginCallback>(fastifyPlugin((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }))

expectAssignable<FastifyPluginCallback>(fp(pluginCallback, '' ))
expectAssignable<FastifyPluginCallback>(fp(pluginCallback, {
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback, '' ))
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback, {
fastify: '',
name: '',
decorators: {
@@ -37,29 +37,29 @@ const pluginCallbackWithOptions: FastifyPluginCallback<Options> = (fastify, opti
expectAssignable<string>(options.foo)
}

expectAssignable<FastifyPluginCallback<Options>>(fp(pluginCallbackWithOptions))
expectAssignable<FastifyPluginCallback<Options>>(fastifyPlugin(pluginCallbackWithOptions))

const pluginCallbackWithServer: FastifyPluginCallback<Options, Server> = (fastify, options, next) => {
expectAssignable<Server>(fastify.server)
}

expectAssignable<FastifyPluginCallback<Options, Server>>(fp(pluginCallbackWithServer))
expectAssignable<FastifyPluginCallback<Options, Server>>(fastifyPlugin(pluginCallbackWithServer))

const pluginCallbackWithTypeProvider: FastifyPluginCallback<Options, Server, TypeBoxTypeProvider> = (fastify, options, next) => {}

expectAssignable<FastifyPluginCallback<Options, Server, TypeBoxTypeProvider>>(fp(pluginCallbackWithTypeProvider))
expectAssignable<FastifyPluginCallback<Options, Server, TypeBoxTypeProvider>>(fastifyPlugin(pluginCallbackWithTypeProvider))

// Async

const pluginAsync: FastifyPluginAsync = async (fastify, options) => { }
expectAssignable<FastifyPluginAsync>(fp(pluginAsync))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync))

const pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }
expectAssignable<FastifyPluginAsync>(fp(pluginAsyncWithTypes))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsyncWithTypes))

expectAssignable<FastifyPluginAsync>(fp(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }))
expectAssignable<FastifyPluginAsync>(fp(pluginAsync, '' ))
expectAssignable<FastifyPluginAsync>(fp(pluginAsync, {
expectAssignable<FastifyPluginAsync>(fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync, '' ))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync, {
fastify: '',
name: '',
decorators: {
@@ -75,28 +75,28 @@ const pluginAsyncWithOptions: FastifyPluginAsync<Options> = async (fastify, opti
expectAssignable<string>(options.foo)
}

expectAssignable<FastifyPluginAsync<Options>>(fp(pluginAsyncWithOptions))
expectAssignable<FastifyPluginAsync<Options>>(fastifyPlugin(pluginAsyncWithOptions))

const pluginAsyncWithServer: FastifyPluginAsync<Options, Server> = async (fastify, options) => {
expectAssignable<Server>(fastify.server)
}

expectAssignable<FastifyPluginAsync<Options, Server>>(fp(pluginAsyncWithServer))
expectAssignable<FastifyPluginAsync<Options, Server>>(fastifyPlugin(pluginAsyncWithServer))

const pluginAsyncWithTypeProvider: FastifyPluginAsync<Options, Server, TypeBoxTypeProvider> = async (fastify, options) => {}

expectAssignable<FastifyPluginAsync<Options, Server, TypeBoxTypeProvider>>(fp(pluginAsyncWithTypeProvider))
expectAssignable<FastifyPluginAsync<Options, Server, TypeBoxTypeProvider>>(fastifyPlugin(pluginAsyncWithTypeProvider))

// Fastify register

const server = fastify()
server.register(fp(pluginCallback))
server.register(fp(pluginCallbackWithTypes))
server.register(fp(pluginCallbackWithOptions))
server.register(fp(pluginCallbackWithServer))
server.register(fp(pluginCallbackWithTypeProvider))
server.register(fp(pluginAsync))
server.register(fp(pluginAsyncWithTypes))
server.register(fp(pluginAsyncWithOptions))
server.register(fp(pluginAsyncWithServer))
server.register(fp(pluginAsyncWithTypeProvider))
server.register(fastifyPlugin(pluginCallback))
server.register(fastifyPlugin(pluginCallbackWithTypes))
server.register(fastifyPlugin(pluginCallbackWithOptions))
server.register(fastifyPlugin(pluginCallbackWithServer))
server.register(fastifyPlugin(pluginCallbackWithTypeProvider))
server.register(fastifyPlugin(pluginAsync))
server.register(fastifyPlugin(pluginAsyncWithTypes))
server.register(fastifyPlugin(pluginAsyncWithOptions))
server.register(fastifyPlugin(pluginAsyncWithServer))
server.register(fastifyPlugin(pluginAsyncWithTypeProvider))