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

Function addHook

fastify.js:570–615  ·  fastify.js::addHook
(name, fn)

Source from the content-addressed store, hash-verified

568
569 class="cm">// wrapper that we expose to the user for hooks handling
570 function addHook (name, fn) {
571 throwIfAlreadyStarted(class="st">'Cannot call "addHook"!')
572
573 if (fn == null) {
574 throw new errorCodes.FST_ERR_HOOK_INVALID_HANDLER(name, fn)
575 }
576
577 if (name === class="st">'onSend' || name === class="st">'preSerialization' || name === class="st">'onError' || name === class="st">'preParsing') {
578 if (fn.constructor.name === class="st">'AsyncFunction' && fn.length === 4) {
579 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
580 }
581 } else if (name === class="st">'onReady' || name === class="st">'onListen') {
582 if (fn.constructor.name === class="st">'AsyncFunction' && fn.length !== 0) {
583 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
584 }
585 } else if (name === class="st">'onRequestAbort') {
586 if (fn.constructor.name === class="st">'AsyncFunction' && fn.length !== 1) {
587 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
588 }
589 } else {
590 if (fn.constructor.name === class="st">'AsyncFunction' && fn.length === 3) {
591 throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
592 }
593 }
594
595 if (name === class="st">'onClose') {
596 this.onClose(fn.bind(this))
597 } else if (name === class="st">'onReady' || name === class="st">'onListen' || name === class="st">'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 class="cm">// 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