Skip to content

Commit 7488859

Browse files
nicholaspaiclaude
andauthored
feat: Add postWithTimeout to FetchUtils (#1373)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 537d407 commit 7488859

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@across-protocol/sdk",
33
"author": "UMA Team",
4-
"version": "4.3.137",
4+
"version": "4.3.138",
55
"license": "AGPL-3.0",
66
"homepage": "https://docs.across.to/reference/sdk",
77
"repository": {

src/utils/FetchUtils.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const toStringRecord = (headers: FetchHeaders): Record<string, string> => {
55
const normalizedHeaders: Record<string, string> = {};
66
for (const [key, value] of Object.entries(headers)) {
77
if (value !== undefined && value !== null) {
8-
normalizedHeaders[key] = String(value);
8+
normalizedHeaders[key.toLowerCase()] = String(value);
99
}
1010
}
1111
return normalizedHeaders;
@@ -24,15 +24,19 @@ const applyQueryParams = (url: string, params: FetchQueryParams): string => {
2424
return parsedUrl.toString();
2525
};
2626

27-
export async function fetchWithTimeout<T = unknown>(
27+
async function baseFetch<T = unknown>(
2828
url: string,
29-
params: FetchQueryParams = {},
30-
headers: FetchHeaders = {},
29+
method: string,
30+
body: string | undefined,
31+
params: FetchQueryParams,
32+
headers: FetchHeaders,
3133
timeout?: number,
3234
responseType: "json" | "text" = "json"
3335
): Promise<T> {
3436
const fullUrl = applyQueryParams(url, params);
3537
const response = await fetch(fullUrl, {
38+
method,
39+
body,
3640
headers: toStringRecord(headers),
3741
...(timeout && timeout > 0 && { signal: AbortSignal.timeout(timeout) }),
3842
});
@@ -64,3 +68,32 @@ export async function fetchWithTimeout<T = unknown>(
6468
);
6569
}
6670
}
71+
72+
export function fetchWithTimeout<T = unknown>(
73+
url: string,
74+
params: FetchQueryParams = {},
75+
headers: FetchHeaders = {},
76+
timeout?: number,
77+
responseType: "json" | "text" = "json"
78+
): Promise<T> {
79+
return baseFetch<T>(url, "GET", undefined, params, headers, timeout, responseType);
80+
}
81+
82+
export function postWithTimeout<T = unknown>(
83+
url: string,
84+
body: unknown,
85+
params: FetchQueryParams = {},
86+
headers: FetchHeaders = {},
87+
timeout?: number,
88+
responseType: "json" | "text" = "json"
89+
): Promise<T> {
90+
return baseFetch<T>(
91+
url,
92+
"POST",
93+
JSON.stringify(body),
94+
params,
95+
{ "Content-Type": "application/json", ...headers },
96+
timeout,
97+
responseType
98+
);
99+
}

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5984,7 +5984,7 @@ fn.name@1.x.x:
59845984
resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc"
59855985
integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==
59865986

5987-
follow-redirects@^1.12.1, follow-redirects@^1.14.0, follow-redirects@^1.15.4, follow-redirects@^1.15.6:
5987+
follow-redirects@^1.12.1, follow-redirects@^1.14.0, follow-redirects@^1.15.4:
59885988
version "1.15.9"
59895989
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1"
59905990
integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==

0 commit comments

Comments
 (0)