Skip to content

Commit

Permalink
chore: minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Jan 8, 2025
1 parent 6cf3bfc commit a20b390
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ import { deepMerge } from './utils'
export async function loadConfig<T>({
name,
cwd,
endpoint = '/api/config',
defaultConfig,
endpoint,
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
defaultConfig,
}: Config<T>): Promise<T> {
// If running in a server (Bun) environment, load the config from the file system
if (typeof window === 'undefined') {
const configPath = resolve(cwd || process.cwd(), `${name}.config`)

try {
const importedConfig = await import(configPath)
const importedConfig = await import(/* @vite-ignore */configPath)
const loadedConfig = importedConfig.default || importedConfig
return deepMerge(defaultConfig, loadedConfig) as T
}
Expand All @@ -47,6 +47,11 @@ export async function loadConfig<T>({
}
}

if (!endpoint) {
console.warn('An endpoint is required to fetch the client config. Using the default config.')
return defaultConfig
}

// If running in a browser environment, load the config from an API endpoint
try {
const response = await fetch(endpoint, {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/
export interface Config<T> {
name: string
defaultConfig: T
cwd?: string
endpoint?: string
headers?: Record<string, string>
defaultConfig: T
}

export type SimplifyDeep<T> = T extends object
Expand Down

0 comments on commit a20b390

Please sign in to comment.