Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@across-protocol/sdk",
"author": "UMA Team",
"version": "4.3.137",
"version": "4.3.138",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/reference/sdk",
"repository": {
Expand Down
41 changes: 37 additions & 4 deletions src/utils/FetchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const toStringRecord = (headers: FetchHeaders): Record<string, string> => {
const normalizedHeaders: Record<string, string> = {};
for (const [key, value] of Object.entries(headers)) {
if (value !== undefined && value !== null) {
normalizedHeaders[key] = String(value);
normalizedHeaders[key.toLowerCase()] = String(value);
}
}
return normalizedHeaders;
Expand All @@ -24,15 +24,19 @@ const applyQueryParams = (url: string, params: FetchQueryParams): string => {
return parsedUrl.toString();
};

export async function fetchWithTimeout<T = unknown>(
async function baseFetch<T = unknown>(
url: string,
params: FetchQueryParams = {},
headers: FetchHeaders = {},
method: string,
body: string | undefined,
params: FetchQueryParams,
headers: FetchHeaders,
timeout?: number,
responseType: "json" | "text" = "json"
): Promise<T> {
const fullUrl = applyQueryParams(url, params);
const response = await fetch(fullUrl, {
method,
body,
headers: toStringRecord(headers),
...(timeout && timeout > 0 && { signal: AbortSignal.timeout(timeout) }),
});
Expand Down Expand Up @@ -64,3 +68,32 @@ export async function fetchWithTimeout<T = unknown>(
);
}
}

export function fetchWithTimeout<T = unknown>(
url: string,
params: FetchQueryParams = {},
headers: FetchHeaders = {},
timeout?: number,
responseType: "json" | "text" = "json"
): Promise<T> {
return baseFetch<T>(url, "GET", undefined, params, headers, timeout, responseType);
}

export function postWithTimeout<T = unknown>(
url: string,
body: unknown,
params: FetchQueryParams = {},
headers: FetchHeaders = {},
timeout?: number,
responseType: "json" | "text" = "json"
): Promise<T> {
return baseFetch<T>(
url,
"POST",
JSON.stringify(body),
params,
{ "Content-Type": "application/json", ...headers },
timeout,
responseType
);
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5984,7 +5984,7 @@ fn.name@1.x.x:
resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc"
integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==

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