Repository navigation
Expand file tree
/
Copy pathwingbits.js
More file actions
168 lines (149 loc) · 4.58 KB
/
Copy pathwingbits.js
File metadata and controls
168 lines (149 loc) · 4.58 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Wingbits API proxy - keeps API key server-side
// Note: Edge runtime is stateless - caching happens client-side and via HTTP Cache-Control
export const config = { runtime: 'edge' };
export default async function handler(req) {
const url = new URL(req.url);
const path = url.pathname.replace('/api/wingbits', '');
// Get API key from server-side env
const apiKey = process.env.WINGBITS_API_KEY;
if (!apiKey) {
return Response.json({
error: 'Wingbits not configured',
configured: false
}, {
status: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
});
}
// Handle CORS preflight
if (req.method === 'OPTIONS') {
return new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
}
// Route: GET /details/:icao24 - Aircraft details
const detailsMatch = path.match(/^\/details\/([a-fA-F0-9]+)$/);
if (detailsMatch) {
const icao24 = detailsMatch[1].toLowerCase();
try {
const response = await fetch(`https://customer-api.wingbits.com/v1/flights/details/${icao24}`, {
headers: {
'x-api-key': apiKey,
'Accept': 'application/json',
},
});
if (!response.ok) {
return Response.json({
error: `Wingbits API error: ${response.status}`,
icao24,
}, {
status: response.status,
headers: { 'Access-Control-Allow-Origin': '*' },
});
}
const data = await response.json();
return Response.json(data, {
headers: {
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=86400', // 24h - aircraft details rarely change
},
});
} catch (error) {
return Response.json({
error: `Fetch failed: ${error.message}`,
icao24,
}, {
status: 500,
headers: { 'Access-Control-Allow-Origin': '*' },
});
}
}
// Route: POST /details/batch - Batch lookup multiple aircraft (parallel)
if (path === '/details/batch' && req.method === 'POST') {
try {
const body = await req.json();
const icao24List = body.icao24s || [];
if (!Array.isArray(icao24List) || icao24List.length === 0) {
return Response.json({ error: 'icao24s array required' }, {
status: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
});
}
// Limit batch size to avoid overwhelming the API
const limitedList = icao24List.slice(0, 20).map(id => id.toLowerCase());
const results = {};
// Fetch all in parallel
const fetchPromises = limitedList.map(async (icao24) => {
try {
const response = await fetch(`https://customer-api.wingbits.com/v1/flights/details/${icao24}`, {
headers: {
'x-api-key': apiKey,
'Accept': 'application/json',
},
});
if (response.ok) {
const data = await response.json();
return { icao24, data };
}
} catch {
// Skip failed lookups
}
return null;
});
const fetchResults = await Promise.all(fetchPromises);
for (const result of fetchResults) {
if (result) {
results[result.icao24] = result.data;
}
}
return Response.json({
results,
fetched: Object.keys(results).length,
requested: limitedList.length,
}, {
headers: {
'Access-Control-Allow-Origin': '*',
},
});
} catch (error) {
return Response.json({
error: `Batch lookup failed: ${error.message}`,
}, {
status: 500,
headers: { 'Access-Control-Allow-Origin': '*' },
});
}
}
// Route: GET /health - Check Wingbits status
if (path === '/health' || path === '') {
try {
const response = await fetch('https://customer-api.wingbits.com/health', {
headers: { 'x-api-key': apiKey },
});
const data = await response.json();
return Response.json({
...data,
configured: true,
}, {
headers: { 'Access-Control-Allow-Origin': '*' },
});
} catch (error) {
return Response.json({
error: error.message,
configured: true,
}, {
status: 500,
headers: { 'Access-Control-Allow-Origin': '*' },
});
}
}
return Response.json({ error: 'Not found' }, {
status: 404,
headers: { 'Access-Control-Allow-Origin': '*' },
});
}