feat: Centralize configuration management for Nostr and API settings
- Introduce a new config module to manage Nostr relays, admin pubkeys, and API settings. - Update components to utilize the centralized config instead of environment variables directly. - Refactor relevant files to improve maintainability and reduce reliance on environment variables.
This commit is contained in:
parent
17a1504771
commit
0324cf8ec5
6 changed files with 83 additions and 13 deletions
69
src/lib/config/index.ts
Normal file
69
src/lib/config/index.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
// Centralized configuration management
|
||||
// Handles all environment variables and app configuration
|
||||
|
||||
interface NostrConfig {
|
||||
relays: string[]
|
||||
adminPubkeys: string[]
|
||||
}
|
||||
|
||||
interface ApiConfig {
|
||||
baseUrl: string
|
||||
key: string
|
||||
}
|
||||
|
||||
interface AppConfig {
|
||||
nostr: NostrConfig
|
||||
api: ApiConfig
|
||||
support: {
|
||||
npub: string
|
||||
}
|
||||
}
|
||||
|
||||
// Parse JSON environment variables safely
|
||||
function parseJsonEnv(envVar: string | undefined, fallback: any = []): any {
|
||||
if (!envVar) return fallback
|
||||
|
||||
try {
|
||||
return JSON.parse(envVar)
|
||||
} catch (error) {
|
||||
console.warn(`Failed to parse environment variable: ${envVar}`)
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
// Create the configuration object
|
||||
export const config: AppConfig = {
|
||||
nostr: {
|
||||
relays: parseJsonEnv(import.meta.env.VITE_NOSTR_RELAYS, []),
|
||||
adminPubkeys: parseJsonEnv(import.meta.env.VITE_ADMIN_PUBKEYS, [])
|
||||
},
|
||||
api: {
|
||||
baseUrl: import.meta.env.VITE_API_BASE_URL || '',
|
||||
key: import.meta.env.VITE_API_KEY || ''
|
||||
},
|
||||
support: {
|
||||
npub: import.meta.env.VITE_SUPPORT_NPUB || ''
|
||||
}
|
||||
} as const
|
||||
|
||||
// Utility functions for common config operations
|
||||
export const configUtils = {
|
||||
isAdminPubkey: (pubkey: string): boolean => {
|
||||
return config.nostr.adminPubkeys.includes(pubkey)
|
||||
},
|
||||
|
||||
hasNostrConfig: (): boolean => {
|
||||
return config.nostr.relays.length > 0
|
||||
},
|
||||
|
||||
hasApiConfig: (): boolean => {
|
||||
return Boolean(config.api.baseUrl && config.api.key)
|
||||
},
|
||||
|
||||
getDefaultRelays: (): string[] => {
|
||||
return config.nostr.relays
|
||||
}
|
||||
}
|
||||
|
||||
// Export individual config sections for convenience
|
||||
export const { nostr: nostrConfig, api: apiConfig } = config
|
||||
Loading…
Add table
Add a link
Reference in a new issue