@discordjs/rest 1.2.0-dev.1661256247-28bc4f4.0 → 1.2.0-dev.1662293018-8b3d006.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +839 -9
- package/dist/index.js +1009 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +965 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -21
- package/dist/index.cjs +0 -31
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/lib/CDN.cjs +0 -87
- package/dist/lib/CDN.cjs.map +0 -1
- package/dist/lib/CDN.d.ts +0 -199
- package/dist/lib/CDN.d.ts.map +0 -1
- package/dist/lib/CDN.mjs +0 -83
- package/dist/lib/CDN.mjs.map +0 -1
- package/dist/lib/REST.cjs +0 -61
- package/dist/lib/REST.cjs.map +0 -1
- package/dist/lib/REST.d.ts +0 -279
- package/dist/lib/REST.d.ts.map +0 -1
- package/dist/lib/REST.mjs +0 -57
- package/dist/lib/REST.mjs.map +0 -1
- package/dist/lib/RequestManager.cjs +0 -223
- package/dist/lib/RequestManager.cjs.map +0 -1
- package/dist/lib/RequestManager.d.ts +0 -232
- package/dist/lib/RequestManager.d.ts.map +0 -1
- package/dist/lib/RequestManager.mjs +0 -218
- package/dist/lib/RequestManager.mjs.map +0 -1
- package/dist/lib/errors/DiscordAPIError.cjs +0 -55
- package/dist/lib/errors/DiscordAPIError.cjs.map +0 -1
- package/dist/lib/errors/DiscordAPIError.d.ts +0 -53
- package/dist/lib/errors/DiscordAPIError.d.ts.map +0 -1
- package/dist/lib/errors/DiscordAPIError.mjs +0 -51
- package/dist/lib/errors/DiscordAPIError.mjs.map +0 -1
- package/dist/lib/errors/HTTPError.cjs +0 -17
- package/dist/lib/errors/HTTPError.cjs.map +0 -1
- package/dist/lib/errors/HTTPError.d.ts +0 -21
- package/dist/lib/errors/HTTPError.d.ts.map +0 -1
- package/dist/lib/errors/HTTPError.mjs +0 -13
- package/dist/lib/errors/HTTPError.mjs.map +0 -1
- package/dist/lib/errors/RateLimitError.cjs +0 -23
- package/dist/lib/errors/RateLimitError.cjs.map +0 -1
- package/dist/lib/errors/RateLimitError.d.ts +0 -17
- package/dist/lib/errors/RateLimitError.d.ts.map +0 -1
- package/dist/lib/errors/RateLimitError.mjs +0 -19
- package/dist/lib/errors/RateLimitError.mjs.map +0 -1
- package/dist/lib/handlers/IHandler.d.ts +0 -23
- package/dist/lib/handlers/IHandler.d.ts.map +0 -1
- package/dist/lib/handlers/SequentialHandler.cjs +0 -305
- package/dist/lib/handlers/SequentialHandler.cjs.map +0 -1
- package/dist/lib/handlers/SequentialHandler.d.ts +0 -83
- package/dist/lib/handlers/SequentialHandler.d.ts.map +0 -1
- package/dist/lib/handlers/SequentialHandler.mjs +0 -301
- package/dist/lib/handlers/SequentialHandler.mjs.map +0 -1
- package/dist/lib/utils/constants.cjs +0 -49
- package/dist/lib/utils/constants.cjs.map +0 -1
- package/dist/lib/utils/constants.d.ts +0 -21
- package/dist/lib/utils/constants.d.ts.map +0 -1
- package/dist/lib/utils/constants.mjs +0 -49
- package/dist/lib/utils/constants.mjs.map +0 -1
- package/dist/lib/utils/utils.cjs +0 -111
- package/dist/lib/utils/utils.cjs.map +0 -1
- package/dist/lib/utils/utils.d.ts +0 -32
- package/dist/lib/utils/utils.d.ts.map +0 -1
- package/dist/lib/utils/utils.mjs +0 -103
- package/dist/lib/utils/utils.mjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,839 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { Collection } from '@discordjs/collection';
|
|
3
|
+
import { Dispatcher, BodyInit, Agent, request } from 'undici';
|
|
4
|
+
import { Buffer } from 'node:buffer';
|
|
5
|
+
import { URLSearchParams } from 'node:url';
|
|
6
|
+
|
|
7
|
+
interface IHandler {
|
|
8
|
+
/**
|
|
9
|
+
* The unique id of the handler
|
|
10
|
+
*/
|
|
11
|
+
readonly id: string;
|
|
12
|
+
/**
|
|
13
|
+
* If the bucket is currently inactive (no pending requests)
|
|
14
|
+
*/
|
|
15
|
+
get inactive(): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Queues a request to be sent
|
|
18
|
+
*
|
|
19
|
+
* @param routeId - The generalized api route with literal ids for major parameters
|
|
20
|
+
* @param url - The url to do the request on
|
|
21
|
+
* @param options - All the information needed to make a request
|
|
22
|
+
* @param requestData - Extra data from the user's request needed for errors and additional processing
|
|
23
|
+
*/
|
|
24
|
+
queueRequest(routeId: RouteData, url: string, options: RequestOptions, requestData: HandlerRequestData): Promise<Dispatcher.ResponseData>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Represents a file to be added to the request
|
|
29
|
+
*/
|
|
30
|
+
interface RawFile {
|
|
31
|
+
/**
|
|
32
|
+
* Content-Type of the file
|
|
33
|
+
*/
|
|
34
|
+
contentType?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The actual data for the file
|
|
37
|
+
*/
|
|
38
|
+
data: Buffer | boolean | number | string;
|
|
39
|
+
/**
|
|
40
|
+
* An explicit key to use for key of the formdata field for this file.
|
|
41
|
+
* When not provided, the index of the file in the files array is used in the form `files[${index}]`.
|
|
42
|
+
* If you wish to alter the placeholder snowflake, you must provide this property in the same form (`files[${placeholder}]`)
|
|
43
|
+
*/
|
|
44
|
+
key?: string;
|
|
45
|
+
/**
|
|
46
|
+
* The name of the file
|
|
47
|
+
*/
|
|
48
|
+
name: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Represents possible data to be given to an endpoint
|
|
52
|
+
*/
|
|
53
|
+
interface RequestData {
|
|
54
|
+
/**
|
|
55
|
+
* Whether to append JSON data to form data instead of `payload_json` when sending files
|
|
56
|
+
*/
|
|
57
|
+
appendToFormData?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* If this request needs the `Authorization` header
|
|
60
|
+
*
|
|
61
|
+
* @defaultValue `true`
|
|
62
|
+
*/
|
|
63
|
+
auth?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* The authorization prefix to use for this request, useful if you use this with bearer tokens
|
|
66
|
+
*
|
|
67
|
+
* @defaultValue `'Bot'`
|
|
68
|
+
*/
|
|
69
|
+
authPrefix?: 'Bearer' | 'Bot';
|
|
70
|
+
/**
|
|
71
|
+
* The body to send to this request.
|
|
72
|
+
* If providing as BodyInit, set `passThroughBody: true`
|
|
73
|
+
*/
|
|
74
|
+
body?: BodyInit | unknown;
|
|
75
|
+
/**
|
|
76
|
+
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} to use for the request.
|
|
77
|
+
*/
|
|
78
|
+
dispatcher?: Agent;
|
|
79
|
+
/**
|
|
80
|
+
* Files to be attached to this request
|
|
81
|
+
*/
|
|
82
|
+
files?: RawFile[] | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Additional headers to add to this request
|
|
85
|
+
*/
|
|
86
|
+
headers?: Record<string, string>;
|
|
87
|
+
/**
|
|
88
|
+
* Whether to pass-through the body property directly to `fetch()`.
|
|
89
|
+
* <warn>This only applies when files is NOT present</warn>
|
|
90
|
+
*/
|
|
91
|
+
passThroughBody?: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Query string parameters to append to the called endpoint
|
|
94
|
+
*/
|
|
95
|
+
query?: URLSearchParams;
|
|
96
|
+
/**
|
|
97
|
+
* Reason to show in the audit logs
|
|
98
|
+
*/
|
|
99
|
+
reason?: string;
|
|
100
|
+
/**
|
|
101
|
+
* If this request should be versioned
|
|
102
|
+
*
|
|
103
|
+
* @defaultValue `true`
|
|
104
|
+
*/
|
|
105
|
+
versioned?: boolean;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Possible headers for an API call
|
|
109
|
+
*/
|
|
110
|
+
interface RequestHeaders {
|
|
111
|
+
Authorization?: string;
|
|
112
|
+
'User-Agent': string;
|
|
113
|
+
'X-Audit-Log-Reason'?: string;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Possible API methods to be used when doing requests
|
|
117
|
+
*/
|
|
118
|
+
declare const enum RequestMethod {
|
|
119
|
+
Delete = "DELETE",
|
|
120
|
+
Get = "GET",
|
|
121
|
+
Patch = "PATCH",
|
|
122
|
+
Post = "POST",
|
|
123
|
+
Put = "PUT"
|
|
124
|
+
}
|
|
125
|
+
declare type RouteLike = `/${string}`;
|
|
126
|
+
/**
|
|
127
|
+
* Internal request options
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
interface InternalRequest extends RequestData {
|
|
132
|
+
fullRoute: RouteLike;
|
|
133
|
+
method: RequestMethod;
|
|
134
|
+
}
|
|
135
|
+
declare type HandlerRequestData = Pick<InternalRequest, 'auth' | 'body' | 'files'>;
|
|
136
|
+
/**
|
|
137
|
+
* Parsed route data for an endpoint
|
|
138
|
+
*
|
|
139
|
+
* @internal
|
|
140
|
+
*/
|
|
141
|
+
interface RouteData {
|
|
142
|
+
bucketRoute: string;
|
|
143
|
+
majorParameter: string;
|
|
144
|
+
original: RouteLike;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Represents a hash and its associated fields
|
|
148
|
+
*
|
|
149
|
+
* @internal
|
|
150
|
+
*/
|
|
151
|
+
interface HashData {
|
|
152
|
+
lastAccess: number;
|
|
153
|
+
value: string;
|
|
154
|
+
}
|
|
155
|
+
interface RequestManager {
|
|
156
|
+
emit: (<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]) => boolean) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]) => boolean);
|
|
157
|
+
off: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
|
158
|
+
on: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
|
159
|
+
once: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
|
160
|
+
removeAllListeners: (<K extends keyof RestEvents>(event?: K) => this) & (<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>) => this);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Represents the class that manages handlers for endpoints
|
|
164
|
+
*/
|
|
165
|
+
declare class RequestManager extends EventEmitter {
|
|
166
|
+
#private;
|
|
167
|
+
/**
|
|
168
|
+
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} for all requests
|
|
169
|
+
* performed by this manager.
|
|
170
|
+
*/
|
|
171
|
+
agent: Dispatcher | null;
|
|
172
|
+
/**
|
|
173
|
+
* The number of requests remaining in the global bucket
|
|
174
|
+
*/
|
|
175
|
+
globalRemaining: number;
|
|
176
|
+
/**
|
|
177
|
+
* The promise used to wait out the global rate limit
|
|
178
|
+
*/
|
|
179
|
+
globalDelay: Promise<void> | null;
|
|
180
|
+
/**
|
|
181
|
+
* The timestamp at which the global bucket resets
|
|
182
|
+
*/
|
|
183
|
+
globalReset: number;
|
|
184
|
+
/**
|
|
185
|
+
* API bucket hashes that are cached from provided routes
|
|
186
|
+
*/
|
|
187
|
+
readonly hashes: Collection<string, HashData>;
|
|
188
|
+
/**
|
|
189
|
+
* Request handlers created from the bucket hash and the major parameters
|
|
190
|
+
*/
|
|
191
|
+
readonly handlers: Collection<string, IHandler>;
|
|
192
|
+
private hashTimer;
|
|
193
|
+
private handlerTimer;
|
|
194
|
+
readonly options: RESTOptions;
|
|
195
|
+
constructor(options: Partial<RESTOptions>);
|
|
196
|
+
private setupSweepers;
|
|
197
|
+
/**
|
|
198
|
+
* Sets the default agent to use for requests performed by this manager
|
|
199
|
+
*
|
|
200
|
+
* @param agent - The agent to use
|
|
201
|
+
*/
|
|
202
|
+
setAgent(agent: Dispatcher): this;
|
|
203
|
+
/**
|
|
204
|
+
* Sets the authorization token that should be used for requests
|
|
205
|
+
*
|
|
206
|
+
* @param token - The authorization token to use
|
|
207
|
+
*/
|
|
208
|
+
setToken(token: string): this;
|
|
209
|
+
/**
|
|
210
|
+
* Queues a request to be sent
|
|
211
|
+
*
|
|
212
|
+
* @param request - All the information needed to make a request
|
|
213
|
+
* @returns The response from the api request
|
|
214
|
+
*/
|
|
215
|
+
queueRequest(request: InternalRequest): Promise<Dispatcher.ResponseData>;
|
|
216
|
+
/**
|
|
217
|
+
* Creates a new rate limit handler from a hash, based on the hash and the major parameter
|
|
218
|
+
*
|
|
219
|
+
* @param hash - The hash for the route
|
|
220
|
+
* @param majorParameter - The major parameter for this handler
|
|
221
|
+
* @internal
|
|
222
|
+
*/
|
|
223
|
+
private createHandler;
|
|
224
|
+
/**
|
|
225
|
+
* Formats the request data to a usable format for fetch
|
|
226
|
+
*
|
|
227
|
+
* @param request - The request data
|
|
228
|
+
*/
|
|
229
|
+
private resolveRequest;
|
|
230
|
+
/**
|
|
231
|
+
* Stops the hash sweeping interval
|
|
232
|
+
*/
|
|
233
|
+
clearHashSweeper(): void;
|
|
234
|
+
/**
|
|
235
|
+
* Stops the request handler sweeping interval
|
|
236
|
+
*/
|
|
237
|
+
clearHandlerSweeper(): void;
|
|
238
|
+
/**
|
|
239
|
+
* Generates route data for an endpoint:method
|
|
240
|
+
*
|
|
241
|
+
* @param endpoint - The raw endpoint to generalize
|
|
242
|
+
* @param method - The HTTP method this endpoint is called without
|
|
243
|
+
* @internal
|
|
244
|
+
*/
|
|
245
|
+
private static generateRouteData;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Options to be passed when creating the REST instance
|
|
250
|
+
*/
|
|
251
|
+
interface RESTOptions {
|
|
252
|
+
/**
|
|
253
|
+
* The agent to set globally
|
|
254
|
+
*/
|
|
255
|
+
agent: Dispatcher;
|
|
256
|
+
/**
|
|
257
|
+
* The base api path, without version
|
|
258
|
+
*
|
|
259
|
+
* @defaultValue `'https://discord.com/api'`
|
|
260
|
+
*/
|
|
261
|
+
api: string;
|
|
262
|
+
/**
|
|
263
|
+
* The authorization prefix to use for requests, useful if you want to use
|
|
264
|
+
* bearer tokens
|
|
265
|
+
*
|
|
266
|
+
* @defaultValue `'Bot'`
|
|
267
|
+
*/
|
|
268
|
+
authPrefix: 'Bearer' | 'Bot';
|
|
269
|
+
/**
|
|
270
|
+
* The cdn path
|
|
271
|
+
*
|
|
272
|
+
* @defaultValue 'https://cdn.discordapp.com'
|
|
273
|
+
*/
|
|
274
|
+
cdn: string;
|
|
275
|
+
/**
|
|
276
|
+
* How many requests to allow sending per second (Infinity for unlimited, 50 for the standard global limit used by Discord)
|
|
277
|
+
*
|
|
278
|
+
* @defaultValue `50`
|
|
279
|
+
*/
|
|
280
|
+
globalRequestsPerSecond: number;
|
|
281
|
+
/**
|
|
282
|
+
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 1h)
|
|
283
|
+
*
|
|
284
|
+
* @defaultValue `3_600_000`
|
|
285
|
+
*/
|
|
286
|
+
handlerSweepInterval: number;
|
|
287
|
+
/**
|
|
288
|
+
* The maximum amount of time a hash can exist in milliseconds without being hit with a request (defaults to 24h)
|
|
289
|
+
*
|
|
290
|
+
* @defaultValue `86_400_000`
|
|
291
|
+
*/
|
|
292
|
+
hashLifetime: number;
|
|
293
|
+
/**
|
|
294
|
+
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 4h)
|
|
295
|
+
*
|
|
296
|
+
* @defaultValue `14_400_000`
|
|
297
|
+
*/
|
|
298
|
+
hashSweepInterval: number;
|
|
299
|
+
/**
|
|
300
|
+
* Additional headers to send for all API requests
|
|
301
|
+
*
|
|
302
|
+
* @defaultValue `{}`
|
|
303
|
+
*/
|
|
304
|
+
headers: Record<string, string>;
|
|
305
|
+
/**
|
|
306
|
+
* The number of invalid REST requests (those that return 401, 403, or 429) in a 10 minute window between emitted warnings (0 for no warnings).
|
|
307
|
+
* That is, if set to 500, warnings will be emitted at invalid request number 500, 1000, 1500, and so on.
|
|
308
|
+
*
|
|
309
|
+
* @defaultValue `0`
|
|
310
|
+
*/
|
|
311
|
+
invalidRequestWarningInterval: number;
|
|
312
|
+
/**
|
|
313
|
+
* The extra offset to add to rate limits in milliseconds
|
|
314
|
+
*
|
|
315
|
+
* @defaultValue `50`
|
|
316
|
+
*/
|
|
317
|
+
offset: number;
|
|
318
|
+
/**
|
|
319
|
+
* Determines how rate limiting and pre-emptive throttling should be handled.
|
|
320
|
+
* When an array of strings, each element is treated as a prefix for the request route
|
|
321
|
+
* (e.g. `/channels` to match any route starting with `/channels` such as `/channels/:id/messages`)
|
|
322
|
+
* for which to throw {@link RateLimitError}s. All other request routes will be queued normally
|
|
323
|
+
*
|
|
324
|
+
* @defaultValue `null`
|
|
325
|
+
*/
|
|
326
|
+
rejectOnRateLimit: RateLimitQueueFilter | string[] | null;
|
|
327
|
+
/**
|
|
328
|
+
* The number of retries for errors with the 500 code, or errors
|
|
329
|
+
* that timeout
|
|
330
|
+
*
|
|
331
|
+
* @defaultValue `3`
|
|
332
|
+
*/
|
|
333
|
+
retries: number;
|
|
334
|
+
/**
|
|
335
|
+
* The time to wait in milliseconds before a request is aborted
|
|
336
|
+
*
|
|
337
|
+
* @defaultValue `15_000`
|
|
338
|
+
*/
|
|
339
|
+
timeout: number;
|
|
340
|
+
/**
|
|
341
|
+
* Extra information to add to the user agent
|
|
342
|
+
*
|
|
343
|
+
* @defaultValue `Node.js ${process.version}`
|
|
344
|
+
*/
|
|
345
|
+
userAgentAppendix: string;
|
|
346
|
+
/**
|
|
347
|
+
* The version of the API to use
|
|
348
|
+
*
|
|
349
|
+
* @defaultValue `'10'`
|
|
350
|
+
*/
|
|
351
|
+
version: string;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Data emitted on `RESTEvents.RateLimited`
|
|
355
|
+
*/
|
|
356
|
+
interface RateLimitData {
|
|
357
|
+
/**
|
|
358
|
+
* Whether the rate limit that was reached was the global limit
|
|
359
|
+
*/
|
|
360
|
+
global: boolean;
|
|
361
|
+
/**
|
|
362
|
+
* The bucket hash for this request
|
|
363
|
+
*/
|
|
364
|
+
hash: string;
|
|
365
|
+
/**
|
|
366
|
+
* The amount of requests we can perform before locking requests
|
|
367
|
+
*/
|
|
368
|
+
limit: number;
|
|
369
|
+
/**
|
|
370
|
+
* The major parameter of the route
|
|
371
|
+
*
|
|
372
|
+
* For example, in `/channels/x`, this will be `x`.
|
|
373
|
+
* If there is no major parameter (e.g: `/bot/gateway`) this will be `global`.
|
|
374
|
+
*/
|
|
375
|
+
majorParameter: string;
|
|
376
|
+
/**
|
|
377
|
+
* The HTTP method being performed
|
|
378
|
+
*/
|
|
379
|
+
method: string;
|
|
380
|
+
/**
|
|
381
|
+
* The route being hit in this request
|
|
382
|
+
*/
|
|
383
|
+
route: string;
|
|
384
|
+
/**
|
|
385
|
+
* The time, in milliseconds, until the request-lock is reset
|
|
386
|
+
*/
|
|
387
|
+
timeToReset: number;
|
|
388
|
+
/**
|
|
389
|
+
* The full URL for this request
|
|
390
|
+
*/
|
|
391
|
+
url: string;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* A function that determines whether the rate limit hit should throw an Error
|
|
395
|
+
*/
|
|
396
|
+
declare type RateLimitQueueFilter = (rateLimitData: RateLimitData) => Promise<boolean> | boolean;
|
|
397
|
+
interface APIRequest {
|
|
398
|
+
/**
|
|
399
|
+
* The data that was used to form the body of this request
|
|
400
|
+
*/
|
|
401
|
+
data: HandlerRequestData;
|
|
402
|
+
/**
|
|
403
|
+
* The HTTP method used in this request
|
|
404
|
+
*/
|
|
405
|
+
method: string;
|
|
406
|
+
/**
|
|
407
|
+
* Additional HTTP options for this request
|
|
408
|
+
*/
|
|
409
|
+
options: RequestOptions;
|
|
410
|
+
/**
|
|
411
|
+
* The full path used to make the request
|
|
412
|
+
*/
|
|
413
|
+
path: RouteLike;
|
|
414
|
+
/**
|
|
415
|
+
* The number of times this request has been attempted
|
|
416
|
+
*/
|
|
417
|
+
retries: number;
|
|
418
|
+
/**
|
|
419
|
+
* The API route identifying the ratelimit for this request
|
|
420
|
+
*/
|
|
421
|
+
route: string;
|
|
422
|
+
}
|
|
423
|
+
interface InvalidRequestWarningData {
|
|
424
|
+
/**
|
|
425
|
+
* Number of invalid requests that have been made in the window
|
|
426
|
+
*/
|
|
427
|
+
count: number;
|
|
428
|
+
/**
|
|
429
|
+
* Time in milliseconds remaining before the count resets
|
|
430
|
+
*/
|
|
431
|
+
remainingTime: number;
|
|
432
|
+
}
|
|
433
|
+
interface RestEvents {
|
|
434
|
+
handlerSweep: [sweptHandlers: Collection<string, IHandler>];
|
|
435
|
+
hashSweep: [sweptHashes: Collection<string, HashData>];
|
|
436
|
+
invalidRequestWarning: [invalidRequestInfo: InvalidRequestWarningData];
|
|
437
|
+
newListener: [name: string, listener: (...args: any) => void];
|
|
438
|
+
rateLimited: [rateLimitInfo: RateLimitData];
|
|
439
|
+
removeListener: [name: string, listener: (...args: any) => void];
|
|
440
|
+
response: [request: APIRequest, response: Dispatcher.ResponseData];
|
|
441
|
+
restDebug: [info: string];
|
|
442
|
+
}
|
|
443
|
+
declare type RequestOptions = Exclude<Parameters<typeof request>[1], undefined>;
|
|
444
|
+
interface REST {
|
|
445
|
+
emit: (<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]) => boolean) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]) => boolean);
|
|
446
|
+
off: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
|
447
|
+
on: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
|
448
|
+
once: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
|
449
|
+
removeAllListeners: (<K extends keyof RestEvents>(event?: K) => this) & (<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>) => this);
|
|
450
|
+
}
|
|
451
|
+
declare class REST extends EventEmitter {
|
|
452
|
+
readonly cdn: CDN;
|
|
453
|
+
readonly requestManager: RequestManager;
|
|
454
|
+
constructor(options?: Partial<RESTOptions>);
|
|
455
|
+
/**
|
|
456
|
+
* Gets the agent set for this instance
|
|
457
|
+
*/
|
|
458
|
+
getAgent(): Dispatcher | null;
|
|
459
|
+
/**
|
|
460
|
+
* Sets the default agent to use for requests performed by this instance
|
|
461
|
+
*
|
|
462
|
+
* @param agent - Sets the agent to use
|
|
463
|
+
*/
|
|
464
|
+
setAgent(agent: Dispatcher): this;
|
|
465
|
+
/**
|
|
466
|
+
* Sets the authorization token that should be used for requests
|
|
467
|
+
*
|
|
468
|
+
* @param token - The authorization token to use
|
|
469
|
+
*/
|
|
470
|
+
setToken(token: string): this;
|
|
471
|
+
/**
|
|
472
|
+
* Runs a get request from the api
|
|
473
|
+
*
|
|
474
|
+
* @param fullRoute - The full route to query
|
|
475
|
+
* @param options - Optional request options
|
|
476
|
+
*/
|
|
477
|
+
get(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
|
478
|
+
/**
|
|
479
|
+
* Runs a delete request from the api
|
|
480
|
+
*
|
|
481
|
+
* @param fullRoute - The full route to query
|
|
482
|
+
* @param options - Optional request options
|
|
483
|
+
*/
|
|
484
|
+
delete(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
|
485
|
+
/**
|
|
486
|
+
* Runs a post request from the api
|
|
487
|
+
*
|
|
488
|
+
* @param fullRoute - The full route to query
|
|
489
|
+
* @param options - Optional request options
|
|
490
|
+
*/
|
|
491
|
+
post(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
|
492
|
+
/**
|
|
493
|
+
* Runs a put request from the api
|
|
494
|
+
*
|
|
495
|
+
* @param fullRoute - The full route to query
|
|
496
|
+
* @param options - Optional request options
|
|
497
|
+
*/
|
|
498
|
+
put(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
|
499
|
+
/**
|
|
500
|
+
* Runs a patch request from the api
|
|
501
|
+
*
|
|
502
|
+
* @param fullRoute - The full route to query
|
|
503
|
+
* @param options - Optional request options
|
|
504
|
+
*/
|
|
505
|
+
patch(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
|
506
|
+
/**
|
|
507
|
+
* Runs a request from the api
|
|
508
|
+
*
|
|
509
|
+
* @param options - Request options
|
|
510
|
+
*/
|
|
511
|
+
request(options: InternalRequest): Promise<unknown>;
|
|
512
|
+
/**
|
|
513
|
+
* Runs a request from the API, yielding the raw Response object
|
|
514
|
+
*
|
|
515
|
+
* @param options - Request options
|
|
516
|
+
*/
|
|
517
|
+
raw(options: InternalRequest): Promise<Dispatcher.ResponseData>;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
declare const DefaultUserAgent: string;
|
|
521
|
+
declare const DefaultRestOptions: Required<RESTOptions>;
|
|
522
|
+
/**
|
|
523
|
+
* The events that the REST manager emits
|
|
524
|
+
*/
|
|
525
|
+
declare const enum RESTEvents {
|
|
526
|
+
Debug = "restDebug",
|
|
527
|
+
HandlerSweep = "handlerSweep",
|
|
528
|
+
HashSweep = "hashSweep",
|
|
529
|
+
InvalidRequestWarning = "invalidRequestWarning",
|
|
530
|
+
RateLimited = "rateLimited",
|
|
531
|
+
Response = "response"
|
|
532
|
+
}
|
|
533
|
+
declare const ALLOWED_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif"];
|
|
534
|
+
declare const ALLOWED_STICKER_EXTENSIONS: readonly ["png", "json"];
|
|
535
|
+
declare const ALLOWED_SIZES: readonly [16, 32, 64, 128, 256, 512, 1024, 2048, 4096];
|
|
536
|
+
declare type ImageExtension = typeof ALLOWED_EXTENSIONS[number];
|
|
537
|
+
declare type StickerExtension = typeof ALLOWED_STICKER_EXTENSIONS[number];
|
|
538
|
+
declare type ImageSize = typeof ALLOWED_SIZES[number];
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* The options used for image URLs
|
|
542
|
+
*/
|
|
543
|
+
interface BaseImageURLOptions {
|
|
544
|
+
/**
|
|
545
|
+
* The extension to use for the image URL
|
|
546
|
+
*
|
|
547
|
+
* @defaultValue `'webp'`
|
|
548
|
+
*/
|
|
549
|
+
extension?: ImageExtension;
|
|
550
|
+
/**
|
|
551
|
+
* The size specified in the image URL
|
|
552
|
+
*/
|
|
553
|
+
size?: ImageSize;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* The options used for image URLs with animated content
|
|
557
|
+
*/
|
|
558
|
+
interface ImageURLOptions extends BaseImageURLOptions {
|
|
559
|
+
/**
|
|
560
|
+
* Whether or not to prefer the static version of an image asset.
|
|
561
|
+
*/
|
|
562
|
+
forceStatic?: boolean;
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* The options to use when making a CDN URL
|
|
566
|
+
*/
|
|
567
|
+
interface MakeURLOptions {
|
|
568
|
+
/**
|
|
569
|
+
* The allowed extensions that can be used
|
|
570
|
+
*/
|
|
571
|
+
allowedExtensions?: readonly string[];
|
|
572
|
+
/**
|
|
573
|
+
* The extension to use for the image URL
|
|
574
|
+
*
|
|
575
|
+
* @defaultValue `'webp'`
|
|
576
|
+
*/
|
|
577
|
+
extension?: string | undefined;
|
|
578
|
+
/**
|
|
579
|
+
* The size specified in the image URL
|
|
580
|
+
*/
|
|
581
|
+
size?: ImageSize;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* The CDN link builder
|
|
585
|
+
*/
|
|
586
|
+
declare class CDN {
|
|
587
|
+
private readonly base;
|
|
588
|
+
constructor(base?: string);
|
|
589
|
+
/**
|
|
590
|
+
* Generates an app asset URL for a client's asset.
|
|
591
|
+
*
|
|
592
|
+
* @param clientId - The client id that has the asset
|
|
593
|
+
* @param assetHash - The hash provided by Discord for this asset
|
|
594
|
+
* @param options - Optional options for the asset
|
|
595
|
+
*/
|
|
596
|
+
appAsset(clientId: string, assetHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
597
|
+
/**
|
|
598
|
+
* Generates an app icon URL for a client's icon.
|
|
599
|
+
*
|
|
600
|
+
* @param clientId - The client id that has the icon
|
|
601
|
+
* @param iconHash - The hash provided by Discord for this icon
|
|
602
|
+
* @param options - Optional options for the icon
|
|
603
|
+
*/
|
|
604
|
+
appIcon(clientId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
605
|
+
/**
|
|
606
|
+
* Generates an avatar URL, e.g. for a user or a webhook.
|
|
607
|
+
*
|
|
608
|
+
* @param id - The id that has the icon
|
|
609
|
+
* @param avatarHash - The hash provided by Discord for this avatar
|
|
610
|
+
* @param options - Optional options for the avatar
|
|
611
|
+
*/
|
|
612
|
+
avatar(id: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
|
613
|
+
/**
|
|
614
|
+
* Generates a banner URL, e.g. for a user or a guild.
|
|
615
|
+
*
|
|
616
|
+
* @param id - The id that has the banner splash
|
|
617
|
+
* @param bannerHash - The hash provided by Discord for this banner
|
|
618
|
+
* @param options - Optional options for the banner
|
|
619
|
+
*/
|
|
620
|
+
banner(id: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
|
621
|
+
/**
|
|
622
|
+
* Generates an icon URL for a channel, e.g. a group DM.
|
|
623
|
+
*
|
|
624
|
+
* @param channelId - The channel id that has the icon
|
|
625
|
+
* @param iconHash - The hash provided by Discord for this channel
|
|
626
|
+
* @param options - Optional options for the icon
|
|
627
|
+
*/
|
|
628
|
+
channelIcon(channelId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
629
|
+
/**
|
|
630
|
+
* Generates the default avatar URL for a discriminator.
|
|
631
|
+
*
|
|
632
|
+
* @param discriminator - The discriminator modulo 5
|
|
633
|
+
*/
|
|
634
|
+
defaultAvatar(discriminator: number): string;
|
|
635
|
+
/**
|
|
636
|
+
* Generates a discovery splash URL for a guild's discovery splash.
|
|
637
|
+
*
|
|
638
|
+
* @param guildId - The guild id that has the discovery splash
|
|
639
|
+
* @param splashHash - The hash provided by Discord for this splash
|
|
640
|
+
* @param options - Optional options for the splash
|
|
641
|
+
*/
|
|
642
|
+
discoverySplash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
643
|
+
/**
|
|
644
|
+
* Generates an emoji's URL for an emoji.
|
|
645
|
+
*
|
|
646
|
+
* @param emojiId - The emoji id
|
|
647
|
+
* @param extension - The extension of the emoji
|
|
648
|
+
*/
|
|
649
|
+
emoji(emojiId: string, extension?: ImageExtension): string;
|
|
650
|
+
/**
|
|
651
|
+
* Generates a guild member avatar URL.
|
|
652
|
+
*
|
|
653
|
+
* @param guildId - The id of the guild
|
|
654
|
+
* @param userId - The id of the user
|
|
655
|
+
* @param avatarHash - The hash provided by Discord for this avatar
|
|
656
|
+
* @param options - Optional options for the avatar
|
|
657
|
+
*/
|
|
658
|
+
guildMemberAvatar(guildId: string, userId: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
|
659
|
+
/**
|
|
660
|
+
* Generates a guild member banner URL.
|
|
661
|
+
*
|
|
662
|
+
* @param guildId - The id of the guild
|
|
663
|
+
* @param userId - The id of the user
|
|
664
|
+
* @param bannerHash - The hash provided by Discord for this banner
|
|
665
|
+
* @param options - Optional options for the banner
|
|
666
|
+
*/
|
|
667
|
+
guildMemberBanner(guildId: string, userId: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
|
668
|
+
/**
|
|
669
|
+
* Generates an icon URL, e.g. for a guild.
|
|
670
|
+
*
|
|
671
|
+
* @param id - The id that has the icon splash
|
|
672
|
+
* @param iconHash - The hash provided by Discord for this icon
|
|
673
|
+
* @param options - Optional options for the icon
|
|
674
|
+
*/
|
|
675
|
+
icon(id: string, iconHash: string, options?: Readonly<ImageURLOptions>): string;
|
|
676
|
+
/**
|
|
677
|
+
* Generates a URL for the icon of a role
|
|
678
|
+
*
|
|
679
|
+
* @param roleId - The id of the role that has the icon
|
|
680
|
+
* @param roleIconHash - The hash provided by Discord for this role icon
|
|
681
|
+
* @param options - Optional options for the role icon
|
|
682
|
+
*/
|
|
683
|
+
roleIcon(roleId: string, roleIconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
684
|
+
/**
|
|
685
|
+
* Generates a guild invite splash URL for a guild's invite splash.
|
|
686
|
+
*
|
|
687
|
+
* @param guildId - The guild id that has the invite splash
|
|
688
|
+
* @param splashHash - The hash provided by Discord for this splash
|
|
689
|
+
* @param options - Optional options for the splash
|
|
690
|
+
*/
|
|
691
|
+
splash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
692
|
+
/**
|
|
693
|
+
* Generates a sticker URL.
|
|
694
|
+
*
|
|
695
|
+
* @param stickerId - The sticker id
|
|
696
|
+
* @param extension - The extension of the sticker
|
|
697
|
+
*/
|
|
698
|
+
sticker(stickerId: string, extension?: StickerExtension): string;
|
|
699
|
+
/**
|
|
700
|
+
* Generates a sticker pack banner URL.
|
|
701
|
+
*
|
|
702
|
+
* @param bannerId - The banner id
|
|
703
|
+
* @param options - Optional options for the banner
|
|
704
|
+
*/
|
|
705
|
+
stickerPackBanner(bannerId: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
706
|
+
/**
|
|
707
|
+
* Generates a team icon URL for a team's icon.
|
|
708
|
+
*
|
|
709
|
+
* @param teamId - The team id that has the icon
|
|
710
|
+
* @param iconHash - The hash provided by Discord for this icon
|
|
711
|
+
* @param options - Optional options for the icon
|
|
712
|
+
*/
|
|
713
|
+
teamIcon(teamId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
714
|
+
/**
|
|
715
|
+
* Generates a cover image for a guild scheduled event.
|
|
716
|
+
*
|
|
717
|
+
* @param scheduledEventId - The scheduled event id
|
|
718
|
+
* @param coverHash - The hash provided by discord for this cover image
|
|
719
|
+
* @param options - Optional options for the cover image
|
|
720
|
+
*/
|
|
721
|
+
guildScheduledEventCover(scheduledEventId: string, coverHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
|
722
|
+
/**
|
|
723
|
+
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
|
|
724
|
+
*
|
|
725
|
+
* @param route - The base cdn route
|
|
726
|
+
* @param hash - The hash provided by Discord for this icon
|
|
727
|
+
* @param options - Optional options for the link
|
|
728
|
+
*/
|
|
729
|
+
private dynamicMakeURL;
|
|
730
|
+
/**
|
|
731
|
+
* Constructs the URL for the resource
|
|
732
|
+
*
|
|
733
|
+
* @param route - The base cdn route
|
|
734
|
+
* @param options - The extension/size options for the link
|
|
735
|
+
*/
|
|
736
|
+
private makeURL;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
interface DiscordErrorFieldInformation {
|
|
740
|
+
code: string;
|
|
741
|
+
message: string;
|
|
742
|
+
}
|
|
743
|
+
interface DiscordErrorGroupWrapper {
|
|
744
|
+
_errors: DiscordError[];
|
|
745
|
+
}
|
|
746
|
+
declare type DiscordError = DiscordErrorFieldInformation | DiscordErrorGroupWrapper | string | {
|
|
747
|
+
[k: string]: DiscordError;
|
|
748
|
+
};
|
|
749
|
+
interface DiscordErrorData {
|
|
750
|
+
code: number;
|
|
751
|
+
errors?: DiscordError;
|
|
752
|
+
message: string;
|
|
753
|
+
}
|
|
754
|
+
interface OAuthErrorData {
|
|
755
|
+
error: string;
|
|
756
|
+
error_description?: string;
|
|
757
|
+
}
|
|
758
|
+
interface RequestBody {
|
|
759
|
+
files: RawFile[] | undefined;
|
|
760
|
+
json: unknown | undefined;
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Represents an API error returned by Discord
|
|
764
|
+
*/
|
|
765
|
+
declare class DiscordAPIError extends Error {
|
|
766
|
+
rawError: DiscordErrorData | OAuthErrorData;
|
|
767
|
+
code: number | string;
|
|
768
|
+
status: number;
|
|
769
|
+
method: string;
|
|
770
|
+
url: string;
|
|
771
|
+
requestBody: RequestBody;
|
|
772
|
+
/**
|
|
773
|
+
* @param rawError - The error reported by Discord
|
|
774
|
+
* @param code - The error code reported by Discord
|
|
775
|
+
* @param status - The status code of the response
|
|
776
|
+
* @param method - The method of the request that erred
|
|
777
|
+
* @param url - The url of the request that erred
|
|
778
|
+
* @param bodyData - The unparsed data for the request that errored
|
|
779
|
+
*/
|
|
780
|
+
constructor(rawError: DiscordErrorData | OAuthErrorData, code: number | string, status: number, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
|
781
|
+
/**
|
|
782
|
+
* The name of the error
|
|
783
|
+
*/
|
|
784
|
+
get name(): string;
|
|
785
|
+
private static getMessage;
|
|
786
|
+
private static flattenDiscordError;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Represents a HTTP error
|
|
791
|
+
*/
|
|
792
|
+
declare class HTTPError extends Error {
|
|
793
|
+
name: string;
|
|
794
|
+
status: number;
|
|
795
|
+
method: string;
|
|
796
|
+
url: string;
|
|
797
|
+
requestBody: RequestBody;
|
|
798
|
+
/**
|
|
799
|
+
* @param name - The name of the error
|
|
800
|
+
* @param status - The status code of the response
|
|
801
|
+
* @param method - The method of the request that erred
|
|
802
|
+
* @param url - The url of the request that erred
|
|
803
|
+
* @param bodyData - The unparsed data for the request that errored
|
|
804
|
+
*/
|
|
805
|
+
constructor(name: string, status: number, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
declare class RateLimitError extends Error implements RateLimitData {
|
|
809
|
+
timeToReset: number;
|
|
810
|
+
limit: number;
|
|
811
|
+
method: string;
|
|
812
|
+
hash: string;
|
|
813
|
+
url: string;
|
|
814
|
+
route: string;
|
|
815
|
+
majorParameter: string;
|
|
816
|
+
global: boolean;
|
|
817
|
+
constructor({ timeToReset, limit, method, hash, url, route, majorParameter, global }: RateLimitData);
|
|
818
|
+
/**
|
|
819
|
+
* The name of the error
|
|
820
|
+
*/
|
|
821
|
+
get name(): string;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Creates and populates an URLSearchParams instance from an object, stripping
|
|
826
|
+
* out null and undefined values, while also coercing non-strings to strings.
|
|
827
|
+
*
|
|
828
|
+
* @param options - The options to use
|
|
829
|
+
* @returns A populated URLSearchParams instance
|
|
830
|
+
*/
|
|
831
|
+
declare function makeURLSearchParams(options?: Record<string, unknown>): URLSearchParams;
|
|
832
|
+
/**
|
|
833
|
+
* Converts the response to usable data
|
|
834
|
+
*
|
|
835
|
+
* @param res - The fetch response
|
|
836
|
+
*/
|
|
837
|
+
declare function parseResponse(res: Dispatcher.ResponseData): Promise<unknown>;
|
|
838
|
+
|
|
839
|
+
export { ALLOWED_EXTENSIONS, ALLOWED_SIZES, ALLOWED_STICKER_EXTENSIONS, APIRequest, BaseImageURLOptions, CDN, DefaultRestOptions, DefaultUserAgent, DiscordAPIError, DiscordErrorData, HTTPError, HandlerRequestData, HashData, ImageExtension, ImageSize, ImageURLOptions, InternalRequest, InvalidRequestWarningData, MakeURLOptions, OAuthErrorData, REST, RESTEvents, RESTOptions, RateLimitData, RateLimitError, RateLimitQueueFilter, RawFile, RequestBody, RequestData, RequestHeaders, RequestManager, RequestMethod, RequestOptions, RestEvents, RouteData, RouteLike, StickerExtension, makeURLSearchParams, parseResponse };
|