@saws/redis-client 2.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +18 -0
- package/src/RedisClient.ts +54 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +8 -0
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@saws/redis-client",
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"ioredis": "^5.4.1"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Redis as IORedis, type RedisOptions } from "ioredis";
|
|
2
|
+
|
|
3
|
+
export interface RedisClientOptions extends RedisOptions {
|
|
4
|
+
/** Explicit environment source for application adapters. */
|
|
5
|
+
environment?: Record<string, string | undefined>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class RedisClient extends IORedis {
|
|
9
|
+
constructor(serviceName: string, options: RedisClientOptions = {}) {
|
|
10
|
+
const { environment, ...redisOptions } = options;
|
|
11
|
+
|
|
12
|
+
super(resolveRedisServiceUrl(serviceName, environment), redisOptions);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function resolveRedisServiceUrl(
|
|
17
|
+
serviceName: string,
|
|
18
|
+
environment?: Record<string, string | undefined>,
|
|
19
|
+
) {
|
|
20
|
+
const variableName = redisServiceUrlEnvironmentVariable(serviceName);
|
|
21
|
+
const value =
|
|
22
|
+
environment?.[variableName] ??
|
|
23
|
+
getRuntimeEnvironment()?.[variableName] ??
|
|
24
|
+
getProcessEnvironment()?.[variableName];
|
|
25
|
+
|
|
26
|
+
if (value == null || value.trim().length === 0) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Redis service "${serviceName}" is not configured: ` +
|
|
29
|
+
`${variableName} must be present in the SAWS environment`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function redisServiceUrlEnvironmentVariable(serviceName: string) {
|
|
37
|
+
return `${serviceName.replace(/[^a-zA-Z\d]/g, "_").toUpperCase()}_REDIS_URL`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getRuntimeEnvironment() {
|
|
41
|
+
return (
|
|
42
|
+
globalThis as typeof globalThis & {
|
|
43
|
+
ENV?: Record<string, string | undefined>;
|
|
44
|
+
}
|
|
45
|
+
).ENV;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getProcessEnvironment() {
|
|
49
|
+
return (
|
|
50
|
+
globalThis as typeof globalThis & {
|
|
51
|
+
process?: { env?: Record<string, string | undefined> };
|
|
52
|
+
}
|
|
53
|
+
).process?.env;
|
|
54
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./RedisClient.js";
|