diff --git a/packages/ratelimit/package.json b/packages/ratelimit/package.json index c4d3d0d5a6..76623edcfd 100644 --- a/packages/ratelimit/package.json +++ b/packages/ratelimit/package.json @@ -26,6 +26,6 @@ "typescript": "^5.5.3" }, "dependencies": { - "@unkey/api": "^0.35.0" + "@unkey/api": "2.0.0-alpha.7" } } diff --git a/packages/ratelimit/src/overrides.ts b/packages/ratelimit/src/overrides.ts index b66ba2e51e..44ea236732 100644 --- a/packages/ratelimit/src/overrides.ts +++ b/packages/ratelimit/src/overrides.ts @@ -7,7 +7,7 @@ export type OverrideConfig = { baseUrl?: string; /** - * The unkey root key. You can create one at https://unkey.dev/app/settings/root-keys + * The unkey root key. You can create one at https://app.unkey.com/settings/root-keys * * Make sure the root key has permissions to use overrides. */ @@ -19,21 +19,21 @@ export class Overrides { constructor(config: OverrideConfig) { this.unkey = new Unkey({ - baseUrl: config.baseUrl, + serverURL: config.baseUrl, rootKey: config.rootKey, }); } public get getOverride() { - return this.unkey.ratelimits.getOverride; + return this.unkey.ratelimit.getOverride; } public get setOverride() { - return this.unkey.ratelimits.setOverride; + return this.unkey.ratelimit.setOverride; } public get deleteOverride() { - return this.unkey.ratelimits.deleteOverride; + return this.unkey.ratelimit.deleteOverride; } public get listOverrides() { - return this.unkey.ratelimits.listOverrides; + return this.unkey.ratelimit.listOverrides; } } diff --git a/packages/ratelimit/src/ratelimit.ts b/packages/ratelimit/src/ratelimit.ts index dbb83a535d..6355e3b91d 100644 --- a/packages/ratelimit/src/ratelimit.ts +++ b/packages/ratelimit/src/ratelimit.ts @@ -1,4 +1,5 @@ import { Unkey } from "@unkey/api"; +import { APIError } from "@unkey/api/models/errors"; import { type Duration, ms } from "./duration"; import type { Ratelimiter } from "./interface"; import type { Limit, LimitOptions, RatelimitResponse } from "./types"; @@ -124,7 +125,7 @@ export class Ratelimit implements Ratelimiter { constructor(config: RatelimitConfig) { this.config = config; this.unkey = new Unkey({ - baseUrl: config.baseUrl, + serverURL: config.baseUrl, rootKey: config.rootKey, }); } @@ -168,7 +169,7 @@ export class Ratelimit implements Ratelimiter { let timeoutId: any = null; try { const ps: Promise[] = [ - this.unkey.ratelimits + this.unkey.ratelimit .limit({ namespace: this.config.namespace, identifier, @@ -177,13 +178,16 @@ export class Ratelimit implements Ratelimiter { cost: opts?.cost, }) .then(async (res) => { - if (res.error) { + return res.data; + }) + .catch((err) => { + if (err instanceof APIError) { throw new Error( - `Ratelimit failed: [${res.error.code} - ${res.error.requestId}]: ${res.error.message}`, + `Ratelimit failed: [${err.statusCode} - ${err.message}]: ${err.body}`, ); } - return res.result; + throw new Error(`Ratelimit failed: ${err}`); }), ]; if (timeout) { diff --git a/packages/ratelimit/src/types.ts b/packages/ratelimit/src/types.ts index 958b06bb30..0c48e0c882 100644 --- a/packages/ratelimit/src/types.ts +++ b/packages/ratelimit/src/types.ts @@ -29,6 +29,11 @@ export type RatelimitResponse = { * Unix timestamp in milliseconds when the limits are reset. */ reset: number; + + /** + * The override id for the request that was used to override the limit. + */ + overrideId?: string; }; export type LimitOptions = { diff --git a/packages/ratelimit/tsconfig.json b/packages/ratelimit/tsconfig.json index 49fb1f9ddb..5df2cb4286 100644 --- a/packages/ratelimit/tsconfig.json +++ b/packages/ratelimit/tsconfig.json @@ -28,7 +28,7 @@ /* Modules */ // "module": "CommonJS", /* Specify what module code is generated. */ // "rootDir": "./", /* Specify the root folder within your source files. */ - "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, + "moduleResolution": "bundler" /* Specify how TypeScript looks up a file from a given module specifier. */, // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 56598e6da7..3b977985a0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1535,8 +1535,8 @@ importers: packages/ratelimit: dependencies: '@unkey/api': - specifier: ^0.35.0 - version: link:../api + specifier: 2.0.0-alpha.7 + version: 2.0.0-alpha.7(zod@3.23.8) devDependencies: '@types/node': specifier: ^20.14.9 @@ -11496,7 +11496,7 @@ packages: /@types/accepts@1.3.7: resolution: {integrity: sha512-Pay9fq2lM2wXPWbteBsRAGiWH2hig4ZE2asK+mm7kUzlxRTfL961rj89I6zV/E3PcIkDqyuBEcMxFT7rccugeQ==} dependencies: - '@types/node': 22.14.0 + '@types/node': 20.14.9 dev: false /@types/acorn@4.0.6: @@ -11513,13 +11513,13 @@ packages: resolution: {integrity: sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==} dependencies: '@types/connect': 3.4.38 - '@types/node': 22.14.0 + '@types/node': 20.14.9 dev: false /@types/connect@3.4.38: resolution: {integrity: sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==} dependencies: - '@types/node': 22.14.0 + '@types/node': 20.14.9 dev: false /@types/content-disposition@0.5.8: @@ -11529,7 +11529,7 @@ packages: /@types/conventional-commits-parser@5.0.1: resolution: {integrity: sha512-7uz5EHdzz2TqoMfV7ee61Egf5y6NkcO4FB/1iCCQnbeiI1F3xzv3vK5dBCXUCLQgGYS+mUeigK1iKQzvED+QnQ==} dependencies: - '@types/node': 22.14.0 + '@types/node': 20.14.9 dev: true optional: true @@ -11550,7 +11550,7 @@ packages: '@types/connect': 3.4.38 '@types/express': 4.17.21 '@types/keygrip': 1.0.6 - '@types/node': 22.14.0 + '@types/node': 20.14.9 dev: false /@types/cors@2.8.17: @@ -11658,7 +11658,7 @@ packages: /@types/express-serve-static-core@4.19.6: resolution: {integrity: sha512-N4LZ2xG7DatVqhCZzOGb1Yi5lMbXSZcmdLDe9EzSndPV2HpWYWzRbaerl2n27irrm94EPpprqa8KpskPT085+A==} dependencies: - '@types/node': 22.14.0 + '@types/node': 20.14.9 '@types/qs': 6.9.18 '@types/range-parser': 1.2.7 '@types/send': 0.17.4 @@ -11732,7 +11732,7 @@ packages: '@types/http-errors': 2.0.4 '@types/keygrip': 1.0.6 '@types/koa-compose': 3.2.8 - '@types/node': 22.14.0 + '@types/node': 20.14.9 dev: false /@types/mdast@3.0.15: @@ -11865,14 +11865,14 @@ packages: resolution: {integrity: sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==} dependencies: '@types/mime': 1.3.5 - '@types/node': 22.14.0 + '@types/node': 20.14.9 dev: false /@types/serve-static@1.15.7: resolution: {integrity: sha512-W8Ym+h8nhuRwaKPaDw34QUkwsGi6Rc4yYqvKFo5rm2FUEhCFbzVWrxXUxuKK8TASjWsysJY0nsmNCGhCOIsrOw==} dependencies: '@types/http-errors': 2.0.4 - '@types/node': 22.14.0 + '@types/node': 20.14.9 '@types/send': 0.17.4 dev: false @@ -11999,6 +11999,19 @@ packages: '@unkey/rbac': 0.1.13 dev: false + /@unkey/api@2.0.0-alpha.7(zod@3.23.8): + resolution: {integrity: sha512-iiA+PVXn5T3dlZT8woKrnr0kB15XLtmkFWHV2+ox+980W/7vQLP1gdgcU/lrX/qA09Z+0w0wN+n0C88RLd4nvw==} + hasBin: true + peerDependencies: + '@modelcontextprotocol/sdk': ^1.5.0 + zod: '>= 3' + peerDependenciesMeta: + '@modelcontextprotocol/sdk': + optional: true + dependencies: + zod: 3.23.8 + dev: false + /@unkey/error@0.0.2: resolution: {integrity: sha512-E4/2PClLeZ/X9nGUqe36xy/tX2KFwBsOrqCMZbG3mscAUsHtnjSLzV8xa9TVQlfH4tXt2mfgAqxLqEwr1ayZDA==} dependencies: @@ -14228,7 +14241,7 @@ packages: dependencies: is-arguments: 1.2.0 is-date-object: 1.1.0 - is-regex: 1.1.4 + is-regex: 1.2.1 object-is: 1.1.6 object-keys: 1.1.1 regexp.prototype.flags: 1.5.4 @@ -20012,6 +20025,7 @@ packages: /minipass@6.0.2: resolution: {integrity: sha512-MzWSV5nYVT7mVyWCwn2o7JH13w2TBRmmSqSRCKzTw+lmft9X4z+3wjvs06Tzijo5z4W/kahUCDpRXTF+ZrmF/w==} engines: {node: '>=16 || 14 >=14.17'} + dev: true /minipass@7.1.2: resolution: {integrity: sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==} @@ -21180,7 +21194,7 @@ packages: engines: {node: '>=16 || 14 >=14.18'} dependencies: lru-cache: 10.4.3 - minipass: 6.0.2 + minipass: 7.1.2 /path-to-regexp@0.1.12: resolution: {integrity: sha512-RA1GjUVMnvYFxuqovrEqZoxxW5NUZqbwKtYz/Tt7nXerk0LbLblQmrsgdeOxV5SFHf0UDggjS/bSeOZwt1pmEQ==}