diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000..380e9bd Binary files /dev/null and b/bun.lockb differ diff --git a/packages/redis-api-client/bun.lockb b/packages/redis-api-client/bun.lockb new file mode 100755 index 0000000..db314b7 Binary files /dev/null and b/packages/redis-api-client/bun.lockb differ diff --git a/packages/redis-api-client/package.json b/packages/redis-api-client/package.json index 9e65dab..8f2c11e 100644 --- a/packages/redis-api-client/package.json +++ b/packages/redis-api-client/package.json @@ -10,5 +10,8 @@ "devDependencies": { "esbuild": "^0.15.11", "typescript": "^4.8.4" + }, + "dependencies": { + "@upstash/redis": "^1.34.3" } } diff --git a/packages/redis-api-client/src/index.ts b/packages/redis-api-client/src/index.ts index 75d1b9f..47ed68d 100644 --- a/packages/redis-api-client/src/index.ts +++ b/packages/redis-api-client/src/index.ts @@ -1,72 +1,136 @@ -export class RedisAPIClient { - private apiKey: string; - private host: string; +interface RedisClient { + connect(): Promise | void; - constructor(apiKey: string, host: string) { - this.apiKey = apiKey; - this.host = host; - } - - public async get(key: string): Promise { - const url = `${this.host}/get?key=${key}`; - const response = await fetch(url, { - headers: { - Authorization: this.apiKey, - }, - }); - - const text = await response.text(); - return text === "null" ? null : text; - } - - public async set(key: string, value: string): Promise { - const url = `${this.host}/set`; - const response = await fetch(url, { - method: "POST", - headers: { - Authorization: this.apiKey, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - key, - value, - }), - }); - - return response.text(); - } - - public async setex( - key: string, - value: string, - seconds: number, - ): Promise { - const url = `${this.host}/setex`; - const response = await fetch(url, { - method: "POST", - headers: { - Authorization: this.apiKey, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - key, - value, - seconds, - }), - }); - - return response.text(); - } - - public async del(key: string): Promise { - const url = `${this.host}/del?key=${key}`; - const response = await fetch(url, { - method: "DELETE", - headers: { - Authorization: this.apiKey, - }, - }); - - return response.text(); - } + get(key: string): Promise; + set(key: string, value: string): Promise; + setex(key: string, value: string, seconds: number): Promise; + del(key: string): Promise; +} + +export class Upstash implements RedisClient { + private url: string; + private token: string; + + private redis: import("@upstash/redis").Redis | undefined; + + constructor(url: string, token: string) { + this.url = url; + this.token = token; + + this.redis = undefined; + } + + public async connect() { + if (this.redis) { + return; + } + + this.redis = new (await import("@upstash/redis")).Redis({ + url: this.url, + token: this.token, + }); + } + + public async get(key: string): Promise { + await this.connect(); + + return (await this.redis?.get(key)) ?? null; + } + + public async set(key: string, value: string): Promise { + await this.connect(); + + return (await this.redis?.set(key, value)) ?? "OK"; + } + + public async setex( + key: string, + value: string, + seconds: number + ): Promise { + await this.connect(); + + return (await this.redis?.setex(key, seconds, value)) ?? "OK"; + } + + public async del(key: string): Promise { + await this.connect(); + + return (await this.redis?.del(key)) ?? 0; + } +} + +export class Raw implements RedisClient { + private apiKey: string; + private host: string; + + constructor(apiKey: string, host: string) { + this.apiKey = apiKey; + this.host = host; + } + + public connect() {} + + public async get(key: string): Promise { + const url = `${this.host}/get?key=${key}`; + const response = await fetch(url, { + headers: { + Authorization: this.apiKey, + }, + }); + + const text = await response.text(); + return text === "null" ? null : text; + } + + public async set(key: string, value: string): Promise { + const url = `${this.host}/set`; + const response = await fetch(url, { + method: "POST", + headers: { + Authorization: this.apiKey, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + key, + value, + }), + }); + + return response.text(); + } + + public async setex( + key: string, + value: string, + seconds: number + ): Promise { + const url = `${this.host}/setex`; + const response = await fetch(url, { + method: "POST", + headers: { + Authorization: this.apiKey, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + key, + value, + seconds, + }), + }); + + return response.text(); + } + + public async del(key: string): Promise { + const url = `${this.host}/del?key=${key}`; + const response = await fetch(url, { + method: "DELETE", + headers: { + Authorization: this.apiKey, + }, + }); + + return parseInt(await response.text()); + } }