near-rate-limiter
v1.1.9272
Published
npm Package - near-rate-limiter
Readme
near-rate-limiter
A rate limiting package with NEAR Protocol blockchain integration.
Installation
npm install near-rate-limiter
API
checkRateLimit(config: RateLimitConfig): Promise<RateLimitStatus>
Check whether a request is within rate limits without consuming a token.
consumeRateLimit(config: RateLimitConfig): Promise<RateLimitStatus>
Check and consume a rate limit token. Throws if the limit is exceeded.
resetRateLimit(config: RateLimitConfig): Promise<void>
Reset the rate limit state for the given config.
getNearBlockHeight(): Promise<number>
Fetch the current NEAR block height from the RPC.
recordOnChainRateLimit(account, contractId, config): Promise<OnChainRateLimitResult>
Record a rate limit event on-chain via a NEAR smart contract.
Usage
import { checkRateLimit, consumeRateLimit, resetRateLimit, getNearBlockHeight, recordOnChainRateLimit, } from 'near-rate-limiter';
const config = { accountId: 'alice.near', limit: 10, windowMs: 60000 };
// Check without consuming const status = await checkRateLimit(config); console.log(status.allowed);
// Consume a token (throws if exceeded) const result = await consumeRateLimit(config);
// Reset limits await resetRateLimit(config);
// Get current NEAR block height const height = await getNearBlockHeight();
// Record on-chain const onChainResult = await recordOnChainRateLimit(account, 'contract.near', config);
Types
interface RateLimitConfig { accountId: string; limit: number; windowMs: number; }
interface RateLimitStatus { allowed: boolean; remaining: number; resetAt: number; }
License
MIT
