fix: normalize abort signals for fetch
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { resolveFetch } from "../infra/fetch.js";
|
||||
|
||||
const DISCORD_API_BASE = "https://discord.com/api/v10";
|
||||
|
||||
type DiscordApiErrorPayload = {
|
||||
@@ -48,7 +50,11 @@ export async function fetchDiscord<T>(
|
||||
token: string,
|
||||
fetcher: typeof fetch = fetch,
|
||||
): Promise<T> {
|
||||
const res = await fetcher(`${DISCORD_API_BASE}${path}`, {
|
||||
const fetchImpl = resolveFetch(fetcher);
|
||||
if (!fetchImpl) {
|
||||
throw new Error("fetch is not available");
|
||||
}
|
||||
const res = await fetchImpl(`${DISCORD_API_BASE}${path}`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
});
|
||||
if (!res.ok) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { resolveFetch } from "../infra/fetch.js";
|
||||
import { normalizeDiscordToken } from "./token.js";
|
||||
|
||||
const DISCORD_API_BASE = "https://discord.com/api/v10";
|
||||
@@ -90,10 +91,14 @@ async function fetchWithTimeout(
|
||||
fetcher: typeof fetch,
|
||||
headers?: HeadersInit,
|
||||
): Promise<Response> {
|
||||
const fetchImpl = resolveFetch(fetcher);
|
||||
if (!fetchImpl) {
|
||||
throw new Error("fetch is not available");
|
||||
}
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
||||
try {
|
||||
return await fetcher(url, { signal: controller.signal, headers });
|
||||
return await fetchImpl(url, { signal: controller.signal, headers });
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user