MCPcopy
hub / github.com/fastify/fastify / addHook

Function addHook

fastify.js:570–615  ·  view source on GitHub ↗
(name, fn)

Source from the content-addressed store, hash-verified

568
569 // wrapper that we expose to the user for hooks handling
570 function addHook (name, fn) {
571 throwIfAlreadyStarted('Cannot call "addHook"!')
572
573 if (fn == null) {
574 throw new errorCodes.FST_ERR_HOOK_INVALID_HANDLER(name, fn)
575 }
576
577 if (name === 'onSend' || name === 'preSerialization' || name === 'onError' || name === 'preParsing') {
578 if (fn.constructor.name === 'AsyncFunction' && fn.length === 4) {
579 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
580 }
581 } else if (name === 'onReady' || name === 'onListen') {
582 if (fn.constructor.name === 'AsyncFunction' && fn.length !== 0) {
583 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
584 }
585 } else if (name === 'onRequestAbort') {
586 if (fn.constructor.name === 'AsyncFunction' && fn.length !== 1) {
587 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
588 }
589 } else {
590 if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
591 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
592 }
593 }
594
595 if (name === 'onClose') {
596 this.onClose(fn.bind(this))
597 } else if (name === 'onReady' || name === 'onListen' || name === 'onRoute') {
598 this[kHooks].add(name, fn)
599 } else {
600 this.after((err, done) => {
601 try {
602 _addHook.call(this, name, fn)
603 done(err)
604 } catch (err) {
605 done(err)
606 }
607 })
608 }
609 return this
610
611 function _addHook (name, fn) {
612 this[kHooks].add(name, fn)
613 this[kChildren].forEach(child => _addHook.call(child, name, fn))
614 }
615 }
616
617 // wrapper that we expose to the user for schemas handling
618 function addSchema (schema) {

Callers

nothing calls this directly

Calls 4

throwIfAlreadyStartedFunction · 0.85
addMethod · 0.80
afterMethod · 0.80
doneFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…