Skip to content

Commit ccfa020

Browse files
committed
Refactor environment variable parsing in config.ts
- Introduced helper functions for parsing comma-separated values for chat IDs, user IDs, and strings. - Replaced inline parsing logic with these helper functions for better readability and maintainability.
1 parent 92fd663 commit ccfa020

File tree

1 file changed

+44
-63
lines changed

1 file changed

+44
-63
lines changed

src/config.ts

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,46 @@ import { MessageType, TemplateType } from "./sampling/types.js";
44

55
config();
66

7+
// Helper functions for parsing environment variables
8+
function parseCommaSeparatedChatIds(val: string): (number | string)[] {
9+
if (!val) return [];
10+
11+
return val
12+
.split(",")
13+
.map((id) => id.trim())
14+
.filter((id) => id.length > 0)
15+
.map((id) => {
16+
// Handle numeric IDs
17+
const numId = Number.parseInt(id);
18+
if (!Number.isNaN(numId)) return numId;
19+
// Handle username strings (normalize to include @)
20+
return id.startsWith("@") ? id : `@${id}`;
21+
});
22+
}
23+
24+
function parseCommaSeparatedUserIds(val: string): number[] {
25+
if (!val) return [];
26+
27+
return val
28+
.split(",")
29+
.map((id) => Number.parseInt(id.trim()))
30+
.filter((id) => !Number.isNaN(id));
31+
}
32+
33+
function parseCommaSeparatedStrings(val: string): string[] {
34+
if (!val) return [];
35+
36+
return val
37+
.split(",")
38+
.map((item) => item.trim())
39+
.filter((item) => item.length > 0);
40+
}
41+
742
const envSchema = z.object({
843
// Required
9-
TELEGRAM_BOT_TOKEN: z.string().min(1, "TELEGRAM_BOT_TOKEN environment variable must be set"),
44+
TELEGRAM_BOT_TOKEN: z
45+
.string()
46+
.min(1, "TELEGRAM_BOT_TOKEN environment variable must be set"),
1047

1148
// Sampling control
1249
SAMPLING_ENABLED: z.coerce.boolean().default(true),
@@ -19,72 +56,23 @@ const envSchema = z.object({
1956
SAMPLING_ALLOWED_CHATS: z
2057
.string()
2158
.default("")
22-
.transform((val) =>
23-
val
24-
? val
25-
.split(",")
26-
.map((id) => id.trim())
27-
.filter((id) => id.length > 0)
28-
.map((id) => {
29-
// Handle numeric IDs
30-
const numId = Number.parseInt(id);
31-
if (!Number.isNaN(numId)) return numId;
32-
// Handle username strings (normalize to include @)
33-
return id.startsWith("@") ? id : `@${id}`;
34-
})
35-
: [],
36-
),
59+
.transform(parseCommaSeparatedChatIds),
3760
SAMPLING_BLOCKED_CHATS: z
3861
.string()
3962
.default("")
40-
.transform((val) =>
41-
val
42-
? val
43-
.split(",")
44-
.map((id) => id.trim())
45-
.filter((id) => id.length > 0)
46-
.map((id) => {
47-
// Handle numeric IDs
48-
const numId = Number.parseInt(id);
49-
if (!Number.isNaN(numId)) return numId;
50-
// Handle username strings (normalize to include @)
51-
return id.startsWith("@") ? id : `@${id}`;
52-
})
53-
: [],
54-
),
63+
.transform(parseCommaSeparatedChatIds),
5564
SAMPLING_ALLOWED_USERS: z
5665
.string()
5766
.default("")
58-
.transform((val) =>
59-
val
60-
? val
61-
.split(",")
62-
.map((id) => Number.parseInt(id.trim()))
63-
.filter((id) => !Number.isNaN(id))
64-
: [],
65-
),
67+
.transform(parseCommaSeparatedUserIds),
6668
SAMPLING_BLOCKED_USERS: z
6769
.string()
6870
.default("")
69-
.transform((val) =>
70-
val
71-
? val
72-
.split(",")
73-
.map((id) => Number.parseInt(id.trim()))
74-
.filter((id) => !Number.isNaN(id))
75-
: [],
76-
),
71+
.transform(parseCommaSeparatedUserIds),
7772
SAMPLING_ADMIN_USERS: z
7873
.string()
7974
.default("")
80-
.transform((val) =>
81-
val
82-
? val
83-
.split(",")
84-
.map((id) => Number.parseInt(id.trim()))
85-
.filter((id) => !Number.isNaN(id))
86-
: [],
87-
),
75+
.transform(parseCommaSeparatedUserIds),
8876

8977
// Message type handlers
9078
SAMPLING_ENABLE_TEXT: z.coerce.boolean().default(true),
@@ -112,14 +100,7 @@ const envSchema = z.object({
112100
SAMPLING_KEYWORD_TRIGGERS: z
113101
.string()
114102
.default("")
115-
.transform((val) =>
116-
val
117-
? val
118-
.split(",")
119-
.map((keyword) => keyword.trim())
120-
.filter((keyword) => keyword.length > 0)
121-
: [],
122-
),
103+
.transform(parseCommaSeparatedStrings),
123104
SAMPLING_IGNORE_COMMANDS: z.coerce.boolean().default(true),
124105
});
125106

0 commit comments

Comments
 (0)