You've already forked AstralRinth
forked from didirus/AstralRinth
feat: switch to api-client to prevent rate limit errors on game-versions (#5089)
* feat: switch to api-client to prevent rate limit errors on game-versions * feat: sentry temp * Revert "feat: sentry temp" This reverts commit aaa21a3f8099b86fa8e8d9d64845273e66c28bbe.
This commit is contained in:
@@ -6,3 +6,4 @@ src/generated/**
|
|||||||
src/locales/**
|
src/locales/**
|
||||||
src/public/news/feed
|
src/public/news/feed
|
||||||
src/assets/**/*.svg
|
src/assets/**/*.svg
|
||||||
|
**/.wrangler
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
import type { Labrinth } from '@modrinth/api-client'
|
import type { Labrinth } from '@modrinth/api-client'
|
||||||
|
|
||||||
|
import { useServerModrinthClient } from '~/server/utils/api-client'
|
||||||
|
|
||||||
const CACHE_MAX_AGE = 60 * 10 // 10 minutes
|
const CACHE_MAX_AGE = 60 * 10 // 10 minutes
|
||||||
|
|
||||||
export default defineCachedEventHandler(
|
export default defineCachedEventHandler(
|
||||||
async (event) => {
|
async (event) => {
|
||||||
const config = useRuntimeConfig(event)
|
const client = useServerModrinthClient(event)
|
||||||
const apiBaseUrl = config.apiBaseUrl || config.public.apiBaseUrl
|
|
||||||
|
|
||||||
const response = await $fetch<Labrinth.Tags.v2.GameVersion[]>(`${apiBaseUrl}tag/game_version`)
|
const response = await client.request<Labrinth.Tags.v2.GameVersion[]>('/tag/game_version', {
|
||||||
|
api: 'labrinth',
|
||||||
|
version: 2,
|
||||||
|
method: 'GET',
|
||||||
|
})
|
||||||
|
|
||||||
// nitro wont cache if we throw an error
|
// nitro wont cache if we throw an error
|
||||||
if (!response || !Array.isArray(response)) {
|
if (!response || !Array.isArray(response)) {
|
||||||
|
|||||||
25
apps/frontend/src/server/utils/api-client.ts
Normal file
25
apps/frontend/src/server/utils/api-client.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { type NuxtClientConfig, NuxtModrinthClient } from '@modrinth/api-client'
|
||||||
|
import type { H3Event } from 'h3'
|
||||||
|
|
||||||
|
async function getRateLimitKeyFromSecretsStore(): Promise<string | undefined> {
|
||||||
|
try {
|
||||||
|
const mod = 'cloudflare:workers'
|
||||||
|
const { env } = await import(/* @vite-ignore */ mod)
|
||||||
|
return await env.RATE_LIMIT_IGNORE_KEY?.get()
|
||||||
|
} catch {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useServerModrinthClient(event: H3Event): NuxtModrinthClient {
|
||||||
|
const config = useRuntimeConfig(event)
|
||||||
|
const apiBaseUrl = (config.apiBaseUrl || config.public.apiBaseUrl).replace('/v2/', '/')
|
||||||
|
|
||||||
|
const clientConfig: NuxtClientConfig = {
|
||||||
|
labrinthBaseUrl: apiBaseUrl,
|
||||||
|
rateLimitKey: config.rateLimitKey || getRateLimitKeyFromSecretsStore,
|
||||||
|
features: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NuxtModrinthClient(clientConfig)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user