mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16: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,6 +1,6 @@
|
||||
import { Blob, Buffer } from 'node:buffer';
|
||||
import { URLSearchParams } from 'node:url';
|
||||
import { MockAgent, setGlobalDispatcher } from 'undici';
|
||||
import { MockAgent, setGlobalDispatcher, FormData as UndiciFormData } from 'undici';
|
||||
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor.js';
|
||||
import { beforeEach, afterEach, test, expect, vitest } from 'vitest';
|
||||
import { REST } from '../src/index.js';
|
||||
@@ -77,6 +77,26 @@ test('resolveBody', async () => {
|
||||
};
|
||||
await expect(resolveBody(asyncIterable)).resolves.toStrictEqual(Buffer.from([1, 2, 3, 1, 2, 3, 1, 2, 3]));
|
||||
|
||||
{
|
||||
const fd = new globalThis.FormData();
|
||||
fd.append('key', 'value');
|
||||
|
||||
const resolved = await resolveBody(fd);
|
||||
|
||||
expect(resolved).toBeInstanceOf(UndiciFormData);
|
||||
expect([...(resolved as UndiciFormData).entries()]).toStrictEqual([['key', 'value']]);
|
||||
}
|
||||
|
||||
{
|
||||
const ufd = new UndiciFormData();
|
||||
ufd.append('key', 'value');
|
||||
|
||||
const resolved = await resolveBody(ufd);
|
||||
|
||||
expect(resolved).toBeInstanceOf(UndiciFormData);
|
||||
expect([...(resolved as UndiciFormData).entries()]).toStrictEqual([['key', 'value']]);
|
||||
}
|
||||
|
||||
// Unknown type
|
||||
// @ts-expect-error: This test is ensuring that this throws
|
||||
await expect(resolveBody(true)).rejects.toThrow(TypeError);
|
||||
|
||||
@@ -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