Skip to content

Commit d3e861f

Browse files
committed
fix(web): add proxy url for web support
1 parent 868f85f commit d3e861f

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

example/src/App.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import { StyleSheet, View, Text, TextInput, ScrollView } from 'react-native';
1+
import {
2+
StyleSheet,
3+
View,
4+
Text,
5+
TextInput,
6+
ScrollView,
7+
Platform,
8+
} from 'react-native';
29
import { useGoogleAutocomplete } from '@appandflow/react-native-google-autocomplete';
310

411
const API_KEY = '';
512

13+
const isWeb = Platform.OS === 'web';
14+
615
export default function App() {
716
const { setTerm, locationResults, isSearching } = useGoogleAutocomplete(
817
API_KEY,
918
{
1019
language: 'en',
1120
minLength: 3,
21+
proxyUrl: isWeb ? 'https://cors-anywhere.herokuapp.com/' : undefined,
1222
}
1323
);
1424

src/GoogleAutocomplete.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
} from './services/google.service';
66
import { useDebounce } from 'use-debounce';
77
import { useIsMounted } from './useIsMounted';
8+
import { Platform } from 'react-native';
9+
10+
const isWeb = Platform.OS === 'web';
811

912
interface Options {
1013
/**
@@ -58,6 +61,11 @@ interface Options {
5861
* Enable strict mode to return search result only in the area defined by radius, lat and lng
5962
*/
6063
strictBounds?: boolean;
64+
65+
/**
66+
* Proxy url if you want to use the web, this is needed cause of CORS issue
67+
*/
68+
proxyUrl?: string;
6169
}
6270

6371
export const useGoogleAutocomplete = (apiKey: string, opts: Options = {}) => {
@@ -77,17 +85,25 @@ export const useGoogleAutocomplete = (apiKey: string, opts: Options = {}) => {
7785
const [searchError, setSearchError] = useState<Error | null>(null);
7886

7987
const search = async (value: string) => {
88+
if (isWeb && !opts.proxyUrl) {
89+
throw new Error('A proxy url is needed for web');
90+
}
91+
8092
setIsSearching(true);
8193
try {
82-
const results = await GoogleService.search(value, {
83-
key: apiKey,
84-
language,
85-
types: queryTypes,
86-
strictBounds: opts.strictBounds,
87-
lat: opts.lat,
88-
lng: opts.lng,
89-
radius: opts.radius,
90-
});
94+
const results = await GoogleService.search(
95+
value,
96+
{
97+
key: apiKey,
98+
language,
99+
types: queryTypes,
100+
strictBounds: opts.strictBounds,
101+
lat: opts.lat,
102+
lng: opts.lng,
103+
radius: opts.radius,
104+
},
105+
opts.proxyUrl
106+
);
91107

92108
setLocationResults(results.predictions);
93109
} catch (error) {

src/services/google.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ const normalizeQuery = (query: Query): NormalizeQuery => {
112112
export class GoogleService {
113113
public static async search(
114114
term: string,
115-
query: Query
115+
query: Query,
116+
proxyUrl?: string
116117
): Promise<{
117118
predictions: GoogleLocationResult[];
118119
status: string;
@@ -123,7 +124,9 @@ export class GoogleService {
123124
query.strictBounds ? '&strictbounds' : ''
124125
}`;
125126

126-
const res = await fetch(url);
127+
const _url = proxyUrl ? proxyUrl + url : url;
128+
129+
const res = await fetch(_url);
127130

128131
if (!res.ok) {
129132
throw new Error(res.statusText);

0 commit comments

Comments
 (0)