UNPKG

1.3 kBPlain TextView Raw
1import invariant from 'invariant'
2
3export interface LoadScriptUrlOptions {
4 googleMapsApiKey?: string
5 googleMapsClientId?: string
6 version?: string
7 language?: string
8 region?: string
9 libraries?: string[]
10 channel?: string
11}
12
13export function makeLoadScriptUrl({
14 googleMapsApiKey,
15 googleMapsClientId,
16 version = 'weekly',
17 language,
18 region,
19 libraries,
20 channel,
21}: LoadScriptUrlOptions) {
22 const params = []
23
24 invariant(
25 (googleMapsApiKey && googleMapsClientId) || !(googleMapsApiKey && googleMapsClientId),
26 'You need to specify either googleMapsApiKey or googleMapsClientId for @react-google-maps/api load script to work. You cannot use both at the same time.'
27 )
28
29 if (googleMapsApiKey) {
30 params.push(`key=${googleMapsApiKey}`)
31 } else if (googleMapsClientId) {
32 params.push(`client=${googleMapsClientId}`)
33 }
34
35 if (version) {
36 params.push(`v=${version}`)
37 }
38
39 if (language) {
40 params.push(`language=${language}`)
41 }
42
43 if (region) {
44 params.push(`region=${region}`)
45 }
46
47 if (libraries && libraries.length) {
48 params.push(`libraries=${libraries.sort().join(',')}`)
49 }
50
51 if (channel) {
52 params.push(`channel=${channel}`)
53 }
54
55 params.push('callback=initMap')
56
57 return `https://maps.googleapis.com/maps/api/js?${params.join('&')}`
58}