anurag008w commited on
Commit
f419fae
·
1 Parent(s): 470bb3f

Update auth header injection in request options

Browse files

Refactor authorization header handling for HTTP requests.

Files changed (1) hide show
  1. multi-provider-key-rotator.cjs +5 -1
multi-provider-key-rotator.cjs CHANGED
@@ -334,13 +334,17 @@ function patchHttpModule(mod) {
334
  ? { ...options, path: `${u.pathname}${u.search}` }
335
  : u.toString();
336
  } else if (typeof options === 'string' || options instanceof URL) {
 
 
337
  const u = new URL(String(options));
 
338
  args[0] = {
339
  protocol: u.protocol,
340
  hostname: u.hostname,
341
  port: u.port,
342
  path: `${u.pathname}${u.search}`,
343
- headers: { authorization: `Bearer ${key}` },
 
344
  };
345
  } else if (options && typeof options === 'object') {
346
  args[0] = { ...options, headers: setAuthHeader(options.headers, key) };
 
334
  ? { ...options, path: `${u.pathname}${u.search}` }
335
  : u.toString();
336
  } else if (typeof options === 'string' || options instanceof URL) {
337
+ // Convert string/URL to options object and inject auth header.
338
+ // Also preserve any extra options passed as args[1] (3-arg form of http.request).
339
  const u = new URL(String(options));
340
+ const extraOpts = (args[1] && typeof args[1] === 'object' && typeof args[1].on !== 'function') ? args[1] : {};
341
  args[0] = {
342
  protocol: u.protocol,
343
  hostname: u.hostname,
344
  port: u.port,
345
  path: `${u.pathname}${u.search}`,
346
+ ...extraOpts,
347
+ headers: setAuthHeader(extraOpts.headers, key),
348
  };
349
  } else if (options && typeof options === 'object') {
350
  args[0] = { ...options, headers: setAuthHeader(options.headers, key) };