diff --git a/packages/core-ts/src/address/types.ts b/packages/core-ts/src/address/types.ts index f995d171..d74fc2a5 100644 --- a/packages/core-ts/src/address/types.ts +++ b/packages/core-ts/src/address/types.ts @@ -1,6 +1,12 @@ import { ErrorCode } from "./errors"; export type AddressKind = "G" | "M" | "C"; +/** + * Severity levels for validation warnings. + * Used with `minSeverityLevel` to filter warnings by importance. + */ +export type WarningSeverity = "info" | "warn" | "error"; + export type WarningCode = | "NON_CANONICAL_ADDRESS" | "NON_CANONICAL_ROUTING_ID" diff --git a/packages/core-ts/src/routing/extract.ts b/packages/core-ts/src/routing/extract.ts index 8f632b2a..5f967e6e 100644 --- a/packages/core-ts/src/routing/extract.ts +++ b/packages/core-ts/src/routing/extract.ts @@ -1,9 +1,23 @@ import { RoutingInput, RoutingResult } from "./types"; -import { Warning } from "../address/types"; +import { Warning, WarningSeverity } from "../address/types"; import { parse } from "../address/parse"; import { AddressParseError } from "../address/errors"; import { normalizeMemoTextId } from "./memo"; +const SEVERITY_ORDER: Record = { + info: 0, + warn: 1, + error: 2, +}; + +function filterBySeverity( + warnings: Warning[], + minSeverity: WarningSeverity +): Warning[] { + const threshold = SEVERITY_ORDER[minSeverity]; + return warnings.filter((w) => SEVERITY_ORDER[w.severity] >= threshold); +} + export class ExtractRoutingError extends Error { constructor(message: string) { super(message); @@ -46,6 +60,8 @@ function assertRoutableAddress(destination: string): void { export function extractRouting(input: RoutingInput): RoutingResult { assertRoutableAddress(input.destination); + const minSeverity = input.minSeverityLevel ?? "info"; + let parsed; try { parsed = parse(input.destination); @@ -90,7 +106,7 @@ export function extractRouting(input: RoutingInput): RoutingResult { destinationBaseAccount: null, routingId: null, routingSource: "none", - warnings, + warnings: filterBySeverity(warnings, minSeverity), }; } @@ -120,7 +136,7 @@ export function extractRouting(input: RoutingInput): RoutingResult { destinationBaseAccount: parsed.baseG, routingId: parsed.muxedId, routingSource: "muxed", - warnings, + warnings: filterBySeverity(warnings, minSeverity), }; } @@ -178,6 +194,6 @@ export function extractRouting(input: RoutingInput): RoutingResult { destinationBaseAccount: parsed.address, routingId, routingSource, - warnings, + warnings: filterBySeverity(warnings, minSeverity), }; } \ No newline at end of file diff --git a/packages/core-ts/src/routing/types.ts b/packages/core-ts/src/routing/types.ts index 504e2882..72e1be3c 100644 --- a/packages/core-ts/src/routing/types.ts +++ b/packages/core-ts/src/routing/types.ts @@ -1,4 +1,4 @@ -import { ErrorCode, Warning, WarningCode } from "../address/types"; +import { ErrorCode, Warning, WarningCode, WarningSeverity } from "../address/types"; export type RoutingSource = "muxed" | "memo" | "none"; @@ -7,6 +7,12 @@ export type RoutingInput = { memoType: string; memoValue: string | null; sourceAccount: string | null; + /** + * Minimum severity level for warnings to include in the result. + * Warnings below this threshold are filtered out. + * Defaults to `'info'` (all warnings are returned). + */ + minSeverityLevel?: WarningSeverity; }; export type KnownMemoType = "none" | "id" | "text" | "hash" | "return";