Skip to content

Commit

Permalink
feat(cf): added cloudflare types
Browse files Browse the repository at this point in the history
  • Loading branch information
MathurAditya724 committed Aug 31, 2024
1 parent dc17f16 commit 9fea4d7
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@cloudflare/workers-types": "^4.20240821.1",
"@hono/node-ws": "^1.0.1",
"@nx/devkit": "19.3.1",
"@nx/js": "19.3.1",
Expand Down
7 changes: 0 additions & 7 deletions packages/cloudflare/src/stores/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { Env, Input } from "hono/types";
import type { Options } from "../types";

export class WorkersKVStore<
KVNamespace,
E extends Env = Env,
P extends string = string,
I extends Input = Input,
Expand Down Expand Up @@ -66,7 +65,6 @@ export class WorkersKVStore<
* @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
*/
async get(key: string): Promise<ClientRateLimitInfo | undefined> {
// @ts-expect-error
const result = await this.namespace.get<ClientRateLimitInfo>(
this.prefixKey(key),
"json",
Expand All @@ -93,7 +91,6 @@ export class WorkersKVStore<
};

const record: Required<ClientRateLimitInfo> | null =
// @ts-expect-error
await this.namespace.get<Required<ClientRateLimitInfo>>(
keyWithPrefix,
"json",
Expand All @@ -109,7 +106,6 @@ export class WorkersKVStore<
: {}),
};

// @ts-expect-error
await this.namespace.put(keyWithPrefix, JSON.stringify(payload), {
expiration: payload.resetTime.getTime() / 1000,
});
Expand All @@ -125,7 +121,6 @@ export class WorkersKVStore<
async decrement(key: string): Promise<void> {
const keyWithPrefix = this.prefixKey(key);

// @ts-expect-error
const payload = await this.namespace.get<Required<ClientRateLimitInfo>>(
keyWithPrefix,
"json",
Expand All @@ -135,7 +130,6 @@ export class WorkersKVStore<

payload.totalHits -= 1;

// @ts-expect-error
await this.namespace.put(keyWithPrefix, JSON.stringify(payload), {
expiration: Math.floor(payload.resetTime.getTime() / 1000),
});
Expand All @@ -147,7 +141,6 @@ export class WorkersKVStore<
* @param key {string} - The identifier for a client
*/
async resetKey(key: string): Promise<void> {
// @ts-expect-error
await this.namespace.delete(this.prefixKey(key));
}
}
16 changes: 3 additions & 13 deletions packages/cloudflare/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ export type RateLimitExceededEventHandler<
I extends Input = Input,
> = (c: Context<E, P, I>, next: Next, optionsUsed: ConfigType<E, P, I>) => void;

export type RateLimitBinding = {
limit: (options: { key: string }) => Promise<{ success: boolean }>;
};

export type RateLimitBindingProp<
E extends Env = Env,
P extends string = string,
I extends Input = Input,
> = RateLimitBinding | ((c: Context<E, P, I>) => RateLimitBinding);

/**
* The configuration options for the rate limiter.
*/
Expand Down Expand Up @@ -61,7 +51,7 @@ export interface ConfigType<
/**
* Method to generate custom identifiers for clients.
*/
rateLimitBinding: RateLimitBindingProp<E, P, I>;
rateLimitBinding: RateLimit | ((c: Context<E, P, I>) => RateLimit);

/**
* Method to generate custom identifiers for clients.
Expand All @@ -88,11 +78,11 @@ export interface ConfigType<
/**
* The configuration options for the store.
*/
export type Options<KVNamespace> = {
export type Options<Binding> = {
/**
* The KV namespace to use.
*/
namespace: KVNamespace;
namespace: Binding;

/**
* The text to prepend to the key in Redis.
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
"types": ["node", "@cloudflare/workers-types/2023-07-01"]
},
"include": ["src/**/*.ts"],
"exclude": [
Expand Down
3 changes: 2 additions & 1 deletion packages/cloudflare/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"vitest/importMeta",
"vite/client",
"node",
"vitest"
"vitest",
"@cloudflare/workers-types/2023-07-01"
]
},
"include": [
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 9fea4d7

Please sign in to comment.