diff --git a/lib/context.js b/lib/context.js index d92019f4b65..6066711c7fe 100644 --- a/lib/context.js +++ b/lib/context.js @@ -13,7 +13,7 @@ const { kRouteByFastify, kRequestValidateWeakMap, kReplySerializeWeakMap, - // kPublicRouteContext + kPublicRouteContext } = require('./symbols.js') // Objects that holds the context of every request @@ -57,7 +57,10 @@ function Context ({ this[kFourOhFourContext] = null this.attachValidation = attachValidation this[kReplySerializerDefault] = replySerializer - this.schemaErrorFormatter = schemaErrorFormatter || server[kSchemaErrorFormatter] || defaultSchemaErrorFormatter + this.schemaErrorFormatter = + schemaErrorFormatter || + server[kSchemaErrorFormatter] || + defaultSchemaErrorFormatter this[kRouteByFastify] = isFastify this[kRequestValidateWeakMap] = null @@ -65,9 +68,38 @@ function Context ({ this.validatorCompiler = validatorCompiler || null this.serializerCompiler = serializerCompiler || null + // Route + Userland configurations for the route + this[kPublicRouteContext] = getPublicRouteContext(this) + this.server = server } +function getPublicRouteContext (context) { + return Object.create(null, { + schema: { + enumerable: true, + get () { + return context.schema + } + }, + config: { + enumerable: true, + get () { + return context.config + } + }, + logLevel: { + enumerable: true, + set (level) { + context.logLevel = level + }, + get () { + return context.logLevel + } + } + }) +} + function defaultSchemaErrorFormatter (errors, dataVar) { let text = '' const separator = ', ' diff --git a/lib/request.js b/lib/request.js index 2a299aa2863..c58bb790004 100644 --- a/lib/request.js +++ b/lib/request.js @@ -12,7 +12,8 @@ const { kSchemaController, kOptions, kRequestValidateWeakMap, - kRouteContext + kRouteContext, + kPublicRouteContext } = require('./symbols') const { FST_ERR_REQ_INVALID_VALIDATION_INVOCATION } = require('./errors') @@ -163,6 +164,11 @@ Object.defineProperties(Request.prototype, { return this[kRouteContext].config.method } }, + routeContext: { + get () { + return this[kRouteContext][kPublicRouteContext] + } + }, is404: { get () { return this[kRouteContext].config.url === undefined