Skip to content

Commit

Permalink
Add header support when using custom request url (#734)
Browse files Browse the repository at this point in the history
* Add support for headers on the request
  • Loading branch information
ryanb-jobber authored Aug 9, 2021
1 parent 1fe9405 commit 3988e45
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions GooglePlacesAutocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ interface Place {
interface RequestUrl {
url: string;
useOnPlatform: 'web' | 'all';
headers?: Record<string, string>;
}

interface GooglePlacesAutocompleteProps {
Expand Down
18 changes: 18 additions & 0 deletions GooglePlacesAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
}
};

const getRequestHeaders = (requestUrl) => {
if (requestUrl) {
return requestUrl.headers;
} else {
return {};
}
};

const setRequestHeaders = (request, headers) => {
Object.keys(headers).map((headerKey) =>
request.setRequestHeader(headerKey, headers[headerKey]),
);
};

const [stateText, setStateText] = useState('');
const [dataSource, setDataSource] = useState(buildRowsFromResults([]));
const [listViewDisplayed, setListViewDisplayed] = useState(
Expand Down Expand Up @@ -299,6 +313,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
);

request.withCredentials = requestShouldUseWithCredentials();
setRequestHeaders(request, getRequestHeaders(props.requestUrl));

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

request.withCredentials = requestShouldUseWithCredentials();
setRequestHeaders(request, getRequestHeaders(props.requestUrl));

request.send();
} else {
Expand Down Expand Up @@ -521,6 +537,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
);

request.withCredentials = requestShouldUseWithCredentials();
setRequestHeaders(request, getRequestHeaders(props.requestUrl));

request.send();
} else {
Expand Down Expand Up @@ -876,6 +893,7 @@ GooglePlacesAutocomplete.propTypes = {
requestUrl: PropTypes.shape({
url: PropTypes.string,
useOnPlatform: PropTypes.oneOf(['web', 'all']),
headers: PropTypes.objectOf(PropTypes.string),
}),
styles: PropTypes.object,
suppressDefaultStyles: PropTypes.bool,
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,14 @@ export default GooglePlacesInput;
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.
The `requestUrl` prop takes an object with two properties: `useOnPlatform` and `url`.
The `requestUrl` prop takes an object with two required properties: `useOnPlatform` and `url`, and an optional `headers` property.
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.
`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.
You can optionally specify headers to apply to your request in the `headers` object.
### Example:
```js
Expand All @@ -432,6 +434,9 @@ const GooglePlacesInput = () => {
useOnPlatform: 'web', // or "all"
url:
'https://cors-anywhere.herokuapp.com/https://maps.googleapis.com/maps/api', // or any proxy server that hits https://maps.googleapis.com/maps/api
headers: {
Authorization: `an auth token` // if required for your proxy
}
}}
/>
);
Expand Down

0 comments on commit 3988e45

Please sign in to comment.