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.
3 Replies
euan
euan2mo 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 User2mo ago
Message Not Public
Sign In & Join Server To View
euan
euan2mo 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);
}
Want results from more Discord servers?
Add your server