mirror of
https://github.com/discordeno/discordeno.git
synced 2026-05-21 02:40:08 +00:00
refactor(rest): simplify manager.simplifyUrl (#2905)
* refactor(rest): simplify `manager.simplifyUrl` The changes improve readability and performance by a small margin. * fix tests
This commit is contained in:
@@ -71,6 +71,7 @@ import type { CreateRestManagerOptions, RestManager, SendRequestOptions } from '
|
||||
|
||||
// TODO: make dynamic based on package.json file
|
||||
const version = '19.0.0-alpha.1'
|
||||
const URL_PARTS_REGEX = /([a-z]+)\/(?:[0-9]{17,}(\/@me)?)/g
|
||||
|
||||
export function createRestManager(options: CreateRestManagerOptions): RestManager {
|
||||
// Falsy token string check
|
||||
@@ -912,20 +913,27 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage
|
||||
// Credits: github.com/abalabahaha/eris lib/rest/RequestHandler.js#L397
|
||||
// Modified for our use-case
|
||||
simplifyUrl(url, method) {
|
||||
let route = url
|
||||
.replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, function (match, p: string) {
|
||||
return ['channels', 'guilds'].includes(p) ? match : `/${p}/x`
|
||||
})
|
||||
.replace(/\/reactions\/[^/]+/g, '/reactions/x')
|
||||
let route = url.replace(URL_PARTS_REGEX, function (match, pattern: string) {
|
||||
if (pattern.startsWith('channels') || pattern.startsWith('guilds')) {
|
||||
return match
|
||||
}
|
||||
|
||||
// GENERAL /reactions and /reactions/emoji/@me share the buckets
|
||||
if (pattern.startsWith('reactions')) {
|
||||
return 'reactions'
|
||||
}
|
||||
|
||||
return `${pattern}/x`
|
||||
})
|
||||
|
||||
// GENERAL /reactions and /reactions/emoji/@me share the buckets
|
||||
if (route.includes('/reactions')) {
|
||||
route = route.substring(0, route.indexOf('/reactions') + '/reactions'.length)
|
||||
// 10 is the length of `/reactions`
|
||||
route = route.substring(0, route.indexOf('/reactions') + 10)
|
||||
}
|
||||
|
||||
// Delete Message endpoint has its own rate limit
|
||||
if (method === 'DELETE' && route.endsWith('/messages/x')) {
|
||||
route = method + route
|
||||
route = 'D' + route
|
||||
}
|
||||
|
||||
return route
|
||||
|
||||
@@ -24,8 +24,8 @@ describe('[rest] manager', () => {
|
||||
version: 9,
|
||||
proxy: {
|
||||
baseUrl: 'https://localhost:8000',
|
||||
authorization: token
|
||||
}
|
||||
authorization: token,
|
||||
},
|
||||
} as const
|
||||
|
||||
const rest = createRestManager(options)
|
||||
@@ -80,7 +80,7 @@ describe('[rest] manager', () => {
|
||||
it('Will add method in front route if method is DELETE', () => {
|
||||
const rest = createRestManager({ token })
|
||||
expect(rest.simplifyUrl('/channels/555555555555555555/messages/555555555555555555', 'DELETE')).to.be.equal(
|
||||
'DELETE/channels/555555555555555555/messages/x',
|
||||
'D/channels/555555555555555555/messages/x',
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user