@bigdatacloudapi/vue-reverse-geocode-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # @bigdatacloudapi/vue-reverse-geocode-client
2
+
3
+ **Vue composable for free reverse geocoding — get city, country, and locality from GPS coordinates with automatic IP geolocation fallback. No API key needed.**
4
+
5
+ [![npm](https://img.shields.io/npm/v/@bigdatacloudapi/vue-reverse-geocode-client)](https://www.npmjs.com/package/@bigdatacloudapi/vue-reverse-geocode-client)
6
+ [![license](https://img.shields.io/npm/l/@bigdatacloudapi/vue-reverse-geocode-client)](https://github.com/bigdatacloudapi/vue-reverse-geocode-client/blob/main/LICENSE)
7
+
8
+ ---
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install @bigdatacloudapi/vue-reverse-geocode-client
14
+ ```
15
+
16
+ ```bash
17
+ yarn add @bigdatacloudapi/vue-reverse-geocode-client
18
+ ```
19
+
20
+ ```bash
21
+ pnpm add @bigdatacloudapi/vue-reverse-geocode-client
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ```vue
27
+ <script setup>
28
+ import { useGeoLocation } from '@bigdatacloudapi/vue-reverse-geocode-client';
29
+
30
+ const { data, loading, error, source } = useGeoLocation();
31
+ </script>
32
+
33
+ <template>
34
+ <p v-if="loading">Detecting location...</p>
35
+ <p v-else-if="error">Error: {{ error }}</p>
36
+ <div v-else>
37
+ <h1>📍 {{ data?.city }}, {{ data?.countryName }}</h1>
38
+ <p>Detected via: {{ source }}</p>
39
+ </div>
40
+ </template>
41
+ ```
42
+
43
+ That's it. No API key, no configuration, no backend required.
44
+
45
+ ---
46
+
47
+ ## How It Works
48
+
49
+ 1. **GPS first** — requests the browser's Geolocation API (`enableHighAccuracy: true`)
50
+ 2. **IP fallback** — if the user denies GPS or it's unavailable, the API automatically geolocates by IP address
51
+ 3. **Reverse geocode** — coordinates (or IP) are resolved to a full locality via [BigDataCloud's free API](https://www.bigdatacloud.com/free-api/free-reverse-geocode-to-city-api)
52
+
53
+ No server-side proxy needed — the API is called directly from the browser.
54
+
55
+ ---
56
+
57
+ ## API
58
+
59
+ ### `useGeoLocation(options?)`
60
+
61
+ The main composable. Call it in `<script setup>` or inside `setup()`.
62
+
63
+ #### Options
64
+
65
+ | Option | Type | Default | Description |
66
+ |---|---|---|---|
67
+ | `language` | `string` | Browser language | Locality language code (e.g. `'en'`, `'de'`, `'zh'`) |
68
+ | `manual` | `boolean` | `false` | If `true`, don't fetch on mount — call `refresh()` manually |
69
+ | `timeout` | `number` | `10000` | GPS timeout in milliseconds |
70
+
71
+ #### Returns
72
+
73
+ | Property | Type | Description |
74
+ |---|---|---|
75
+ | `data` | `Ref<LocationData \| null>` | The full location response |
76
+ | `loading` | `Ref<boolean>` | `true` while fetching |
77
+ | `error` | `Ref<string \| null>` | Error message, if any |
78
+ | `source` | `Ref<'gps' \| 'ip' \| null>` | How the location was detected |
79
+ | `refresh` | `() => void` | Re-trigger the location lookup |
80
+
81
+ ### `reverseGeocode(coords?, language?)`
82
+
83
+ Standalone async function — no Vue dependency needed. Great for one-off lookups or use outside components.
84
+
85
+ ```ts
86
+ import { reverseGeocode } from '@bigdatacloudapi/vue-reverse-geocode-client';
87
+
88
+ // With coordinates
89
+ const location = await reverseGeocode({ latitude: -34.9285, longitude: 138.6007 });
90
+ console.log(location.city); // "Adelaide"
91
+
92
+ // IP-based fallback (no coordinates)
93
+ const ipLocation = await reverseGeocode();
94
+ console.log(ipLocation.countryName);
95
+ ```
96
+
97
+ #### Parameters
98
+
99
+ | Parameter | Type | Description |
100
+ |---|---|---|
101
+ | `coords` | `{ latitude: number, longitude: number }` | Optional. Omit for IP-based geolocation. |
102
+ | `language` | `string` | Optional locality language code. |
103
+
104
+ ### `LocationData`
105
+
106
+ ```ts
107
+ interface LocationData {
108
+ latitude: number;
109
+ longitude: number;
110
+ lookupSource: string;
111
+ continent: string;
112
+ continentCode: string;
113
+ countryName: string;
114
+ countryCode: string;
115
+ principalSubdivision: string;
116
+ principalSubdivisionCode: string;
117
+ city: string;
118
+ locality: string;
119
+ postcode: string;
120
+ plusCode: string;
121
+ localityInfo: {
122
+ administrative: LocalityInfoEntry[];
123
+ informative: LocalityInfoEntry[];
124
+ };
125
+ }
126
+ ```
127
+
128
+ ---
129
+
130
+ ## Examples
131
+
132
+ ### Multi-language
133
+
134
+ ```vue
135
+ <script setup>
136
+ import { useGeoLocation } from '@bigdatacloudapi/vue-reverse-geocode-client';
137
+
138
+ // Get locality names in German
139
+ const { data, loading } = useGeoLocation({ language: 'de' });
140
+ </script>
141
+ ```
142
+
143
+ ### Manual trigger
144
+
145
+ ```vue
146
+ <script setup>
147
+ import { useGeoLocation } from '@bigdatacloudapi/vue-reverse-geocode-client';
148
+
149
+ const { data, loading, refresh } = useGeoLocation({ manual: true });
150
+ </script>
151
+
152
+ <template>
153
+ <button @click="refresh" :disabled="loading">
154
+ {{ loading ? 'Detecting...' : 'Detect my location' }}
155
+ </button>
156
+ <p v-if="data">{{ data.city }}, {{ data.countryName }}</p>
157
+ </template>
158
+ ```
159
+
160
+ ### Nuxt 3
161
+
162
+ Works out of the box in Nuxt 3 — no plugins or special configuration needed. Just import and use:
163
+
164
+ ```vue
165
+ <!-- pages/index.vue -->
166
+ <script setup>
167
+ import { useGeoLocation } from '@bigdatacloudapi/vue-reverse-geocode-client';
168
+
169
+ const { data, loading, error, source } = useGeoLocation();
170
+ </script>
171
+
172
+ <template>
173
+ <div>
174
+ <p v-if="loading">Detecting location...</p>
175
+ <p v-else-if="error">{{ error }}</p>
176
+ <div v-else-if="data">
177
+ <h1>{{ data.city }}, {{ data.countryName }}</h1>
178
+ <p>{{ data.continent }} · {{ data.principalSubdivision }}</p>
179
+ <p>Source: {{ source }}</p>
180
+ </div>
181
+ </div>
182
+ </template>
183
+ ```
184
+
185
+ > **Note:** Since this composable uses `navigator.geolocation`, it only runs client-side. During SSR, `data` will be `null` and `loading` will be `false` until the component hydrates in the browser.
186
+
187
+ ### Using the standalone function
188
+
189
+ ```ts
190
+ // In a Pinia store, utility, or anywhere
191
+ import { reverseGeocode } from '@bigdatacloudapi/vue-reverse-geocode-client';
192
+
193
+ export async function getLocationLabel(): Promise<string> {
194
+ const loc = await reverseGeocode();
195
+ return `${loc.city}, ${loc.countryName}`;
196
+ }
197
+ ```
198
+
199
+ ---
200
+
201
+ ## Why BigDataCloud?
202
+
203
+ - **Free** — no API key, no sign-up, no credit card
204
+ - **Fast** — global CDN, sub-100ms responses
205
+ - **Accurate** — powered by BigDataCloud's proprietary geocoding database
206
+ - **Privacy-friendly** — no cookies, no tracking, no personal data stored
207
+
208
+ ---
209
+
210
+ ## Fair Use
211
+
212
+ This package uses BigDataCloud's free reverse geocoding API. It's intended for **light, client-side use** — perfect for showing a visitor their detected city, personalizing content, or building location-aware UIs.
213
+
214
+ For high-volume, server-side, or commercial use, please use an [API key](https://www.bigdatacloud.com/pricing).
215
+
216
+ ---
217
+
218
+ ## Need More?
219
+
220
+ BigDataCloud offers a full suite of geolocation and data APIs:
221
+
222
+ - 🌐 **[IP Geolocation API](https://www.bigdatacloud.com/ip-geolocation)** — detailed IP-to-location with confidence scores
223
+ - 🗺️ **[Server-side Reverse Geocoding](https://www.bigdatacloud.com/reverse-geocoding)** — high-volume, authenticated API
224
+ - 📦 **[@bigdatacloudapi/react-reverse-geocode-client](https://www.npmjs.com/package/@bigdatacloudapi/react-reverse-geocode-client)** — React version of this package
225
+ - 🔧 **[@bigdatacloudapi/mcp-server](https://www.npmjs.com/package/@bigdatacloudapi/mcp-server)** — MCP server for AI agents
226
+
227
+ Visit [bigdatacloud.com](https://www.bigdatacloud.com) for the full API catalog.
228
+
229
+ ---
230
+
231
+ ## License
232
+
233
+ MIT © [BigDataCloud Pty Ltd](https://www.bigdatacloud.com)
@@ -0,0 +1,65 @@
1
+ import type { Ref } from 'vue';
2
+ export interface LocalityInfoEntry {
3
+ order: number;
4
+ adminLevel: number;
5
+ name: string;
6
+ description: string;
7
+ isoName: string;
8
+ isoCode: string;
9
+ wikidataId: string;
10
+ geonameId: number;
11
+ }
12
+ export interface LocationData {
13
+ latitude: number;
14
+ longitude: number;
15
+ lookupSource: string;
16
+ continent: string;
17
+ continentCode: string;
18
+ countryName: string;
19
+ countryCode: string;
20
+ principalSubdivision: string;
21
+ principalSubdivisionCode: string;
22
+ city: string;
23
+ locality: string;
24
+ postcode: string;
25
+ plusCode: string;
26
+ localityInfo: {
27
+ administrative: LocalityInfoEntry[];
28
+ informative: LocalityInfoEntry[];
29
+ };
30
+ }
31
+ export interface UseGeoLocationOptions {
32
+ /** Language for locality names (e.g. 'en', 'de', 'zh'). Default: browser language. */
33
+ language?: string;
34
+ /** If true, don't fetch on mount — call refresh() manually. */
35
+ manual?: boolean;
36
+ /** GPS timeout in milliseconds. Default: 10000. */
37
+ timeout?: number;
38
+ }
39
+ export interface UseGeoLocationReturn {
40
+ data: Ref<LocationData | null>;
41
+ loading: Ref<boolean>;
42
+ error: Ref<string | null>;
43
+ source: Ref<'gps' | 'ip' | null>;
44
+ refresh: () => void;
45
+ }
46
+ /**
47
+ * Reverse-geocode coordinates (or fall back to IP geolocation if none provided).
48
+ *
49
+ * @param coords Optional `{ latitude, longitude }`.
50
+ * @param language Optional locality language code.
51
+ * @returns LocationData from the BigDataCloud API.
52
+ */
53
+ export declare function reverseGeocode(coords?: {
54
+ latitude: number;
55
+ longitude: number;
56
+ }, language?: string): Promise<LocationData>;
57
+ /**
58
+ * Vue composable for free reverse geocoding.
59
+ *
60
+ * Tries the browser Geolocation API first (GPS / Wi-Fi), then falls back to
61
+ * IP-based geolocation — no API key needed.
62
+ */
63
+ export declare function useGeoLocation(options?: UseGeoLocationOptions): UseGeoLocationReturn;
64
+ /** Alias for backward compatibility. */
65
+ export declare const useLocation: typeof useGeoLocation;
package/dist/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import { ref, onMounted } from 'vue';
2
+ const API_URL = 'https://api.bigdatacloud.net/data/reverse-geocode-client';
3
+ // ── Standalone function ────────────────────────────────────────────────────
4
+ /**
5
+ * Reverse-geocode coordinates (or fall back to IP geolocation if none provided).
6
+ *
7
+ * @param coords Optional `{ latitude, longitude }`.
8
+ * @param language Optional locality language code.
9
+ * @returns LocationData from the BigDataCloud API.
10
+ */
11
+ export async function reverseGeocode(coords, language) {
12
+ const params = new URLSearchParams();
13
+ if (coords) {
14
+ params.set('latitude', String(coords.latitude));
15
+ params.set('longitude', String(coords.longitude));
16
+ }
17
+ if (language) {
18
+ params.set('localityLanguage', language);
19
+ }
20
+ const url = `${API_URL}?${params.toString()}`;
21
+ const res = await fetch(url);
22
+ if (!res.ok) {
23
+ throw new Error(`Reverse geocode request failed: ${res.status} ${res.statusText}`);
24
+ }
25
+ return res.json();
26
+ }
27
+ // ── Composable ─────────────────────────────────────────────────────────────
28
+ /**
29
+ * Vue composable for free reverse geocoding.
30
+ *
31
+ * Tries the browser Geolocation API first (GPS / Wi-Fi), then falls back to
32
+ * IP-based geolocation — no API key needed.
33
+ */
34
+ export function useGeoLocation(options = {}) {
35
+ const { language, manual = false, timeout = 10000 } = options;
36
+ const data = ref(null);
37
+ const loading = ref(false);
38
+ const error = ref(null);
39
+ const source = ref(null);
40
+ async function fetchLocation(coords) {
41
+ loading.value = true;
42
+ error.value = null;
43
+ try {
44
+ const result = await reverseGeocode(coords, language);
45
+ data.value = result;
46
+ source.value = result.lookupSource === 'ip address' ? 'ip' : 'gps';
47
+ }
48
+ catch (err) {
49
+ error.value = err instanceof Error ? err.message : 'Unknown error';
50
+ }
51
+ finally {
52
+ loading.value = false;
53
+ }
54
+ }
55
+ function tryGps() {
56
+ return new Promise((resolve, reject) => {
57
+ if (typeof navigator === 'undefined' || !navigator.geolocation) {
58
+ reject(new Error('Geolocation not available'));
59
+ return;
60
+ }
61
+ navigator.geolocation.getCurrentPosition(resolve, reject, {
62
+ enableHighAccuracy: true,
63
+ timeout,
64
+ maximumAge: 0,
65
+ });
66
+ });
67
+ }
68
+ async function refresh() {
69
+ loading.value = true;
70
+ error.value = null;
71
+ try {
72
+ const position = await tryGps();
73
+ await fetchLocation({
74
+ latitude: position.coords.latitude,
75
+ longitude: position.coords.longitude,
76
+ });
77
+ }
78
+ catch {
79
+ // GPS failed or denied — fall back to IP geolocation
80
+ await fetchLocation();
81
+ }
82
+ }
83
+ onMounted(() => {
84
+ if (!manual) {
85
+ refresh();
86
+ }
87
+ });
88
+ return { data, loading, error, source, refresh };
89
+ }
90
+ /** Alias for backward compatibility. */
91
+ export const useLocation = useGeoLocation;
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@bigdatacloudapi/vue-reverse-geocode-client",
3
+ "version": "1.0.0",
4
+ "description": "Vue composable for free reverse geocoding — get city, country, and locality from GPS coordinates with automatic IP geolocation fallback. No API key needed.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc"
14
+ },
15
+ "keywords": [
16
+ "vue",
17
+ "nuxt",
18
+ "composable",
19
+ "geolocation",
20
+ "reverse-geocoding",
21
+ "free",
22
+ "no-api-key",
23
+ "bigdatacloud"
24
+ ],
25
+ "author": "BigDataCloud Pty Ltd",
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/bigdatacloudapi/vue-reverse-geocode-client"
30
+ },
31
+ "peerDependencies": {
32
+ "vue": ">=3.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^22.0.0",
36
+ "typescript": "^5.7.0",
37
+ "vue": "^3.5.0"
38
+ }
39
+ }