File size: 2,225 Bytes
1f21206 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import { openTargetService } from '../services/openTargetService.js'
import { ApiError, errorResponse } from '../middleware/errorHandler.js'
export async function handleOpenTargetsApi(
req: Request,
url: URL,
segments: string[],
): Promise<Response> {
try {
const action = segments[2]
if (!action) {
if (req.method !== 'GET') {
throw new ApiError(405, `Method ${req.method} not allowed`, 'METHOD_NOT_ALLOWED')
}
return Response.json(await openTargetService.listTargets())
}
if (action === 'open') {
if (req.method !== 'POST') {
throw new ApiError(405, `Method ${req.method} not allowed`, 'METHOD_NOT_ALLOWED')
}
const body = await parseJsonBody(req)
const targetId = typeof body.targetId === 'string' ? body.targetId.trim() : ''
const path = typeof body.path === 'string' ? body.path : ''
if (!targetId) {
throw ApiError.badRequest('Missing or invalid "targetId" in request body')
}
if (!path || !path.trim()) {
throw ApiError.badRequest('Missing or invalid "path" in request body')
}
return Response.json(await openTargetService.openTarget({ targetId, path }))
}
if (action === 'icons') {
if (req.method !== 'GET') {
throw new ApiError(405, `Method ${req.method} not allowed`, 'METHOD_NOT_ALLOWED')
}
const targetId = typeof segments[3] === 'string' ? decodeURIComponent(segments[3]).trim() : ''
if (!targetId) {
throw ApiError.badRequest('Missing open target icon id')
}
const icon = await openTargetService.getTargetIcon(targetId)
return new Response(icon.data, {
headers: {
'Cache-Control': 'private, max-age=86400',
'Content-Type': icon.contentType,
},
})
}
throw ApiError.notFound(`Unknown open-targets endpoint: ${action}`)
} catch (error) {
return errorResponse(error)
}
}
async function parseJsonBody(req: Request): Promise<Record<string, unknown>> {
try {
const body = await req.json()
return body && typeof body === 'object' ? body as Record<string, unknown> : {}
} catch {
throw ApiError.badRequest('Invalid JSON body')
}
}
|