I tried this for the Rate Limiting API,

I tried this for the Rate Limiting API, but identifying the method signatures to add to the EnvBinding blocked me - maybe I was just looking in the wrong place, but I couldn't get it by referencing existing EnvBindings and the docs for the Typescript version.
6 Replies
euan
euanOP6mo ago
If anyone wants to grab my efforts as a starting block, feel free:
#[wasm_bindgen::prelude::wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends=js_sys::Object)]
#[derive(Clone, PartialEq, Eq)]
pub type RateLimiterSys;

#[wasm_bindgen(method, getter, js_name=limit)]
pub fn limit(this: &RateLimiterSys) -> String;
}
struct RateLimiter(RateLimiterSys);
impl EnvBinding for RateLimiter {
const TYPE_NAME: &'static str = "RateLimiter";
}
impl RateLimiter {
pub fn limit(&self) -> String {
self.0.limit()
}
}
#[wasm_bindgen::prelude::wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends=js_sys::Object)]
#[derive(Clone, PartialEq, Eq)]
pub type RateLimiterSys;

#[wasm_bindgen(method, getter, js_name=limit)]
pub fn limit(this: &RateLimiterSys) -> String;
}
struct RateLimiter(RateLimiterSys);
impl EnvBinding for RateLimiter {
const TYPE_NAME: &'static str = "RateLimiter";
}
impl RateLimiter {
pub fn limit(&self) -> String {
self.0.limit()
}
}
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
euan
euanOP3mo ago
I should have time tomorrow, will give it a go and update . Actually, had a quick install and test:
field success of RateLimitOutcome is private
In context:
if ctx
.env
.get_binding::<RateLimiter>("RATE_LIMITER")?
.limit(api_key)
.await?
.success
{
return Response::error("Rate limit exceeded", 429);
}
if ctx
.env
.get_binding::<RateLimiter>("RATE_LIMITER")?
.limit(api_key)
.await?
.success
{
return Response::error("Rate limit exceeded", 429);
}
@kflansburg Any word on making success a public field and this being merged?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
euan
euanOP3mo ago
Amazing, thank you! I haven't fully validated it with an actual rate limit since I couldn't deploy it as-was, but that was the only issue rust-analyzer raised.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server