feat(redis api client): improve

This commit is contained in:
Jozef Steinhübl 2024-12-14 21:51:14 +01:00
parent 6678ded2f9
commit 3cd6065ab8
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
4 changed files with 137 additions and 70 deletions

BIN
bun.lockb Executable file

Binary file not shown.

Binary file not shown.

View file

@ -10,5 +10,8 @@
"devDependencies": {
"esbuild": "^0.15.11",
"typescript": "^4.8.4"
},
"dependencies": {
"@upstash/redis": "^1.34.3"
}
}

View file

@ -1,72 +1,136 @@
export class RedisAPIClient {
private apiKey: string;
private host: string;
interface RedisClient {
connect(): Promise<void> | void;
constructor(apiKey: string, host: string) {
this.apiKey = apiKey;
this.host = host;
}
public async get(key: string): Promise<string | null> {
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<string> {
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<string> {
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<string> {
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<string | null>;
set(key: string, value: string): Promise<string>;
setex(key: string, value: string, seconds: number): Promise<string>;
del(key: string): Promise<number>;
}
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<string | null> {
await this.connect();
return (await this.redis?.get(key)) ?? null;
}
public async set(key: string, value: string): Promise<string> {
await this.connect();
return (await this.redis?.set(key, value)) ?? "OK";
}
public async setex(
key: string,
value: string,
seconds: number
): Promise<string> {
await this.connect();
return (await this.redis?.setex(key, seconds, value)) ?? "OK";
}
public async del(key: string): Promise<number> {
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<string | null> {
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<string> {
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<string> {
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<number> {
const url = `${this.host}/del?key=${key}`;
const response = await fetch(url, {
method: "DELETE",
headers: {
Authorization: this.apiKey,
},
});
return parseInt(await response.text());
}
}