65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
import { onRequest } from "firebase-functions/v2/https";
|
|
import { Request } from "firebase-functions/v2/https";
|
|
import * as express from "express";
|
|
import { logger } from "../shared/config";
|
|
import axios from "axios";
|
|
import { corsHandler } from "../shared/middleware";
|
|
|
|
export const getPlacesAutocomplete = onRequest({
|
|
region: '#{SERVICES_RGN}#'
|
|
}, async (request: Request, response: express.Response) => {
|
|
return corsHandler(request, response, async () => {
|
|
try {
|
|
const { input, location, radius, types, components, sessiontoken } = request.query;
|
|
|
|
if (!input) {
|
|
response.status(400).json({
|
|
error: 'Input parameter is required for autocomplete'
|
|
});
|
|
return;
|
|
}
|
|
|
|
const apiKey = process.env.GOOGLE_MAPS_API_KEY;
|
|
if (!apiKey) {
|
|
logger.error('Google Places API key is not configured');
|
|
response.status(500).json({ error: 'Server configuration error' });
|
|
return;
|
|
}
|
|
|
|
const url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json';
|
|
const params: any = {
|
|
key: apiKey,
|
|
input: input
|
|
};
|
|
|
|
if (location && radius) {
|
|
params.location = location;
|
|
params.radius = radius;
|
|
}
|
|
|
|
if (types) {
|
|
params.types = types;
|
|
}
|
|
|
|
if (components) {
|
|
params.components = components;
|
|
}
|
|
|
|
if (sessiontoken) {
|
|
params.sessiontoken = sessiontoken;
|
|
}
|
|
|
|
const result = await axios.get(url, { params });
|
|
|
|
logger.info('Google Places Autocomplete API request completed successfully');
|
|
response.json(result.data);
|
|
} catch (error) {
|
|
logger.error('Error fetching place autocomplete suggestions:', error);
|
|
response.status(500).json({
|
|
success: false,
|
|
error: error instanceof Error ? error.message : String(error)
|
|
});
|
|
}
|
|
});
|
|
});
|