// SPDX-License-Identifier: AGPL-3.0-or-later import {MimeType} from '@fluxer/constants/src/HttpConstants'; interface JsonResponseOptions { status: number; payload: Record; headers?: Record; } interface JsonErrorResponseOptions { status: number; code: string; message: string; data?: Record; headers?: Record; } 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("'", '''); }