Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract validatePlugin and create isBundledOrTypescriptPlugin #216

Merged
merged 5 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const inherits = require('util').inherits
const {
AVV_ERR_EXPOSE_ALREADY_DEFINED,
AVV_ERR_CALLBACK_NOT_FN,
AVV_ERR_PLUGIN_NOT_VALID,
AVV_ERR_ROOT_PLG_BOOTED,
AVV_ERR_READY_TIMEOUT
} = require('./lib/errors')
const TimeTree = require('./time-tree')
const Plugin = require('./plugin')
const { debug } = require('./lib/debug')
const { validatePlugin } = require('./lib/validate-plugin')
const { isBundledOrTypescriptPlugin } = require('./lib/is-bundled-or-typescript-plugin')
const kAvvio = Symbol('kAvvio')
const kThenifyDoNotWrap = Symbol('kThenifyDoNotWrap')

Expand Down Expand Up @@ -197,18 +198,6 @@ Boot.prototype.override = function (server, func, opts) {
return server
}

function assertPlugin (plugin) {
// Faux modules are modules built with TypeScript
// or Babel that they export a .default property.
if (plugin && typeof plugin === 'object' && typeof plugin.default === 'function') {
plugin = plugin.default
}
if (!(plugin && (typeof plugin === 'function' || typeof plugin.then === 'function'))) {
throw new AVV_ERR_PLUGIN_NOT_VALID(typeof plugin)
}
return plugin
}

Boot.prototype[kAvvio] = true

// load a plugin
Expand Down Expand Up @@ -237,7 +226,10 @@ Boot.prototype._loadRegistered = function () {
Object.defineProperty(Boot.prototype, 'then', { get: thenify })

Boot.prototype._addPlugin = function (plugin, opts, isAfter) {
plugin = assertPlugin(plugin)
validatePlugin(plugin)
if (isBundledOrTypescriptPlugin(plugin)) {
plugin = plugin.default
}
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
opts = opts || {}

if (this.booted) {
Expand Down
20 changes: 20 additions & 0 deletions lib/is-bundled-or-typescript-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

/**
* bundled or typescript plugin
* @typedef {object} BundledOrTypescriptPlugin
* @property {function} default
*/

/**
* @param {unknown} plugin
* @returns {plugin is BundledOrTypescriptPlugin}
*/

function isBundledOrTypescriptPlugin (plugin) {
return plugin !== null && typeof plugin === 'object' && typeof plugin.default === 'function'
}

module.exports = {
isBundledOrTypescriptPlugin
}
34 changes: 34 additions & 0 deletions lib/validate-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const { AVV_ERR_PLUGIN_NOT_VALID } = require('./errors')
const { isBundledOrTypescriptPlugin } = require('./is-bundled-or-typescript-plugin')

/**
* @typedef {import('./is-bundled-or-typescript-plugin').BundledOrTypescriptPlugin} BundledOrTypescriptPlugin
*/

/**
* @param {unknown} plugin
* @throws {AVV_ERR_PLUGIN_NOT_VALID}
* @returns {plugin is BundledOrTypescriptPlugin|Function}
*/
function validatePlugin (plugin) {
if (isBundledOrTypescriptPlugin(plugin)) {
plugin = plugin.default
}

// validate if plugin is a function or Promise
if (!(plugin && (typeof plugin === 'function' || typeof plugin.then === 'function'))) {
if (Array.isArray(plugin)) {
throw new AVV_ERR_PLUGIN_NOT_VALID('array')
} else if (plugin === null) {
throw new AVV_ERR_PLUGIN_NOT_VALID('null')
} else {
throw new AVV_ERR_PLUGIN_NOT_VALID(typeof plugin)
}
}
}

module.exports = {
validatePlugin
}
20 changes: 20 additions & 0 deletions test/lib/is-bundled-or-typescript-plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const { test } = require('tap')
const { isBundledOrTypescriptPlugin } = require('../../lib/is-bundled-or-typescript-plugin')

test('isBundledOrTypescriptPlugin', (t) => {
t.plan(9)

t.equal(isBundledOrTypescriptPlugin(1), false)
t.equal(isBundledOrTypescriptPlugin('function'), false)
t.equal(isBundledOrTypescriptPlugin({}), false)
t.equal(isBundledOrTypescriptPlugin([]), false)
t.equal(isBundledOrTypescriptPlugin(null), false)

t.equal(isBundledOrTypescriptPlugin(function () {}), false)
t.equal(isBundledOrTypescriptPlugin(new Promise((resolve) => resolve)), false)
t.equal(isBundledOrTypescriptPlugin(Promise.resolve()), false)

t.equal(isBundledOrTypescriptPlugin({ default: () => {} }), true)
})
21 changes: 21 additions & 0 deletions test/lib/validate-plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const { test } = require('tap')
const { validatePlugin } = require('../../lib/validate-plugin')
const { AVV_ERR_PLUGIN_NOT_VALID } = require('../../lib/errors')

test('validatePlugin', (t) => {
t.plan(9)

t.throws(() => validatePlugin(1), new AVV_ERR_PLUGIN_NOT_VALID('number'))
t.throws(() => validatePlugin('function'), new AVV_ERR_PLUGIN_NOT_VALID('string'))
t.throws(() => validatePlugin({}), new AVV_ERR_PLUGIN_NOT_VALID('object'))
t.throws(() => validatePlugin([]), new AVV_ERR_PLUGIN_NOT_VALID('array'))
t.throws(() => validatePlugin(null), new AVV_ERR_PLUGIN_NOT_VALID('null'))

t.doesNotThrow(() => validatePlugin(function () {}))
t.doesNotThrow(() => validatePlugin(new Promise((resolve) => resolve)))
t.doesNotThrow(() => validatePlugin(Promise.resolve()))

t.doesNotThrow(() => validatePlugin({ default: () => {} }))
})