• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / supabase-js / 11540641030

27 Oct 2024 12:52PM UTC coverage: 64.684% (-1.4%) from 66.038%
11540641030

Pull #1293

github

web-flow
Merge 6193a967a into ce1e2f072
Pull Request #1293: feat: do not send non-JWTs in `Authorization` header

54 of 110 branches covered (49.09%)

Branch coverage included in aggregate %.

23 of 34 new or added lines in 3 files covered. (67.65%)

120 of 159 relevant lines covered (75.47%)

5.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.91
/src/lib/fetch.ts
1
// @ts-ignore
2
import nodeFetch, { Headers as NodeFetchHeaders } from '@supabase/node-fetch'
2✔
3
import { isJWT } from './helpers'
2✔
4

5
type Fetch = typeof fetch
6

7
export const resolveFetch = (customFetch?: Fetch): Fetch => {
2✔
8
  let _fetch: Fetch
9
  if (customFetch) {
8!
10
    _fetch = customFetch
×
11
  } else if (typeof fetch === 'undefined') {
8!
12
    _fetch = nodeFetch as unknown as Fetch
×
13
  } else {
14
    _fetch = fetch
8✔
15
  }
16
  return (...args: Parameters<Fetch>) => _fetch(...args)
8✔
17
}
18

19
export const resolveHeadersConstructor = () => {
2✔
20
  if (typeof Headers === 'undefined') {
8!
21
    return NodeFetchHeaders
×
22
  }
23

24
  return Headers
8✔
25
}
26

27
export const fetchWithAuth = (
2✔
28
  supabaseKey: string,
29
  getAccessToken: () => Promise<string | null>,
30
  customFetch?: Fetch
31
): Fetch => {
32
  const fetch = resolveFetch(customFetch)
8✔
33
  const HeadersConstructor = resolveHeadersConstructor()
8✔
34

35
  const defaultAccessToken = isJWT(supabaseKey) ? supabaseKey : null
8!
36

37
  return async (input, init) => {
8✔
NEW
38
    const accessToken = (await getAccessToken()) ?? defaultAccessToken
×
39
    let headers = new HeadersConstructor(init?.headers)
×
40

41
    if (!headers.has('apikey')) {
×
42
      headers.set('apikey', supabaseKey)
×
43
    }
44

NEW
45
    if (!headers.has('Authorization') && accessToken) {
×
46
      headers.set('Authorization', `Bearer ${accessToken}`)
×
47
    }
48

49
    return fetch(input, { ...init, headers })
×
50
  }
51
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc