Public
Script
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
import { type Options, type KeyOptions, type Result, RateLimit as RawRateLimit } from "npm:@rlimit/http@0.0.5";
export const valTownRlimitFreeTierNamespace = "5210ab90-8661-4f1c-9c0f-cdde1f1fbfa3"
type valOptions = Omit<Options, 'namespace' | 'subnamespace' | 'password' | 'baseURL'>
/**
* API Client for interfacing with the RateLimit API. Uses free tier rlimit.com credentials.
*/
export class RateLimit {
private client;
constructor(options: valOptions) {
this.client = new RawRateLimit({
baseURL: "https://rlimit-proxy.web.val.run",
namespace: valTownRlimitFreeTierNamespace,
maximum: options.maximum,
interval: options.interval,
});
}
/**
* Determine if a request should be allowed.
* @param key The request's identifier.
* @param options Custom interval and/or maximum options for this key, if any.
*/
async check(key: string, options?: valOptions): Promise<Result> {
return this.client.check(key, {
interval: options?.interval,
maximum: options?.maximum
});
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 5, 2024