Response.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export function csvAttachment(res, body, filename) {
  2. if (!filename || !filename.endsWith('.csv')) {
  3. throw new Error('filename must end with .csv')
  4. }
  5. // res.attachment sets both content-type and content-disposition headers.
  6. res.attachment(filename)
  7. res.setHeader('X-Content-Type-Options', 'nosniff')
  8. res.send(body)
  9. }
  10. export function preparePlainTextResponse(res) {
  11. res.setHeader('X-Content-Type-Options', 'nosniff')
  12. res.contentType('text/plain; charset=utf-8')
  13. }
  14. export function plainTextResponse(res, body) {
  15. preparePlainTextResponse(res)
  16. res.send(body)
  17. }
  18. export function xmlResponse(res, body) {
  19. res.setHeader('X-Content-Type-Options', 'nosniff')
  20. res.contentType('application/xml; charset=utf-8')
  21. res.send(body)
  22. }
  23. export function prepareZipAttachment(res, filename) {
  24. if (!filename || !filename.endsWith('.zip')) {
  25. throw new Error('filename must end with .zip')
  26. }
  27. // res.attachment sets both content-type and content-disposition headers.
  28. res.attachment(filename)
  29. res.setHeader('X-Content-Type-Options', 'nosniff')
  30. }
  31. export function zipAttachment(res, body, filename) {
  32. prepareZipAttachment(res, filename)
  33. res.send(body)
  34. }
  35. export default {
  36. csvAttachment,
  37. plainTextResponse,
  38. preparePlainTextResponse,
  39. prepareZipAttachment,
  40. xmlResponse,
  41. zipAttachment,
  42. }