opencode-google-auth 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-google-auth",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "_description_",
5
5
  "keywords": [
6
6
  "opencode-google-auth"
package/src/main.ts CHANGED
@@ -19,6 +19,7 @@ const fetchModelsDev = Effect.gen(function* () {
19
19
 
20
20
  const customFetch = Effect.fn(function* (input: FetchInput, init: FetchInit) {
21
21
  const config = yield* ProviderConfig
22
+ const session = yield* Session
22
23
 
23
24
  let lastResponse: Response | null = null
24
25
 
@@ -27,12 +28,35 @@ const customFetch = Effect.fn(function* (input: FetchInput, init: FetchInit) {
27
28
  yield* Effect.log("Input", input)
28
29
  yield* Effect.log("Init", init)
29
30
 
30
- const [finalInput, finalInit] = config.requestTransform?.(input, init) ?? [
31
+ let finalInitObj: FetchInit
32
+ const [finalInput, finalInit0] = config.requestTransform?.(input, init) ?? [
31
33
  input,
32
34
  init,
33
35
  ]
36
+ if (finalInit0 !== undefined) {
37
+ finalInitObj = finalInit0
38
+ } else if (init !== undefined) {
39
+ finalInitObj = init
40
+ } else {
41
+ finalInitObj = {}
42
+ }
43
+
44
+ const accessToken = yield* session.getAccessToken
45
+
46
+ const authHeaders: Record<string, string> = {}
47
+ if (finalInitObj.headers) {
48
+ for (const [key, value] of Object.entries(finalInitObj.headers)) {
49
+ authHeaders[key] = value as string
50
+ }
51
+ }
52
+ authHeaders.Authorization = `Bearer ${accessToken}`
53
+
54
+ const authInit: FetchInit = {
55
+ ...finalInitObj,
56
+ headers: authHeaders,
57
+ }
34
58
 
35
- const response = yield* Effect.promise(() => fetch(finalInput, finalInit))
59
+ const response = yield* Effect.promise(() => fetch(finalInput, authInit))
36
60
 
37
61
  // On 429 or 403, try next endpoint
38
62
  if (response.status === 429 || response.status === 403) {
@@ -207,6 +207,7 @@ export class Session extends Effect.Service<Session>()("Session", {
207
207
  Ref.set(credentialsRef, credentials),
208
208
  getAccessToken,
209
209
  ensureProject,
210
+ refreshTokens,
210
211
  }
211
212
  }),
212
213
  }) {}