mirror of
https://github.com/supabase/supabase.git
synced 2026-07-05 00:14:35 +08:00
* Update telemetry calls for docs to support PH * Update telemetry calls for www to support PH * Add ts ignore * Remove use of useRouter for docs * Add credentials include for www and docs for telemetry calls * Update TELEMETRY_CONSENT in common for www and docs to reset telemetry opt in preference * Update common telemetry to use new endpoint and payload, and trigger reset request when opting out of telemetry from ui patterns PrigacySettings * Fix * Fix * Fix * Fix build issue in docs * Fix build issue in docs * I hope this fixes the build issues * once more... * Fix * Add credentials include * Fix
67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import type { paths } from 'api-types'
|
|
import createClient from 'openapi-fetch'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { API_URL } from '../constants'
|
|
import { getAccessToken } from '../userAuth'
|
|
|
|
const DEFAULT_HEADERS = {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
}
|
|
|
|
const { GET: _get, POST: _post } = createClient<paths>({
|
|
baseUrl: API_URL,
|
|
referrerPolicy: 'no-referrer-when-downgrade',
|
|
headers: DEFAULT_HEADERS,
|
|
})
|
|
|
|
export async function constructHeaders(
|
|
headersInit?: HeadersInit | undefined,
|
|
{ allowUnauthenticated = false }: { allowUnauthenticated?: boolean } = {}
|
|
) {
|
|
const requestId = uuidv4()
|
|
const headers = new Headers(headersInit)
|
|
|
|
headers.set('X-Request-Id', requestId)
|
|
|
|
if (!headers.has('Authorization')) {
|
|
const accessToken = await getAccessToken()
|
|
if (accessToken) {
|
|
headers.set('Authorization', `Bearer ${accessToken}`)
|
|
} else if (!allowUnauthenticated) {
|
|
throw Error("can't fetch authenticated routes without signing in")
|
|
}
|
|
}
|
|
|
|
return headers
|
|
}
|
|
|
|
export const get: typeof _get = async (url, init) => {
|
|
const headers = await constructHeaders(init?.headers)
|
|
|
|
return await _get(url, {
|
|
...init,
|
|
headers,
|
|
})
|
|
}
|
|
|
|
export const post: typeof _post = async (url, init) => {
|
|
const headers = await constructHeaders(init?.headers)
|
|
|
|
return await _post(url, {
|
|
credentials: 'include',
|
|
...init,
|
|
headers,
|
|
})
|
|
}
|
|
|
|
export const unauthedAllowedPost: typeof _post = async (url, init) => {
|
|
const headers = await constructHeaders(init?.headers, { allowUnauthenticated: true })
|
|
|
|
return await _post(url, {
|
|
credentials: 'include',
|
|
...init,
|
|
headers,
|
|
})
|
|
}
|