diff --git a/extensions/mattermost/src/mattermost/monitor.ts b/extensions/mattermost/src/mattermost/monitor.ts index c132f902e69..b84b5dae9a8 100644 --- a/extensions/mattermost/src/mattermost/monitor.ts +++ b/extensions/mattermost/src/mattermost/monitor.ts @@ -63,7 +63,6 @@ export type MonitorMattermostOpts = { webSocketFactory?: MattermostWebSocketFactory; }; -type FetchLike = (input: URL | RequestInfo, init?: RequestInit) => Promise; type MediaKind = "image" | "audio" | "video" | "document" | "unknown"; type MattermostReaction = { @@ -224,12 +223,6 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {} log: (message) => logVerboseMessage(message), }); - const fetchWithAuth: FetchLike = (input, init) => { - const headers = new Headers(init?.headers); - headers.set("Authorization", `Bearer ${client.token}`); - return fetch(input, { ...init, headers }); - }; - const resolveMattermostMedia = async ( fileIds?: string[] | null, ): Promise => { @@ -242,7 +235,11 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {} try { const fetched = await core.channel.media.fetchRemoteMedia({ url: `${client.apiBaseUrl}/files/${fileId}`, - fetchImpl: fetchWithAuth, + requestInit: { + headers: { + Authorization: `Bearer ${client.token}`, + }, + }, filePathHint: fileId, maxBytes: mediaMaxBytes, }); diff --git a/src/media/fetch.ts b/src/media/fetch.ts index 158e6d88d57..2991cda5bea 100644 --- a/src/media/fetch.ts +++ b/src/media/fetch.ts @@ -27,6 +27,7 @@ export type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promis type FetchMediaOptions = { url: string; fetchImpl?: FetchLike; + requestInit?: RequestInit; filePathHint?: string; maxBytes?: number; maxRedirects?: number; @@ -79,7 +80,16 @@ async function readErrorBodySnippet(res: Response, maxChars = 200): Promise { - const { url, fetchImpl, filePathHint, maxBytes, maxRedirects, ssrfPolicy, lookupFn } = options; + const { + url, + fetchImpl, + requestInit, + filePathHint, + maxBytes, + maxRedirects, + ssrfPolicy, + lookupFn, + } = options; let res: Response; let finalUrl = url; @@ -88,6 +98,7 @@ export async function fetchRemoteMedia(options: FetchMediaOptions): Promise