mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 17:43:30 +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:
@@ -23,7 +23,7 @@
|
||||
|
||||
## Installation
|
||||
|
||||
**Node.js 16.9.0 or newer is required.**
|
||||
**Node.js 18.12.0 or newer is required.**
|
||||
|
||||
```sh
|
||||
npm install @discordjs/proxy
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
"devDependencies": {
|
||||
"@favware/cliff-jumper": "^2.0.0",
|
||||
"@microsoft/api-extractor": "^7.34.8",
|
||||
"@types/node": "16.18.25",
|
||||
"@types/node": "18.15.11",
|
||||
"@types/supertest": "^2.0.12",
|
||||
"@vitest/coverage-c8": "^0.31.0",
|
||||
"cross-env": "^7.0.3",
|
||||
@@ -79,7 +79,7 @@
|
||||
"vitest": "^0.31.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16.9.0"
|
||||
"node": ">=18.12.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
|
||||
@@ -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