import { createError } from "h3"; import { getRedis } from "./redis"; export async function rateLimitOrThrow( key: string, limit: number, windowSeconds: number, ): Promise { const redis = getRedis(); const n = await redis.incr(key); if (n === 1) { await redis.expire(key, windowSeconds); } if (n > limit) { throw createError({ statusCode: 429, data: { error: { code: "RATE_LIMITED", message: "请求过于频繁,请稍后再试" }, }, }); } }