Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header support when using custom request url #734

Merged
merged 7 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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