(request)
| 66 | }; |
| 67 | |
| 68 | async function handleResolveBatchRequest(request) { |
| 69 | const headers = { |
| 70 | 'Content-Type': 'application/json', |
| 71 | 'Access-Control-Allow-Origin': '*', |
| 72 | 'Access-Control-Allow-Methods': 'POST, OPTIONS', |
| 73 | 'Access-Control-Allow-Headers': 'Content-Type' |
| 74 | }; |
| 75 | |
| 76 | if (request.method === 'OPTIONS') { |
| 77 | return new Response(null, { |
| 78 | status: 204, |
| 79 | headers |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | if (request.method !== 'POST') { |
| 84 | return new Response(JSON.stringify({ error: 'Method not allowed' }), { |
| 85 | status: 405, |
| 86 | headers: { |
| 87 | ...headers, |
| 88 | 'Allow': 'POST, OPTIONS' |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | let payload; |
| 94 | try { |
| 95 | payload = await request.json(); |
| 96 | } catch (error) { |
| 97 | return new Response(JSON.stringify({ error: 'Invalid JSON body' }), { |
| 98 | status: 400, |
| 99 | headers |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | const inputs = getResolveBatchInputs(payload); |
| 104 | if (!inputs.length) { |
| 105 | return new Response(JSON.stringify({ error: 'Missing targets' }), { |
| 106 | status: 400, |
| 107 | headers |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | if (inputs.length > RESOLVE_BATCH_LIMIT) { |
| 112 | return new Response(JSON.stringify({ error: `Resolve batch limit is ${RESOLVE_BATCH_LIMIT}` }), { |
| 113 | status: 400, |
| 114 | headers |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | const results = await Promise.all(inputs.map(async input => { |
| 119 | try { |
| 120 | return { |
| 121 | input, |
| 122 | targets: await handleResolve(input) |
| 123 | }; |
| 124 | } catch (error) { |
| 125 | return { |
no test coverage detected