-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
61 lines (53 loc) · 1.45 KB
/
api.js
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
import { DIGITRANSIT_API_KEY } from '@env';
// Details for fetching from DigiTransit API
async function fetchGraphQLData(query, variables = {}) {
const url = 'https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'digitransit-subscription-key': DIGITRANSIT_API_KEY,
},
body: JSON.stringify({ query, variables }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
// Fetch gtfsId by using name/number of stop from DigiTransit API
export async function fetchStopIdByNameOrNumber(nameOrNumber) {
const query = `
{
stops(name: "${nameOrNumber}") {
gtfsId
name
code
lat
lon
}
}
`;
const data = await fetchGraphQLData(query);
return data.data.stops; // Array of stops
}
// Fetch nearby stops by radius from DigiTransit API
export async function fetchStopsByRadius(lat, lon, radius) {
const query = `
{
stopsByRadius(lat:${lat}, lon:${lon}, radius:${radius}) {
edges {
node {
stop {
gtfsId
name
}
}
}
}
}
`;
const data = await fetchGraphQLData(query);
return data.data.stopsByRadius.edges.map(edge => edge.node.stop);
}
export default fetchGraphQLData;