API Reference
handleUndefinedResponsesMiddleware
import {handleUndefinedResponsesMiddleware} from 'papupata'
Availability
This functionality is available from papupata version 1.5.0 onwards.Purpose
Middleware that changes how papupata deals with undefined responses, automatically setting up response code 204 (if not otherwise specified) and sending a response.Usage
By default papupata assumes that if your route implementation returns undefined, it takes the responsibilities for sending the response.
This if often not the case, though. You might have APIs that do things but do not return anything, in which case it makes sense for the response to be undefined or void. But what it means by default is that you have either do something like
res.status(204); res.end()
This middleware changes things in two ways; one: if headers have not been sent by the time the route implementation returns undefined, an empty response is automatically sent. Also, in that case, if the response status code has not been explicitly set, it becomes 204 (no content).
The behaviour of this middleware might become the default mode of operation for papupata 2.0.
Examples
Enabling middleware for all roues
const API = new APIDeclaration()
API.configure({
// other options
inherentMiddleware: [handleUndefinedResponsesMiddleware]
})
Enabling the middleware for a single route
const api = API.declareGetAPI('/test').response<string>()
api.implementWithPapupataMiddleware([handleUndefinedResponsesMiddleware], () => skipHandlingRoute)
Expected effect: simple case
api.implement(async () => {
await doStuff()
})
// Without middleware there is never a response; with the middleware the response is a 204
Expected effect: explicit status
api.implement(async (_req, res) => {
res.status(500)
await doStuff()
})
// Without middleware there is never a response; with the middleware the response is a 500
Expected effect: redirect
api.implement(async (_req, res) => {
res.redirect('/')
})
// Works with and without middleware