mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 19:13:31 +01:00
fix: attachment sending (#11015)
* fix: attachment sending * test: add tests --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { STATUS_CODES } from 'node:http';
|
||||
import { URLSearchParams } from 'node:url';
|
||||
import { types } from 'node:util';
|
||||
import { type RequestInit, request, Headers } from 'undici';
|
||||
import { type RequestInit, request, Headers, FormData as UndiciFormData } from 'undici';
|
||||
import type { HeaderRecord } from 'undici/types/header.js';
|
||||
import type { ResponseLike } from '../shared.js';
|
||||
|
||||
@@ -52,8 +52,10 @@ export async function resolveBody(body: RequestInit['body']): Promise<Exclude<Re
|
||||
return new Uint8Array(body.buffer);
|
||||
} else if (body instanceof Blob) {
|
||||
return new Uint8Array(await body.arrayBuffer());
|
||||
} else if (body instanceof FormData) {
|
||||
} else if (body instanceof UndiciFormData) {
|
||||
return body;
|
||||
} else if (body instanceof FormData) {
|
||||
return globalToUndiciFormData(body);
|
||||
} else if ((body as Iterable<Uint8Array>)[Symbol.iterator]) {
|
||||
const chunks = [...(body as Iterable<Uint8Array>)];
|
||||
|
||||
@@ -70,3 +72,17 @@ export async function resolveBody(body: RequestInit['body']): Promise<Exclude<Re
|
||||
|
||||
throw new TypeError(`Unable to resolve body.`);
|
||||
}
|
||||
|
||||
function globalToUndiciFormData(fd: globalThis.FormData): UndiciFormData {
|
||||
const clone = new UndiciFormData();
|
||||
|
||||
for (const [name, value] of fd.entries()) {
|
||||
if (typeof value === 'string') {
|
||||
clone.append(name, value);
|
||||
} else {
|
||||
clone.append(name, value, value.name);
|
||||
}
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user