CaptainCodeman / svelte-api-keys

API Key Generation, Validation, and Rate Limiting for SvelteKit
https://captaincodeman.github.io/svelte-api-keys/
37 stars 2 forks source link

Overload .limit() for import-free endpoints #10

Closed CaptainCodeman closed 5 months ago

CaptainCodeman commented 5 months ago

Make it easier to call the limit fn in endpoints, without requiring any imports, by accepting rate and size parameters directly (which are normally used for the Refill constructor). The api object on locals could also expose the SECOND, MINUTE, etc... constants

before

import { MINUTE, Refill } from 'svelte-api-keys'

const rate = new Refill(30 / MINUTE, 10)

export async function GET({ locals }) {
  await locals.api.has('read').limit(rate)
  // process request
}

after

export async function GET({ locals }) {
  await locals.api.has('read').limit(30 / locals.api.MINUTE, 10)
  // process request
}