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": { "devDependencies": {
"esbuild": "^0.15.11", "esbuild": "^0.15.11",
"typescript": "^4.8.4" "typescript": "^4.8.4"
},
"dependencies": {
"@upstash/redis": "^1.34.3"
} }
} }

View file

@ -1,72 +1,136 @@
export class RedisAPIClient { interface RedisClient {
private apiKey: string; connect(): Promise<void> | void;
private host: string;
constructor(apiKey: string, host: string) { get(key: string): Promise<string | null>;
this.apiKey = apiKey; set(key: string, value: string): Promise<string>;
this.host = host; setex(key: string, value: string, seconds: number): Promise<string>;
} del(key: string): Promise<number>;
}
public async get(key: string): Promise<string | null> {
const url = `${this.host}/get?key=${key}`; export class Upstash implements RedisClient {
const response = await fetch(url, { private url: string;
headers: { private token: string;
Authorization: this.apiKey,
}, private redis: import("@upstash/redis").Redis | undefined;
});
constructor(url: string, token: string) {
const text = await response.text(); this.url = url;
return text === "null" ? null : text; this.token = token;
}
this.redis = undefined;
public async set(key: string, value: string): Promise<string> { }
const url = `${this.host}/set`;
const response = await fetch(url, { public async connect() {
method: "POST", if (this.redis) {
headers: { return;
Authorization: this.apiKey, }
"Content-Type": "application/json",
}, this.redis = new (await import("@upstash/redis")).Redis({
body: JSON.stringify({ url: this.url,
key, token: this.token,
value, });
}), }
});
public async get(key: string): Promise<string | null> {
return response.text(); await this.connect();
}
return (await this.redis?.get(key)) ?? null;
public async setex( }
key: string,
value: string, public async set(key: string, value: string): Promise<string> {
seconds: number, await this.connect();
): Promise<string> {
const url = `${this.host}/setex`; return (await this.redis?.set(key, value)) ?? "OK";
const response = await fetch(url, { }
method: "POST",
headers: { public async setex(
Authorization: this.apiKey, key: string,
"Content-Type": "application/json", value: string,
}, seconds: number
body: JSON.stringify({ ): Promise<string> {
key, await this.connect();
value,
seconds, return (await this.redis?.setex(key, seconds, value)) ?? "OK";
}), }
});
public async del(key: string): Promise<number> {
return response.text(); await this.connect();
}
return (await this.redis?.del(key)) ?? 0;
public async del(key: string): Promise<string> { }
const url = `${this.host}/del?key=${key}`; }
const response = await fetch(url, {
method: "DELETE", export class Raw implements RedisClient {
headers: { private apiKey: string;
Authorization: this.apiKey, private host: string;
},
}); constructor(apiKey: string, host: string) {
this.apiKey = apiKey;
return response.text(); 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());
}
} }