/**
* REST Router — Dispatches /ucp/v1/* requests to domain handlers.
*/
import type { RESTResponse } from './types.js';
import { parseRequest, notFound } from './types.js';
import { handleCatalog } from './catalog.js';
import { handleCart } from './cart.js';
import { handleCheckout } from './checkout.js';
import { handleOrders } from './orders.js';
import { handleNegotiate } from './negotiate.js';
import { logger } from '../utils/logger.js';
interface APIGatewayEvent {
httpMethod: string;
path: string;
headers: Record<string, string | undefined>;
body: string | null;
isBase64Encoded: boolean;
queryStringParameters: Record<string, string | undefined> | null;
}
/**
* Route a /ucp/v1/* request to the appropriate domain handler.
*/
export async function routeUCPv1(event: APIGatewayEvent): Promise<RESTResponse> {
const path = event.path;
const method = event.httpMethod.toUpperCase();
logger.info('REST router', { method, path });
// OPTIONS handled at the Lambda level, but guard here too
if (method === 'OPTIONS') {
return { statusCode: 204, body: '' };
}
if (path.startsWith('/ucp/v1/catalog')) {
const req = parseRequest(event, 'catalog');
return handleCatalog(req);
}
if (path.startsWith('/ucp/v1/cart')) {
const req = parseRequest(event, 'cart');
return handleCart(req);
}
if (path.startsWith('/ucp/v1/checkout')) {
const req = parseRequest(event, 'checkout');
return handleCheckout(req);
}
if (path.startsWith('/ucp/v1/orders')) {
const req = parseRequest(event, 'orders');
return handleOrders(req);
}
if (path.startsWith('/ucp/v1/negotiate')) {
const req = parseRequest(event, 'negotiate');
return handleNegotiate(req);
}
return notFound(`No handler for ${method} ${path}`);
}