From f9b8870d06497d95351559c2a4ced78d5130e07b Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Mon, 9 Dec 2024 15:37:06 -0500 Subject: [PATCH 01/11] added overide methods to ratelimit sdk --- packages/ratelimit/src/ratelimit.ts | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/ratelimit/src/ratelimit.ts b/packages/ratelimit/src/ratelimit.ts index 274bc1122b..ce0e1004e0 100644 --- a/packages/ratelimit/src/ratelimit.ts +++ b/packages/ratelimit/src/ratelimit.ts @@ -212,4 +212,37 @@ export class Ratelimit implements Ratelimiter { } } } + + public async setOverride(identifier: string, limit: number, duration: number, namespaceName?: string, namespaceId?:string, async?: boolean) { + return this.unkey.ratelimits.setOverride({ + namespaceId, + namespaceName, + identifier, + limit, + duration, + async + }); + } + public async getOverride(identifier: string, namespaceName?: string, namespaceId?: string,) { + return this.unkey.ratelimits.getOverride({ + namespaceName, + namespaceId, + identifier, + }); + } + public async listOverrides(namespaceName?: string, namespaceId?: string, limit?: number, cursor?: string) { + return this.unkey.ratelimits.listOverrides({ + namespaceName, + namespaceId, + limit, + cursor, + }); + } + public async deleteOverride(identifier: string, namespaceName?: string, namespaceId?: string,) { + return this.unkey.ratelimits.deleteOverride({ + namespaceName, + namespaceId, + identifier, + }); + } } From 6b79bed6bc97b7af31d8e69aedd1ef76198e7900 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 9 Dec 2024 20:41:54 +0000 Subject: [PATCH 02/11] [autofix.ci] apply automated fixes --- packages/ratelimit/src/ratelimit.ts | 34 +++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/ratelimit/src/ratelimit.ts b/packages/ratelimit/src/ratelimit.ts index ce0e1004e0..224843aaec 100644 --- a/packages/ratelimit/src/ratelimit.ts +++ b/packages/ratelimit/src/ratelimit.ts @@ -213,24 +213,36 @@ export class Ratelimit implements Ratelimiter { } } - public async setOverride(identifier: string, limit: number, duration: number, namespaceName?: string, namespaceId?:string, async?: boolean) { + public async setOverride( + identifier: string, + limit: number, + duration: number, + namespaceName?: string, + namespaceId?: string, + async?: boolean, + ) { return this.unkey.ratelimits.setOverride({ - namespaceId, - namespaceName, - identifier, - limit, - duration, - async + namespaceId, + namespaceName, + identifier, + limit, + duration, + async, }); } - public async getOverride(identifier: string, namespaceName?: string, namespaceId?: string,) { + public async getOverride(identifier: string, namespaceName?: string, namespaceId?: string) { return this.unkey.ratelimits.getOverride({ namespaceName, namespaceId, identifier, }); } - public async listOverrides(namespaceName?: string, namespaceId?: string, limit?: number, cursor?: string) { + public async listOverrides( + namespaceName?: string, + namespaceId?: string, + limit?: number, + cursor?: string, + ) { return this.unkey.ratelimits.listOverrides({ namespaceName, namespaceId, @@ -238,11 +250,11 @@ export class Ratelimit implements Ratelimiter { cursor, }); } - public async deleteOverride(identifier: string, namespaceName?: string, namespaceId?: string,) { + public async deleteOverride(identifier: string, namespaceName?: string, namespaceId?: string) { return this.unkey.ratelimits.deleteOverride({ namespaceName, namespaceId, identifier, }); - } + } } From 9782f161972fb7020a43efe5eb9fce88d7d6e61a Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Tue, 10 Dec 2024 09:37:57 -0500 Subject: [PATCH 03/11] new docs --- .../ratelimits/overrides/delete-override.mdx | 11 ++ .../sdk/ratelimits/overrides/get-override.mdx | 20 +++ .../ratelimits/overrides/list-overrides.mdx | 17 +++ .../sdk/ratelimits/overrides/set-override.mdx | 123 ++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx create mode 100644 apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx create mode 100644 apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx create mode 100644 apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx 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..e78dc49629 --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx @@ -0,0 +1,11 @@ +--- +title: "Delete Override" +description: "Deletes an override" +--- + +## Request + + +const del = await unkey.deleteOverride("test11", "test.namespace",undefined); + +{ result: {} } \ No newline at end of file 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..3b3700c1b0 --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx @@ -0,0 +1,20 @@ +--- +title: "Get Override" +description: "Gets a ratelimit override" +--- + +## Request + +const get = await unkey.getOverride( "test12", "test.namespace"); + + + +{ + result: { + id: 'rlor_46rUe1ys6xn2PFsdX5yU38bXmVCF', + identifier: 'test12', + limit: 10, + duration: 60000, + async: true + } +} \ 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..373dc26f04 --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx @@ -0,0 +1,17 @@ +--- +title: "List Overrides" +description: "Lists all overrides" +--- + +## Request + +const list = await unkey.listOverrides(undefined,"rlns_2L5r7Aupqd4YLGmrokXC6PQQKDhu"); + + + { + result: { + overrides: [ [Object], [Object], [Object] ], + total: 3, + cursor: 'rlor_qp4tpb5us7fwVDVMHiM3HMyis3t' + } +} \ No newline at end of file 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..10938f8257 --- /dev/null +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx @@ -0,0 +1,123 @@ +--- +title: "Set Override" +description: "Sets a ratelimit override" +--- + +## 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 + + + +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. + + + +## Response + + + + + +Whether the request may pass(true) or exceeded the limit(false). + + +Maximum number of requests allowed within a window. + + +How many requests the user has left within the current window. + + +Unix timestamp in milliseconds when the limits are reset. + + + + + + +```ts +await unkey.ratelimits.setOverride({ + identifier: "user_123", + limit: 10, + duration: 60000, + namespaceName: "email.outbound", + namespaceId: "rlns_1234", + async: true + }) +``` + + + + + +```ts +await unkey.ratelimits.setOverride("testId", 10, 60000, undefined ,"rlns_12345", true) +``` + + + + +```ts +{ + result: { + overrideId: 'rlor_12345' + } +} +``` + + + + +```ts +{ + result: { + overrides: [ + { + id: 'rlor_qp3VKARgDKkJeFyuJqmRTBjxkoC', + identifier: 'test11', + limit: 10, + duration: 60000 + }, + { + id: 'rlor_qp4ActUXH5JuTV2TpbUAAz9Kpyg', + identifier: 'test1123', + limit: 10, + duration: 60000, + async: true + }, + { + id: 'rlor_qp4tpb5us7fwVDVMHiM3HMyis3t', + identifier: 'test1123321', + limit: 105, + duration: 60000, + async: true + } + ], + total: 3, + cursor: 'rlor_qp4tpb5us7fwVDVMHiM3HMyis3t' + } +} +``` + + \ No newline at end of file From 09bfa6f737bb634def5fea1fd667f6d0a3011c00 Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Tue, 10 Dec 2024 14:30:31 -0500 Subject: [PATCH 04/11] Override sdk docs --- .../ratelimits/overrides/delete-override.mdx | 19 +++- .../sdk/ratelimits/overrides/get-override.mdx | 60 +++++++++++-- .../ratelimits/overrides/list-overrides.mdx | 86 +++++++++++++++++-- .../sdk/ratelimits/overrides/set-override.mdx | 83 +++++------------- apps/docs/mint.json | 8 +- 5 files changed, 179 insertions(+), 77 deletions(-) diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx index e78dc49629..a9216a57bd 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx @@ -4,8 +4,23 @@ 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. + -const del = await unkey.deleteOverride("test11", "test.namespace",undefined); + +The id of the namespace. Either namespaceId or namespaceName must be provided + -{ result: {} } \ No newline at end of file + +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. diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx index 3b3700c1b0..07ba307164 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx @@ -4,17 +4,67 @@ 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 + -const get = await unkey.getOverride( "test12", "test.namespace"); + +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 list = await unkey.getOverride({ + identifier:"user.example", + namespaceName: "email.outbound" + nameSpaceId:"rlns_12345", + }); + ``` + + + +```ts { result: { - id: 'rlor_46rUe1ys6xn2PFsdX5yU38bXmVCF', - identifier: 'test12', + id: "rlor_4567", + identifier: "user.example", limit: 10, duration: 60000, - async: true + async: false } -} \ No newline at end of file +} +``` + + \ 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 index 373dc26f04..d200e21e3a 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx @@ -5,13 +5,87 @@ description: "Lists all overrides" ## Request -const list = await unkey.listOverrides(undefined,"rlns_2L5r7Aupqd4YLGmrokXC6PQQKDhu"); + +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 list = await unkey.listOverrides({ + nameSpaceId:"rlns_12345", + namespaceName: "email.outbound" + }); + ``` + + + +```ts +{ result: { - overrides: [ [Object], [Object], [Object] ], - total: 3, - cursor: 'rlor_qp4tpb5us7fwVDVMHiM3HMyis3t' + overrides: [ + { + id: 'rlor_1234', + identifier: 'customer_123', + limit: 10, + duration: 50000, + async: false + } + ], + total: 1, + cursor: 'rlor_1234' } -} \ No newline at end of file +} +``` + + \ No newline at end of file diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx index 10938f8257..742d4ffa4b 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx @@ -1,10 +1,23 @@ --- title: "Set Override" -description: "Sets a ratelimit 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. @@ -17,18 +30,6 @@ 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 - -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. @@ -37,23 +38,12 @@ Async will return a response immediately, lowering latency at the cost of accura ## Response - - - -Whether the request may pass(true) or exceeded the limit(false). - - -Maximum number of requests allowed within a window. - - -How many requests the user has left within the current window. - - -Unix timestamp in milliseconds when the limits are reset. - - - - + + + The id of the override that was set. + + + ```ts @@ -88,36 +78,3 @@ await unkey.ratelimits.setOverride("testId", 10, 60000, undefined ,"rlns_12345" - -```ts -{ - result: { - overrides: [ - { - id: 'rlor_qp3VKARgDKkJeFyuJqmRTBjxkoC', - identifier: 'test11', - limit: 10, - duration: 60000 - }, - { - id: 'rlor_qp4ActUXH5JuTV2TpbUAAz9Kpyg', - identifier: 'test1123', - limit: 10, - duration: 60000, - async: true - }, - { - id: 'rlor_qp4tpb5us7fwVDVMHiM3HMyis3t', - identifier: 'test1123321', - limit: 105, - duration: 60000, - async: true - } - ], - total: 3, - cursor: 'rlor_qp4tpb5us7fwVDVMHiM3HMyis3t' - } -} -``` - - \ No newline at end of file diff --git a/apps/docs/mint.json b/apps/docs/mint.json index 25317ee705..59cd02a0a4 100644 --- a/apps/docs/mint.json +++ b/apps/docs/mint.json @@ -311,7 +311,13 @@ }, { "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"]}] } ] }, From 862b142a5d65d9d764a1a5e5962fbad73bc8b23b Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Tue, 10 Dec 2024 14:32:05 -0500 Subject: [PATCH 05/11] fmt --- apps/docs/mint.json | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/apps/docs/mint.json b/apps/docs/mint.json index 59cd02a0a4..297e765e5f 100644 --- a/apps/docs/mint.json +++ b/apps/docs/mint.json @@ -311,13 +311,18 @@ }, { "group": "Ratelimits", - "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"]}] + "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" + ] + } + ] } ] }, From f1930da72bbe9e7ab9265c14e9b5a2cb93eafbf2 Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Tue, 10 Dec 2024 22:14:44 -0500 Subject: [PATCH 06/11] changed to getters fixed docs --- .../ratelimits/overrides/delete-override.mdx | 15 ++++++ .../sdk/ratelimits/overrides/get-override.mdx | 20 +++++--- .../ratelimits/overrides/list-overrides.mdx | 8 +-- .../sdk/ratelimits/overrides/set-override.mdx | 12 +---- packages/ratelimit/src/index.ts | 1 + packages/ratelimit/src/overrides.ts | 51 +++++++++++++++++++ packages/ratelimit/src/ratelimit.ts | 45 ---------------- 7 files changed, 85 insertions(+), 67 deletions(-) create mode 100644 packages/ratelimit/src/overrides.ts diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx index a9216a57bd..cebed241ac 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx @@ -24,3 +24,18 @@ Namespaces group different limits together for better analytics. You might have ## 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", + 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 index 07ba307164..109877f4ee 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx @@ -23,6 +23,9 @@ Namespaces group different limits together for better analytics. You might have ## Response + + + Identifier of the override requested @@ -42,16 +45,18 @@ The window duration in milliseconds. Async will return a response immediately, lowering latency at the cost of accuracy. + + - ```ts - const list = await unkey.getOverride({ - identifier:"user.example", - namespaceName: "email.outbound" - nameSpaceId:"rlns_12345", - }); - ``` +```ts +await unkey.ratelimit.getOverride({ + identifier:"user.example", + namespaceName: "email.outbound" + nameSpaceId:"rlns_12345", +}); +``` @@ -66,5 +71,4 @@ Async will return a response immediately, lowering latency at the cost of accura } } ``` - \ 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 index d200e21e3a..1aeac23bfa 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx @@ -61,12 +61,12 @@ The cursor to use for pagination - ```ts - const list = await unkey.listOverrides({ +```ts +await unkey.ratelimit.listOverrides({ nameSpaceId:"rlns_12345", namespaceName: "email.outbound" - }); - ``` +}); +``` diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx index 742d4ffa4b..14930db3eb 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx @@ -47,22 +47,14 @@ Async will return a response immediately, lowering latency at the cost of accura ```ts -await unkey.ratelimits.setOverride({ +await unkey.ratelimit.setOverride({ identifier: "user_123", limit: 10, duration: 60000, namespaceName: "email.outbound", namespaceId: "rlns_1234", async: true - }) -``` - - - - - -```ts -await unkey.ratelimits.setOverride("testId", 10, 60000, undefined ,"rlns_12345", true) +}) ``` 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..ef3e5915a5 --- /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 async 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; + } +} diff --git a/packages/ratelimit/src/ratelimit.ts b/packages/ratelimit/src/ratelimit.ts index 224843aaec..274bc1122b 100644 --- a/packages/ratelimit/src/ratelimit.ts +++ b/packages/ratelimit/src/ratelimit.ts @@ -212,49 +212,4 @@ export class Ratelimit implements Ratelimiter { } } } - - public async setOverride( - identifier: string, - limit: number, - duration: number, - namespaceName?: string, - namespaceId?: string, - async?: boolean, - ) { - return this.unkey.ratelimits.setOverride({ - namespaceId, - namespaceName, - identifier, - limit, - duration, - async, - }); - } - public async getOverride(identifier: string, namespaceName?: string, namespaceId?: string) { - return this.unkey.ratelimits.getOverride({ - namespaceName, - namespaceId, - identifier, - }); - } - public async listOverrides( - namespaceName?: string, - namespaceId?: string, - limit?: number, - cursor?: string, - ) { - return this.unkey.ratelimits.listOverrides({ - namespaceName, - namespaceId, - limit, - cursor, - }); - } - public async deleteOverride(identifier: string, namespaceName?: string, namespaceId?: string) { - return this.unkey.ratelimits.deleteOverride({ - namespaceName, - namespaceId, - identifier, - }); - } } From 1dd6d0deb89be28ba097ebef11929e84feaa477b Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Wed, 11 Dec 2024 11:06:37 -0500 Subject: [PATCH 07/11] PR change --- packages/ratelimit/src/overrides.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ratelimit/src/overrides.ts b/packages/ratelimit/src/overrides.ts index ef3e5915a5..d031a3809c 100644 --- a/packages/ratelimit/src/overrides.ts +++ b/packages/ratelimit/src/overrides.ts @@ -36,7 +36,7 @@ export class Overrides { }); } - public async getOverride() { + public get getOverride() { return this.unkey.ratelimits.getOverride; } public get setOverride() { From 2c82e561138cd428eaddf6cc0dadfb7928299291 Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Thu, 12 Dec 2024 12:56:16 -0500 Subject: [PATCH 08/11] pr changes --- .../sdk/ratelimits/overrides/delete-override.mdx | 11 ++++++++--- .../ts/sdk/ratelimits/overrides/get-override.mdx | 15 ++++++++++----- .../sdk/ratelimits/overrides/list-overrides.mdx | 14 +++++++++----- .../ts/sdk/ratelimits/overrides/set-override.mdx | 16 ++++++++++++---- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx index cebed241ac..cd91126f3d 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx @@ -12,11 +12,11 @@ Identifier of your user, this can be their userId, an email, an ip or anything e 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 @@ -29,9 +29,14 @@ No response, but if no error is returned the override has been deleted successfu ```ts -await unkey.ratelimit.deleteOverride({ +const override = await unkey.ratelimit.deleteOverride({ identifier: "user_123", namespaceName: "email.outbound", +}) +``` +```ts +const override = 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 index 109877f4ee..fb093a9a16 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/get-override.mdx @@ -12,11 +12,11 @@ Identifier of your user, this can be their userId, an email, an ip or anything e 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 @@ -42,7 +42,7 @@ 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. @@ -51,12 +51,17 @@ Async will return a response immediately, lowering latency at the cost of accura ```ts -await unkey.ratelimit.getOverride({ +const override = await unkey.ratelimit.getOverride({ identifier:"user.example", - namespaceName: "email.outbound" nameSpaceId:"rlns_12345", }); ``` +```ts +const override = await unkey.ratelimit.getOverride({ + identifier:"user.example", + namespaceName: "email.outbound" +}); +``` diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx index 1aeac23bfa..3cc514751f 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx @@ -9,11 +9,11 @@ description: "Lists all overrides" 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 @@ -41,7 +41,7 @@ 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. @@ -51,7 +51,7 @@ Async will return a response immediately, lowering latency at the cost of accura The total number of overrides - + The cursor to use for pagination @@ -62,8 +62,12 @@ The cursor to use for pagination ```ts -await unkey.ratelimit.listOverrides({ +override = await unkey.ratelimit.listOverrides({ nameSpaceId:"rlns_12345", +}); +``` +```ts +override = await unkey.ratelimit.listOverrides({ namespaceName: "email.outbound" }); ``` diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx index 14930db3eb..6ec23e7f1f 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx @@ -22,11 +22,11 @@ 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 @@ -47,15 +47,23 @@ Async will return a response immediately, lowering latency at the cost of accura ```ts -await unkey.ratelimit.setOverride({ +override = await unkey.ratelimit.setOverride({ identifier: "user_123", limit: 10, duration: 60000, - namespaceName: "email.outbound", namespaceId: "rlns_1234", async: true }) ``` +```ts +override = await unkey.ratelimit.setOverride({ + identifier: "user_123", + limit: 10, + duration: 60000, + namespaceName: "email.outbound", + async: true +}) +``` From e34d8b43e498b930cbf6b11c3012ed0206bc1b19 Mon Sep 17 00:00:00 2001 From: MichaelUnkey <148160799+MichaelUnkey@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:11:43 -0500 Subject: [PATCH 09/11] Update list-overrides.mdx --- .../ts/sdk/ratelimits/overrides/list-overrides.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx index 3cc514751f..df536ce402 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx @@ -62,12 +62,12 @@ The cursor to use for pagination ```ts -override = await unkey.ratelimit.listOverrides({ +const override = await unkey.ratelimit.listOverrides({ nameSpaceId:"rlns_12345", }); ``` ```ts -override = await unkey.ratelimit.listOverrides({ +const override = await unkey.ratelimit.listOverrides({ namespaceName: "email.outbound" }); ``` @@ -92,4 +92,4 @@ override = await unkey.ratelimit.listOverrides({ } ``` - \ No newline at end of file + From 3d022815aff13adadfa555bf16ba01f38e92d6d5 Mon Sep 17 00:00:00 2001 From: MichaelUnkey <148160799+MichaelUnkey@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:12:24 -0500 Subject: [PATCH 10/11] Update set-override.mdx --- .../libraries/ts/sdk/ratelimits/overrides/set-override.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx index 6ec23e7f1f..98aa88f5df 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/set-override.mdx @@ -47,7 +47,7 @@ Async will return a response immediately, lowering latency at the cost of accura ```ts -override = await unkey.ratelimit.setOverride({ +const override = await unkey.ratelimit.setOverride({ identifier: "user_123", limit: 10, duration: 60000, @@ -56,7 +56,7 @@ override = await unkey.ratelimit.setOverride({ }) ``` ```ts -override = await unkey.ratelimit.setOverride({ +const override = await unkey.ratelimit.setOverride({ identifier: "user_123", limit: 10, duration: 60000, From 89a2782e47737f03a1ab82d1b94c9dd8034e16e6 Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Thu, 12 Dec 2024 14:51:31 -0500 Subject: [PATCH 11/11] pr changes --- apps/docs/libraries/ts/sdk/overview.mdx | 4 ++-- .../libraries/ts/sdk/ratelimits/overrides/delete-override.mdx | 4 ++-- .../libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) 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 index cd91126f3d..2da5f5cd50 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/delete-override.mdx @@ -29,13 +29,13 @@ No response, but if no error is returned the override has been deleted successfu ```ts -const override = await unkey.ratelimit.deleteOverride({ +await unkey.ratelimit.deleteOverride({ identifier: "user_123", namespaceName: "email.outbound", }) ``` ```ts -const override = await unkey.ratelimit.deleteOverride({ +await unkey.ratelimit.deleteOverride({ identifier: "user_123", namespaceId: "rlns_1234", }) diff --git a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx index df536ce402..703397620a 100644 --- a/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx +++ b/apps/docs/libraries/ts/sdk/ratelimits/overrides/list-overrides.mdx @@ -62,12 +62,12 @@ The cursor to use for pagination ```ts -const override = await unkey.ratelimit.listOverrides({ +const overrides = await unkey.ratelimit.listOverrides({ nameSpaceId:"rlns_12345", }); ``` ```ts -const override = await unkey.ratelimit.listOverrides({ +const overrides = await unkey.ratelimit.listOverrides({ namespaceName: "email.outbound" }); ```