diff --git a/lib/context.js b/lib/context.js index ddd8edbdeb..fd0a0b72a3 100644 --- a/lib/context.js +++ b/lib/context.js @@ -1,15 +1,37 @@ 'use strict' -const { kFourOhFourContext, kReplySerializerDefault } = require('./symbols.js') +const { + kFourOhFourContext, + kReplySerializerDefault, + kSchemaErrorFormatter, + kErrorHandler, + kReply, + kRequest, + kBodyLimit, + kLogLevel, + kContentTypeParser +} = require('./symbols.js') // Objects that holds the context of every request // Every route holds an instance of this object. -function Context (schema, handler, Reply, Request, contentTypeParser, config, errorHandler, bodyLimit, logLevel, logSerializers, attachValidation, replySerializer, schemaErrorFormatter, server) { +function Context ({ + schema, + handler, + config, + errorHandler, + bodyLimit, + logLevel, + logSerializers, + attachValidation, + replySerializer, + schemaErrorFormatter, + server +}) { this.schema = schema this.handler = handler - this.Reply = Reply - this.Request = Request - this.contentTypeParser = contentTypeParser + this.Reply = server[kReply] + this.Request = server[kRequest] + this.contentTypeParser = server[kContentTypeParser] this.onRequest = null this.onSend = null this.onError = null @@ -17,16 +39,18 @@ function Context (schema, handler, Reply, Request, contentTypeParser, config, er this.preHandler = null this.onResponse = null this.config = config - this.errorHandler = errorHandler + this.errorHandler = errorHandler || server[kErrorHandler] this._middie = null - this._parserOptions = { limit: bodyLimit || null } - this.logLevel = logLevel + this._parserOptions = { + limit: bodyLimit || server[kBodyLimit] + } + this.logLevel = logLevel || server[kLogLevel] this.logSerializers = logSerializers this[kFourOhFourContext] = null this.attachValidation = attachValidation this[kReplySerializerDefault] = replySerializer - this.schemaErrorFormatter = schemaErrorFormatter || defaultSchemaErrorFormatter - // TODO simplify the arguments, we can pass way less arguments if we pass server + this.schemaErrorFormatter = schemaErrorFormatter || server[kSchemaErrorFormatter] || defaultSchemaErrorFormatter + this.server = server } diff --git a/lib/fourOhFour.js b/lib/fourOhFour.js index 346907b263..bcbdc7416e 100644 --- a/lib/fourOhFour.js +++ b/lib/fourOhFour.js @@ -9,14 +9,8 @@ const { kRoutePrefix, kCanSetNotFoundHandler, kFourOhFourLevelInstance, - kReply, - kRequest, - kContentTypeParser, - kBodyLimit, - kLogLevel, kFourOhFourContext, - kHooks, - kErrorHandler + kHooks } = require('./symbols.js') const { lifecycleHooks } = require('./hooks') const { buildErrorHandler } = require('./error-handler.js') @@ -140,17 +134,12 @@ function fourOhFour (options) { } function _setNotFoundHandler (prefix, opts, handler, avvio, routeHandler) { - const context = new Context( - opts.schema, - handler, - this[kReply], - this[kRequest], - this[kContentTypeParser], - opts.config || {}, - this[kErrorHandler], - this[kBodyLimit], - this[kLogLevel] - ) + const context = new Context({ + schema: opts.schema, + handler: handler, + config: opts.config || {}, + server: this + }) avvio.once('preReady', () => { const context = this[kFourOhFourContext] diff --git a/lib/route.js b/lib/route.js index f5bcd197d5..d39704e0e1 100644 --- a/lib/route.js +++ b/lib/route.js @@ -28,11 +28,8 @@ const { kHooks, kSchemaController, kOptions, - kContentTypeParser, - kReply, kReplySerializerDefault, kReplyIsError, - kRequest, kRequestPayloadStream, kDisableRequestLogging, kSchemaErrorFormatter, @@ -211,22 +208,19 @@ function buildRouting (options) { method: opts.method } - const context = new Context( - opts.schema, - opts.handler.bind(this), - this[kReply], - this[kRequest], - this[kContentTypeParser], - config, - opts.errorHandler || this[kErrorHandler], - opts.bodyLimit, - opts.logLevel, - opts.logSerializers, - opts.attachValidation, - this[kReplySerializerDefault], - opts.schemaErrorFormatter || this[kSchemaErrorFormatter], - this - ) + const context = new Context({ + schema: opts.schema, + handler: opts.handler.bind(this), + config: config, + errorHandler: opts.errorHandler, + bodyLimit: opts.bodyLimit, + logLevel: opts.logLevel, + logSerializers: opts.logSerializers, + attachValidation: opts.attachValidation, + schemaErrorFormatter: opts.schemaErrorFormatter, + replySerializer: this[kReplySerializerDefault], + server: this + }) if (opts.version) { warning.emit('FSTDEP008')