Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ratelimit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"typescript": "^5.5.3"
},
"dependencies": {
"@unkey/api": "^0.35.0"
"@unkey/api": "2.0.0-alpha.7"
}
}
12 changes: 6 additions & 6 deletions packages/ratelimit/src/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
}
}
14 changes: 9 additions & 5 deletions packages/ratelimit/src/ratelimit.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
});
}
Expand Down Expand Up @@ -168,7 +169,7 @@ export class Ratelimit implements Ratelimiter {
let timeoutId: any = null;
try {
const ps: Promise<RatelimitResponse>[] = [
this.unkey.ratelimits
this.unkey.ratelimit
.limit({
namespace: this.config.namespace,
identifier,
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/ratelimit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/ratelimit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
40 changes: 27 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading