mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +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
|
// TODO: make dynamic based on package.json file
|
||||||
const version = '19.0.0-alpha.1'
|
const version = '19.0.0-alpha.1'
|
||||||
|
const URL_PARTS_REGEX = /([a-z]+)\/(?:[0-9]{17,}(\/@me)?)/g
|
||||||
|
|
||||||
export function createRestManager(options: CreateRestManagerOptions): RestManager {
|
export function createRestManager(options: CreateRestManagerOptions): RestManager {
|
||||||
// Falsy token string check
|
// Falsy token string check
|
||||||
@@ -912,20 +913,27 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage
|
|||||||
// Credits: github.com/abalabahaha/eris lib/rest/RequestHandler.js#L397
|
// Credits: github.com/abalabahaha/eris lib/rest/RequestHandler.js#L397
|
||||||
// Modified for our use-case
|
// Modified for our use-case
|
||||||
simplifyUrl(url, method) {
|
simplifyUrl(url, method) {
|
||||||
let route = url
|
let route = url.replace(URL_PARTS_REGEX, function (match, pattern: string) {
|
||||||
.replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, function (match, p: string) {
|
if (pattern.startsWith('channels') || pattern.startsWith('guilds')) {
|
||||||
return ['channels', 'guilds'].includes(p) ? match : `/${p}/x`
|
return match
|
||||||
})
|
}
|
||||||
.replace(/\/reactions\/[^/]+/g, '/reactions/x')
|
|
||||||
|
// 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')) {
|
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
|
// Delete Message endpoint has its own rate limit
|
||||||
if (method === 'DELETE' && route.endsWith('/messages/x')) {
|
if (method === 'DELETE' && route.endsWith('/messages/x')) {
|
||||||
route = method + route
|
route = 'D' + route
|
||||||
}
|
}
|
||||||
|
|
||||||
return route
|
return route
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ describe('[rest] manager', () => {
|
|||||||
version: 9,
|
version: 9,
|
||||||
proxy: {
|
proxy: {
|
||||||
baseUrl: 'https://localhost:8000',
|
baseUrl: 'https://localhost:8000',
|
||||||
authorization: token
|
authorization: token,
|
||||||
}
|
},
|
||||||
} as const
|
} as const
|
||||||
|
|
||||||
const rest = createRestManager(options)
|
const rest = createRestManager(options)
|
||||||
@@ -80,7 +80,7 @@ describe('[rest] manager', () => {
|
|||||||
it('Will add method in front route if method is DELETE', () => {
|
it('Will add method in front route if method is DELETE', () => {
|
||||||
const rest = createRestManager({ token })
|
const rest = createRestManager({ token })
|
||||||
expect(rest.simplifyUrl('/channels/555555555555555555/messages/555555555555555555', 'DELETE')).to.be.equal(
|
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