Skip to content

Commit d90cc62

Browse files
benblack86crudo
authored andcommitted
Name middleware (#583)
* Name middleware * Fix this * Add comment * Updates * Lowercase
1 parent f98c425 commit d90cc62

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/openapi.validator.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ export class OpenApiValidator {
116116
};
117117
});
118118

119+
const self = this; // using named functions instead of anonymous functions to allow traces to be more useful
119120
let inited = false;
120121
// install path params
121-
middlewares.push((req, res, next) =>
122-
pContext
122+
middlewares.push(function pathParamsMiddleware(req, res, next) {
123+
return pContext
123124
.then(({ context, error }) => {
124125
// Throw if any error occurred during spec load.
125126
if (error) throw error;
@@ -129,61 +130,61 @@ export class OpenApiValidator {
129130
// Doing so would enable path params to be type coerced when provided to
130131
// the final middleware.
131132
// Unfortunately, it is not possible to get the current Router from a handler function
132-
this.installPathParams(req.app, context);
133+
self.installPathParams(req.app, context);
133134
inited = true;
134135
}
135136
next();
136137
})
137-
.catch(next),
138-
);
138+
.catch(next);
139+
});
139140

140141
// metadata middleware
141142
let metamw;
142-
middlewares.push((req, res, next) =>
143-
pContext
143+
middlewares.push(function metadataMiddleware(req, res, next) {
144+
return pContext
144145
.then(({ context, responseApiDoc }) => {
145-
metamw = metamw || this.metadataMiddleware(context, responseApiDoc);
146+
metamw = metamw || self.metadataMiddleware(context, responseApiDoc);
146147
return metamw(req, res, next);
147148
})
148-
.catch(next),
149-
);
149+
.catch(next);
150+
});
150151

151152
if (this.options.fileUploader) {
152153
// multipart middleware
153154
let fumw;
154-
middlewares.push((req, res, next) =>
155-
pContext
155+
middlewares.push(function multipartMiddleware(req, res, next) {
156+
return pContext
156157
.then(({ context: { apiDoc } }) => {
157-
fumw = fumw || this.multipartMiddleware(apiDoc);
158+
fumw = fumw || self.multipartMiddleware(apiDoc);
158159
return fumw(req, res, next);
159160
})
160-
.catch(next),
161-
);
161+
.catch(next);
162+
});
162163
}
163164

164165
// security middlware
165166
let scmw;
166-
middlewares.push((req, res, next) =>
167-
pContext
167+
middlewares.push(function securityMiddleware(req, res, next) {
168+
return pContext
168169
.then(({ context: { apiDoc } }) => {
169170
const components = apiDoc.components;
170-
if (this.options.validateSecurity && components?.securitySchemes) {
171-
scmw = scmw || this.securityMiddleware(apiDoc);
171+
if (self.options.validateSecurity && components?.securitySchemes) {
172+
scmw = scmw || self.securityMiddleware(apiDoc);
172173
return scmw(req, res, next);
173174
} else {
174175
next();
175176
}
176177
})
177-
.catch(next),
178-
);
178+
.catch(next);
179+
});
179180

180181
// request middlweare
181182
if (this.options.validateRequests) {
182183
let reqmw;
183-
middlewares.push((req, res, next) => {
184+
middlewares.push(function requestMiddleware(req, res, next) {
184185
return pContext
185186
.then(({ context: { apiDoc } }) => {
186-
reqmw = reqmw || this.requestValidationMiddleware(apiDoc);
187+
reqmw = reqmw || self.requestValidationMiddleware(apiDoc);
187188
return reqmw(req, res, next);
188189
})
189190
.catch(next);
@@ -193,25 +194,25 @@ export class OpenApiValidator {
193194
// response middleware
194195
if (this.options.validateResponses) {
195196
let resmw;
196-
middlewares.push((req, res, next) =>
197-
pContext
197+
middlewares.push(function responseMiddleware(req, res, next) {
198+
return pContext
198199
.then(({ responseApiDoc }) => {
199-
resmw = resmw || this.responseValidationMiddleware(responseApiDoc);
200+
resmw = resmw || self.responseValidationMiddleware(responseApiDoc);
200201
return resmw(req, res, next);
201202
})
202-
.catch(next),
203-
);
203+
.catch(next);
204+
})
204205
}
205206

206207
// op handler middleware
207208
if (this.options.operationHandlers) {
208209
let router: Router = null;
209-
middlewares.push((req, res, next) => {
210+
middlewares.push(function operationHandlersMiddleware(req, res, next) {
210211
if (router) return router(req, res, next);
211-
pContext
212+
return pContext
212213
.then(
213214
({ context }) =>
214-
(router = this.installOperationHandlers(req.baseUrl, context)),
215+
(router = self.installOperationHandlers(req.baseUrl, context)),
215216
)
216217
.then((router) => router(req, res, next))
217218
.catch(next);

0 commit comments

Comments
 (0)