mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
refactor(rest): switch api to fetch-like and provide strategies (#9416)
BREAKING CHANGE: NodeJS v18+ is required when using node due to the use of global `fetch` BREAKING CHANGE: The raw method of REST now returns a web compatible `Respone` object. BREAKING CHANGE: The `parseResponse` utility method has been updated to operate on a web compatible `Response` object. BREAKING CHANGE: Many underlying internals have changed, some of which were exported. BREAKING CHANGE: `DefaultRestOptions` used to contain a default `agent`, which is now set to `null` instead.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { ServerResponse } from 'node:http';
|
||||
import { Readable } from 'node:stream';
|
||||
import { pipeline } from 'node:stream/promises';
|
||||
import { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';
|
||||
import type { Dispatcher } from 'undici';
|
||||
import { DiscordAPIError, HTTPError, RateLimitError, type ResponseLike } from '@discordjs/rest';
|
||||
|
||||
/**
|
||||
* Populates a server response with the data from a Discord 2xx REST response
|
||||
@@ -9,19 +9,21 @@ import type { Dispatcher } from 'undici';
|
||||
* @param res - The server response to populate
|
||||
* @param data - The data to populate the response with
|
||||
*/
|
||||
export async function populateSuccessfulResponse(res: ServerResponse, data: Dispatcher.ResponseData): Promise<void> {
|
||||
res.statusCode = data.statusCode;
|
||||
export async function populateSuccessfulResponse(res: ServerResponse, data: ResponseLike): Promise<void> {
|
||||
res.statusCode = data.status;
|
||||
|
||||
for (const header of Object.keys(data.headers)) {
|
||||
for (const [header, value] of data.headers) {
|
||||
// Strip ratelimit headers
|
||||
if (header.startsWith('x-ratelimit')) {
|
||||
if (/^x-ratelimit/i.test(header)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
res.setHeader(header, data.headers[header]!);
|
||||
res.setHeader(header, value);
|
||||
}
|
||||
|
||||
await pipeline(data.body, res);
|
||||
if (data.body) {
|
||||
await pipeline(data.body instanceof Readable ? data.body : Readable.fromWeb(data.body), res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user