Skip to content

Commit b58c226

Browse files
feat: updated unstorage type to a custom type
1 parent e4831c3 commit b58c226

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"hono": "^4.10.8",
5151
"unstorage": "^1.17.3"
5252
},
53+
"peerDependenciesMeta": {
54+
"unstorage": {
55+
"optional": true
56+
}
57+
},
5358
"devDependencies": {
5459
"@biomejs/biome": "^2.3.8",
5560
"@hono/node-server": "^1.19.7",

src/stores/unstorage.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { Env, Input } from "hono/types";
2-
import type { Storage } from "unstorage";
32
import type { ClientRateLimitInfo, HonoConfigType, Store } from "../types.ts";
43

4+
export type UnstorageInstance = {
5+
get: (key: string) => Promise<any>;
6+
set: (key: string, value: any) => Promise<void>;
7+
remove: (key: string) => Promise<void>;
8+
};
9+
510
/**
611
* A `Store` that stores the hit count for each client using Unstorage
712
*
@@ -10,7 +15,7 @@ import type { ClientRateLimitInfo, HonoConfigType, Store } from "../types.ts";
1015
export class UnstorageStore<
1116
E extends Env = Env,
1217
P extends string = string,
13-
I extends Input = Input,
18+
I extends Input = Input
1419
> implements Store<E, P, I>
1520
{
1621
/**
@@ -26,14 +31,14 @@ export class UnstorageStore<
2631
/**
2732
* The unstorage storage instance.
2833
*/
29-
storage: Storage;
34+
storage: UnstorageInstance;
3035

3136
/**
3237
* @constructor for `UnstorageStore`.
3338
*
3439
* @param options {Options} - The configuration options for the store.
3540
*/
36-
constructor(options: { storage: Storage; prefix?: string }) {
41+
constructor(options: { storage: UnstorageInstance; prefix?: string }) {
3742
this.storage = options.storage;
3843
this.prefix = options.prefix ?? "hrl:";
3944
}
@@ -69,7 +74,7 @@ export class UnstorageStore<
6974
const result = await this.storage
7075
.get(this.prefixKey(key))
7176
.then((value) =>
72-
value ? (JSON.parse(String(value)) as ClientRateLimitInfo) : undefined,
77+
value ? (JSON.parse(String(value)) as ClientRateLimitInfo) : undefined
7378
);
7479

7580
return result;
@@ -152,7 +157,7 @@ export class UnstorageStore<
152157
*/
153158
private async updateRecord(
154159
key: string,
155-
payload: ClientRateLimitInfo,
160+
payload: ClientRateLimitInfo
156161
): Promise<void> {
157162
await this.storage.set(this.prefixKey(key), JSON.stringify(payload));
158163
}

0 commit comments

Comments
 (0)