Skip to content

Commit

Permalink
fix: Refactor Context constructor and avoid positional arguments (#3483)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtcaregorodtcev committed Nov 24, 2021
1 parent 94aab54 commit a174829
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 47 deletions.
44 changes: 34 additions & 10 deletions lib/context.js
@@ -1,32 +1,56 @@
'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
this.onTimeout = null
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
}

Expand Down
25 changes: 7 additions & 18 deletions lib/fourOhFour.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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]
Expand Down
32 changes: 13 additions & 19 deletions lib/route.js
Expand Up @@ -28,11 +28,8 @@ const {
kHooks,
kSchemaController,
kOptions,
kContentTypeParser,
kReply,
kReplySerializerDefault,
kReplyIsError,
kRequest,
kRequestPayloadStream,
kDisableRequestLogging,
kSchemaErrorFormatter,
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit a174829

Please sign in to comment.