Skip to content

Commit b751ad8

Browse files
Centralize baseURL validation error messages
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 1f7a165 commit b751ad8

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/ai/src/chatTransport.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,14 @@ export function createTriggerChatTransport<
448448
return new TriggerChatTransport<UI_MESSAGE, PAYLOAD>(options);
449449
}
450450

451+
const BASE_URL_VALIDATION_ERRORS = {
452+
empty: "baseURL must not be empty",
453+
invalidAbsoluteUrl: "baseURL must be a valid absolute URL",
454+
invalidProtocol: "baseURL must use http or https protocol",
455+
queryOrHash: "baseURL must not include query parameters or hash fragments",
456+
credentials: "baseURL must not include username or password credentials",
457+
} as const;
458+
451459
function resolvePayloadMapper<
452460
UI_MESSAGE extends UIMessage,
453461
PAYLOAD,
@@ -463,14 +471,14 @@ function normalizeBaseUrl(baseURL: string) {
463471
const normalizedBaseUrl = baseURL.trim().replace(/\/+$/, "");
464472

465473
if (normalizedBaseUrl.length === 0) {
466-
throw new Error("baseURL must not be empty");
474+
throw new Error(BASE_URL_VALIDATION_ERRORS.empty);
467475
}
468476

469477
let parsedBaseUrl: URL;
470478
try {
471479
parsedBaseUrl = new URL(normalizedBaseUrl);
472480
} catch {
473-
throw new Error("baseURL must be a valid absolute URL");
481+
throw new Error(BASE_URL_VALIDATION_ERRORS.invalidAbsoluteUrl);
474482
}
475483

476484
assertValidBaseUrlProtocol(parsedBaseUrl);
@@ -485,19 +493,19 @@ function assertValidBaseUrlProtocol(parsedBaseUrl: URL) {
485493
parsedBaseUrl.protocol !== "http:" &&
486494
parsedBaseUrl.protocol !== "https:"
487495
) {
488-
throw new Error("baseURL must use http or https protocol");
496+
throw new Error(BASE_URL_VALIDATION_ERRORS.invalidProtocol);
489497
}
490498
}
491499

492500
function assertBaseUrlHasNoQueryOrHash(parsedBaseUrl: URL) {
493501
if (parsedBaseUrl.search.length > 0 || parsedBaseUrl.hash.length > 0) {
494-
throw new Error("baseURL must not include query parameters or hash fragments");
502+
throw new Error(BASE_URL_VALIDATION_ERRORS.queryOrHash);
495503
}
496504
}
497505

498506
function assertBaseUrlHasNoCredentials(parsedBaseUrl: URL) {
499507
if (parsedBaseUrl.username.length > 0 || parsedBaseUrl.password.length > 0) {
500-
throw new Error("baseURL must not include username or password credentials");
508+
throw new Error(BASE_URL_VALIDATION_ERRORS.credentials);
501509
}
502510
}
503511

0 commit comments

Comments
 (0)