-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
72 lines (61 loc) · 1.49 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
import invariant from 'invariant';
import PathStrategy from '../path';
import NativeStrategy from './nativeStrategy';
import FetchStrategy from './fetchStrategy';
const directionStrategy = ({ props, type: { defaultProps } }, parentProps) => {
const {
baseURL,
requestStrategy,
origin,
destination,
apiKey,
waypoints,
avoid,
travelMode,
transitMode,
transitRoutingPreference,
weight,
color,
fillcolor,
geodesic,
...rest
} = props;
invariant(origin, 'Origin prop is required');
invariant(destination, 'Destination prop is required');
// Use the parent's API key if one isn't set here.
const key = apiKey ? apiKey : parentProps ? parentProps.apiKey : '';
const data = {
key,
baseURL,
origin,
destination,
waypoints,
avoid,
travelMode,
transitMode,
transitRoutingPreference,
...rest,
};
let pathPromise;
if (typeof requestStrategy !== 'string') {
pathPromise = requestStrategy(data);
} else {
switch (requestStrategy) {
case 'native':
pathPromise = NativeStrategy(data);
break;
case 'fetch':
pathPromise = FetchStrategy(data);
break;
default:
throw new Error('Specify a Request strategy to get directions from');
}
}
return pathPromise.then(path =>
PathStrategy({
props: { weight, color, fillcolor, geodesic, points: `enc:${path}` },
type: { defaultProps },
})
);
};
export default directionStrategy;