Skip to content

Commit 8f4256d

Browse files
authored
refactor(REST): remove double classing (#9722)
* refactor(REST): remove double classing BREAKING CHANGE: `REST` and `RequestManager` have been combined, most of the properties, methods, and events from both classes can now be found on `REST` BREAKING CHANGE: `REST#raw` has been removed in favor of `REST#queueRequest` BREAKING CHANGE: `REST#getAgent` has been removed in favor of `REST#agent` * chore: update for /rest changes
1 parent 6307f81 commit 8f4256d

19 files changed

+759
-817
lines changed

packages/proxy/src/handlers/proxyRequests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function proxyRequests(rest: REST): RequestHandler {
3333
}
3434

3535
try {
36-
const discordResponse = await rest.raw({
36+
const discordResponse = await rest.queueRequest({
3737
body: req,
3838
fullRoute,
3939
// This type cast is technically incorrect, but we want Discord to throw Method Not Allowed for us

packages/rest/__tests__/BurstHandler.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ const responseOptions: MockInterceptor.MockResponseOptions = {
4040
test('Interaction callback creates burst handler', async () => {
4141
mockPool.intercept({ path: callbackPath, method: 'POST' }).reply(200);
4242

43-
expect(api.requestManager.handlers.get(callbackKey)).toBe(undefined);
43+
expect(api.handlers.get(callbackKey)).toBe(undefined);
4444
expect(
4545
await api.post('/interactions/1234567890123456789/totallyarealtoken/callback', {
4646
auth: false,
4747
body: { type: 4, data: { content: 'Reply' } },
4848
}),
4949
// TODO: This should be ArrayBuffer, there is a bug in undici request
5050
).toBeInstanceOf(Uint8Array);
51-
expect(api.requestManager.handlers.get(callbackKey)).toBeInstanceOf(BurstHandler);
51+
expect(api.handlers.get(callbackKey)).toBeInstanceOf(BurstHandler);
5252
});
5353

5454
test('Requests are handled in bursts', async () => {

packages/rest/__tests__/RequestHandler.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ test('Significant Invalid Requests', async () => {
106106
await expect(e).rejects.toThrowError('Missing Permissions');
107107
expect(invalidListener).toHaveBeenCalledTimes(0);
108108
// eslint-disable-next-line require-atomic-updates
109-
api.requestManager.options.invalidRequestWarningInterval = 2;
109+
api.options.invalidRequestWarningInterval = 2;
110110

111111
const [f, g, h, i, j] = [
112112
api.get('/badRequest'),
@@ -504,7 +504,7 @@ test('Unauthorized', async () => {
504504
.reply(401, { message: '401: Unauthorized', code: 0 }, responseOptions)
505505
.times(2);
506506

507-
const setTokenSpy = vitest.spyOn(invalidAuthApi.requestManager, 'setToken');
507+
const setTokenSpy = vitest.spyOn(invalidAuthApi, 'setToken');
508508

509509
// Ensure authless requests don't reset the token
510510
const promiseWithoutTokenClear = invalidAuthApi.get('/unauthorized', { auth: false });

packages/rest/__tests__/RequestManager.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ test('no token', async () => {
3636
test('negative offset', () => {
3737
const badREST = new REST({ offset: -5_000 });
3838

39-
expect(badREST.requestManager.options.offset).toEqual(0);
39+
expect(badREST.options.offset).toEqual(0);
4040
});

packages/rest/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import { Blob } from 'node:buffer';
12
import { shouldUseGlobalFetchAndWebSocket } from '@discordjs/util';
3+
import { FormData } from 'undici';
24
import { setDefaultStrategy } from './environment.js';
35
import { makeRequest } from './strategies/undiciRequest.js';
46

7+
// TODO(ckohen): remove once node engine req is bumped to > v18
8+
(globalThis as any).FormData ??= FormData;
9+
globalThis.Blob ??= Blob;
10+
511
setDefaultStrategy(shouldUseGlobalFetchAndWebSocket() ? fetch : makeRequest);
612

713
export * from './shared.js';

0 commit comments

Comments
 (0)