1- import { Bucket , RatelimitData } from './Bucket' ;
1+ import {
2+ BaseBucket ,
3+ BucketConstructor ,
4+ Bucket ,
5+ RatelimitData ,
6+ DiscordFetchOptions ,
7+ File ,
8+ RequestBodyData ,
9+ StringRecord
10+ } from '../fetcher' ;
211import { USER_AGENT } from '../Constants' ;
312import { EventEmitter } from 'events' ;
413import { Headers , Response } from 'node-fetch' ;
514import { Mutex , MemoryMutex } from '../mutex' ;
615import AbortController from 'abort-controller' ;
716import { CordisRestError , HTTPError } from '../Error' ;
817import { halt } from '@cordis/common' ;
9- import type { DiscordFetchOptions , File , RequestBodyData , StringRecord } from '../Fetch' ;
18+ import type { Readable } from 'stream' ;
19+ import { RouteBases } from 'discord-api-types/v9' ;
1020
1121/**
1222 * Options for constructing a rest manager
1323 */
1424export interface RestOptions {
1525 /**
1626 * How many times to retry making a request before giving up
27+ *
28+ * Tip: If using ProxyBucket you should probably set this to 1 depending on your proxy server's implementation
1729 */
1830 retries ?: number ;
1931 /**
@@ -34,6 +46,14 @@ export interface RestOptions {
3446 * Overwrites the default for `{@link RequestOptions.cacheTime}`
3547 */
3648 cacheTime ?: number ;
49+ /**
50+ * Bucket constructor to use
51+ */
52+ bucket ?: BucketConstructor ;
53+ /**
54+ * Overwrites the default domain used for every request
55+ */
56+ domain ?: string ;
3757}
3858
3959export interface Rest {
@@ -114,7 +134,7 @@ export interface RequestOptions<D, Q> {
114134 /**
115135 * Body to send, if any
116136 */
117- data ?: D ;
137+ data ?: D | Readable ;
118138 /**
119139 * Wether or not this request should be re-attempted after a ratelimit is waited out
120140 */
@@ -132,6 +152,10 @@ export interface RequestOptions<D, Q> {
132152 * @default 10000
133153 */
134154 cacheTime ?: number ;
155+ /**
156+ * Overwrites the domain used for this request - not taking into account the option passed into {@link RestOptions}
157+ */
158+ domain ?: string ;
135159}
136160
137161/**
@@ -151,13 +175,15 @@ export class Rest extends EventEmitter {
151175 /**
152176 * Current active rate limiting Buckets
153177 */
154- public readonly buckets = new Map < string , Bucket > ( ) ;
178+ public readonly buckets = new Map < string , BaseBucket > ( ) ;
155179
156180 public readonly retries : number ;
157181 public readonly abortAfter : number ;
158182 public readonly mutex : Mutex ;
159183 public readonly retryAfterRatelimit : boolean ;
160184 public readonly cacheTime : number ;
185+ public readonly bucket : BucketConstructor ;
186+ public readonly domain : string ;
161187
162188 /**
163189 * @param auth Your bot's Discord token
@@ -174,26 +200,30 @@ export class Rest extends EventEmitter {
174200 mutex = new MemoryMutex ( ) ,
175201 retryAfterRatelimit = true ,
176202 cacheTime = 10000 ,
203+ bucket = Bucket ,
204+ domain = RouteBases . api
177205 } = options ;
178206
179207 this . retries = retries ;
180208 this . abortAfter = abortAfter ;
181209 this . mutex = mutex ;
182210 this . retryAfterRatelimit = retryAfterRatelimit ;
183211 this . cacheTime = cacheTime ;
212+ this . bucket = bucket ;
213+ this . domain = domain ;
184214 }
185215
186216 /**
187217 * Prepares a request to Discord, associating it to the correct Bucket and attempting to prevent rate limits
188218 * @param options Options needed for making a request; only the path is required
189219 */
190220 public async make < T , D = RequestBodyData , Q = StringRecord > ( options : RequestOptions < D , Q > ) : Promise < T > {
191- const route = Bucket . makeRoute ( options . method , options . path ) ;
221+ const route = this . bucket . makeRoute ( options . method , options . path ) ;
192222
193223 let bucket = this . buckets . get ( route ) ;
194224
195225 if ( ! bucket ) {
196- bucket = new Bucket ( this , route ) ;
226+ bucket = new this . bucket ( this , route ) ;
197227 this . buckets . set ( route , bucket ) ;
198228 }
199229
@@ -209,6 +239,7 @@ export class Rest extends EventEmitter {
209239 }
210240
211241 options . cacheTime ??= this . cacheTime ;
242+ options . domain ??= this . domain ;
212243
213244 let isRetryAfterRatelimit = false ;
214245
0 commit comments