shelf
shelf copied to clipboard
Set a default header
Hello, I would like to implement a variable [static Map<String, Object> defaultHeaders] from within the [Response] class to define an instance of default [headers] for all [Response] of the web server app. If possible, this will make developers' lives much easier without having to create [Middleware] to intercept, process and add the standard header to each requested response.
Thank you in advance, Suebersson.
How would this look:
Olá, eu gostaria que fosso implementado uma variável
[static Map<String, Object> defaultHeaders] de dentro da classe [Response] para definirmos uma instância de [headers] padrão da para todas as [Response] do app web server. Sendo possível, isso irá facilitar muito a vida dos desenvolvedores sem precisar ter que criar uma [Middleware] para interceptar, processar e adicionar a header padrão dentro cada response de requisitada.
Desde já agradeço, Suebersson.
Como isso isso ficaria:
class Response extends Message {
static Map<String, Object>? defaultHeaders;
Response(
this.statusCode, {
Object? body,
Map<String, Object>? headers,
Encoding? encoding,
Map<String, Object>? context,
}) : super(body, encoding: encoding, headers: headers ?? defaultHeaders, context: context) {
if (statusCode < 100) {
throw ArgumentError('Invalid status code: $statusCode.');
}
}
}
Probably not as a top-level static. So many anti-patterns there.
Maybe as a zone variable?
It may even be an anti-pattern, but I saw that using the responder.change(...) constructor creates a new instance, reprocessing everything again, reprocessing all internal methods again. This can be avoided if we could define the app's default response header, even if this is done through a variable
Probably not as a top-level static. So many anti-patterns there.
Maybe as a zone variable?
I'm sorry, I researched "zone" and didn't find anything that could define the default header in the web server app with shelf