diff --git a/apps/docs/libraries/ts/ratelimit/override/delete-override.mdx b/apps/docs/libraries/ts/ratelimit/override/delete-override.mdx new file mode 100644 index 0000000000..69c1e5efee --- /dev/null +++ b/apps/docs/libraries/ts/ratelimit/override/delete-override.mdx @@ -0,0 +1,44 @@ +--- +title: "Delete Override" +description: "Deletes an override" +--- + +## Request + +Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +Either `namespaceId` or `namespaceName` is required. Not both. + + + +The id of the namespace. Either namespaceId or namespaceName must be provided + + + +Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +## Response + +No response, but if no error is returned the override has been deleted successfully. + + + +```ts +await unkey.deleteOverride({ + identifier: "user_123", + namespaceName: "email.outbound", +}) + +``` +```ts +await unkey.deleteOverride({ + identifier: "user_123", + namespaceId:"rlns_12345", +}) +``` + + diff --git a/apps/docs/libraries/ts/ratelimit/override/get-override.mdx b/apps/docs/libraries/ts/ratelimit/override/get-override.mdx new file mode 100644 index 0000000000..196b9425b0 --- /dev/null +++ b/apps/docs/libraries/ts/ratelimit/override/get-override.mdx @@ -0,0 +1,80 @@ +--- +title: "Get Override" +description: "Gets a ratelimit override" +--- + +## Request + +Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +Either `namespaceId` or `namespaceName` is required. Not both. + + + +The id of the namespace. Either namespaceId or namespaceName must be provided + + + +Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +## Response + + + + + +Identifier of the override requested + + + +Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +How many requests may pass in a given window. + + + +The window duration in milliseconds. + + + +Async will return a response immediately, lowering latency at the cost of accuracy. + + + + + + +```ts +const override = await unkey.getOverride({ + identifier:"user.example", + namespaceName: "email.outbound" +}); + +``` +```ts +const override = await unkey.getOverride({ + identifier:"user.example", + namespaceId: "rlns_1234", +}); +``` + + + +```ts +{ + result: { + id: "rlor_4567", + identifier: "user.example", + limit: 10, + duration: 60000, + async: false + } +} +``` + \ No newline at end of file diff --git a/apps/docs/libraries/ts/ratelimit/override/list-overrides.mdx b/apps/docs/libraries/ts/ratelimit/override/list-overrides.mdx new file mode 100644 index 0000000000..1349111e32 --- /dev/null +++ b/apps/docs/libraries/ts/ratelimit/override/list-overrides.mdx @@ -0,0 +1,96 @@ +--- +title: "List Overrides" +description: "Lists all overrides" +--- + +## Request + + +Either `namespaceId` or `namespaceName` is required. Not both. + + + +The id of the namespace. Either namespaceId or namespaceName must be provided + + + +Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + +## Response + + + + + + + + +Identifier of the override requested + + + +Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +How many requests may pass in a given window. + + + +The window duration in milliseconds. + + + +Async will return a response immediately, lowering latency at the cost of accuracy. + + + + + + +The total number of overrides + + +The cursor to use for pagination + + + + + + + +```ts +const overrides = await unkey.listOverrides({ + namespaceName: "email.outbound" +}); +``` + +```ts +const overrides = await unkey.listOverrides({ + nameSpaceId:"rlns_12345", +}); +``` + + + + +```ts +{ + result: { + overrides: [ + { + id: 'rlor_1234', + identifier: 'customer_123', + limit: 10, + duration: 50000, + async: false + } + ], + total: 1, + cursor: 'rlor_1234' + } +} +``` + + \ No newline at end of file diff --git a/apps/docs/libraries/ts/ratelimit/override/overview.mdx b/apps/docs/libraries/ts/ratelimit/override/overview.mdx new file mode 100644 index 0000000000..b17f9b0332 --- /dev/null +++ b/apps/docs/libraries/ts/ratelimit/override/overview.mdx @@ -0,0 +1,48 @@ +--- +title: "Overview" +description: "Ratelimit overrides" +--- + + +Ratelimit overrides are a way to override the ratelimit for specific users or group using an identifier. + +## Configure your override + +```ts +import { Override } from "@unkey/ratelimit" + +const unkey = new Override({ + rootKey: process.env.UNKEY_ROOT_KEY, +}) +``` + +## Use it + +```ts +async function handler(request) { + + const identifier = request.getUserId() // or ip or anything else you want + + const override = await unkey.setOverride({ + identifier: identifier, + limit: 10, + duration: 60000, + namespaceName: "email.outbound", + async: true + }) + if (override.error){ + // handle the error here + console.error(override.error.message); + return; + } + // handle the request here +} +``` + +There are four main functions to interact with overrides: + +- [setOverride](/libraries/ts/ratelimit/override/set-override) Sets an override for a ratelimit. +- [getOverride](/libraries/ts/ratelimit/override/get-override) Gets a ratelimit override. +- [deleteOverride](/libraries/ts/ratelimit/override/delete-override) Deletes an override. +- [listOverrides](/libraries/ts/ratelimit/override/list-overrides) Lists all overrides for a namnespace. + diff --git a/apps/docs/libraries/ts/ratelimit/override/set-override.mdx b/apps/docs/libraries/ts/ratelimit/override/set-override.mdx new file mode 100644 index 0000000000..e96e78d997 --- /dev/null +++ b/apps/docs/libraries/ts/ratelimit/override/set-override.mdx @@ -0,0 +1,77 @@ +--- +title: "Set Override" +description: "Sets an override for a ratelimit" +--- + +## Request + + + +Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +How many requests may pass in a given window. + + + +The window duration in milliseconds. + + + +Either `namespaceId` or `namespaceName` is required. Not both. + + + +The id of the namespace. Either namespaceId or namespaceName must be provided + + + +Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules + + + +Async will return a response immediately, lowering latency at the cost of accuracy. + + + +## Response + + + + + The id of the override that was set. + + + + +```ts +const override = await unkey.setOverride({ + identifier: "user_123", + limit: 10, + duration: 60000, + namespaceName: "email.outbound", + async: true +}) +``` + +```ts +const override = await unkey.setOverride({ + identifier: "user_123", + limit: 5, + duration: 50000, + namespaceId: "rlns_1234", + async: false +}) +``` + + + +```ts +{ + result: { + overrideId: 'rlor_12345' + } +} +``` + diff --git a/apps/docs/libraries/ts/ratelimit.mdx b/apps/docs/libraries/ts/ratelimit/ratelimit.mdx similarity index 99% rename from apps/docs/libraries/ts/ratelimit.mdx rename to apps/docs/libraries/ts/ratelimit/ratelimit.mdx index b01cae63b0..e9022fdef2 100644 --- a/apps/docs/libraries/ts/ratelimit.mdx +++ b/apps/docs/libraries/ts/ratelimit/ratelimit.mdx @@ -1,5 +1,5 @@ --- -title: "@unkey/ratelimit" +title: "Ratelimit" description: "Serverless ratelimiting" --- diff --git a/apps/docs/libraries/ts/sdk/overview.mdx b/apps/docs/libraries/ts/sdk/overview.mdx index 39d06b7cf7..86b44fbf0a 100644 --- a/apps/docs/libraries/ts/sdk/overview.mdx +++ b/apps/docs/libraries/ts/sdk/overview.mdx @@ -120,7 +120,6 @@ const unkey = new Unkey({ ``` - ### Retries By default the client will retry on network errors, you can customize this behavior: diff --git a/apps/docs/mint.json b/apps/docs/mint.json index 297e765e5f..e7ca397b1c 100644 --- a/apps/docs/mint.json +++ b/apps/docs/mint.json @@ -326,7 +326,22 @@ } ] }, - "libraries/ts/ratelimit", + { + "group": "@unkey/ratelimit", + "pages": [ + "libraries/ts/ratelimit/ratelimit", + { + "group": "Overrides", + "pages": [ + "libraries/ts/ratelimit/override/overview", + "libraries/ts/ratelimit/override/set-override", + "libraries/ts/ratelimit/override/get-override", + "libraries/ts/ratelimit/override/list-overrides", + "libraries/ts/ratelimit/override/delete-override" + ] + } + ] + }, "libraries/ts/nextjs", "libraries/ts/hono", "libraries/ts/cache/overview",