json method

HttpResponse json([
  1. dynamic data
])

Implementation

HttpResponse json([dynamic data]) {
  final config = App().container.make<AppConfig>();

  final cookie = Cookie(
    'xsrf-api-token-${config.get('app.timestamp').toString().replaceAll(':', '-')}',
    "${config.get('app.id')}",
  )
    ..httpOnly = true
    ..secure = true
    ..sameSite = SameSite.lax;

  response.headers.contentType = ContentType.json;

  return response
    ..cookies.add(cookie)
    ..write(jsonEncode(data))
    ..close();
}