@geolonia/geonicdb-sdk 0.7.1 → 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.iife.js CHANGED
@@ -32,6 +32,7 @@ var GeonicDBModule = (() => {
32
32
  NotFoundError: () => NotFoundError,
33
33
  RateLimitError: () => RateLimitError,
34
34
  ValidationError: () => ValidationError,
35
+ buildPathWithParams: () => buildPathWithParams,
35
36
  default: () => index_default
36
37
  });
37
38
 
@@ -872,6 +873,7 @@ var GeonicDBModule = (() => {
872
873
  if (options) {
873
874
  if (options.entityTypes) msg.entityTypes = options.entityTypes;
874
875
  if (options.idPattern) msg.idPattern = options.idPattern;
876
+ if (options.scopeQ) msg.scopeQ = options.scopeQ;
875
877
  }
876
878
  this._subscription = msg;
877
879
  if (this._pendingSubscription) return;
@@ -1224,14 +1226,14 @@ var GeonicDBModule = (() => {
1224
1226
  }
1225
1227
  /** Count entities matching the given filters. */
1226
1228
  async count(params) {
1227
- const parts = ["count=true", "limit=0"];
1228
- if (params?.type) parts.push("type=" + encodeURIComponent(params.type));
1229
- if (params?.q) parts.push("q=" + encodeURIComponent(params.q));
1230
- const qs = "?" + parts.join("&");
1231
- const res = await this._auth.request(
1232
- "GET",
1233
- "/ngsi-ld/v1/entities" + qs
1234
- );
1229
+ const path = buildPathWithParams("/ngsi-ld/v1/entities", {
1230
+ count: true,
1231
+ limit: 0,
1232
+ type: params?.type,
1233
+ q: params?.q,
1234
+ scopeQ: params?.scopeQ
1235
+ });
1236
+ const res = await this._auth.request("GET", path);
1235
1237
  if (!res.ok) {
1236
1238
  const e = await res.json().catch(() => ({}));
1237
1239
  throw createErrorFromResponse(res.status, e, "Count failed");
@@ -1290,24 +1292,11 @@ var GeonicDBModule = (() => {
1290
1292
  // --- Temporal API ---
1291
1293
  /** Query temporal entities. */
1292
1294
  async getTemporalEntities(params) {
1293
- let qs = "";
1294
- if (params) {
1295
- const parts = [];
1296
- if (params.type) parts.push("type=" + encodeURIComponent(params.type));
1297
- if (params.id) parts.push("id=" + encodeURIComponent(params.id));
1298
- if (params.idPattern) parts.push("idPattern=" + encodeURIComponent(params.idPattern));
1299
- if (params.attrs) parts.push("attrs=" + encodeURIComponent(params.attrs));
1300
- if (params.q) parts.push("q=" + encodeURIComponent(params.q));
1301
- if (params.timeproperty) parts.push("timeproperty=" + encodeURIComponent(params.timeproperty));
1302
- if (params.timeAt) parts.push("timeAt=" + encodeURIComponent(params.timeAt));
1303
- if (params.endTimeAt) parts.push("endTimeAt=" + encodeURIComponent(params.endTimeAt));
1304
- if (params.timerel) parts.push("timerel=" + encodeURIComponent(params.timerel));
1305
- if (params.limit != null) parts.push("limit=" + params.limit);
1306
- if (params.offset != null) parts.push("offset=" + params.offset);
1307
- if (parts.length) qs = "?" + parts.join("&");
1308
- }
1309
1295
  return this._jsonGet(
1310
- "/ngsi-ld/v1/temporal/entities" + qs,
1296
+ buildPathWithParams(
1297
+ "/ngsi-ld/v1/temporal/entities",
1298
+ params
1299
+ ),
1311
1300
  "Temporal query failed"
1312
1301
  );
1313
1302
  }
@@ -1375,6 +1364,11 @@ var GeonicDBModule = (() => {
1375
1364
  /**
1376
1365
  * Make an authenticated API request.
1377
1366
  * Automatically checks response status, parses JSON, and throws on error.
1367
+ *
1368
+ * Empty bodies (`Content-Length: 0` 含む) は Content-Type に関係なく `null`
1369
+ * を返す (#1145)。NGSI-LD の POST / DELETE / PATCH 系は仕様上ボディが
1370
+ * 空だが、サーバ実装が `Content-Type: application/ld+json` を付けて返す
1371
+ * ケースがあり、そこで `res.json()` が SyntaxError を投げて止まっていた。
1378
1372
  */
1379
1373
  async request(method, path, body) {
1380
1374
  const res = await this._auth.request(method, path, body);
@@ -1382,9 +1376,15 @@ var GeonicDBModule = (() => {
1382
1376
  const e = await res.json().catch(() => ({}));
1383
1377
  throw createErrorFromResponse(res.status, e, "Request failed: " + res.status);
1384
1378
  }
1379
+ if (res.status === 204) return null;
1380
+ if (res.headers.get("Content-Length") === "0") return null;
1385
1381
  const ct = res.headers.get("Content-Type") || "";
1386
- if (res.status === 204 || !ct) return null;
1387
- if (ct.indexOf("json") !== -1) return res.json();
1382
+ if (!ct) return null;
1383
+ if (ct.indexOf("json") !== -1) {
1384
+ const text = await res.text();
1385
+ if (!text) return null;
1386
+ return JSON.parse(text);
1387
+ }
1388
1388
  return res.text();
1389
1389
  }
1390
1390
  /**
@@ -1421,13 +1421,29 @@ var GeonicDBModule = (() => {
1421
1421
  window.GeonicDB = GeonicDB;
1422
1422
  }
1423
1423
  function buildEntitiesPath(params) {
1424
- if (!params) return "/ngsi-ld/v1/entities";
1425
- const parts = [];
1426
- if (params.type) parts.push("type=" + encodeURIComponent(params.type));
1427
- if (params.limit != null) parts.push("limit=" + params.limit);
1428
- if (params.offset != null) parts.push("offset=" + params.offset);
1429
- if (params.q) parts.push("q=" + encodeURIComponent(params.q));
1430
- return parts.length > 0 ? "/ngsi-ld/v1/entities?" + parts.join("&") : "/ngsi-ld/v1/entities";
1424
+ return buildPathWithParams(
1425
+ "/ngsi-ld/v1/entities",
1426
+ params
1427
+ );
1428
+ }
1429
+ function buildPathWithParams(path, params) {
1430
+ if (!params) return path;
1431
+ const sp = new URLSearchParams();
1432
+ for (const [key, value] of Object.entries(params)) {
1433
+ if (value === void 0 || value === null) continue;
1434
+ if (typeof value === "boolean") {
1435
+ sp.append(key, value ? "true" : "false");
1436
+ } else if (typeof value === "number") {
1437
+ sp.append(key, String(value));
1438
+ } else if (typeof value === "string") {
1439
+ if (value === "") continue;
1440
+ sp.append(key, value);
1441
+ } else {
1442
+ continue;
1443
+ }
1444
+ }
1445
+ const qs = sp.toString();
1446
+ return qs ? `${path}?${qs}` : path;
1431
1447
  }
1432
1448
  return __toCommonJS(index_exports);
1433
1449
  })();