Skip to content

Commit fea55ca

Browse files
authored
Merge pull request #1 from akncnkoc/proxy-url
Adds optional proxyUrl prop to support routing Google Places API requests through a backend proxy. Useful for hiding the API key on the client side. When proxyUrl is set, the component skips adding the X-Goog-Api-Key header, allowing the backend to handle authentication securely. Backward-compatible with existing usage.
2 parents e6ea98b + d22a947 commit fea55ca

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/GooglePlacesTextInput.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ const styles = StyleSheet.create({
6969
},
7070
});
7171

72+
const DEFAULT_GOOGLE_API_URL =
73+
'https://places.googleapis.com/v1/places:autocomplete';
7274
const GooglePlacesTextInput = forwardRef(
7375
(
7476
{
7577
apiKey,
7678
value,
7779
placeHolderText,
80+
proxyUrl,
7881
languageCode,
7982
includedRegionCodes,
8083
types = [],
@@ -136,22 +139,23 @@ const GooglePlacesTextInput = forwardRef(
136139

137140
try {
138141
setLoading(true);
139-
const response = await fetch(
140-
'https://places.googleapis.com/v1/places:autocomplete',
141-
{
142-
method: 'POST',
143-
headers: {
144-
'Content-Type': 'application/json',
145-
'X-Goog-Api-Key': apiKey,
146-
},
147-
body: JSON.stringify({
148-
input: processedText,
149-
languageCode,
150-
...(includedRegionCodes?.length > 0 && { includedRegionCodes }),
151-
...(types.length > 0 && { includedPrimaryTypes: types }),
152-
}),
153-
}
154-
);
142+
const API_URL = proxyUrl ? proxyUrl : DEFAULT_GOOGLE_API_URL;
143+
const headers = {
144+
'Content-Type': 'application/json',
145+
};
146+
if (apiKey || apiKey != '') {
147+
headers['X-Goog-Api-Key'] = apiKey;
148+
}
149+
const response = await fetch(API_URL, {
150+
method: 'POST',
151+
headers,
152+
body: JSON.stringify({
153+
input: processedText,
154+
languageCode,
155+
...(includedRegionCodes?.length > 0 && { includedRegionCodes }),
156+
...(types.length > 0 && { includedPrimaryTypes: types }),
157+
}),
158+
});
155159

156160
const data = await response.json();
157161

0 commit comments

Comments
 (0)