geo-engine-node 1.1.0 → 1.1.2

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/dist/index.cjs ADDED
@@ -0,0 +1,115 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // index.js
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ default: () => index_default
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+ var DEFAULTS = {
26
+ managementUrl: "https://api.geoengine.dev",
27
+ ingestUrl: "https://ingest.geoengine.dev",
28
+ timeout: 1e4
29
+ };
30
+ var GeoEngine = class {
31
+ /**
32
+ * Inicializa el cliente de Geo-Engine.
33
+ * @param {string} apiKey - Tu API Key.
34
+ * @param {Object} [options] - Configuración opcional.
35
+ */
36
+ constructor(apiKey, options = {}) {
37
+ if (!apiKey) {
38
+ throw new Error("GeoEngine: API Key es requerida.");
39
+ }
40
+ this.apiKey = apiKey;
41
+ this.config = { ...DEFAULTS, ...options };
42
+ this.userAgent = "GeoEngineNode/1.1.2";
43
+ }
44
+ /**
45
+ * Helper privado para hacer peticiones con timeout
46
+ */
47
+ async _request(url, method, body) {
48
+ const controller = new AbortController();
49
+ const id = setTimeout(() => controller.abort(), this.config.timeout);
50
+ try {
51
+ const response = await fetch(url, {
52
+ method,
53
+ headers: {
54
+ "Content-Type": "application/json",
55
+ "X-API-Key": this.apiKey,
56
+ "User-Agent": this.userAgent
57
+ },
58
+ body: JSON.stringify(body),
59
+ signal: controller.signal
60
+ });
61
+ clearTimeout(id);
62
+ if (!response.ok) {
63
+ const errorText = await response.text();
64
+ throw new Error(`GeoEngine API Error (${response.status}): ${errorText}`);
65
+ }
66
+ if (response.status === 204) return null;
67
+ return await response.json();
68
+ } catch (error) {
69
+ clearTimeout(id);
70
+ if (error.name === "AbortError") {
71
+ throw new Error(`GeoEngine: La petici\xF3n excedi\xF3 el tiempo l\xEDmite de ${this.config.timeout}ms`);
72
+ }
73
+ throw error;
74
+ }
75
+ }
76
+ /**
77
+ * Envía una ubicación al motor de ingestión.
78
+ */
79
+ async sendLocation(deviceId, lat, lng) {
80
+ if (!deviceId || lat === void 0 || lng === void 0) {
81
+ throw new Error("GeoEngine: deviceId, lat y lng son obligatorios.");
82
+ }
83
+ const payload = {
84
+ device_id: deviceId,
85
+ latitude: parseFloat(lat),
86
+ longitude: parseFloat(lng),
87
+ timestamp: Math.floor(Date.now() / 1e3)
88
+ };
89
+ return this._request(`${this.config.ingestUrl}/ingest`, "POST", payload);
90
+ }
91
+ /**
92
+ * Crea una nueva geocerca.
93
+ */
94
+ async createGeofence(name, coordinates, webhookUrl) {
95
+ if (!name || !coordinates || coordinates.length < 3) {
96
+ throw new Error("GeoEngine: Se requiere un nombre y al menos 3 coordenadas.");
97
+ }
98
+ const polygon = coordinates.map((p) => [p[1], p[0]]);
99
+ const first = polygon[0];
100
+ const last = polygon[polygon.length - 1];
101
+ if (first[0] !== last[0] || first[1] !== last[1]) {
102
+ polygon.push(first);
103
+ }
104
+ const payload = {
105
+ name,
106
+ webhook_url: webhookUrl,
107
+ geojson: {
108
+ type: "Polygon",
109
+ coordinates: [polygon]
110
+ }
111
+ };
112
+ return this._request(`${this.config.managementUrl}/geofences`, "POST", payload);
113
+ }
114
+ };
115
+ var index_default = GeoEngine;
@@ -0,0 +1,110 @@
1
+ const DEFAULTS = {
2
+ managementUrl: 'https://api.geoengine.dev',
3
+ ingestUrl: 'https://ingest.geoengine.dev',
4
+ timeout: 10000
5
+ };
6
+
7
+ class GeoEngine {
8
+ /**
9
+ * Inicializa el cliente de Geo-Engine.
10
+ * @param {string} apiKey - Tu API Key.
11
+ * @param {Object} [options] - Configuración opcional.
12
+ */
13
+ constructor(apiKey, options = {}) {
14
+ if (!apiKey) {
15
+ throw new Error('GeoEngine: API Key es requerida.');
16
+ }
17
+
18
+ this.apiKey = apiKey;
19
+ this.config = { ...DEFAULTS, ...options };
20
+ this.userAgent = 'GeoEngineNode/1.1.2';
21
+ }
22
+
23
+ /**
24
+ * Helper privado para hacer peticiones con timeout
25
+ */
26
+ async _request(url, method, body) {
27
+ const controller = new AbortController();
28
+ const id = setTimeout(() => controller.abort(), this.config.timeout);
29
+
30
+ try {
31
+ const response = await fetch(url, {
32
+ method: method,
33
+ headers: {
34
+ 'Content-Type': 'application/json',
35
+ 'X-API-Key': this.apiKey,
36
+ 'User-Agent': this.userAgent
37
+ },
38
+ body: JSON.stringify(body),
39
+ signal: controller.signal
40
+ });
41
+
42
+ clearTimeout(id);
43
+
44
+ if (!response.ok) {
45
+ const errorText = await response.text();
46
+ throw new Error(`GeoEngine API Error (${response.status}): ${errorText}`);
47
+ }
48
+
49
+ if (response.status === 204) return null;
50
+
51
+ return await response.json();
52
+ } catch (error) {
53
+ clearTimeout(id);
54
+ if (error.name === 'AbortError') {
55
+ throw new Error(`GeoEngine: La petición excedió el tiempo límite de ${this.config.timeout}ms`);
56
+ }
57
+ throw error;
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Envía una ubicación al motor de ingestión.
63
+ */
64
+ async sendLocation(deviceId, lat, lng) {
65
+ if (!deviceId || lat === undefined || lng === undefined) {
66
+ throw new Error("GeoEngine: deviceId, lat y lng son obligatorios.");
67
+ }
68
+
69
+ const payload = {
70
+ device_id: deviceId,
71
+ latitude: parseFloat(lat),
72
+ longitude: parseFloat(lng),
73
+ timestamp: Math.floor(Date.now() / 1000)
74
+ };
75
+
76
+ return this._request(`${this.config.ingestUrl}/ingest`, 'POST', payload);
77
+ }
78
+
79
+ /**
80
+ * Crea una nueva geocerca.
81
+ */
82
+ async createGeofence(name, coordinates, webhookUrl) {
83
+ if (!name || !coordinates || coordinates.length < 3) {
84
+ throw new Error("GeoEngine: Se requiere un nombre y al menos 3 coordenadas.");
85
+ }
86
+
87
+ // Convertir formato simple [[lat,lng]] a GeoJSON [Lng, Lat]
88
+ const polygon = coordinates.map(p => [p[1], p[0]]);
89
+
90
+ // Cerrar polígono automáticamente
91
+ const first = polygon[0];
92
+ const last = polygon[polygon.length - 1];
93
+ if (first[0] !== last[0] || first[1] !== last[1]) {
94
+ polygon.push(first);
95
+ }
96
+
97
+ const payload = {
98
+ name,
99
+ webhook_url: webhookUrl,
100
+ geojson: {
101
+ type: 'Polygon',
102
+ coordinates: [polygon]
103
+ }
104
+ };
105
+
106
+ return this._request(`${this.config.managementUrl}/geofences`, 'POST', payload);
107
+ }
108
+ }
109
+
110
+ export { GeoEngine as default };
@@ -0,0 +1,110 @@
1
+ const DEFAULTS = {
2
+ managementUrl: 'https://api.geoengine.dev',
3
+ ingestUrl: 'https://ingest.geoengine.dev',
4
+ timeout: 10000
5
+ };
6
+
7
+ class GeoEngine {
8
+ /**
9
+ * Inicializa el cliente de Geo-Engine.
10
+ * @param {string} apiKey - Tu API Key.
11
+ * @param {Object} [options] - Configuración opcional.
12
+ */
13
+ constructor(apiKey, options = {}) {
14
+ if (!apiKey) {
15
+ throw new Error('GeoEngine: API Key es requerida.');
16
+ }
17
+
18
+ this.apiKey = apiKey;
19
+ this.config = { ...DEFAULTS, ...options };
20
+ this.userAgent = 'GeoEngineNode/1.1.2';
21
+ }
22
+
23
+ /**
24
+ * Helper privado para hacer peticiones con timeout
25
+ */
26
+ async _request(url, method, body) {
27
+ const controller = new AbortController();
28
+ const id = setTimeout(() => controller.abort(), this.config.timeout);
29
+
30
+ try {
31
+ const response = await fetch(url, {
32
+ method: method,
33
+ headers: {
34
+ 'Content-Type': 'application/json',
35
+ 'X-API-Key': this.apiKey,
36
+ 'User-Agent': this.userAgent
37
+ },
38
+ body: JSON.stringify(body),
39
+ signal: controller.signal
40
+ });
41
+
42
+ clearTimeout(id);
43
+
44
+ if (!response.ok) {
45
+ const errorText = await response.text();
46
+ throw new Error(`GeoEngine API Error (${response.status}): ${errorText}`);
47
+ }
48
+
49
+ if (response.status === 204) return null;
50
+
51
+ return await response.json();
52
+ } catch (error) {
53
+ clearTimeout(id);
54
+ if (error.name === 'AbortError') {
55
+ throw new Error(`GeoEngine: La petición excedió el tiempo límite de ${this.config.timeout}ms`);
56
+ }
57
+ throw error;
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Envía una ubicación al motor de ingestión.
63
+ */
64
+ async sendLocation(deviceId, lat, lng) {
65
+ if (!deviceId || lat === undefined || lng === undefined) {
66
+ throw new Error("GeoEngine: deviceId, lat y lng son obligatorios.");
67
+ }
68
+
69
+ const payload = {
70
+ device_id: deviceId,
71
+ latitude: parseFloat(lat),
72
+ longitude: parseFloat(lng),
73
+ timestamp: Math.floor(Date.now() / 1000)
74
+ };
75
+
76
+ return this._request(`${this.config.ingestUrl}/ingest`, 'POST', payload);
77
+ }
78
+
79
+ /**
80
+ * Crea una nueva geocerca.
81
+ */
82
+ async createGeofence(name, coordinates, webhookUrl) {
83
+ if (!name || !coordinates || coordinates.length < 3) {
84
+ throw new Error("GeoEngine: Se requiere un nombre y al menos 3 coordenadas.");
85
+ }
86
+
87
+ // Convertir formato simple [[lat,lng]] a GeoJSON [Lng, Lat]
88
+ const polygon = coordinates.map(p => [p[1], p[0]]);
89
+
90
+ // Cerrar polígono automáticamente
91
+ const first = polygon[0];
92
+ const last = polygon[polygon.length - 1];
93
+ if (first[0] !== last[0] || first[1] !== last[1]) {
94
+ polygon.push(first);
95
+ }
96
+
97
+ const payload = {
98
+ name,
99
+ webhook_url: webhookUrl,
100
+ geojson: {
101
+ type: 'Polygon',
102
+ coordinates: [polygon]
103
+ }
104
+ };
105
+
106
+ return this._request(`${this.config.managementUrl}/geofences`, 'POST', payload);
107
+ }
108
+ }
109
+
110
+ export { GeoEngine as default };
package/dist/index.js ADDED
@@ -0,0 +1,95 @@
1
+ // index.js
2
+ var DEFAULTS = {
3
+ managementUrl: "https://api.geoengine.dev",
4
+ ingestUrl: "https://ingest.geoengine.dev",
5
+ timeout: 1e4
6
+ };
7
+ var GeoEngine = class {
8
+ /**
9
+ * Inicializa el cliente de Geo-Engine.
10
+ * @param {string} apiKey - Tu API Key.
11
+ * @param {Object} [options] - Configuración opcional.
12
+ */
13
+ constructor(apiKey, options = {}) {
14
+ if (!apiKey) {
15
+ throw new Error("GeoEngine: API Key es requerida.");
16
+ }
17
+ this.apiKey = apiKey;
18
+ this.config = { ...DEFAULTS, ...options };
19
+ this.userAgent = "GeoEngineNode/1.1.2";
20
+ }
21
+ /**
22
+ * Helper privado para hacer peticiones con timeout
23
+ */
24
+ async _request(url, method, body) {
25
+ const controller = new AbortController();
26
+ const id = setTimeout(() => controller.abort(), this.config.timeout);
27
+ try {
28
+ const response = await fetch(url, {
29
+ method,
30
+ headers: {
31
+ "Content-Type": "application/json",
32
+ "X-API-Key": this.apiKey,
33
+ "User-Agent": this.userAgent
34
+ },
35
+ body: JSON.stringify(body),
36
+ signal: controller.signal
37
+ });
38
+ clearTimeout(id);
39
+ if (!response.ok) {
40
+ const errorText = await response.text();
41
+ throw new Error(`GeoEngine API Error (${response.status}): ${errorText}`);
42
+ }
43
+ if (response.status === 204) return null;
44
+ return await response.json();
45
+ } catch (error) {
46
+ clearTimeout(id);
47
+ if (error.name === "AbortError") {
48
+ throw new Error(`GeoEngine: La petici\xF3n excedi\xF3 el tiempo l\xEDmite de ${this.config.timeout}ms`);
49
+ }
50
+ throw error;
51
+ }
52
+ }
53
+ /**
54
+ * Envía una ubicación al motor de ingestión.
55
+ */
56
+ async sendLocation(deviceId, lat, lng) {
57
+ if (!deviceId || lat === void 0 || lng === void 0) {
58
+ throw new Error("GeoEngine: deviceId, lat y lng son obligatorios.");
59
+ }
60
+ const payload = {
61
+ device_id: deviceId,
62
+ latitude: parseFloat(lat),
63
+ longitude: parseFloat(lng),
64
+ timestamp: Math.floor(Date.now() / 1e3)
65
+ };
66
+ return this._request(`${this.config.ingestUrl}/ingest`, "POST", payload);
67
+ }
68
+ /**
69
+ * Crea una nueva geocerca.
70
+ */
71
+ async createGeofence(name, coordinates, webhookUrl) {
72
+ if (!name || !coordinates || coordinates.length < 3) {
73
+ throw new Error("GeoEngine: Se requiere un nombre y al menos 3 coordenadas.");
74
+ }
75
+ const polygon = coordinates.map((p) => [p[1], p[0]]);
76
+ const first = polygon[0];
77
+ const last = polygon[polygon.length - 1];
78
+ if (first[0] !== last[0] || first[1] !== last[1]) {
79
+ polygon.push(first);
80
+ }
81
+ const payload = {
82
+ name,
83
+ webhook_url: webhookUrl,
84
+ geojson: {
85
+ type: "Polygon",
86
+ coordinates: [polygon]
87
+ }
88
+ };
89
+ return this._request(`${this.config.managementUrl}/geofences`, "POST", payload);
90
+ }
91
+ };
92
+ var index_default = GeoEngine;
93
+ export {
94
+ index_default as default
95
+ };
package/index.js CHANGED
@@ -17,7 +17,7 @@ class GeoEngine {
17
17
 
18
18
  this.apiKey = apiKey;
19
19
  this.config = { ...DEFAULTS, ...options };
20
- this.userAgent = 'GeoEngineNode/1.0.0';
20
+ this.userAgent = 'GeoEngineNode/1.1.2';
21
21
  }
22
22
 
23
23
  /**
@@ -42,22 +42,13 @@ class GeoEngine {
42
42
  clearTimeout(id);
43
43
 
44
44
  if (!response.ok) {
45
- // Intentar leer el error en JSON, si falla, leer texto
46
- let errorData;
47
- try {
48
- errorData = await response.json();
49
- } catch (e) {
50
- errorData = { error: await response.text() };
51
- }
52
-
53
- throw new Error(`GeoEngine API Error (${response.status}): ${errorData.error || JSON.stringify(errorData)}`);
45
+ const errorText = await response.text();
46
+ throw new Error(`GeoEngine API Error (${response.status}): ${errorText}`);
54
47
  }
55
48
 
56
- // Si la respuesta es 204 No Content o vacía, devolver null
57
49
  if (response.status === 204) return null;
58
50
 
59
51
  return await response.json();
60
-
61
52
  } catch (error) {
62
53
  clearTimeout(id);
63
54
  if (error.name === 'AbortError') {
package/package.json CHANGED
@@ -1,14 +1,24 @@
1
1
  {
2
2
  "name": "geo-engine-node",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "type": "module",
5
- "description": "SDK oficial para Geo-Engine. Rastreo de activos y gestión de geocercas.",
6
- "main": "index.js",
5
+ "description": "SDK oficial para Geo-Engine...",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
7
16
  "scripts": {
8
17
  "test": "echo \"Error: no test specified\" && exit 1",
9
18
  "example": "node examples/simulation.js",
10
19
  "release:patch": "npm version patch && npm publish --access public",
11
- "release:minor": "npm version minor && npm publish --access public"
20
+ "release:minor": "npm version minor && npm publish --access public",
21
+ "build": "tsup index.js --format cjs,esm --dts --clean"
12
22
  },
13
23
  "keywords": [
14
24
  "geofencing",
@@ -18,8 +28,11 @@
18
28
  ],
19
29
  "author": "Geo-Engine Team",
20
30
  "license": "MIT",
21
- "dependencies": {},
22
31
  "engines": {
23
32
  "node": ">=18.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "tsup": "^8.5.1",
36
+ "typescript": "^5.9.3"
24
37
  }
25
- }
38
+ }