{"version":3,"file":"middleware.mjs","names":["rateLimit","errors","config","defaultErrors","key","input","tenantContext","status","z"],"sources":["../src/middleware.ts"],"sourcesContent":["import { z } from 'zod'\nimport { defaultErrors, type ErrorFactory } from './errors'\n\n/**\n * Structural surface of @convex-dev/rate-limiter's RateLimiter (checked\n * against 0.3.x; re-verify at install). The host mounts the component and\n * constructs the limiter; the kit only ever receives the instance —\n * injection keeps the kit dependency-free.\n */\nexport type RateLimiterLike = {\n\tlimit: (\n\t\tctx: never,\n\t\tname: string,\n\t\toptions?: { key?: string },\n\t) => Promise<{ ok: boolean; retryAfter?: number }>\n}\n\n/**\n * The first packaged middleware: rate limiting on the Command/Query\n * middleware seams. Runs before guards and the handler; a rejected call\n * throws RATE_LIMITED (audit and handler never run). Keyed by ctx.tenant by\n * default — when no key fn is given and ctx.tenant is undefined, this is a\n * configuration error, not a silent shared bucket.\n *\n * The returned middleware is shape-compatible with both AnyCommandMiddleware\n * and AnyQueryMiddleware.\n */\nexport function rateLimit<Context = never>(config: {\n\tlimiter: RateLimiterLike\n\tname: string\n\t/** Defaults to ctx.tenant. */\n\tkey?: (context: Context) => string\n\t/** Observe a rejection before the throw (metrics, headers). */\n\tonLimit?: (status: { ok: boolean; retryAfter?: number }, context: Context) => void | Promise<void>\n\terrors?: ErrorFactory\n}) {\n\tconst errors = config.errors ?? defaultErrors\n\treturn async <Result>(input: {\n\t\tcontext: never\n\t\tnext: (options?: { context?: object }) => Promise<Result>\n\t}): Promise<Result> => {\n\t\tconst key = config.key ? config.key(input.context) : tenantContext.parse(input.context).tenant\n\t\tif (key === undefined) {\n\t\t\treturn errors.throw({\n\t\t\t\tcode: 'RATE_LIMIT_KEY_MISSING',\n\t\t\t\tmessage: `rateLimit(\"${config.name}\") has no key: ctx.tenant is undefined and no key fn was configured`,\n\t\t\t})\n\t\t}\n\t\tconst status = await config.limiter.limit(input.context, config.name, {\n\t\t\tkey,\n\t\t})\n\t\tif (!status.ok) {\n\t\t\tawait config.onLimit?.(status, input.context)\n\t\t\treturn errors.throw({\n\t\t\t\tcode: 'RATE_LIMITED',\n\t\t\t\tmessage: `Rate limit \"${config.name}\" exceeded${\n\t\t\t\t\tstatus.retryAfter === undefined ? '' : `; retry after ${Math.ceil(status.retryAfter)}ms`\n\t\t\t\t}`,\n\t\t\t})\n\t\t}\n\t\treturn input.next()\n\t}\n}\n\nconst tenantContext = z.object({ tenant: z.string().optional() })\n"],"mappings":"qEA2BA,SAAgBA,EAA2B,EAQxC,CACF,IAAMC,EAASC,EAAO,QAAUC,EAChC,OAAO,KAAe,IAGC,CACtB,IAAMC,EAAMF,EAAO,IAAMA,EAAO,IAAIG,EAAM,OAAO,EAAIC,EAAc,MAAMD,EAAM,OAAO,CAAC,CAAC,OACxF,GAAID,IAAQ,IAAA,GACX,OAAOH,EAAO,MAAM,CACnB,KAAM,yBACN,QAAS,cAAcC,EAAO,KAAK,oEACpC,CAAC,EAEF,IAAMK,EAAS,MAAML,EAAO,QAAQ,MAAMG,EAAM,QAASH,EAAO,KAAM,CACrE,KACD,CAAC,EAUD,OATKK,EAAO,GASLF,EAAM,KAAK,GARjB,MAAMH,EAAO,UAAUK,EAAQF,EAAM,OAAO,EACrCJ,EAAO,MAAM,CACnB,KAAM,eACN,QAAS,eAAeC,EAAO,KAAK,YACnCK,EAAO,aAAe,IAAA,GAAY,GAAK,iBAAiB,KAAK,KAAKA,EAAO,UAAU,EAAE,KAEvF,CAAC,EAGH,CACD,CAEA,MAAMD,EAAgBE,EAAE,OAAO,CAAE,OAAQA,EAAE,OAAO,CAAC,CAAC,SAAS,CAAE,CAAC"}