2026-02-27 17:21:35 +00:00
|
|
|
/**
|
|
|
|
|
* @deprecated Use `@modrinth/api-client` via `injectModrinthClient()` instead.
|
|
|
|
|
* This composable is kept for legacy code that hasn't been migrated yet.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-13 16:29:08 +00:00
|
|
|
import { withLabrinthCanaryHeader } from '~/helpers/canary.ts'
|
|
|
|
|
|
2026-01-08 11:25:45 -05:00
|
|
|
let cachedRateLimitKey = undefined
|
|
|
|
|
let rateLimitKeyPromise = undefined
|
|
|
|
|
|
|
|
|
|
async function getRateLimitKey(config) {
|
|
|
|
|
if (config.rateLimitKey) return config.rateLimitKey
|
|
|
|
|
if (cachedRateLimitKey !== undefined) return cachedRateLimitKey
|
|
|
|
|
|
|
|
|
|
if (!rateLimitKeyPromise) {
|
|
|
|
|
rateLimitKeyPromise = (async () => {
|
|
|
|
|
try {
|
2026-01-10 15:11:00 +01:00
|
|
|
const mod = 'cloudflare:workers'
|
|
|
|
|
const { env } = await import(/* @vite-ignore */ mod)
|
2026-01-08 11:25:45 -05:00
|
|
|
return await env.RATE_LIMIT_IGNORE_KEY?.get()
|
|
|
|
|
} catch {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
})()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cachedRateLimitKey = await rateLimitKeyPromise
|
|
|
|
|
return cachedRateLimitKey
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:19:42 -07:00
|
|
|
export const useBaseFetch = async (url, options = {}, skipAuth = false) => {
|
2025-08-14 21:48:38 +01:00
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
let base = import.meta.server ? config.apiBaseUrl : config.public.apiBaseUrl
|
2023-03-09 10:05:32 -07:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
if (!options.headers) {
|
|
|
|
|
options.headers = {}
|
|
|
|
|
}
|
2023-07-20 11:19:42 -07:00
|
|
|
|
2026-03-13 16:29:08 +00:00
|
|
|
options.headers = withLabrinthCanaryHeader(options.headers)
|
|
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
if (import.meta.server) {
|
2026-01-08 11:25:45 -05:00
|
|
|
options.headers['x-ratelimit-key'] = await getRateLimitKey(config)
|
2025-08-14 21:48:38 +01:00
|
|
|
}
|
2023-07-20 11:19:42 -07:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
if (!skipAuth) {
|
|
|
|
|
const auth = await useAuth()
|
2023-07-20 11:19:42 -07:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
options.headers.Authorization = auth.value.token
|
|
|
|
|
}
|
2023-03-09 10:05:32 -07:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
if (options.apiVersion || options.internal) {
|
|
|
|
|
// Base may end in /vD/ or /vD. We would need to replace the digit with the new version number
|
|
|
|
|
// and keep the trailing slash if it exists
|
|
|
|
|
const baseVersion = base.match(/\/v\d\//)
|
2023-12-04 19:26:50 -08:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
const replaceStr = options.internal ? `/_internal/` : `/v${options.apiVersion}/`
|
2023-12-04 19:26:50 -08:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
if (baseVersion) {
|
|
|
|
|
base = base.replace(baseVersion[0], replaceStr)
|
|
|
|
|
} else {
|
|
|
|
|
base = base.replace(/\/v\d$/, replaceStr)
|
|
|
|
|
}
|
2023-12-04 19:26:50 -08:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
delete options.apiVersion
|
|
|
|
|
}
|
2023-12-04 19:26:50 -08:00
|
|
|
|
2026-01-20 11:52:34 -08:00
|
|
|
return await $fetch(`${base}${url}`, {
|
|
|
|
|
timeout: import.meta.server ? 10000 : undefined,
|
|
|
|
|
...options,
|
|
|
|
|
})
|
2025-08-14 21:48:38 +01:00
|
|
|
}
|