diff --git a/fixtures/input/example/0009.ts b/fixtures/input/example/0009.ts new file mode 100644 index 0000000..1c5e26a --- /dev/null +++ b/fixtures/input/example/0009.ts @@ -0,0 +1,77 @@ +import type { Config } from './types' +import { resolve } from 'node:path' +import process from 'node:process' +import { deepMerge } from './utils' + +/** + * Load Config + * + * @param {object} options - The configuration options. + * @param {string} options.name - The name of the configuration file. + * @param {string} [options.cwd] - The current working directory. + * @param {string} [options.endpoint] - The API endpoint to fetch config from in browser environments. + * @param {string} [options.headers] - The headers to send with the request in browser environments. + * @param {T} options.defaultConfig - The default configuration. + * @returns {Promise} The merged configuration. + * @example ```ts + * // Merges arrays if both configs are arrays, otherwise does object deep merge + * await loadConfig({ + * name: 'example', + * endpoint: '/api/my-custom-config/endpoint', + * defaultConfig: [{ foo: 'bar' }] + * }) + * ``` + */ +export async function loadConfig({ + name, + cwd, + defaultConfig, + endpoint, + headers = { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, +}: Config): Promise { + // If running in a server (Bun) environment, load the config from the file system + if (typeof window === 'undefined') { + // back 3 times to get out of node_modules into the root directory, assuming the config is in the root directory + const configPath = resolve(cwd || '../../../', `${name}.config`) + + try { + const importedConfig = await import(configPath) + const loadedConfig = importedConfig.default || importedConfig + return deepMerge(defaultConfig, loadedConfig) as T + } + // eslint-disable-next-line unused-imports/no-unused-vars + catch (error: any) { + return defaultConfig + } + } + + if (!endpoint) { + console.warn('An API endpoint is required to load the client config.') + return defaultConfig + } + + // If running in a browser environment, load the config from an API endpoint + try { + const response = await fetch(endpoint, { + method: 'GET', + headers, + }) + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + + const loadedConfig = await response.json() as T + return deepMerge(defaultConfig, loadedConfig) as T + } + catch (error) { + console.error('Failed to load client config:', error) + return defaultConfig + } +} + +export * from './types' +export * from './utils' diff --git a/fixtures/output/0009.d.ts b/fixtures/output/0009.d.ts new file mode 100644 index 0000000..644d6a8 --- /dev/null +++ b/fixtures/output/0009.d.ts @@ -0,0 +1,4 @@ +export declare function loadConfig({ name, cwd, defaultConfig, endpoint, headers): Promise; + +export * from './types' +export * from './utils' \ No newline at end of file