mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 09:39:09 +00:00
feat(api): add rate-limiting and /clear-guild-cache (#198)
This commit is contained in:
parent
57d83699d5
commit
a4fd37d71c
7 changed files with 138 additions and 12 deletions
|
@ -10,6 +10,7 @@ import {
|
|||
import { AuthType, cacheLayer, discordFetch } from './api-tools';
|
||||
import { botClientID, botToken } from './config';
|
||||
import { GuildData, Guilds } from './kv';
|
||||
import { useRateLimiter } from './rate-limiting';
|
||||
|
||||
type APIGuild = {
|
||||
// Only relevant stuff
|
||||
|
@ -161,3 +162,14 @@ const calculateRoleSafety = (role: Role | APIRole, highestBotRolePosition: numbe
|
|||
|
||||
return safety;
|
||||
};
|
||||
|
||||
export enum GuildRateLimiterKey {
|
||||
legacyImport = 'legacyImport',
|
||||
cacheClear = 'cacheClear',
|
||||
}
|
||||
|
||||
export const useGuildRateLimiter = (
|
||||
guildID: string,
|
||||
key: GuildRateLimiterKey,
|
||||
timeoutSeconds: number
|
||||
) => useRateLimiter(Guilds, `guilds/${guildID}/rate-limit/${key}`, timeoutSeconds);
|
||||
|
|
15
packages/api/utils/rate-limiting.ts
Normal file
15
packages/api/utils/rate-limiting.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { WrappedKVNamespace } from './kv';
|
||||
|
||||
export const useRateLimiter = (
|
||||
kv: WrappedKVNamespace,
|
||||
key: string,
|
||||
timeoutSeconds: number
|
||||
) => async (): Promise<boolean> => {
|
||||
const value = await kv.get<boolean>(key);
|
||||
if (value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
await kv.put(key, true, timeoutSeconds);
|
||||
return false;
|
||||
};
|
16
packages/api/utils/responses.ts
Normal file
16
packages/api/utils/responses.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { respond } from './api-tools';
|
||||
|
||||
export const ok = () => respond({ ok: true });
|
||||
|
||||
export const missingParameters = () =>
|
||||
respond({ error: 'missing parameters' }, { status: 400 });
|
||||
|
||||
export const lowPermissions = () =>
|
||||
respond({ error: 'no permissions for this action' }, { status: 403 });
|
||||
|
||||
export const notFound = () => respond({ error: 'not found' }, { status: 404 });
|
||||
|
||||
export const conflict = () => respond({ error: 'conflict' }, { status: 409 });
|
||||
|
||||
export const rateLimited = () =>
|
||||
respond({ error: 'rate limit hit, enhance your calm' }, { status: 419 });
|
Loading…
Add table
Add a link
Reference in a new issue