@ailuracode/alpine-geo 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) ailuracode
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # @ailuracode/alpine-geo
2
+
3
+ Geolocation store for Alpine.js.
4
+
5
+ **[Full documentation →](../../docs/geo.md)**
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @ailuracode/alpine-geo alpinejs
11
+ ```
12
+
13
+ ## Quick example
14
+
15
+ ```js
16
+ import Alpine from "alpinejs";
17
+ import geo from "@ailuracode/alpine-geo";
18
+
19
+ Alpine.plugin(geo);
20
+ Alpine.start();
21
+ ```
22
+
23
+ ```html
24
+ <button @click="$store.geo.request()" :disabled="$store.geo.isLoading">
25
+ Get location
26
+ </button>
27
+
28
+ <p x-show="$store.geo.hasPosition">
29
+ Lat: <span x-text="$store.geo.latitude"></span>,
30
+ Lng: <span x-text="$store.geo.longitude"></span>
31
+ </p>
32
+
33
+ <p x-show="$store.geo.hasError" x-text="$store.geo.error"></p>
34
+ ```
35
+
36
+ ## API summary
37
+
38
+ | | |
39
+ |-|-|
40
+ | **Store** | `$store.geo` |
41
+ | **Actions** | `request()`, `watch()`, `unwatch()`, `reset()` |
42
+ | **Getters** | `hasPosition`, `isSupported`, `isWatching`, `isLoading`, `hasError` |
43
+
44
+ ## License
45
+
46
+ MIT
@@ -0,0 +1,39 @@
1
+ /// <reference types="@types/alpinejs" />
2
+
3
+ export interface GeoPositionOptions {
4
+ enableHighAccuracy?: boolean;
5
+ timeout?: number;
6
+ maximumAge?: number;
7
+ }
8
+
9
+ export interface GeoStore {
10
+ latitude: number | null;
11
+ longitude: number | null;
12
+ accuracy: number | null;
13
+ altitude: number | null;
14
+ altitudeAccuracy: number | null;
15
+ heading: number | null;
16
+ speed: number | null;
17
+ timestamp: number | null;
18
+ error: string | null;
19
+ errorCode: number | null;
20
+ loading: boolean;
21
+ watching: boolean;
22
+ request(options?: GeoPositionOptions): Promise<boolean>;
23
+ watch(options?: GeoPositionOptions): boolean;
24
+ unwatch(): boolean;
25
+ reset(): boolean;
26
+ readonly hasPosition: boolean;
27
+ readonly isSupported: boolean;
28
+ readonly isWatching: boolean;
29
+ readonly isLoading: boolean;
30
+ readonly hasError: boolean;
31
+ }
32
+
33
+ declare global {
34
+ namespace Alpine {
35
+ interface Stores {
36
+ geo: GeoStore;
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,41 @@
1
+ import AlpineType from 'alpinejs';
2
+
3
+ interface GeoPositionOptions {
4
+ enableHighAccuracy?: boolean;
5
+ timeout?: number;
6
+ maximumAge?: number;
7
+ }
8
+ interface GeoStore {
9
+ latitude: number | null;
10
+ longitude: number | null;
11
+ accuracy: number | null;
12
+ altitude: number | null;
13
+ altitudeAccuracy: number | null;
14
+ heading: number | null;
15
+ speed: number | null;
16
+ timestamp: number | null;
17
+ error: string | null;
18
+ errorCode: number | null;
19
+ loading: boolean;
20
+ watching: boolean;
21
+ request(options?: GeoPositionOptions): Promise<boolean>;
22
+ watch(options?: GeoPositionOptions): boolean;
23
+ unwatch(): boolean;
24
+ reset(): boolean;
25
+ readonly hasPosition: boolean;
26
+ readonly isSupported: boolean;
27
+ readonly isWatching: boolean;
28
+ readonly isLoading: boolean;
29
+ readonly hasError: boolean;
30
+ }
31
+ /** Alpine.js geolocation plugin. Registers `$store.geo`. */
32
+ declare function geoPlugin(Alpine: AlpineType.Alpine): void;
33
+ declare global {
34
+ namespace Alpine {
35
+ interface Stores {
36
+ geo: GeoStore;
37
+ }
38
+ }
39
+ }
40
+
41
+ export { type GeoPositionOptions, type GeoStore, geoPlugin as default };
package/dist/index.js ADDED
@@ -0,0 +1,127 @@
1
+ // src/index.ts
2
+ function applyPosition(store, position) {
3
+ store.latitude = position.coords.latitude;
4
+ store.longitude = position.coords.longitude;
5
+ store.accuracy = position.coords.accuracy;
6
+ store.altitude = position.coords.altitude;
7
+ store.altitudeAccuracy = position.coords.altitudeAccuracy;
8
+ store.heading = position.coords.heading;
9
+ store.speed = position.coords.speed;
10
+ store.timestamp = position.timestamp;
11
+ store.error = null;
12
+ store.errorCode = null;
13
+ }
14
+ function applyError(store, error) {
15
+ store.error = error.message;
16
+ store.errorCode = error.code;
17
+ }
18
+ function clearCoords(store) {
19
+ store.latitude = null;
20
+ store.longitude = null;
21
+ store.accuracy = null;
22
+ store.altitude = null;
23
+ store.altitudeAccuracy = null;
24
+ store.heading = null;
25
+ store.speed = null;
26
+ store.timestamp = null;
27
+ }
28
+ function clearPosition(store) {
29
+ clearCoords(store);
30
+ store.error = null;
31
+ store.errorCode = null;
32
+ }
33
+ function geoPlugin(Alpine) {
34
+ const supported = typeof navigator.geolocation?.getCurrentPosition === "function";
35
+ let watchId = null;
36
+ const geoStore = {
37
+ latitude: null,
38
+ longitude: null,
39
+ accuracy: null,
40
+ altitude: null,
41
+ altitudeAccuracy: null,
42
+ heading: null,
43
+ speed: null,
44
+ timestamp: null,
45
+ error: null,
46
+ errorCode: null,
47
+ loading: false,
48
+ watching: false,
49
+ get hasPosition() {
50
+ return this.latitude !== null && this.longitude !== null;
51
+ },
52
+ get isSupported() {
53
+ return supported;
54
+ },
55
+ get isWatching() {
56
+ return this.watching;
57
+ },
58
+ get isLoading() {
59
+ return this.loading;
60
+ },
61
+ get hasError() {
62
+ return this.error !== null;
63
+ },
64
+ request(options) {
65
+ if (!supported) {
66
+ this.error = "Geolocation is not supported";
67
+ this.errorCode = null;
68
+ return Promise.resolve(false);
69
+ }
70
+ this.loading = true;
71
+ this.error = null;
72
+ this.errorCode = null;
73
+ return new Promise((resolve) => {
74
+ navigator.geolocation.getCurrentPosition(
75
+ (position) => {
76
+ applyPosition(this, position);
77
+ this.loading = false;
78
+ resolve(true);
79
+ },
80
+ (error) => {
81
+ clearCoords(this);
82
+ applyError(this, error);
83
+ this.loading = false;
84
+ resolve(false);
85
+ },
86
+ options
87
+ );
88
+ });
89
+ },
90
+ watch(options) {
91
+ if (!supported || this.watching) {
92
+ return false;
93
+ }
94
+ this.error = null;
95
+ this.errorCode = null;
96
+ watchId = navigator.geolocation.watchPosition(
97
+ (position) => {
98
+ applyPosition(this, position);
99
+ },
100
+ (error) => {
101
+ applyError(this, error);
102
+ },
103
+ options
104
+ );
105
+ this.watching = true;
106
+ return true;
107
+ },
108
+ unwatch() {
109
+ if (watchId === null) {
110
+ return false;
111
+ }
112
+ navigator.geolocation.clearWatch(watchId);
113
+ watchId = null;
114
+ this.watching = false;
115
+ return true;
116
+ },
117
+ reset() {
118
+ clearPosition(this);
119
+ return true;
120
+ }
121
+ };
122
+ Alpine.store("geo", geoStore);
123
+ }
124
+ export {
125
+ geoPlugin as default
126
+ };
127
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport interface GeoPositionOptions {\n enableHighAccuracy?: boolean;\n timeout?: number;\n maximumAge?: number;\n}\n\nexport interface GeoStore {\n latitude: number | null;\n longitude: number | null;\n accuracy: number | null;\n altitude: number | null;\n altitudeAccuracy: number | null;\n heading: number | null;\n speed: number | null;\n timestamp: number | null;\n error: string | null;\n errorCode: number | null;\n loading: boolean;\n watching: boolean;\n request(options?: GeoPositionOptions): Promise<boolean>;\n watch(options?: GeoPositionOptions): boolean;\n unwatch(): boolean;\n reset(): boolean;\n readonly hasPosition: boolean;\n readonly isSupported: boolean;\n readonly isWatching: boolean;\n readonly isLoading: boolean;\n readonly hasError: boolean;\n}\n\nfunction applyPosition(store: GeoStore, position: GeolocationPosition): void {\n store.latitude = position.coords.latitude;\n store.longitude = position.coords.longitude;\n store.accuracy = position.coords.accuracy;\n store.altitude = position.coords.altitude;\n store.altitudeAccuracy = position.coords.altitudeAccuracy;\n store.heading = position.coords.heading;\n store.speed = position.coords.speed;\n store.timestamp = position.timestamp;\n store.error = null;\n store.errorCode = null;\n}\n\nfunction applyError(store: GeoStore, error: GeolocationPositionError): void {\n store.error = error.message;\n store.errorCode = error.code;\n}\n\nfunction clearCoords(store: GeoStore): void {\n store.latitude = null;\n store.longitude = null;\n store.accuracy = null;\n store.altitude = null;\n store.altitudeAccuracy = null;\n store.heading = null;\n store.speed = null;\n store.timestamp = null;\n}\n\nfunction clearPosition(store: GeoStore): void {\n clearCoords(store);\n store.error = null;\n store.errorCode = null;\n}\n\n/** Alpine.js geolocation plugin. Registers `$store.geo`. */\nexport default function geoPlugin(Alpine: AlpineType.Alpine): void {\n const supported = typeof navigator.geolocation?.getCurrentPosition === \"function\";\n let watchId: number | null = null;\n\n const geoStore: GeoStore = {\n latitude: null,\n longitude: null,\n accuracy: null,\n altitude: null,\n altitudeAccuracy: null,\n heading: null,\n speed: null,\n timestamp: null,\n error: null,\n errorCode: null,\n loading: false,\n watching: false,\n\n get hasPosition() {\n return this.latitude !== null && this.longitude !== null;\n },\n\n get isSupported() {\n return supported;\n },\n\n get isWatching() {\n return this.watching;\n },\n\n get isLoading() {\n return this.loading;\n },\n\n get hasError() {\n return this.error !== null;\n },\n\n request(options?: GeoPositionOptions) {\n if (!supported) {\n this.error = \"Geolocation is not supported\";\n this.errorCode = null;\n return Promise.resolve(false);\n }\n\n this.loading = true;\n this.error = null;\n this.errorCode = null;\n\n return new Promise((resolve) => {\n navigator.geolocation.getCurrentPosition(\n (position) => {\n applyPosition(this, position);\n this.loading = false;\n resolve(true);\n },\n (error) => {\n clearCoords(this);\n applyError(this, error);\n this.loading = false;\n resolve(false);\n },\n options\n );\n });\n },\n\n watch(options?: GeoPositionOptions) {\n if (!supported || this.watching) {\n return false;\n }\n\n this.error = null;\n this.errorCode = null;\n\n watchId = navigator.geolocation.watchPosition(\n (position) => {\n applyPosition(this, position);\n },\n (error) => {\n applyError(this, error);\n },\n options\n );\n\n this.watching = true;\n return true;\n },\n\n unwatch() {\n if (watchId === null) {\n return false;\n }\n\n navigator.geolocation.clearWatch(watchId);\n watchId = null;\n this.watching = false;\n return true;\n },\n\n reset() {\n clearPosition(this);\n return true;\n },\n };\n\n Alpine.store(\"geo\", geoStore);\n}\n\ndeclare global {\n namespace Alpine {\n interface Stores {\n geo: GeoStore;\n }\n }\n}\n"],"mappings":";AAgCA,SAAS,cAAc,OAAiB,UAAqC;AAC3E,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,YAAY,SAAS,OAAO;AAClC,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,mBAAmB,SAAS,OAAO;AACzC,QAAM,UAAU,SAAS,OAAO;AAChC,QAAM,QAAQ,SAAS,OAAO;AAC9B,QAAM,YAAY,SAAS;AAC3B,QAAM,QAAQ;AACd,QAAM,YAAY;AACpB;AAEA,SAAS,WAAW,OAAiB,OAAuC;AAC1E,QAAM,QAAQ,MAAM;AACpB,QAAM,YAAY,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AA,UACA,CAAC,UAAU;AACT,wBAAY,IAAI;AAChB,uBAAW,MAAM,KAAK;AACtB,iBAAK,UAAU;AACf,oBAAQ,KAAK;AAAA,UACf;AAAA,UACA;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,SAA8B;AAClC,UAAI,CAAC,aAAa,KAAK,UAAU;AAC/B,eAAO;AAAA,MACT;AAEA,WAAK,QAAQ;AACb,WAAK,YAAY;AAEjB,gBAAU,UAAU,YAAY;AAAA,QAC9B,CAAC,aAAa;AACZ,wBAAc,MAAM,QAAQ;AAAA,QAC9B;AAAA,QACA,CAAC,UAAU;AACT,qBAAW,MAAM,KAAK;AAAA,QACxB;AAAA,QACA;AAAA,MACF;AAEA,WAAK,WAAW;AAChB,aAAO;AAAA,IACT;AAAA,IAEA,UAAU;AACR,UAAI,YAAY,MAAM;AACpB,eAAO;AAAA,MACT;AAEA,gBAAU,YAAY,WAAW,OAAO;AACxC,gBAAU;AACV,WAAK,WAAW;AAChB,aAAO;AAAA,IACT;AAAA,IAEA,QAAQ;AACN,oBAAc,IAAI;AAClB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO,MAAM,OAAO,QAAQ;AAC9B;","names":[]}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@ailuracode/alpine-geo",
3
+ "version": "0.1.0",
4
+ "description": "Alpine.js geolocation store",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "ailuracode",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/ailuracode/alpine.git",
14
+ "directory": "packages/geo"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/ailuracode/alpine/issues"
18
+ },
19
+ "homepage": "https://github.com/ailuracode/alpine/tree/master/packages/geo#readme",
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./global": {
30
+ "types": "./dist/global.d.ts"
31
+ }
32
+ },
33
+ "types": "./dist/global.d.ts",
34
+ "peerDependencies": {
35
+ "alpinejs": "^3.0.0",
36
+ "@types/alpinejs": "^3.13.11"
37
+ },
38
+ "peerDependenciesMeta": {
39
+ "@types/alpinejs": {
40
+ "optional": true
41
+ }
42
+ },
43
+ "keywords": [
44
+ "alpinejs",
45
+ "alpine",
46
+ "plugin",
47
+ "geolocation",
48
+ "gps",
49
+ "location"
50
+ ],
51
+ "scripts": {
52
+ "build": "tsup src/index.ts --format esm --dts --clean --sourcemap --out-dir dist && cp src/global.d.ts dist/global.d.ts",
53
+ "test": "vitest run --config ../../vitest.config.js test"
54
+ }
55
+ }