@geolonia/geonicdb-sdk 0.7.0 → 0.8.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/geonicdb.mjs CHANGED
@@ -839,6 +839,7 @@ var WebSocketManager = class {
839
839
  if (options) {
840
840
  if (options.entityTypes) msg.entityTypes = options.entityTypes;
841
841
  if (options.idPattern) msg.idPattern = options.idPattern;
842
+ if (options.scopeQ) msg.scopeQ = options.scopeQ;
842
843
  }
843
844
  this._subscription = msg;
844
845
  if (this._pendingSubscription) return;
@@ -1191,14 +1192,14 @@ var GeonicDB = class extends EventEmitter {
1191
1192
  }
1192
1193
  /** Count entities matching the given filters. */
1193
1194
  async count(params) {
1194
- const parts = ["count=true", "limit=0"];
1195
- if (params?.type) parts.push("type=" + encodeURIComponent(params.type));
1196
- if (params?.q) parts.push("q=" + encodeURIComponent(params.q));
1197
- const qs = "?" + parts.join("&");
1198
- const res = await this._auth.request(
1199
- "GET",
1200
- "/ngsi-ld/v1/entities" + qs
1201
- );
1195
+ const path = buildPathWithParams("/ngsi-ld/v1/entities", {
1196
+ count: true,
1197
+ limit: 0,
1198
+ type: params?.type,
1199
+ q: params?.q,
1200
+ scopeQ: params?.scopeQ
1201
+ });
1202
+ const res = await this._auth.request("GET", path);
1202
1203
  if (!res.ok) {
1203
1204
  const e = await res.json().catch(() => ({}));
1204
1205
  throw createErrorFromResponse(res.status, e, "Count failed");
@@ -1257,24 +1258,11 @@ var GeonicDB = class extends EventEmitter {
1257
1258
  // --- Temporal API ---
1258
1259
  /** Query temporal entities. */
1259
1260
  async getTemporalEntities(params) {
1260
- let qs = "";
1261
- if (params) {
1262
- const parts = [];
1263
- if (params.type) parts.push("type=" + encodeURIComponent(params.type));
1264
- if (params.id) parts.push("id=" + encodeURIComponent(params.id));
1265
- if (params.idPattern) parts.push("idPattern=" + encodeURIComponent(params.idPattern));
1266
- if (params.attrs) parts.push("attrs=" + encodeURIComponent(params.attrs));
1267
- if (params.q) parts.push("q=" + encodeURIComponent(params.q));
1268
- if (params.timeproperty) parts.push("timeproperty=" + encodeURIComponent(params.timeproperty));
1269
- if (params.timeAt) parts.push("timeAt=" + encodeURIComponent(params.timeAt));
1270
- if (params.endTimeAt) parts.push("endTimeAt=" + encodeURIComponent(params.endTimeAt));
1271
- if (params.timerel) parts.push("timerel=" + encodeURIComponent(params.timerel));
1272
- if (params.limit != null) parts.push("limit=" + params.limit);
1273
- if (params.offset != null) parts.push("offset=" + params.offset);
1274
- if (parts.length) qs = "?" + parts.join("&");
1275
- }
1276
1261
  return this._jsonGet(
1277
- "/ngsi-ld/v1/temporal/entities" + qs,
1262
+ buildPathWithParams(
1263
+ "/ngsi-ld/v1/temporal/entities",
1264
+ params
1265
+ ),
1278
1266
  "Temporal query failed"
1279
1267
  );
1280
1268
  }
@@ -1342,6 +1330,11 @@ var GeonicDB = class extends EventEmitter {
1342
1330
  /**
1343
1331
  * Make an authenticated API request.
1344
1332
  * Automatically checks response status, parses JSON, and throws on error.
1333
+ *
1334
+ * Empty bodies (`Content-Length: 0` 含む) は Content-Type に関係なく `null`
1335
+ * を返す (#1145)。NGSI-LD の POST / DELETE / PATCH 系は仕様上ボディが
1336
+ * 空だが、サーバ実装が `Content-Type: application/ld+json` を付けて返す
1337
+ * ケースがあり、そこで `res.json()` が SyntaxError を投げて止まっていた。
1345
1338
  */
1346
1339
  async request(method, path, body) {
1347
1340
  const res = await this._auth.request(method, path, body);
@@ -1349,9 +1342,15 @@ var GeonicDB = class extends EventEmitter {
1349
1342
  const e = await res.json().catch(() => ({}));
1350
1343
  throw createErrorFromResponse(res.status, e, "Request failed: " + res.status);
1351
1344
  }
1345
+ if (res.status === 204) return null;
1346
+ if (res.headers.get("Content-Length") === "0") return null;
1352
1347
  const ct = res.headers.get("Content-Type") || "";
1353
- if (res.status === 204 || !ct) return null;
1354
- if (ct.indexOf("json") !== -1) return res.json();
1348
+ if (!ct) return null;
1349
+ if (ct.indexOf("json") !== -1) {
1350
+ const text = await res.text();
1351
+ if (!text) return null;
1352
+ return JSON.parse(text);
1353
+ }
1355
1354
  return res.text();
1356
1355
  }
1357
1356
  /**
@@ -1388,13 +1387,29 @@ if (typeof window !== "undefined") {
1388
1387
  window.GeonicDB = GeonicDB;
1389
1388
  }
1390
1389
  function buildEntitiesPath(params) {
1391
- if (!params) return "/ngsi-ld/v1/entities";
1392
- const parts = [];
1393
- if (params.type) parts.push("type=" + encodeURIComponent(params.type));
1394
- if (params.limit != null) parts.push("limit=" + params.limit);
1395
- if (params.offset != null) parts.push("offset=" + params.offset);
1396
- if (params.q) parts.push("q=" + encodeURIComponent(params.q));
1397
- return parts.length > 0 ? "/ngsi-ld/v1/entities?" + parts.join("&") : "/ngsi-ld/v1/entities";
1390
+ return buildPathWithParams(
1391
+ "/ngsi-ld/v1/entities",
1392
+ params
1393
+ );
1394
+ }
1395
+ function buildPathWithParams(path, params) {
1396
+ if (!params) return path;
1397
+ const sp = new URLSearchParams();
1398
+ for (const [key, value] of Object.entries(params)) {
1399
+ if (value === void 0 || value === null) continue;
1400
+ if (typeof value === "boolean") {
1401
+ sp.append(key, value ? "true" : "false");
1402
+ } else if (typeof value === "number") {
1403
+ sp.append(key, String(value));
1404
+ } else if (typeof value === "string") {
1405
+ if (value === "") continue;
1406
+ sp.append(key, value);
1407
+ } else {
1408
+ continue;
1409
+ }
1410
+ }
1411
+ const qs = sp.toString();
1412
+ return qs ? `${path}?${qs}` : path;
1398
1413
  }
1399
1414
  export {
1400
1415
  AuthenticationError,
@@ -1406,6 +1421,7 @@ export {
1406
1421
  NotFoundError,
1407
1422
  RateLimitError,
1408
1423
  ValidationError,
1424
+ buildPathWithParams,
1409
1425
  index_default as default
1410
1426
  };
1411
1427
  //# sourceMappingURL=geonicdb.mjs.map