UNPKG

1.27 kBJavaScriptView Raw
1import fetch from 'node-fetch';
2
3import { formatUsersLocations } from '../../Mapper/Geolocation';
4
5const BASE_URL = 'https://geolocations-dev.madbean.ovh';
6
7const updateGeolocation = (location) => {
8 const options = {
9 method: 'POST',
10 headers: {
11 'Content-Type': 'application/json',
12 Accept: 'application/json',
13 },
14 body: JSON.stringify([{
15 latitude: location.lat,
16 longitude: location.lon,
17 key: location.key,
18 }]),
19 };
20
21 fetch(`${BASE_URL}/${location.collectionID}`, options)
22 .then(data => data.json())
23 .catch(console.error);
24};
25
26// resolvers
27const getJSONFromRelativeURL = relativeURL => fetch(`${BASE_URL}${relativeURL}`)
28 .then(res => res.json())
29 .catch(console.error);
30
31const getLocation = ({ id, key }) => getJSONFromRelativeURL(`/${id}/${key}`)
32 .then(json => json);
33
34const getDistanceLocation = ({ id, key, foreignKey }) => getJSONFromRelativeURL(`/${id}/${key}/${foreignKey}`)
35 .then(json => json);
36
37const getCloseUsersLocations = ({
38 id, key, distance, unit,
39}) => getJSONFromRelativeURL(`/${id}/${key}/${distance}/${unit}`)
40 .then(json => formatUsersLocations(json.data))
41 .catch(console.error);
42
43export default {
44 getCloseUsersLocations,
45 getDistanceLocation,
46 getLocation,
47 updateGeolocation,
48};