diff --git a/apps/docs/libraries/ts/sdk/overview.mdx b/apps/docs/libraries/ts/sdk/overview.mdx index 4acfe9f5fb..39d06b7cf7 100644 --- a/apps/docs/libraries/ts/sdk/overview.mdx +++ b/apps/docs/libraries/ts/sdk/overview.mdx @@ -112,14 +112,14 @@ The constructor accepts some options to customize the behavior: Run all requests against your own instance of unkey hosted on your own infrastructure. - - ```ts const unkey = new Unkey({ //... baseUrl: "https://my.domain" }) ``` + + ### Retries diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx new file mode 100644 index 0000000000..2da5f5cd50 --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx @@ -0,0 +1,46 @@ +--- +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.ratelimit.deleteOverride({ + identifier: "user_123", + namespaceName: "email.outbound", +}) +``` +```ts +await unkey.ratelimit.deleteOverride({ + identifier: "user_123", + namespaceId: "rlns_1234", +}) +``` + + + + diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx new file mode 100644 index 0000000000..fb093a9a16 --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx @@ -0,0 +1,79 @@ +--- +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.ratelimit.getOverride({ + identifier:"user.example", + nameSpaceId:"rlns_12345", +}); +``` +```ts +const override = await unkey.ratelimit.getOverride({ + identifier:"user.example", + namespaceName: "email.outbound" +}); +``` + + + +```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/sdk/ratelimits/overrides/list-overrides.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx new file mode 100644 index 0000000000..703397620a --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx @@ -0,0 +1,95 @@ +--- +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.ratelimit.listOverrides({ + nameSpaceId:"rlns_12345", +}); +``` +```ts +const overrides = await unkey.ratelimit.listOverrides({ + namespaceName: "email.outbound" +}); +``` + + + +```ts +{ + result: { + overrides: [ + { + id: 'rlor_1234', + identifier: 'customer_123', + limit: 10, + duration: 50000, + async: false + } + ], + total: 1, + cursor: 'rlor_1234' + } +} +``` + + diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx new file mode 100644 index 0000000000..98aa88f5df --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx @@ -0,0 +1,80 @@ +--- +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.ratelimit.setOverride({ + identifier: "user_123", + limit: 10, + duration: 60000, + namespaceId: "rlns_1234", + async: true +}) +``` +```ts +const override = await unkey.ratelimit.setOverride({ + identifier: "user_123", + limit: 10, + duration: 60000, + namespaceName: "email.outbound", + async: true +}) +``` + + + + +```ts +{ + result: { + overrideId: 'rlor_12345' + } +} +``` + + + diff --git a/apps/docs/mint.json b/apps/docs/mint.json index 25317ee705..297e765e5f 100644 --- a/apps/docs/mint.json +++ b/apps/docs/mint.json @@ -311,7 +311,18 @@ }, { "group": "Ratelimits", - "pages": ["libraries/ts/sdk/ratelimits/limit"] + "pages": [ + "libraries/ts/sdk/ratelimits/limit", + { + "group": "Overrides", + "pages": [ + "libraries/ts/sdk/ratelimits/overrides/set-override", + "libraries/ts/sdk/ratelimits/overrides/get-override", + "libraries/ts/sdk/ratelimits/overrides/list-overrides", + "libraries/ts/sdk/ratelimits/overrides/delete-override" + ] + } + ] } ] }, diff --git a/packages/ratelimit/src/index.ts b/packages/ratelimit/src/index.ts index 4e2267e5f6..882798440c 100644 --- a/packages/ratelimit/src/index.ts +++ b/packages/ratelimit/src/index.ts @@ -2,4 +2,5 @@ export * from "./types"; export * from "./interface"; export * from "./noop"; export * from "./ratelimit"; +export * from "./overrides"; export { type Duration } from "./duration"; diff --git a/packages/ratelimit/src/overrides.ts b/packages/ratelimit/src/overrides.ts new file mode 100644 index 0000000000..d031a3809c --- /dev/null +++ b/packages/ratelimit/src/overrides.ts @@ -0,0 +1,51 @@ +import { Unkey } from "@unkey/api"; +import { version } from "../package.json"; + +export type OverrideConfig = { + /** + * @default https://api.unkey.dev + */ + baseUrl?: string; + + /** + * The unkey root key. You can create one at https://unkey.dev/app/settings/root-keys + * + * Make sure the root key has permissions to use overrides. + */ + rootKey: string; + + /** + * + * By default telemetry data is enabled, and sends: + * runtime (Node.js / Edge) + * platform (Node.js / Vercel / AWS) + * SDK version + */ + disableTelemetry?: boolean; +}; + +export class Overrides { + private readonly unkey: Unkey; + + constructor(config: OverrideConfig) { + this.unkey = new Unkey({ + baseUrl: config.baseUrl, + rootKey: config.rootKey, + disableTelemetry: config.disableTelemetry, + wrapperSdkVersion: `@unkey/ratelimit@${version}`, + }); + } + + public get getOverride() { + return this.unkey.ratelimits.getOverride; + } + public get setOverride() { + return this.unkey.ratelimits.setOverride; + } + public get deleteOverride() { + return this.unkey.ratelimits.deleteOverride; + } + public get listOverrides() { + return this.unkey.ratelimits.listOverrides; + } +}