Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): resolve use func not allowing to mix middlewares #183

Merged
merged 1 commit into from
Jan 1, 2023

Conversation

michaeleliot
Copy link
Contributor

@michaeleliot michaeleliot commented Jun 3, 2021

Currently the use function allows for inputs to be either Middleware or ErrorHandlingMiddleware. However, the typescript type requires the list of arguments to be only Middleware or only ErrorHandlingMiddleware. I.e. you couldn't call api.use with both error middleware and normal middleware. This pr brings the typescript type back into line with code expectation.

Code Section in question:

lambda-api/index.js

Lines 355 to 383 in c1fe83e

use(...args) {
// Extract routes
let routes = typeof args[0] === 'string' ? Array.of(args.shift()) : (Array.isArray(args[0]) ? args.shift() : ['/*'])
// Init middleware stack
let middleware = []
// Add func args as middleware
for (let arg in args) {
if (typeof args[arg] === 'function') {
if (args[arg].length === 3) {
middleware.push(args[arg])
} else if (args[arg].length === 4) {
this._errors.push(args[arg])
} else {
throw new ConfigurationError('Middleware must have 3 or 4 parameters')
}
}
}
// Add middleware to path
if (middleware.length > 0) {
routes.forEach(route => {
this.METHOD('__MW__',route,...middleware)
})
}
} // end use

…lowed for array of middleware inputs to either be standard middleware or normal middleware.
@michaeleliot michaeleliot changed the title Fix Typescript Typing for Use Function Fix Typing for Use Function Jun 3, 2021
@naorpeled
Copy link
Collaborator

Hey @michaeleliot,
thanks for your contribution, you're awesome 😎

@naorpeled naorpeled changed the title Fix Typing for Use Function fix(types): resolve use func not allowing to mix middlewares Jan 1, 2023
@naorpeled naorpeled merged commit ba76c89 into jeremydaly:main Jan 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants