Skip to content

Commit

Permalink
refactor(REST): remove double classing (#9722)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ckohen authored Jul 25, 2023
1 parent 6307f81 commit 8f4256d
Show file tree
Hide file tree
Showing 19 changed files with 759 additions and 817 deletions.
2 changes: 1 addition & 1 deletion packages/proxy/src/handlers/proxyRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function proxyRequests(rest: REST): RequestHandler {
}

try {
const discordResponse = await rest.raw({
const discordResponse = await rest.queueRequest({
body: req,
fullRoute,
// This type cast is technically incorrect, but we want Discord to throw Method Not Allowed for us
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/__tests__/BurstHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ const responseOptions: MockInterceptor.MockResponseOptions = {
test('Interaction callback creates burst handler', async () => {
mockPool.intercept({ path: callbackPath, method: 'POST' }).reply(200);

expect(api.requestManager.handlers.get(callbackKey)).toBe(undefined);
expect(api.handlers.get(callbackKey)).toBe(undefined);
expect(
await api.post('/interactions/1234567890123456789/totallyarealtoken/callback', {
auth: false,
body: { type: 4, data: { content: 'Reply' } },
}),
// TODO: This should be ArrayBuffer, there is a bug in undici request
).toBeInstanceOf(Uint8Array);
expect(api.requestManager.handlers.get(callbackKey)).toBeInstanceOf(BurstHandler);
expect(api.handlers.get(callbackKey)).toBeInstanceOf(BurstHandler);
});

test('Requests are handled in bursts', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/__tests__/RequestHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test('Significant Invalid Requests', async () => {
await expect(e).rejects.toThrowError('Missing Permissions');
expect(invalidListener).toHaveBeenCalledTimes(0);
// eslint-disable-next-line require-atomic-updates
api.requestManager.options.invalidRequestWarningInterval = 2;
api.options.invalidRequestWarningInterval = 2;

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

const setTokenSpy = vitest.spyOn(invalidAuthApi.requestManager, 'setToken');
const setTokenSpy = vitest.spyOn(invalidAuthApi, 'setToken');

// Ensure authless requests don't reset the token
const promiseWithoutTokenClear = invalidAuthApi.get('/unauthorized', { auth: false });
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/__tests__/RequestManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ test('no token', async () => {
test('negative offset', () => {
const badREST = new REST({ offset: -5_000 });

expect(badREST.requestManager.options.offset).toEqual(0);
expect(badREST.options.offset).toEqual(0);
});
6 changes: 6 additions & 0 deletions packages/rest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Blob } from 'node:buffer';
import { shouldUseGlobalFetchAndWebSocket } from '@discordjs/util';
import { FormData } from 'undici';
import { setDefaultStrategy } from './environment.js';
import { makeRequest } from './strategies/undiciRequest.js';

// TODO(ckohen): remove once node engine req is bumped to > v18
(globalThis as any).FormData ??= FormData;
globalThis.Blob ??= Blob;

setDefaultStrategy(shouldUseGlobalFetchAndWebSocket() ? fetch : makeRequest);

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

0 comments on commit 8f4256d

Please sign in to comment.