Skip to content

Commit

Permalink
fix(web): add proxy url for web support
Browse files Browse the repository at this point in the history
  • Loading branch information
EQuimper committed Jul 11, 2024
1 parent 868f85f commit d3e861f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
12 changes: 11 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { StyleSheet, View, Text, TextInput, ScrollView } from 'react-native';
import {
StyleSheet,
View,
Text,
TextInput,
ScrollView,
Platform,
} from 'react-native';
import { useGoogleAutocomplete } from '@appandflow/react-native-google-autocomplete';

const API_KEY = '';

const isWeb = Platform.OS === 'web';

export default function App() {
const { setTerm, locationResults, isSearching } = useGoogleAutocomplete(
API_KEY,
{
language: 'en',
minLength: 3,
proxyUrl: isWeb ? 'https://cors-anywhere.herokuapp.com/' : undefined,
}
);

Expand Down
34 changes: 25 additions & 9 deletions src/GoogleAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
} from './services/google.service';
import { useDebounce } from 'use-debounce';
import { useIsMounted } from './useIsMounted';
import { Platform } from 'react-native';

const isWeb = Platform.OS === 'web';

interface Options {
/**
Expand Down Expand Up @@ -58,6 +61,11 @@ interface Options {
* Enable strict mode to return search result only in the area defined by radius, lat and lng
*/
strictBounds?: boolean;

/**
* Proxy url if you want to use the web, this is needed cause of CORS issue
*/
proxyUrl?: string;
}

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

const search = async (value: string) => {
if (isWeb && !opts.proxyUrl) {
throw new Error('A proxy url is needed for web');
}

setIsSearching(true);
try {
const results = await GoogleService.search(value, {
key: apiKey,
language,
types: queryTypes,
strictBounds: opts.strictBounds,
lat: opts.lat,
lng: opts.lng,
radius: opts.radius,
});
const results = await GoogleService.search(
value,
{
key: apiKey,
language,
types: queryTypes,
strictBounds: opts.strictBounds,
lat: opts.lat,
lng: opts.lng,
radius: opts.radius,
},
opts.proxyUrl
);

setLocationResults(results.predictions);
} catch (error) {
Expand Down
7 changes: 5 additions & 2 deletions src/services/google.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ const normalizeQuery = (query: Query): NormalizeQuery => {
export class GoogleService {
public static async search(
term: string,
query: Query
query: Query,
proxyUrl?: string
): Promise<{
predictions: GoogleLocationResult[];
status: string;
Expand All @@ -123,7 +124,9 @@ export class GoogleService {
query.strictBounds ? '&strictbounds' : ''
}`;

const res = await fetch(url);
const _url = proxyUrl ? proxyUrl + url : url;

const res = await fetch(_url);

if (!res.ok) {
throw new Error(res.statusText);
Expand Down

0 comments on commit d3e861f

Please sign in to comment.