(opts: CommonOptions, deps: ClientFactoryDeps = {})
| 140 | } |
| 141 | |
| 142 | export function makeHttpClient(opts: CommonOptions, deps: ClientFactoryDeps = {}): HttpClient { |
| 143 | const stderr = deps.stderr ?? ((line: string) => process.stderr.write(`${line}\n`)); |
| 144 | const env = deps.env ?? process.env; |
| 145 | const requestTimeoutMs = resolveRequestTimeoutMs(opts, env); |
| 146 | |
| 147 | if (opts.dryRun) { |
| 148 | emitDryRunBanner(stderr); |
| 149 | return new HttpClient({ |
| 150 | baseUrl: facadeBaseUrl(opts.endpointUrl ?? DRY_RUN_DEFAULT_ENDPOINT), |
| 151 | apiKey: DRY_RUN_API_KEY, |
| 152 | fetchImpl: deps.fetchImpl ?? createDryRunFetch(), |
| 153 | onDebug: opts.debug ? (event: DebugEvent) => stderr(formatDryRunDebug(event)) : undefined, |
| 154 | onTransition: opts.verbose ? (msg: string) => stderr(`[verbose] ${msg}`) : undefined, |
| 155 | requestTimeoutMs, |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | const credentialsPath = deps.credentialsPath ?? defaultCredentialsPath(); |
| 160 | const config = loadConfig({ |
| 161 | profile: opts.profile, |
| 162 | endpointUrl: opts.endpointUrl, |
| 163 | env, |
| 164 | credentialsPath, |
| 165 | }); |
| 166 | if (!config.apiKey) throw ApiError.authRequired(); |
| 167 | return new HttpClient({ |
| 168 | baseUrl: facadeBaseUrl(config.apiUrl), |
| 169 | apiKey: config.apiKey, |
| 170 | fetchImpl: deps.fetchImpl, |
| 171 | onDebug: opts.debug ? (event: DebugEvent) => stderr(formatDebug(event)) : undefined, |
| 172 | onTransition: opts.verbose ? (msg: string) => stderr(`[verbose] ${msg}`) : undefined, |
| 173 | requestTimeoutMs, |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | function formatDebug(event: DebugEvent): string { |
| 178 | return `[debug ${new Date().toISOString()}] ${JSON.stringify(event)}`; |
no test coverage detected