diff --git a/index.js b/index.js index 5f9f38c..fd9013e 100644 --- a/index.js +++ b/index.js @@ -3,30 +3,31 @@ const semver = require('semver') const console = require('console') -const DISPLAY_NAME_SYMBOL = Symbol.for('fastify.display-name') const fpStackTracePattern = new RegExp('at\\s{1}(?:.*\\.)?plugin\\s{1}.*\\n\\s*(.*)') const fileNamePattern = new RegExp('(?:\\/|\\\\)(\\w*(\\.\\w*)*)\\..*') -function plugin (fn, options) { +function plugin (fn, options = {}) { if (typeof fn !== 'function') { throw new TypeError(`fastify-plugin expects a function, instead got a '${typeof fn}'`) } - fn[DISPLAY_NAME_SYMBOL] = checkName(fn) - fn[Symbol.for('skip-override')] = true - if (options === undefined) return fn - if (typeof options === 'string') { checkVersion(options) - return fn + options = {} } if (typeof options !== 'object' || Array.isArray(options) || options === null) { throw new TypeError('The options object should be an object') } + if (!options.name) { + options.name = checkName(fn) + } + + fn[Symbol.for('fastify.display-name')] = options.name + if (options.fastify) { checkVersion(options.fastify) delete options.fastify diff --git a/test/composite.test.js b/test/composite.test.js index 2976908..75e8759 100644 --- a/test/composite.test.js +++ b/test/composite.test.js @@ -5,11 +5,12 @@ const test = t.test const fp = require('./../') test('anonymous function should be named composite.test', t => { - t.plan(1) + t.plan(2) const fn = fp((fastify, opts, next) => { next() }) + t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test') t.is(fn[Symbol.for('fastify.display-name')], 'composite.test') }) diff --git a/test/composite.test.ts b/test/composite.test.ts index a406c86..31b313c 100644 --- a/test/composite.test.ts +++ b/test/composite.test.ts @@ -4,11 +4,12 @@ import * as tap from 'tap' const test = tap.test test('anonymous function should be named composite.test', (t: any) => { - t.plan(1) + t.plan(2) const fn = fp((fastify, opts, next) => { next() }) + t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test') t.is(fn[Symbol.for('fastify.display-name')], 'composite.test') }) diff --git a/test/composite.ts b/test/composite.ts index ffce061..e0dfe8a 100755 --- a/test/composite.ts +++ b/test/composite.ts @@ -4,11 +4,12 @@ import * as tap from 'tap' const test = tap.test test('anonymous function should be named composite.test', (t: any) => { - t.plan(1) + t.plan(2) const fn = fp((fastify, opts, next) => { next() }) + t.is(fn[Symbol.for('plugin-meta')].name, 'composite') t.is(fn[Symbol.for('fastify.display-name')], 'composite') }) diff --git a/test/mu1tip1e.composite.test.js b/test/mu1tip1e.composite.test.js index 5b3df70..7e0b68c 100644 --- a/test/mu1tip1e.composite.test.js +++ b/test/mu1tip1e.composite.test.js @@ -5,11 +5,12 @@ const test = t.test const fp = require('./../') test('anonymous function should be named mu1tip1e.composite.test', t => { - t.plan(1) + t.plan(2) const fn = fp((fastify, opts, next) => { next() }) + t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test') t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test') }) diff --git a/test/mu1tip1e.composite.test.ts b/test/mu1tip1e.composite.test.ts index db48c28..3f708ce 100644 --- a/test/mu1tip1e.composite.test.ts +++ b/test/mu1tip1e.composite.test.ts @@ -5,11 +5,12 @@ import * as tap from 'tap' const test = tap.test test('anonymous function should be named mu1tip1e.composite.test', t => { - t.plan(1) + t.plan(2) const fn = fp((fastify, opts, next) => { next() }) + t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test') t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test') }) diff --git a/test/test.js b/test/test.js index 39df0a9..8c1278a 100644 --- a/test/test.js +++ b/test/test.js @@ -172,11 +172,25 @@ test('should throw if the fastify version does not satisfies the plugin requeste }) test('should set anonymous function name to file it was called from', t => { - t.plan(1) + t.plan(2) const fn = fp((fastify, opts, next) => { next() }) + t.is(fn[Symbol.for('plugin-meta')].name, 'test') t.is(fn[Symbol.for('fastify.display-name')], 'test') }) + +test('should set display-name to meta name', t => { + t.plan(2) + + const functionName = 'superDuperSpecialFunction' + + const fn = fp((fastify, opts, next) => next(), { + name: functionName + }) + + t.is(fn[Symbol.for('plugin-meta')].name, functionName) + t.is(fn[Symbol.for('fastify.display-name')], functionName) +}) diff --git a/test/test.ts b/test/test.ts index 15bf527..c3aca96 100644 --- a/test/test.ts +++ b/test/test.ts @@ -4,11 +4,12 @@ import * as tap from 'tap' const test = tap.test test('should set anonymous function name to file it was called from', t => { - t.plan(1) + t.plan(2) const fn = fp((fastify, opts, next) => { next() }) + t.is(fn[Symbol.for('plugin-meta')].name, 'test') t.is(fn[Symbol.for('fastify.display-name')], 'test') }) \ No newline at end of file