Files
big-brother/api/app/middleware/force_json_response_middleware.ts
2026-10-12 14:25:22 +02:00

17 lines
549 B
TypeScript

import type { HttpContext } from '@adonisjs/core/http'
import type { NextFn } from '@adonisjs/core/types/http'
/**
* The API always answers in JSON, including the errors raised before the
* route middleware (CSRF) and the EventSource requests: an unauthenticated
* stream gets a 401 instead of a redirect.
*/
export default class ForceJsonResponseMiddleware {
handle(ctx: HttpContext, next: NextFn) {
if (ctx.request.url().startsWith('/api/')) {
ctx.request.request.headers.accept = 'application/json'
}
return next()
}
}