feat: rest header injection (#3122)

* feat: made some progress on a function which can be overridden to allow users to inject custom headers into a rest request

* made the changes suggested in PR. Renamed inject headers to createBaseHeaders, which adds the user agent into the headers.
This commit is contained in:
8au
2023-09-19 20:14:32 +00:00
committed by GitHub
parent f34c0c3bc0
commit f3a568c4db
2 changed files with 10 additions and 3 deletions
+8 -3
View File
@@ -98,10 +98,17 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage
routes: createRoutes(),
createBaseHeaders() {
return {
'user-agent': `DiscordBot (https://github.com/discordeno/discordeno, v${version})`,
}
},
checkRateLimits(url, headers) {
const authHeader = headers?.authorization ?? ''
const ratelimited = rest.rateLimitedPaths.get(`${authHeader}${url}`)
const global = rest.rateLimitedPaths.get('global')
const now = Date.now()
@@ -161,9 +168,7 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage
},
createRequestBody(method, options) {
const headers: Record<string, string> = {
'user-agent': `DiscordBot (https://github.com/discordeno/discordeno, v${version})`,
}
const headers = this.createBaseHeaders()
if (options?.unauthorized !== true) headers.authorization = `Bot ${rest.token}`
+2
View File
@@ -176,6 +176,8 @@ export interface RestManager {
invalidBucket: InvalidRequestBucket
/** The routes that are available for this manager. */
routes: RestRoutes
/** Allows the user to inject custom headers that will be sent with every request. */
createBaseHeaders: () => Record<string, string>
/** Whether or not the rest manager should keep objects in raw snake case from discord. */
preferSnakeCase: (enabled: boolean) => RestManager
/** Check the rate limits for a url or a bucket. */