Skip to content

Commit 3988e45

Browse files
authored
Add header support when using custom request url (#734)
* Add support for headers on the request
1 parent 1fe9405 commit 3988e45

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

GooglePlacesAutocomplete.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ interface Place {
363363
interface RequestUrl {
364364
url: string;
365365
useOnPlatform: 'web' | 'all';
366+
headers?: Record<string, string>;
366367
}
367368

368369
interface GooglePlacesAutocompleteProps {

GooglePlacesAutocomplete.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
123123
}
124124
};
125125

126+
const getRequestHeaders = (requestUrl) => {
127+
if (requestUrl) {
128+
return requestUrl.headers;
129+
} else {
130+
return {};
131+
}
132+
};
133+
134+
const setRequestHeaders = (request, headers) => {
135+
Object.keys(headers).map((headerKey) =>
136+
request.setRequestHeader(headerKey, headers[headerKey]),
137+
);
138+
};
139+
126140
const [stateText, setStateText] = useState('');
127141
const [dataSource, setDataSource] = useState(buildRowsFromResults([]));
128142
const [listViewDisplayed, setListViewDisplayed] = useState(
@@ -299,6 +313,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
299313
);
300314

301315
request.withCredentials = requestShouldUseWithCredentials();
316+
setRequestHeaders(request, getRequestHeaders(props.requestUrl));
302317

303318
request.send();
304319
} else if (rowData.isCurrentLocation === true) {
@@ -458,6 +473,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
458473
request.open('GET', requestUrl);
459474

460475
request.withCredentials = requestShouldUseWithCredentials();
476+
setRequestHeaders(request, getRequestHeaders(props.requestUrl));
461477

462478
request.send();
463479
} else {
@@ -521,6 +537,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
521537
);
522538

523539
request.withCredentials = requestShouldUseWithCredentials();
540+
setRequestHeaders(request, getRequestHeaders(props.requestUrl));
524541

525542
request.send();
526543
} else {
@@ -876,6 +893,7 @@ GooglePlacesAutocomplete.propTypes = {
876893
requestUrl: PropTypes.shape({
877894
url: PropTypes.string,
878895
useOnPlatform: PropTypes.oneOf(['web', 'all']),
896+
headers: PropTypes.objectOf(PropTypes.string),
879897
}),
880898
styles: PropTypes.object,
881899
suppressDefaultStyles: PropTypes.bool,

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,14 @@ export default GooglePlacesInput;
404404
405405
Web support can be enabled via the `requestUrl` prop, by passing in a URL that you can use to proxy your requests. CORS implemented by the Google Places API prevent using this library directly on the web. You will need to use a proxy server. Please be mindful of this limitation when opening an issue.
406406
407-
The `requestUrl` prop takes an object with two properties: `useOnPlatform` and `url`.
407+
The `requestUrl` prop takes an object with two required properties: `useOnPlatform` and `url`, and an optional `headers` property.
408408
409409
The `url` property is used to set the url that requests will be made to. If you are using the regular google maps API, you need to make sure you are ultimately hitting https://maps.googleapis.com/maps/api.
410410
411411
`useOnPlatform` configures when the proxy url is used. It can be set to either `web`- will be used only when the device platform is detected as web (but not iOS or Android, or `all` - will always be used.
412412
413+
You can optionally specify headers to apply to your request in the `headers` object.
414+
413415
### Example:
414416
415417
```js
@@ -432,6 +434,9 @@ const GooglePlacesInput = () => {
432434
useOnPlatform: 'web', // or "all"
433435
url:
434436
'https://cors-anywhere.herokuapp.com/https://maps.googleapis.com/maps/api', // or any proxy server that hits https://maps.googleapis.com/maps/api
437+
headers: {
438+
Authorization: `an auth token` // if required for your proxy
439+
}
435440
}}
436441
/>
437442
);

0 commit comments

Comments
 (0)