Skip to content

Commit

Permalink
feat(examples): add getApiKey lib to every example
Browse files Browse the repository at this point in the history
  • Loading branch information
mrMetalWood committed Nov 2, 2023
1 parent bfa8316 commit ec40e11
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/_template/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {createRoot} from 'react-dom/client';

import {APIProvider, Map} from '@vis.gl/react-google-maps';

const API_KEY = process.env.GOOGLE_MAPS_API_KEY as string;
import {getApiKey} from './lib/get-api-key';

const API_KEY = getApiKey();

const App = () => <APIProvider apiKey={API_KEY}></APIProvider>;

Expand Down
20 changes: 20 additions & 0 deletions examples/_template/src/lib/get-api-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function getApiKey() {
const url = new URL(location.href);
const apiKey =
url.searchParams.get('apiKey') ||
(process.env.GOOGLE_MAPS_API_KEY as string);

if (!apiKey) {
const key = prompt(
'Please provide your Google Maps API key in the URL\n' +
'(using the parameter `?apiKey=YOUR_API_KEY_HERE`) or enter it here:'
);

if (key) {
url.searchParams.set('apiKey', key);
location.replace(url.toString());
}
}

return apiKey;
}
4 changes: 3 additions & 1 deletion examples/change-map-id/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
} from '@vis.gl/react-google-maps';
import ControlPanel from './control-panel';

const API_KEY = process.env.GOOGLE_MAPS_API_KEY as string;
import {getApiKey} from './lib/get-api-key';

const API_KEY = getApiKey();

const MapTypeId = {
HYBRID: 'hybrid',
Expand Down
20 changes: 20 additions & 0 deletions examples/change-map-id/src/lib/get-api-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function getApiKey() {
const url = new URL(location.href);
const apiKey =
url.searchParams.get('apiKey') ||
(process.env.GOOGLE_MAPS_API_KEY as string);

if (!apiKey) {
const key = prompt(
'Please provide your Google Maps API key in the URL\n' +
'(using the parameter `?apiKey=YOUR_API_KEY_HERE`) or enter it here:'
);

if (key) {
url.searchParams.set('apiKey', key);
location.replace(url.toString());
}
}

return apiKey;
}
4 changes: 3 additions & 1 deletion examples/deckgl-overlay/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const DATA_URL =
import type {Feature, GeoJSON} from 'geojson';
import ControlPanel from './control-panel';

const API_KEY = process.env.GOOGLE_MAPS_API_KEY as string;
import {getApiKey} from './lib/get-api-key';

const API_KEY = getApiKey();

const App = () => {
const [data, setData] = useState<GeoJSON | null>(null);
Expand Down
20 changes: 20 additions & 0 deletions examples/deckgl-overlay/src/lib/get-api-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function getApiKey() {
const url = new URL(location.href);
const apiKey =
url.searchParams.get('apiKey') ||
(process.env.GOOGLE_MAPS_API_KEY as string);

if (!apiKey) {
const key = prompt(
'Please provide your Google Maps API key in the URL\n' +
'(using the parameter `?apiKey=YOUR_API_KEY_HERE`) or enter it here:'
);

if (key) {
url.searchParams.set('apiKey', key);
location.replace(url.toString());
}
}

return apiKey;
}
4 changes: 3 additions & 1 deletion examples/markers-and-infowindows/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import ControlPanel from './control-panel';
import {MovingMarker} from './moving-marker';
import {MarkerWithInfowindow} from './marker-with-infowindow';

const API_KEY = process.env.GOOGLE_MAPS_API_KEY as string;
import {getApiKey} from './lib/get-api-key';

const API_KEY = getApiKey();

const App = () => {
return (
Expand Down
20 changes: 20 additions & 0 deletions examples/markers-and-infowindows/src/lib/get-api-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function getApiKey() {
const url = new URL(location.href);
const apiKey =
url.searchParams.get('apiKey') ||
(process.env.GOOGLE_MAPS_API_KEY as string);

if (!apiKey) {
const key = prompt(
'Please provide your Google Maps API key in the URL\n' +
'(using the parameter `?apiKey=YOUR_API_KEY_HERE`) or enter it here:'
);

if (key) {
url.searchParams.set('apiKey', key);
location.replace(url.toString());
}
}

return apiKey;
}

0 comments on commit ec40e11

Please sign in to comment.