/* * Copyright (C) 2026 Fluxer Contributors * * This file is part of Fluxer. * * Fluxer is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Fluxer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Fluxer. If not, see . */ import {MimeType} from '@fluxer/constants/src/HttpConstants'; export interface JsonResponseOptions { status: number; payload: Record; headers?: Record; } export interface JsonErrorResponseOptions { status: number; code: string; message: string; data?: Record; headers?: Record; } export function createJsonResponse(options: JsonResponseOptions): Response { return new Response(JSON.stringify(options.payload), { status: options.status, headers: { 'Content-Type': MimeType.JSON, ...(options.headers ?? {}), }, }); } export function createJsonErrorResponse(options: JsonErrorResponseOptions): Response { return createJsonResponse({ status: options.status, payload: { code: options.code, message: options.message, ...(options.data ?? {}), }, headers: options.headers, }); } export function createXmlErrorResponse(status: number, code: string, message: string): Response { const xml = ` ${escapeXml(code)} ${escapeXml(message)} `; return new Response(xml, { status, headers: { 'Content-Type': MimeType.XML, }, }); } function escapeXml(value: string): string { return value .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('"', '"') .replaceAll("'", '''); }