Removed awaits, clean up API route
All checks were successful
pipeline / lint-and-format (push) Successful in 4m54s
pipeline / build-and-push-images (push) Successful in 4m43s

This commit is contained in:
2026-01-23 21:22:19 +01:00
parent ef48e748db
commit 653b0c41d7
4 changed files with 77 additions and 17 deletions

View File

@@ -5,15 +5,62 @@ async function championInfos(client: MongoClient, patch: string, championAlias:
const database = client.db('champions')
const collection = database.collection(patch)
const query = { alias: championAlias }
const championInfo = (await collection.findOne(query)) as unknown as ChampionData
const championInfo = (await collection.findOne(query)) as unknown as ChampionData | null
return championInfo
}
export default defineEventHandler(async event => {
const championAlias = (getRouterParam(event, 'alias') as string).toLowerCase()
const client = await connectToDatabase()
const latestPatch = await fetchLatestPatch(client)
const data = await championInfos(client, latestPatch, championAlias)
await client.close()
return data
try {
const championAlias = (getRouterParam(event, 'alias') as string).toLowerCase()
// Validate champion alias
if (!championAlias || typeof championAlias !== 'string') {
throw createError({
statusCode: 400,
statusMessage: 'Invalid champion alias',
data: { alias: championAlias }
})
}
const client = await connectToDatabase()
const latestPatch = await fetchLatestPatch(client)
const data = await championInfos(client, latestPatch, championAlias)
await client.close()
if (!data) {
throw createError({
statusCode: 404,
statusMessage: 'Champion not found',
data: { alias: championAlias, patch: latestPatch }
})
}
return data
} catch (error) {
console.error('Error in champion API:', error)
// Handle different error types
if (error instanceof Error) {
throw createError({
statusCode: 500,
statusMessage: 'Failed to fetch champion data',
data: {
error: error.message,
championAlias: getRouterParam(event, 'alias')
}
})
}
// Re-throw if it's already a proper error
if (error && typeof error === 'object' && 'statusCode' in error) {
throw error
}
// Fallback error
throw createError({
statusCode: 500,
statusMessage: 'Internal server error',
data: { championAlias: getRouterParam(event, 'alias') }
})
}
})