Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Example of using Unkey Ratelimiting

See: https://www.unkey.com/docs/libraries/ts/ratelimit

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
/** @jsxImportSource npm:hono@3/jsx */
import { Ratelimit, RatelimitConfig, type RatelimitResponse } from "npm:@unkey/ratelimit";
import { Hono } from "npm:hono";
import type { Context, MiddlewareHandler } from "npm:hono";
export function unkeyRatelimitMiddleware(config: RatelimitConfig): MiddlewareHandler {
const ratelimiter = new Ratelimit(config);
return async (c, next) => {
const ratelimitResponse = await ratelimiter.limit("home");
console.log(">> ratelimitResponse", ratelimitResponse);
if (!ratelimitResponse.success) {
return c.html(
<div>
Too busy!
</div>,
429,
);
}
await next();
};
}
const app = new Hono();
app.use(
"*",
unkeyRatelimitMiddleware({
rootKey: Deno.env.get("UNKEY_ROOT_KEY"),
namespace: `val-hono-unkey-limit-demo`,
limit: 2,
duration: "60s",
}),
);
app.get("/", async (c) => {
return c.html(
<div>
<form>
Your name: <input type="text" name="name" /> <input type="submit" />
</form>
Hello {c.req.query("name")}
</div>,
);
});
export default app.fetch;
dthyresson-honowithunkeyratelimiting.web.val.run
August 6, 2024