import type { ApiOperation } from "../../contracts/api-operations.ts"; export type OperationRequestInput = Readonly<{ operationId: string; routeId: string; pathParams?: Readonly>; searchParams?: unknown; body?: unknown; signal?: AbortSignal; idempotencyKey?: string; correlationId?: string; }>; export type RequestTargetResult = | Readonly<{ success: true; url: URL }> | Readonly<{ success: false; code: | "BASE_URL_INVALID" | "PATH_PARAMETER_MISSING" | "PATH_PARAMETER_UNEXPECTED" | "PATH_PARAMETER_INVALID" | "SEARCH_PARAMETER_INVALID"; }>; const pathParameterPattern = /:([A-Za-z][A-Za-z0-9_]*)|\{([A-Za-z][A-Za-z0-9_]*)\}/g; export function buildRequestTarget( baseUrl: string, operation: ApiOperation, pathParams: Readonly> = {}, parsedSearch: unknown = {}, ): RequestTargetResult { let base: URL; try { base = new URL(baseUrl); } catch { return { success: false, code: "BASE_URL_INVALID" }; } if ( (base.protocol !== "https:" && !( base.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(base.hostname) )) || base.username || base.password || base.search || base.hash ) { return { success: false, code: "BASE_URL_INVALID" }; } const placeholders = new Set(); for (const match of operation.path.matchAll(pathParameterPattern)) { placeholders.add(match[1] ?? match[2] ?? ""); } if (Object.keys(pathParams).some((key) => !placeholders.has(key))) { return { success: false, code: "PATH_PARAMETER_UNEXPECTED" }; } let missingPathParameter = false; let invalidPathParameter = false; const pathname = operation.path.replace( pathParameterPattern, (_token, colonName: string | undefined, braceName: string | undefined) => { const name = colonName ?? braceName ?? ""; const value = pathParams[name]; if (value === undefined) { missingPathParameter = true; return ""; } const serialized = String(value); if ( serialized.length === 0 || serialized.length > 512 || // Dot segments are not encoded and pathname assignment normalizes them. serialized === "." || serialized === ".." || [...serialized].some((character) => { const code = character.codePointAt(0) ?? 0; return code < 32 || code === 127; }) ) { invalidPathParameter = true; return ""; } return encodeURIComponent(serialized); }, ); if (missingPathParameter) { return { success: false, code: "PATH_PARAMETER_MISSING" }; } if (invalidPathParameter) { return { success: false, code: "PATH_PARAMETER_INVALID" }; } if ( parsedSearch === null || typeof parsedSearch !== "object" || Array.isArray(parsedSearch) ) { return { success: false, code: "SEARCH_PARAMETER_INVALID" }; } const basePrefix = base.pathname.endsWith("/") ? base.pathname : `${base.pathname}/`; const relativePath = pathname.replace(/^\/+/, ""); base.pathname = `${basePrefix}${relativePath}`.replace(/\/{2,}/g, "/"); const url = base; const search = parsedSearch as Readonly>; for (const key of Object.keys(search).sort((left, right) => left.localeCompare(right), )) { const value = search[key]; if (value === undefined || value === null) continue; const values = Array.isArray(value) ? value : [value]; for (const item of values) { if ( typeof item !== "string" && typeof item !== "number" && typeof item !== "boolean" ) { return { success: false, code: "SEARCH_PARAMETER_INVALID" }; } url.searchParams.append(key, String(item)); } } if ( operation.maxEncodedSearchBytes !== undefined && new TextEncoder().encode(url.search).byteLength > operation.maxEncodedSearchBytes ) { return { success: false, code: "SEARCH_PARAMETER_INVALID" }; } return { success: true, url }; }