Skip to content

Commit 1b09a50

Browse files
committed
refactor: extract getFetchInputUrl helper to deduplicate URL extraction
The OpenAI/Codex and Copilot fetch wrappers each contained an identical 15-line IIFE that extracted a URL string from the `fetch` `input` argument (handling string, URL, and Request-like shapes). Extract the logic into a single `getFetchInputUrl` helper so both wrappers share one implementation. Behavior-preserving: the helper returns the same empty-string fallback on unrecognized inputs, so callers continue to fall through to normal fetch behavior without throwing.
1 parent 750f8d8 commit 1b09a50

1 file changed

Lines changed: 29 additions & 30 deletions

File tree

src/node/services/providerModelFactory.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,33 @@ const CODEX_ALLOWED_PARAMS = new Set([
665665
"text", // structured output via Output.object -> text.format
666666
]);
667667

668+
// ---------------------------------------------------------------------------
669+
// Fetch input helpers
670+
// ---------------------------------------------------------------------------
671+
672+
/**
673+
* Extract a URL string from the `input` argument of a `fetch`-compatible call.
674+
* Handles the three shapes AI SDK providers pass through: plain string, `URL`,
675+
* and `Request`-like objects that expose a `url` property. Returns an empty
676+
* string when the URL cannot be determined so callers can fall through to
677+
* normal fetch behavior without throwing.
678+
*/
679+
function getFetchInputUrl(input: Parameters<typeof fetch>[0]): string {
680+
if (typeof input === "string") {
681+
return input;
682+
}
683+
if (input instanceof URL) {
684+
return input.toString();
685+
}
686+
if (typeof input === "object" && input !== null && "url" in input) {
687+
const possibleUrl = (input as { url?: unknown }).url;
688+
if (typeof possibleUrl === "string") {
689+
return possibleUrl;
690+
}
691+
}
692+
return "";
693+
}
694+
668695
// ---------------------------------------------------------------------------
669696
// Content extraction
670697
// ---------------------------------------------------------------------------
@@ -1193,21 +1220,7 @@ export class ProviderModelFactory {
11931220
init?: Parameters<typeof fetch>[1]
11941221
): Promise<Response> => {
11951222
try {
1196-
const urlString = (() => {
1197-
if (typeof input === "string") {
1198-
return input;
1199-
}
1200-
if (input instanceof URL) {
1201-
return input.toString();
1202-
}
1203-
if (typeof input === "object" && input !== null && "url" in input) {
1204-
const possibleUrl = (input as { url?: unknown }).url;
1205-
if (typeof possibleUrl === "string") {
1206-
return possibleUrl;
1207-
}
1208-
}
1209-
return "";
1210-
})();
1223+
const urlString = getFetchInputUrl(input);
12111224

12121225
const method = (init?.method ?? "GET").toUpperCase();
12131226
const isOpenAIResponses = /\/v1\/responses(\?|$)/.test(urlString);
@@ -1667,21 +1680,7 @@ export class ProviderModelFactory {
16671680
headers.set("Authorization", `Bearer ${resolvedApiKey ?? ""}`);
16681681
headers.set("Openai-Intent", "conversation-edits");
16691682

1670-
const urlString = (() => {
1671-
if (typeof input === "string") {
1672-
return input;
1673-
}
1674-
if (input instanceof URL) {
1675-
return input.toString();
1676-
}
1677-
if (typeof input === "object" && input !== null && "url" in input) {
1678-
const possibleUrl = (input as { url?: unknown }).url;
1679-
if (typeof possibleUrl === "string") {
1680-
return possibleUrl;
1681-
}
1682-
}
1683-
return "";
1684-
})();
1683+
const urlString = getFetchInputUrl(input);
16851684

16861685
const method = (
16871686
init?.method ?? (input instanceof Request ? input.method : "GET")

0 commit comments

Comments
 (0)