diff --git a/packages/rest/__tests__/UndiciRequest.test.ts b/packages/rest/__tests__/UndiciRequest.test.ts index 783555ec8..95e0f4b8d 100644 --- a/packages/rest/__tests__/UndiciRequest.test.ts +++ b/packages/rest/__tests__/UndiciRequest.test.ts @@ -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); diff --git a/packages/rest/src/strategies/undiciRequest.ts b/packages/rest/src/strategies/undiciRequest.ts index 76e42b673..5eee90c5e 100644 --- a/packages/rest/src/strategies/undiciRequest.ts +++ b/packages/rest/src/strategies/undiciRequest.ts @@ -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)[Symbol.iterator]) { const chunks = [...(body as Iterable)]; @@ -70,3 +72,17 @@ export async function resolveBody(body: RequestInit['body']): Promise