@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/README.md +37 -2
- package/geonicdb.cjs +50 -34
- package/geonicdb.cjs.map +2 -2
- package/geonicdb.d.ts +28 -1
- package/geonicdb.iife.js +50 -34
- package/geonicdb.iife.js.map +2 -2
- package/geonicdb.mjs +50 -34
- package/geonicdb.mjs.map +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,6 +33,29 @@ await db.createEntity({
|
|
|
33
33
|
});
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
### TypeScript
|
|
37
|
+
|
|
38
|
+
The SDK ships type definitions (`geonicdb.d.ts`) in the published package. Named imports of error classes and types resolve cleanly even under strict tsconfig settings (`strict`, `verbatimModuleSyntax`, etc.):
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import GeonicDB, {
|
|
42
|
+
AuthorizationError,
|
|
43
|
+
AuthenticationError,
|
|
44
|
+
type EntityEvent,
|
|
45
|
+
type RefreshedCredentials,
|
|
46
|
+
} from '@geolonia/geonicdb-sdk';
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
await db.deleteEntity(entityId);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
if (err instanceof AuthorizationError) {
|
|
52
|
+
// Type-safe branch on XACML denial.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
See **Error Handling** below for the full list of exported error classes.
|
|
58
|
+
|
|
36
59
|
### Browser (script tag)
|
|
37
60
|
|
|
38
61
|
```html
|
|
@@ -123,8 +146,20 @@ await db.createEntity({ id: 'urn:ngsi-ld:Room:001', type: 'Room', ... });
|
|
|
123
146
|
// Read
|
|
124
147
|
const entity = await db.getEntity('urn:ngsi-ld:Room:001');
|
|
125
148
|
|
|
126
|
-
// Query
|
|
127
|
-
const entities = await db.getEntities({
|
|
149
|
+
// Query (NGSI-LD §5.7.2 のクエリパラメータを網羅)
|
|
150
|
+
const entities = await db.getEntities({
|
|
151
|
+
type: 'Room',
|
|
152
|
+
limit: 10,
|
|
153
|
+
// scopeQ: '/OpenData;/OpenData/#', // exact + descendants
|
|
154
|
+
// georel: 'near;maxDistance==1000',
|
|
155
|
+
// geometry: 'Point',
|
|
156
|
+
// coordinates: '139.7,35.6',
|
|
157
|
+
// attrs: 'name,location',
|
|
158
|
+
// pick: 'name', omit: 'createdAt',
|
|
159
|
+
// lang: 'ja',
|
|
160
|
+
// orderBy: 'modifiedAt', orderDirection: 'desc',
|
|
161
|
+
// count: true,
|
|
162
|
+
});
|
|
128
163
|
|
|
129
164
|
// Update
|
|
130
165
|
await db.updateEntity('urn:ngsi-ld:Room:001', { temperature: { type: 'Property', value: 25 } });
|
package/geonicdb.cjs
CHANGED
|
@@ -31,6 +31,7 @@ __export(index_exports, {
|
|
|
31
31
|
NotFoundError: () => NotFoundError,
|
|
32
32
|
RateLimitError: () => RateLimitError,
|
|
33
33
|
ValidationError: () => ValidationError,
|
|
34
|
+
buildPathWithParams: () => buildPathWithParams,
|
|
34
35
|
default: () => index_default
|
|
35
36
|
});
|
|
36
37
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -872,6 +873,7 @@ var WebSocketManager = class {
|
|
|
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 GeonicDB = class extends EventEmitter {
|
|
|
1224
1226
|
}
|
|
1225
1227
|
/** Count entities matching the given filters. */
|
|
1226
1228
|
async count(params) {
|
|
1227
|
-
const
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
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 GeonicDB = class extends EventEmitter {
|
|
|
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
|
-
|
|
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 GeonicDB = class extends EventEmitter {
|
|
|
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 GeonicDB = class extends EventEmitter {
|
|
|
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 (
|
|
1387
|
-
if (ct.indexOf("json") !== -1)
|
|
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,12 +1421,28 @@ if (typeof window !== "undefined") {
|
|
|
1421
1421
|
window.GeonicDB = GeonicDB;
|
|
1422
1422
|
}
|
|
1423
1423
|
function buildEntitiesPath(params) {
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
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
|
//# sourceMappingURL=geonicdb.cjs.map
|