diff --git a/.env.example b/.env.example index 17752b694..f0111f51d 100644 --- a/.env.example +++ b/.env.example @@ -13,4 +13,5 @@ SMTP_USER="no-reply@shelf.nu" MAPTILER_TOKEN="maptiler-token" MICROSOFT_CLARITY_ID="microsoft-clarity-id" -INVITE_TOKEN_SECRET="secret-test-invite" \ No newline at end of file +INVITE_TOKEN_SECRET="secret-test-invite" +GEOCODE_API_KEY="geocode-api-key" \ No newline at end of file diff --git a/app/utils/env.ts b/app/utils/env.ts index d15e86526..e9214715e 100644 --- a/app/utils/env.ts +++ b/app/utils/env.ts @@ -38,6 +38,7 @@ declare global { SMTP_HOST: string; SMTP_USER: string; MAINTENANCE_MODE: string; + GEOCODE_API_KEY: string; } } } @@ -107,6 +108,11 @@ export const FORMBRICKS_ENV_ID = getEnv("FORMBRICKS_ENV_ID", { isRequired: false, }); +export const GEOCODE_API_KEY = getEnv("GEOCODE_API_KEY", { + isSecret: true, + isRequired: false, +}); + export const MAINTENANCE_MODE = getEnv("MAINTENANCE_MODE", { isSecret: false, diff --git a/app/utils/geolocate.server.ts b/app/utils/geolocate.server.ts index 295bc3745..05ca29475 100644 --- a/app/utils/geolocate.server.ts +++ b/app/utils/geolocate.server.ts @@ -1,9 +1,17 @@ +import { GEOCODE_API_KEY } from "./env"; + export const geolocate = async ( address: string | null ): Promise<{ lat: number; lon: number } | null> => { - if (!address || address === "") return null; + if (!address || address === "" || !GEOCODE_API_KEY) return null; + // Create URL object and add the address to the url params + const url = new URL("https://geocode.maps.co/search"); + url.searchParams.append("q", address); + url.searchParams.append("api_key", GEOCODE_API_KEY); + + const request = await fetch(url.href); - const request = await fetch(`https://geocode.maps.co/search?q=${address}`); + if (!request.ok) return null; const response = await request.json(); /** Here we take the frist entry of the array. * When there are more entries that means the address is not accurate enought so we just take the first option diff --git a/docs/get-started.md b/docs/get-started.md index f3564d993..101e8f371 100644 --- a/docs/get-started.md +++ b/docs/get-started.md @@ -269,3 +269,8 @@ Shelf hosted version has some premium features that are locked behind different ``` ENABLE_PREMIUM_FEATURES="true" ``` + +## Geocoding + +The locations module allows the users to put an address in a free text field for their "location". For geocoding we use [https://geocode.maps.co/](https://geocode.maps.co/) which is a free api for up to 1 000 000 requests per month. +If you want to use it you need to provide an env variable called `GEOCODE_API_KEY` with your api key. If you would like to use a different api, you can find the code for requesting the location details in `app/utils/geolocate.server.ts`. diff --git a/package-lock.json b/package-lock.json index 661e0292b..41258b10e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5727,9 +5727,9 @@ } }, "node_modules/@vanilla-extract/integration/node_modules/vite": { - "version": "4.4.11", - "resolved": "https://registry.npmjs.org/vite/-/vite-4.4.11.tgz", - "integrity": "sha512-ksNZJlkcU9b0lBwAGZGGaZHCMqHsc8OpgtoYhsQ4/I2v5cnpmmmqe5pM4nv/4Hn6G/2GhTdj0DhZh2e+Er1q5A==", + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/vite/-/vite-4.5.1.tgz", + "integrity": "sha512-AXXFaAJ8yebyqzoNB9fu2pHoo/nWX+xZlaRwoeYUxEqBO+Zj4msE5G+BhGBll9lYEKv9Hfks52PAF2X7qDYXQA==", "dev": true, "dependencies": { "esbuild": "^0.18.10",