@neutrome/open-ai-router 0.1.2 → 0.1.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 +1 -1
- package/src/auth/env.ts +28 -3
package/package.json
CHANGED
package/src/auth/env.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { AuthDriver, TargetAuthContext, AuthResult } from "./types.ts";
|
|
2
2
|
|
|
3
3
|
export function envAuthDriver(suffix = "_API_KEY", prefix = ""): AuthDriver {
|
|
4
|
+
const counters = new Map<string, number>();
|
|
5
|
+
|
|
4
6
|
return {
|
|
5
7
|
name: "env",
|
|
6
8
|
collectTarget(ctx: TargetAuthContext): AuthResult | null {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
if (
|
|
9
|
+
const base = `${prefix}${ctx.providerName.toUpperCase()}`;
|
|
10
|
+
const key = resolveKey(ctx.env, base, suffix, counters);
|
|
11
|
+
if (!key) return null;
|
|
10
12
|
return {
|
|
11
13
|
headers: { authorization: `Bearer ${key}` },
|
|
12
14
|
source: "env",
|
|
@@ -14,3 +16,26 @@ export function envAuthDriver(suffix = "_API_KEY", prefix = ""): AuthDriver {
|
|
|
14
16
|
},
|
|
15
17
|
};
|
|
16
18
|
}
|
|
19
|
+
|
|
20
|
+
function resolveKey(
|
|
21
|
+
env: Record<string, unknown>,
|
|
22
|
+
base: string,
|
|
23
|
+
suffix: string,
|
|
24
|
+
counters: Map<string, number>,
|
|
25
|
+
): string | null {
|
|
26
|
+
const nValue = env[`${base}${suffix}S_N`];
|
|
27
|
+
if (nValue) {
|
|
28
|
+
const n = typeof nValue === "string" ? parseInt(nValue, 10) : typeof nValue === "number" ? nValue : 0;
|
|
29
|
+
if (n > 0) {
|
|
30
|
+
const prev = counters.get(base) ?? 0;
|
|
31
|
+
const index = (prev % n) + 1;
|
|
32
|
+
counters.set(base, prev + 1);
|
|
33
|
+
const key = env[`${base}${suffix}_${index}`];
|
|
34
|
+
if (typeof key === "string" && key) return key;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const single = env[`${base}${suffix}`];
|
|
39
|
+
if (typeof single === "string" && single) return single;
|
|
40
|
+
return null;
|
|
41
|
+
}
|