Refactor to a response function (no WET code)

This commit is contained in:
Alan Grainger
2025-03-07 13:00:41 +08:00
parent 01c3236e0f
commit 9e176e816a
5 changed files with 43 additions and 31 deletions
+30
View File
@@ -0,0 +1,30 @@
/*
This function is in its own file so that *if desired* someone can replace the entire
function with their own custom one, by replacing the invalidRequestHandler.js file
through a Docker volume mount.
*/
import { Response } from 'express-serve-static-core'
import { getConfigOption } from './functions'
export function respondToInvalidRequest (res: Response, defaultResponse: number | string | null) {
let method = getConfigOption('ipp.customInvalidResponse', false)
if (method === false) {
// No custom method specified, use the default
method = defaultResponse
}
if (typeof method === 'number') {
// Respond with an HTTP status code
res.status(method).send()
} else if (method === null) {
// Drop the connection without responding
res.destroy()
} else if (typeof method === 'string' && method.startsWith('http')) {
// Redirect to another URL
res.redirect(method)
} else {
// Fallback to 404
res.status(404).send()
}
}