| 32 | * `CMDOP_API_KEY` independently. |
| 33 | */ |
| 34 | export function resolveConfig(opts: Partial<ClientConfig> = {}): ClientConfig { |
| 35 | const env = typeof process !== "undefined" && process.env ? process.env : {}; |
| 36 | |
| 37 | const token = opts.token ?? env.CMDOP_TOKEN ?? env.CMDOP_API_KEY; |
| 38 | if (!token) { |
| 39 | throw new Error( |
| 40 | "No token. Pass token or set CMDOP_TOKEN (or CMDOP_API_KEY).", |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | const baseUrl = stripTrailingSlash( |
| 45 | opts.baseUrl ?? env.CMDOP_BASE_URL ?? DEFAULT_BASE_URL, |
| 46 | ); |
| 47 | |
| 48 | const fleetId = opts.fleetId ?? env.CMDOP_FLEET_ID ?? undefined; |
| 49 | |
| 50 | const timeoutMs = |
| 51 | opts.timeoutMs ?? |
| 52 | (env.CMDOP_TIMEOUT_MS ? Number(env.CMDOP_TIMEOUT_MS) : DEFAULT_TIMEOUT_MS); |
| 53 | |
| 54 | const apiKey = opts.apiKey ?? env.CMDOP_API_KEY ?? undefined; |
| 55 | |
| 56 | const apiBaseUrl = stripTrailingSlash( |
| 57 | opts.apiBaseUrl ?? env.CMDOP_API_BASE_URL ?? DEFAULT_API_BASE_URL, |
| 58 | ); |
| 59 | |
| 60 | return { token, baseUrl, fleetId, timeoutMs, apiKey, apiBaseUrl }; |
| 61 | } |
| 62 | |
| 63 | function stripTrailingSlash(url: string): string { |
| 64 | return url.endsWith("/") ? url.slice(0, -1) : url; |