Skip to content

Commit a64df19

Browse files
committed
fix: resolved deployment errors
1 parent 8a53f78 commit a64df19

9 files changed

Lines changed: 60 additions & 157 deletions

File tree

lib/util/api.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export function sendError<DataType = any>(
7373
* @param {RateLimitOptions} options Options for the ratelimiter.
7474
* @returns
7575
*/
76-
export function rateLimiter<T>(options: RateLimitOptions): RateLimiter<T> {
76+
export function rateLimiter<T extends NextApiResponse<Response<any>>>(
77+
options: RateLimitOptions
78+
): RateLimiter<T> {
7779
const tokenCache: LRU<unknown, unknown> = new LRU({
7880
max: parseInt((options.uniqueTokenPerInterval || 500).toString(), 10),
7981
ttl: parseInt((options.interval || 60000).toString(), 10)
@@ -90,7 +92,7 @@ export function rateLimiter<T>(options: RateLimitOptions): RateLimiter<T> {
9092
return {
9193
check: (
9294
req: NextApiRequest,
93-
res: NextApiResponse<Response<T>>,
95+
res: T,
9496
limit: number,
9597
token: string
9698
): Promise<void> =>
@@ -135,11 +137,9 @@ export function rateLimiter<T>(options: RateLimitOptions): RateLimiter<T> {
135137
* @param {boolean | undefined} privateRoute Determines whether the API route is private.
136138
* @returns
137139
*/
138-
export async function validateCredentials<T>(
139-
req: NextApiRequest,
140-
res: NextApiResponse<Response<T>>,
141-
privateRoute?: boolean
142-
): Promise<boolean> {
140+
export async function validateCredentials<
141+
T extends NextApiResponse<Response<any>>
142+
>(req: NextApiRequest, res: T, privateRoute?: boolean): Promise<boolean> {
143143
if (privateRoute) {
144144
if (req.headers) {
145145
if (typeof req.headers.authorization === "string") {

pages/api/v2/get/boosters.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

pages/api/v2/get/channels.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,25 @@ import {
1313
validateCredentials
1414
} from "../../../../lib/util/api";
1515

16-
const limiter: RateLimiter<GuildChannel<GuildChannelType>[]> = rateLimiter({
17-
interval: 60 * 1000,
18-
uniqueTokenPerInterval: 500
19-
});
20-
2116
export default async function handler(
2217
req: NextApiRequest,
23-
res: NextApiResponse<Response<GuildChannel<GuildChannelType>[]>>
18+
res: NextApiResponse<Response<GuildChannel<GuildChannelType>[] | undefined>>
2419
): Promise<void> {
20+
const limiter: RateLimiter<typeof res> = rateLimiter({
21+
interval: 60 * 1000,
22+
uniqueTokenPerInterval: 500
23+
});
24+
2525
if (req.method === "GET") {
26-
if (
27-
!(await validateCredentials<GuildChannel<GuildChannelType>[]>(
28-
req,
29-
res,
30-
true
31-
))
32-
)
33-
return;
26+
if (!(await validateCredentials<typeof res>(req, res, true))) return;
3427

3528
try {
3629
await limiter.check(req, res, 10, "CACHE_TOKEN");
3730

38-
const data: GuildChannel<GuildChannelType>[] = await fetchData<
39-
GuildChannel<GuildChannelType>[]
40-
>(
41-
`https://discord.com/api/v${process.env.DISCORD_API_VERSION}/guilds/${process.env.SERVER_ID}/channels`
42-
);
31+
const data: GuildChannel<GuildChannelType>[] | undefined =
32+
await fetchData<GuildChannel<GuildChannelType>[]>(
33+
`https://discord.com/api/v${process.env.DISCORD_API_VERSION}/guilds/${process.env.SERVER_ID}/channels`
34+
);
4335
return sendData(res, data);
4436
} catch {
4537
return sendError(res, {

pages/api/v2/get/events.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,22 @@ import {
1010
validateCredentials
1111
} from "../../../../lib/util/api";
1212

13-
const limiter: RateLimiter<GuildScheduledEvent[]> = rateLimiter({
14-
interval: 60 * 1000,
15-
uniqueTokenPerInterval: 500
16-
});
17-
1813
export default async function handler(
1914
req: NextApiRequest,
20-
res: NextApiResponse<Response<GuildScheduledEvent[]>>
15+
res: NextApiResponse<Response<GuildScheduledEvent[] | undefined>>
2116
): Promise<void> {
17+
const limiter: RateLimiter<typeof res> = rateLimiter({
18+
interval: 60 * 1000,
19+
uniqueTokenPerInterval: 500
20+
});
21+
2222
if (req.method === "GET") {
23-
if (!(await validateCredentials<GuildScheduledEvent[]>(req, res, true)))
24-
return;
23+
if (!(await validateCredentials<typeof res>(req, res, true))) return;
2524

2625
try {
2726
await limiter.check(req, res, 10, "CACHE_TOKEN");
2827

29-
const data: GuildScheduledEvent[] = await fetchData<
28+
const data: GuildScheduledEvent[] | undefined = await fetchData<
3029
GuildScheduledEvent[]
3130
>(
3231
`https://discord.com/api/v${process.env.DISCORD_API_VERSION}/guilds/${process.env.SERVER_ID}/scheduled-events?with_user_count=true`

pages/api/v2/get/guild.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import {
1010
validateCredentials
1111
} from "../../../../lib/util/api";
1212

13-
const limiter: RateLimiter<Guild> = rateLimiter({
14-
interval: 60 * 1000,
15-
uniqueTokenPerInterval: 500
16-
});
17-
1813
export default async function handler(
1914
req: NextApiRequest,
20-
res: NextApiResponse<Response<Guild>>
15+
res: NextApiResponse<Response<Guild | undefined>>
2116
): Promise<void> {
17+
const limiter: RateLimiter<typeof res> = rateLimiter({
18+
interval: 60 * 1000,
19+
uniqueTokenPerInterval: 500
20+
});
21+
2222
if (req.method === "GET") {
23-
if (!(await validateCredentials<Guild>(req, res, true))) return;
23+
if (!(await validateCredentials<typeof res>(req, res, true))) return;
2424

2525
try {
2626
await limiter.check(req, res, 10, "CACHE_TOKEN");
2727

28-
const data: Guild = await fetchData<Guild>(
28+
const data: Guild | undefined = await fetchData<Guild>(
2929
`https://discord.com/api/v${process.env.DISCORD_API_VERSION}/guilds/${process.env.STAFF_SERVER_ID}?with_counts=true`
3030
);
3131
return sendData(res, data);

pages/api/v2/get/members/[memberId].ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ import {
1010
validateCredentials
1111
} from "../../../../../lib/util/api";
1212

13-
const limiter: RateLimiter<GuildMember> = rateLimiter({
14-
interval: 60 * 1000,
15-
uniqueTokenPerInterval: 500
16-
});
17-
1813
export default async function handler(
1914
req: NextApiRequest,
20-
res: NextApiResponse<Response<GuildMember>>
15+
res: NextApiResponse<Response<GuildMember | undefined>>
2116
): Promise<void> {
2217
const {
2318
memberId,
@@ -26,13 +21,18 @@ export default async function handler(
2621
[key: string]: string | string[];
2722
} = req.query;
2823

24+
const limiter: RateLimiter<typeof res> = rateLimiter({
25+
interval: 60 * 1000,
26+
uniqueTokenPerInterval: 500
27+
});
28+
2929
if (req.method === "GET") {
30-
if (!(await validateCredentials<GuildMember>(req, res, true))) return;
30+
if (!(await validateCredentials<typeof res>(req, res, true))) return;
3131

3232
try {
3333
await limiter.check(req, res, 10, "CACHE_TOKEN");
3434

35-
const data: GuildMember =
35+
const data: GuildMember | undefined =
3636
staff === "true"
3737
? await fetchData<GuildMember>(
3838
`https://discord.com/api/v${process.env.DISCORD_API_VERSION}/guilds/${process.env.STAFF_SERVER_ID}/members/${memberId}`

pages/api/v2/get/members/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ import {
1010
validateCredentials
1111
} from "../../../../../lib/util/api";
1212

13-
const limiter: RateLimiter<GuildMember[]> = rateLimiter({
14-
interval: 60 * 1000,
15-
uniqueTokenPerInterval: 500
16-
});
17-
1813
export default async function handler(
1914
req: NextApiRequest,
20-
res: NextApiResponse<Response<GuildMember[]>>
15+
res: NextApiResponse<Response<GuildMember[] | undefined>>
2116
): Promise<void> {
2217
const {
2318
staff
2419
}: {
2520
[key: string]: string | string[];
2621
} = req.query;
2722

23+
const limiter: RateLimiter<typeof res> = rateLimiter({
24+
interval: 60 * 1000,
25+
uniqueTokenPerInterval: 500
26+
});
27+
2828
if (req.method === "GET") {
29-
if (!(await validateCredentials<GuildMember[]>(req, res, true))) return;
29+
if (!(await validateCredentials<typeof res>(req, res, true))) return;
3030

3131
try {
3232
await limiter.check(req, res, 10, "CACHE_TOKEN");
3333

34-
const data: GuildMember[] =
34+
const data: GuildMember[] | undefined =
3535
staff === "true"
3636
? await fetchData<GuildMember[]>(
3737
`https://discord.com/api/v${process.env.DISCORD_API_VERSION}/guilds/${process.env.STAFF_SERVER_ID}/members?limit=1000`

pages/api/v2/get/staff/guild.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import {
1010
validateCredentials
1111
} from "../../../../../lib/util/api";
1212

13-
const limiter: RateLimiter<Guild> = rateLimiter({
14-
interval: 60 * 1000,
15-
uniqueTokenPerInterval: 500
16-
});
17-
1813
export default async function handler(
1914
req: NextApiRequest,
20-
res: NextApiResponse<Response<Guild>>
15+
res: NextApiResponse<Response<Guild | undefined>>
2116
): Promise<void> {
17+
const limiter: RateLimiter<typeof res> = rateLimiter({
18+
interval: 60 * 1000,
19+
uniqueTokenPerInterval: 500
20+
});
21+
2222
if (req.method === "GET") {
23-
if (!(await validateCredentials<Guild>(req, res, true))) return;
23+
if (!(await validateCredentials<typeof res>(req, res, true))) return;
2424

2525
try {
2626
await limiter.check(req, res, 10, "CACHE_TOKEN");
2727

28-
const data: Guild = await fetchData<Guild>(
28+
const data: Guild | undefined = await fetchData<Guild>(
2929
`https://discord.com/api/v${process.env.DISCORD_API_VERSION}/guilds/${process.env.STAFF_SERVER_ID}?with_counts=true`
3030
);
3131
return sendData(res, data);

types/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22

3-
export interface RateLimiter<T> {
3+
export interface RateLimiter<T extends NextApiResponse<Response<any>>> {
44
check: (
55
req: NextApiRequest,
6-
res: NextApiResponse<Response<T>>,
6+
res: T,
77
limit: number,
88
token: string
99
) => Promise<void>;

0 commit comments

Comments
 (0)