anurag008w commited on
Commit
b81080d
·
unverified ·
1 Parent(s): e83a418

Enhance Gemini API key handling with query param support

Browse files

Added support for query parameter in Gemini API key handling.

Files changed (1) hide show
  1. multi-provider-key-rotator.cjs +23 -8
multi-provider-key-rotator.cjs CHANGED
@@ -53,6 +53,7 @@ const PROVIDERS = [
53
  hostname: /(?:^|\.)(?:generativelanguage\.googleapis\.com|aiplatform\.googleapis\.com)$/i,
54
  envPlural: 'GEMINI_API_KEYS',
55
  envSingular:'GEMINI_API_KEY',
 
56
  },
57
  {
58
  name: 'deepseek',
@@ -285,15 +286,22 @@ function patchFetch() {
285
  const hostname = resolveHostname(urlLike);
286
  const provider = matchProvider(hostname);
287
 
288
- if (provider) {
289
  const key = nextKey(provider);
290
  if (key) {
291
- const headers = init.headers || (input && input.headers) || undefined;
292
- const patchedHeaders = setAuthHeader(headers, key);
293
- init = { ...init, headers: patchedHeaders };
294
-
295
- if (input && typeof input === 'object' && !(input instanceof URL) && input.headers) {
296
- try { input = new Request(input, { headers: patchedHeaders }); } catch { /* noop */ }
 
 
 
 
 
 
 
297
  }
298
  }
299
  }
@@ -319,7 +327,14 @@ function patchHttpModule(mod) {
319
  if (provider) {
320
  const key = nextKey(provider);
321
  if (key) {
322
- if (typeof options === 'string' || options instanceof URL) {
 
 
 
 
 
 
 
323
  const u = new URL(String(options));
324
  args[0] = {
325
  protocol: u.protocol,
 
53
  hostname: /(?:^|\.)(?:generativelanguage\.googleapis\.com|aiplatform\.googleapis\.com)$/i,
54
  envPlural: 'GEMINI_API_KEYS',
55
  envSingular:'GEMINI_API_KEY',
56
+ queryParam: true,
57
  },
58
  {
59
  name: 'deepseek',
 
286
  const hostname = resolveHostname(urlLike);
287
  const provider = matchProvider(hostname);
288
 
289
+ if (provider) {
290
  const key = nextKey(provider);
291
  if (key) {
292
+ if (provider.queryParam) {
293
+ // Gemini: key URL query param mein jaata hai, Bearer nahi
294
+ const url = new URL(typeof input === 'string' ? input : input.url);
295
+ url.searchParams.set('key', key);
296
+ input = typeof input === 'string' ? url.toString() : new Request(url.toString(), input);
297
+ } else {
298
+ const headers = init.headers || (input && input.headers) || undefined;
299
+ const patchedHeaders = setAuthHeader(headers, key);
300
+ init = { ...init, headers: patchedHeaders };
301
+
302
+ if (input && typeof input === 'object' && !(input instanceof URL) && input.headers) {
303
+ try { input = new Request(input, { headers: patchedHeaders }); } catch { /* noop */ }
304
+ }
305
  }
306
  }
307
  }
 
327
  if (provider) {
328
  const key = nextKey(provider);
329
  if (key) {
330
+ if (provider.queryParam) {
331
+ // Gemini: ?key= query param use karo
332
+ const u = new URL(String(typeof options === 'string' || options instanceof URL ? options : `https://${options.hostname}${options.path || '/'}`));
333
+ u.searchParams.set('key', key);
334
+ args[0] = typeof options === 'object' && !(options instanceof URL)
335
+ ? { ...options, path: `${u.pathname}${u.search}` }
336
+ : u.toString();
337
+ } else if (typeof options === 'string' || options instanceof URL) {
338
  const u = new URL(String(options));
339
  args[0] = {
340
  protocol: u.protocol,