-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathApiRateLimitSection.server.ts
More file actions
55 lines (52 loc) · 1.65 KB
/
ApiRateLimitSection.server.ts
File metadata and controls
55 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { prisma } from "~/db.server";
import { env } from "~/env.server";
import { logger } from "~/services/logger.server";
import { type Duration } from "~/services/rateLimiter.server";
import { API_RATE_LIMIT_INTENT } from "./ApiRateLimitSection";
import {
handleRateLimitAction,
resolveEffectiveRateLimit,
type RateLimitActionResult,
type RateLimitDomain,
} from "./RateLimitSection.server";
import type { EffectiveRateLimit } from "./RateLimitSection";
export const apiRateLimitDomain: RateLimitDomain = {
intent: API_RATE_LIMIT_INTENT,
systemDefault: () => ({
type: "tokenBucket",
refillRate: env.API_RATE_LIMIT_REFILL_RATE,
interval: env.API_RATE_LIMIT_REFILL_INTERVAL as Duration,
maxTokens: env.API_RATE_LIMIT_MAX,
}),
apply: async (orgId, next, adminUserId) => {
const existing = await prisma.organization.findFirst({
where: { id: orgId },
select: { apiRateLimiterConfig: true },
});
if (!existing) {
throw new Response(null, { status: 404 });
}
await prisma.organization.update({
where: { id: orgId },
data: { apiRateLimiterConfig: next as any },
});
logger.info("admin.backOffice.apiRateLimit", {
adminUserId,
orgId,
previous: existing.apiRateLimiterConfig,
next,
});
},
};
export function resolveEffectiveApiRateLimit(
override: unknown
): EffectiveRateLimit {
return resolveEffectiveRateLimit(override, apiRateLimitDomain);
}
export function handleApiRateLimitAction(
formData: FormData,
orgId: string,
adminUserId: string
): Promise<RateLimitActionResult> {
return handleRateLimitAction(formData, orgId, adminUserId, apiRateLimitDomain);
}