Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (61 loc) · 1.92 KB
/
Copy pathindex.js
File metadata and controls
77 lines (61 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const getPluginName = require('./lib/getPluginName')
const toCamelCase = require('./lib/toCamelCase')
const kSkipOverride = Symbol.for('skip-override')
const kDisplayName = Symbol.for('fastify.display-name')
const kPluginMeta = Symbol.for('plugin-meta')
let count = 0
function plugin (fn, options = {}) {
let autoName = false
if (fn?.default !== undefined) {
// Support for 'export default' behaviour in transpiled ECMAScript module
fn = fn.default
}
if (typeof fn !== 'function') {
throw new TypeError(
`fastify-plugin expects a function, instead got a '${typeof fn}'`
)
}
if (typeof options === 'string') {
options = {
fastify: options
}
}
if (
typeof options !== 'object' ||
Array.isArray(options) ||
options === null
) {
throw new TypeError('The options object should be an object')
}
if (options.fastify !== undefined && typeof options.fastify !== 'string') {
throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof options.fastify}'`)
}
if (options.name && typeof options.name !== 'string') {
throw new TypeError(`fastify-plugin expects a name string, instead got '${typeof options.name}'`)
}
if (!options.name) {
autoName = true
options = { ...options, name: getPluginName(fn) + '-auto-' + count++ }
}
fn[kSkipOverride] = options.encapsulate !== true
fn[kDisplayName] = options.name
fn[kPluginMeta] = options
// Faux modules support
if (!fn.default) {
fn.default = fn
}
// TypeScript support for named imports
// See https://github.com/fastify/fastify/issues/2404 for more details
// The type definitions would have to be update to match this.
if (!autoName) {
const camelCase = toCamelCase(options.name)
if (!fn[camelCase]) {
fn[camelCase] = fn
}
}
return fn
}
module.exports = plugin
module.exports.default = plugin
module.exports.fastifyPlugin = plugin