-
Notifications
You must be signed in to change notification settings - Fork 610
chore: add override routes in unkey ratelimit sdk docs #2741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaelUnkey
merged 11 commits into
main
from
eng-1597-add-override-routes-in-unkeyratelimit-sdk-docs
Dec 12, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ca15e1d
add files
921cf1f
ratelimit sdk docs
8d71362
Merge branch 'main' of https://github.com/unkeyed/unkey into eng-1597…
525bbf0
Fix for sdk overview tag location
3496f21
Pr changes from comments
39ce036
pr changes
63ece8e
Merge branch 'main' of https://github.com/unkeyed/unkey into eng-1597…
5bc34f4
final changes
87add0f
final
5d2ea10
Merge branch 'main' of https://github.com/unkeyed/unkey into eng-1597…
fb6268c
merge
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
apps/docs/libraries/ts/ratelimit/override/delete-override.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| --- | ||
| title: "Delete Override" | ||
| description: "Deletes an override" | ||
| --- | ||
|
|
||
| ## Request | ||
| <ParamField body="identifier" type="string" required> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
| <Warning> | ||
| Either `namespaceId` or `namespaceName` is required. Not both. | ||
| </Warning> | ||
|
|
||
| <ParamField body="namespaceId" type="string"> | ||
| The id of the namespace. Either namespaceId or namespaceName must be provided | ||
| </ParamField> | ||
|
|
||
| <ParamField body="namespaceName" type="string"> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
|
|
||
| ## Response | ||
|
|
||
| No response, but if no error is returned the override has been deleted successfully. | ||
|
|
||
|
|
||
| <RequestExample> | ||
| ```ts | ||
| await unkey.deleteOverride({ | ||
| identifier: "user_123", | ||
| namespaceName: "email.outbound", | ||
| }) | ||
|
|
||
| ``` | ||
| ```ts | ||
| await unkey.deleteOverride({ | ||
| identifier: "user_123", | ||
| namespaceId:"rlns_12345", | ||
| }) | ||
| ``` | ||
| </RequestExample> | ||
|
|
||
80 changes: 80 additions & 0 deletions
80
apps/docs/libraries/ts/ratelimit/override/get-override.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --- | ||
| title: "Get Override" | ||
| description: "Gets a ratelimit override" | ||
| --- | ||
|
|
||
| ## Request | ||
| <ParamField body="identifier" type="string" required> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
| <Warning> | ||
| Either `namespaceId` or `namespaceName` is required. Not both. | ||
| </Warning> | ||
|
|
||
| <ParamField body="namespaceId" type="string"> | ||
| The id of the namespace. Either namespaceId or namespaceName must be provided | ||
| </ParamField> | ||
|
|
||
| <ParamField body="namespaceName" type="string"> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
|
|
||
| ## Response | ||
|
|
||
| <ResponseField name="result"> | ||
| <Expandable title="properties" defaultOpen> | ||
|
|
||
| <ParamField body="id" type="string" required> | ||
| Identifier of the override requested | ||
| </ParamField> | ||
|
|
||
| <ParamField body="identifier" type="string" required> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
| <ParamField body="limit" type="number" required> | ||
| How many requests may pass in a given window. | ||
| </ParamField> | ||
|
|
||
| <ParamField body="duration" type="number" required> | ||
| The window duration in milliseconds. | ||
| </ParamField> | ||
|
|
||
| <ParamField body="async" type="boolean"> | ||
| Async will return a response immediately, lowering latency at the cost of accuracy. | ||
| </ParamField> | ||
| </Expandable> | ||
| </ResponseField > | ||
|
|
||
|
|
||
| <RequestExample> | ||
| ```ts | ||
| const override = await unkey.getOverride({ | ||
| identifier:"user.example", | ||
| namespaceName: "email.outbound" | ||
| }); | ||
|
|
||
| ``` | ||
| ```ts | ||
| const override = await unkey.getOverride({ | ||
| identifier:"user.example", | ||
| namespaceId: "rlns_1234", | ||
| }); | ||
| ``` | ||
| </RequestExample> | ||
|
|
||
| <ResponseExample> | ||
| ```ts | ||
| { | ||
| result: { | ||
| id: "rlor_4567", | ||
| identifier: "user.example", | ||
| limit: 10, | ||
| duration: 60000, | ||
| async: false | ||
| } | ||
| } | ||
| ``` | ||
| </ResponseExample> |
96 changes: 96 additions & 0 deletions
96
apps/docs/libraries/ts/ratelimit/override/list-overrides.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| title: "List Overrides" | ||
| description: "Lists all overrides" | ||
| --- | ||
|
|
||
| ## Request | ||
|
|
||
| <Warning> | ||
| Either `namespaceId` or `namespaceName` is required. Not both. | ||
| </Warning> | ||
|
|
||
| <ParamField body="namespaceId" type="string"> | ||
| The id of the namespace. Either namespaceId or namespaceName must be provided | ||
| </ParamField> | ||
|
|
||
| <ParamField body="namespaceName" type="string"> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
| ## Response | ||
|
|
||
| <ResponseField name="result"> | ||
| <Expandable title="properties" defaultOpen> | ||
|
|
||
| <ResponseField name="overrides" type="Array" required> | ||
|
|
||
| <Expandable> | ||
| <ParamField body="id" type="string" required> | ||
| Identifier of the override requested | ||
| </ParamField> | ||
|
|
||
| <ParamField body="identifier" type="string" required> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
| <ParamField body="limit" type="number" required> | ||
| How many requests may pass in a given window. | ||
| </ParamField> | ||
|
|
||
| <ParamField body="duration" type="number" required> | ||
| The window duration in milliseconds. | ||
| </ParamField> | ||
|
|
||
| <ParamField body="async" type="boolean" optional> | ||
| Async will return a response immediately, lowering latency at the cost of accuracy. | ||
| </ParamField> | ||
|
|
||
| </Expandable> | ||
| </ResponseField> | ||
|
|
||
| <ResponseField name="total" type="number" required> | ||
| The total number of overrides | ||
| </ResponseField> | ||
| <ResponseField name="cursor" type="string" optional> | ||
| The cursor to use for pagination | ||
| </ResponseField> | ||
| </Expandable> | ||
|
|
||
| </ResponseField> | ||
|
|
||
|
|
||
| <RequestExample> | ||
| ```ts | ||
| const overrides = await unkey.listOverrides({ | ||
| namespaceName: "email.outbound" | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts | ||
| const overrides = await unkey.listOverrides({ | ||
| nameSpaceId:"rlns_12345", | ||
| }); | ||
| ``` | ||
| </RequestExample> | ||
|
|
||
|
|
||
| <ResponseExample> | ||
| ```ts | ||
| { | ||
| result: { | ||
| overrides: [ | ||
| { | ||
| id: 'rlor_1234', | ||
| identifier: 'customer_123', | ||
| limit: 10, | ||
| duration: 50000, | ||
| async: false | ||
| } | ||
| ], | ||
| total: 1, | ||
| cursor: 'rlor_1234' | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </ResponseExample> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
MichaelUnkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| // 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. | ||
|
|
||
77 changes: 77 additions & 0 deletions
77
apps/docs/libraries/ts/ratelimit/override/set-override.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| title: "Set Override" | ||
| description: "Sets an override for a ratelimit" | ||
| --- | ||
|
|
||
| ## Request | ||
|
|
||
|
|
||
| <ParamField body="identifier" type="string" required> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
| <ParamField body="limit" type="number" required> | ||
| How many requests may pass in a given window. | ||
| </ParamField> | ||
|
|
||
| <ParamField body="duration" type="number" required> | ||
| The window duration in milliseconds. | ||
| </ParamField> | ||
|
|
||
| <Warning> | ||
| Either `namespaceId` or `namespaceName` is required. Not both. | ||
| </Warning> | ||
|
|
||
| <ParamField body="namespaceId" type="string"> | ||
| The id of the namespace. Either namespaceId or namespaceName must be provided | ||
| </ParamField> | ||
|
|
||
| <ParamField body="namespaceName" type="string"> | ||
| 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 | ||
| </ParamField> | ||
|
|
||
| <ParamField body="async" type="boolean" default="false"> | ||
| Async will return a response immediately, lowering latency at the cost of accuracy. | ||
| </ParamField> | ||
|
|
||
|
|
||
| ## Response | ||
|
|
||
| <ResponseField name="result"> | ||
| <Expandable title="properties" defaultOpen> | ||
| <ResponseField name="overrideId" type="string" required> | ||
| The id of the override that was set. | ||
| </ResponseField> | ||
| </Expandable> | ||
| </ResponseField> | ||
| <RequestExample> | ||
| ```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 | ||
| }) | ||
| ``` | ||
| </RequestExample> | ||
|
|
||
| <ResponseExample> | ||
| ```ts | ||
| { | ||
| result: { | ||
| overrideId: 'rlor_12345' | ||
| } | ||
| } | ||
| ``` | ||
| </ResponseExample> |
2 changes: 1 addition & 1 deletion
2
apps/docs/libraries/ts/ratelimit.mdx → ...docs/libraries/ts/ratelimit/ratelimit.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| title: "@unkey/ratelimit" | ||
| title: "Ratelimit" | ||
| description: "Serverless ratelimiting" | ||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.