UNPKG

@aibtc/types

Version:

TypeScript types for AIBTC

76 lines (75 loc) 2.22 kB
import { ApiError } from "./api-error"; /** * Creates CORS headers for cross-origin requests * * @param origin - Optional origin to allow, defaults to '*' (all origins) * @returns HeadersInit object with CORS headers */ export function corsHeaders(origin) { return { "Access-Control-Allow-Origin": origin || "*", "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization", "Access-Control-Max-Age": "86400", }; } /** * Creates a standardized success response */ export function createSuccessResponse(c, data, status = 200) { const body = { success: true, data, }; return c.json(body, status, { ...corsHeaders(), "Content-Type": "application/json", }); } /** * Creates a standardized error response */ export function createErrorResponse(c, error) { let body; let status = 500; if (error instanceof ApiError) { body = { success: false, error: { id: error.id, code: error.code, message: error.message, details: error.details, }, }; status = error.status; } else { // Generate an error ID for non-ApiError errors const errorId = generateErrorId(); const errorMessage = error instanceof Error ? error.message : String(error); body = { success: false, error: { id: errorId, code: "INTERNAL_ERROR", message: errorMessage || "An unexpected error occurred", }, }; } return c.json(body, status, { ...corsHeaders(), "Content-Type": "application/json", }); } /** * Generates a unique error ID for tracking purposes */ function generateErrorId() { // Use crypto.randomUUID() if available if (typeof crypto !== "undefined" && crypto.randomUUID) { return crypto.randomUUID().split("-")[0]; // Use first segment for brevity } // Fallback to timestamp + random string return Date.now().toString(36) + Math.random().toString(36).substring(2, 5); }