@sinio/sdk 0.1.0 → 0.2.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 +21 -2
- package/dist/cjs/client.d.ts +9 -3
- package/dist/cjs/client.js +50 -30
- package/dist/cjs/client.js.map +1 -1
- package/dist/cjs/errors.d.ts +4 -3
- package/dist/cjs/errors.js +1 -1
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/http.d.ts +2 -2
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +10 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/serialize.d.ts +43 -5
- package/dist/cjs/serialize.js +130 -50
- package/dist/cjs/serialize.js.map +1 -1
- package/dist/cjs/types.d.ts +38 -26
- package/dist/cjs/vocabularies.d.ts +23 -0
- package/dist/cjs/vocabularies.js +39 -0
- package/dist/cjs/vocabularies.js.map +1 -0
- package/dist/esm/client.d.ts +9 -3
- package/dist/esm/client.js +51 -31
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/errors.d.ts +4 -3
- package/dist/esm/errors.js +1 -1
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/http.d.ts +2 -2
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/serialize.d.ts +43 -5
- package/dist/esm/serialize.js +127 -50
- package/dist/esm/serialize.js.map +1 -1
- package/dist/esm/types.d.ts +38 -26
- package/dist/esm/vocabularies.d.ts +23 -0
- package/dist/esm/vocabularies.js +35 -0
- package/dist/esm/vocabularies.js.map +1 -0
- package/package.json +7 -1
package/dist/esm/serialize.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SinioValidationError } from "./errors.js";
|
|
2
|
+
import { ASSIGNMENT_RELATIONS, ENTITY_KINDS, EVENT_ROLES, INGESTION_MODES, LOCATION_LEVELS } from "./vocabularies.js";
|
|
2
3
|
/** The API caps every collection at 500 items per call. */
|
|
3
4
|
export const MAX_ITEMS_PER_CALL = 500;
|
|
4
5
|
/** The most units one event may stand for. */
|
|
@@ -29,74 +30,150 @@ export function chunk(items, size) {
|
|
|
29
30
|
}
|
|
30
31
|
return chunks;
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
// Local checks, each the API's own rule: the types already say it to TypeScript callers, these say it
|
|
34
|
+
// at runtime to plain JavaScript and to values typed loosely (`as`, `any`, parsed JSON).
|
|
35
|
+
function isPlainObject(value) {
|
|
36
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
const prototype = Object.getPrototypeOf(value);
|
|
40
|
+
return prototype === Object.prototype || prototype === null;
|
|
41
|
+
}
|
|
42
|
+
function requireItem(value, field) {
|
|
43
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
44
|
+
throw new SinioValidationError(`${field} must be an object (got ${describe(value)})`);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
function requireString(value, field) {
|
|
49
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
50
|
+
throw new SinioValidationError(`${field} is required and must be a non-empty string (got ${describe(value)})`);
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
function optionalString(value, field) {
|
|
55
|
+
if (value !== undefined && typeof value !== "string") {
|
|
56
|
+
throw new SinioValidationError(`${field} must be a string (got ${describe(value)})`);
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
function optionalNonEmptyString(value, field) {
|
|
61
|
+
return value === undefined ? undefined : requireString(value, field);
|
|
62
|
+
}
|
|
63
|
+
function optionalObject(value, field) {
|
|
64
|
+
if (value !== undefined && !isPlainObject(value)) {
|
|
65
|
+
throw new SinioValidationError(`${field} must be a JSON object (got ${describe(value)})`);
|
|
35
66
|
}
|
|
36
|
-
|
|
37
|
-
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
export function requireOneOf(value, values, field) {
|
|
70
|
+
if (typeof value !== "string" || !values.includes(value)) {
|
|
71
|
+
throw new SinioValidationError(`${field} must be one of ${values.join(", ")} (got ${describe(value)})`);
|
|
38
72
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
export function requireList(value, field) {
|
|
76
|
+
if (!Array.isArray(value)) {
|
|
77
|
+
throw new SinioValidationError(`${field} must be an array (got ${describe(value)})`);
|
|
42
78
|
}
|
|
43
|
-
|
|
44
|
-
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
export function toWireMode(mode) {
|
|
82
|
+
return requireOneOf(mode, INGESTION_MODES, "mode");
|
|
83
|
+
}
|
|
84
|
+
function describe(value) {
|
|
85
|
+
if (value === undefined) {
|
|
86
|
+
return "undefined";
|
|
87
|
+
}
|
|
88
|
+
if (Array.isArray(value)) {
|
|
89
|
+
return "an array";
|
|
90
|
+
}
|
|
91
|
+
if (value instanceof Date) {
|
|
92
|
+
return "a Date";
|
|
93
|
+
}
|
|
94
|
+
return JSON.stringify(value) ?? String(value);
|
|
95
|
+
}
|
|
96
|
+
function toWireRoles(value, field) {
|
|
97
|
+
if (value === undefined) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const roles = requireItem(value, field);
|
|
101
|
+
for (const key of Object.keys(roles)) {
|
|
102
|
+
if (!EVENT_ROLES.includes(key)) {
|
|
103
|
+
throw new SinioValidationError(`${field}.${key} is not a role: roles are ${EVENT_ROLES.join(", ")}`);
|
|
104
|
+
}
|
|
45
105
|
}
|
|
46
106
|
return compact({
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
label: event.label,
|
|
52
|
-
occurred_at: toIso(event.occurredAt, `events[${index}].occurredAt`),
|
|
53
|
-
roles: event.roles === undefined ? undefined : compact({ ...event.roles }),
|
|
54
|
-
payload: event.payload,
|
|
55
|
-
count: event.count,
|
|
107
|
+
device: optionalNonEmptyString(roles["device"], `${field}.device`),
|
|
108
|
+
subject: optionalNonEmptyString(roles["subject"], `${field}.subject`),
|
|
109
|
+
location: optionalNonEmptyString(roles["location"], `${field}.location`),
|
|
110
|
+
actor: optionalNonEmptyString(roles["actor"], `${field}.actor`),
|
|
56
111
|
});
|
|
57
112
|
}
|
|
58
|
-
export function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!
|
|
63
|
-
throw new SinioValidationError(
|
|
113
|
+
export function toWireEvent(event, index) {
|
|
114
|
+
const at = `events[${index}]`;
|
|
115
|
+
const item = requireItem(event, at);
|
|
116
|
+
const count = item["count"];
|
|
117
|
+
if (count !== undefined && (!Number.isInteger(count) || count < 1 || count > MAX_EVENT_COUNT)) {
|
|
118
|
+
throw new SinioValidationError(`${at}.count must be an integer between 1 and ${MAX_EVENT_COUNT} (got ${describe(count)})`);
|
|
64
119
|
}
|
|
65
120
|
return compact({
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
121
|
+
source_event_id: requireString(item["sourceEventId"], `${at}.sourceEventId`),
|
|
122
|
+
source_version: optionalString(item["sourceVersion"], `${at}.sourceVersion`),
|
|
123
|
+
project: optionalString(item["project"], `${at}.project`),
|
|
124
|
+
type: requireString(item["type"], `${at}.type`),
|
|
125
|
+
label: optionalString(item["label"], `${at}.label`),
|
|
126
|
+
occurred_at: toIso(requireTimestamp(item["occurredAt"], `${at}.occurredAt`), `${at}.occurredAt`),
|
|
127
|
+
roles: toWireRoles(item["roles"], `${at}.roles`),
|
|
128
|
+
payload: optionalObject(item["payload"], `${at}.payload`),
|
|
129
|
+
count: count,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
export function toWireEntity(entity, index) {
|
|
133
|
+
const at = `entities[${index}]`;
|
|
134
|
+
const item = requireItem(entity, at);
|
|
135
|
+
return compact({
|
|
136
|
+
external_id: requireString(item["externalId"], `${at}.externalId`),
|
|
137
|
+
kind: requireOneOf(item["kind"], ENTITY_KINDS, `${at}.kind`),
|
|
138
|
+
subkind: optionalNonEmptyString(item["subkind"], `${at}.subkind`),
|
|
139
|
+
label: optionalString(item["label"], `${at}.label`),
|
|
140
|
+
location: optionalString(item["location"], `${at}.location`),
|
|
141
|
+
attributes: optionalObject(item["attributes"], `${at}.attributes`),
|
|
72
142
|
});
|
|
73
143
|
}
|
|
74
144
|
export function toWireLocation(location, index) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
if (!location.name) {
|
|
79
|
-
throw new SinioValidationError(`locations[${index}].name is required`);
|
|
80
|
-
}
|
|
145
|
+
const at = `locations[${index}]`;
|
|
146
|
+
const item = requireItem(location, at);
|
|
81
147
|
return compact({
|
|
82
|
-
external_id:
|
|
83
|
-
kind:
|
|
84
|
-
name:
|
|
85
|
-
parent:
|
|
148
|
+
external_id: requireString(item["externalId"], `${at}.externalId`),
|
|
149
|
+
kind: requireOneOf(item["kind"], LOCATION_LEVELS, `${at}.kind`),
|
|
150
|
+
name: requireString(item["name"], `${at}.name`),
|
|
151
|
+
parent: optionalString(item["parent"], `${at}.parent`),
|
|
86
152
|
});
|
|
87
153
|
}
|
|
88
154
|
export function toWireAssignment(assignment, index) {
|
|
89
|
-
|
|
90
|
-
|
|
155
|
+
const at = `assignments[${index}]`;
|
|
156
|
+
const item = requireItem(assignment, at);
|
|
157
|
+
const relation = item["relation"];
|
|
158
|
+
const validFrom = toIso(requireTimestamp(item["validFrom"], `${at}.validFrom`), `${at}.validFrom`);
|
|
159
|
+
const rawValidTo = item["validTo"];
|
|
160
|
+
const validTo = rawValidTo === undefined || rawValidTo === null ? undefined : toIso(rawValidTo, `${at}.validTo`);
|
|
161
|
+
// The API's `invalid_validity_period`: an assignment has to end after it starts.
|
|
162
|
+
if (validTo !== undefined && Date.parse(validTo) <= Date.parse(validFrom)) {
|
|
163
|
+
throw new SinioValidationError(`${at}.validTo must be after validFrom (got ${validFrom} -> ${validTo})`);
|
|
91
164
|
}
|
|
92
165
|
return compact({
|
|
93
|
-
credential:
|
|
94
|
-
subject:
|
|
95
|
-
relation:
|
|
96
|
-
valid_from:
|
|
97
|
-
valid_to:
|
|
98
|
-
? undefined
|
|
99
|
-
: toIso(assignment.validTo, `assignments[${index}].validTo`),
|
|
166
|
+
credential: requireString(item["credential"], `${at}.credential`),
|
|
167
|
+
subject: requireString(item["subject"], `${at}.subject`),
|
|
168
|
+
relation: relation === undefined ? undefined : requireOneOf(relation, ASSIGNMENT_RELATIONS, `${at}.relation`),
|
|
169
|
+
valid_from: validFrom,
|
|
170
|
+
valid_to: validTo,
|
|
100
171
|
});
|
|
101
172
|
}
|
|
173
|
+
function requireTimestamp(value, field) {
|
|
174
|
+
if (value === undefined || value === null || value === "") {
|
|
175
|
+
throw new SinioValidationError(`${field} is required`);
|
|
176
|
+
}
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
102
179
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../src/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../src/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAcnD,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEtH,2DAA2D;AAC3D,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAEtC,8CAA8C;AAC9C,MAAM,CAAC,MAAM,eAAe,GAAG,SAAS,CAAC;AAEzC,kGAAkG;AAClG,sGAAsG;AACtG,MAAM,eAAe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EAAE,KAAa;IACrD,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;QAC1D,MAAM,IAAI,oBAAoB,CAAC,GAAG,KAAK,cAAc,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,KAAkB,CAAC;AAC5B,CAAC"}
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
export type IngestionMode
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { AssignmentRelation, EntityKind, EventRole, IngestionMode, LocationLevel, RejectionCode } from "./vocabularies.ts";
|
|
2
|
+
export type { AssignmentRelation, EntityKind, EventRole, IngestionMode, LocationLevel, RejectionCode };
|
|
3
|
+
/**
|
|
4
|
+
* A rejection code as read from a response: one of the known `RejectionCode`s (autocompleted), or
|
|
5
|
+
* any other string the API may add later, kept as is.
|
|
6
|
+
*/
|
|
7
|
+
export type RejectionCodeValue = RejectionCode | (string & {});
|
|
8
|
+
/** A JSON object: `payload` and `attributes` are never an array or a scalar. */
|
|
9
|
+
export type JsonObject = Readonly<Record<string, unknown>>;
|
|
7
10
|
/** An ISO 8601 timestamp *with* an offset, or a `Date`. */
|
|
8
11
|
export type Timestamp = string | Date;
|
|
9
12
|
/**
|
|
10
|
-
* The four structural roles the derivation engine resolves, each a raw
|
|
13
|
+
* The four structural roles the derivation engine resolves, each a raw, non-empty
|
|
11
14
|
* identifier from the source. Anything else (ticket number, firmware, ...)
|
|
12
15
|
* belongs in `payload`. The API refuses any other key.
|
|
13
16
|
*/
|
|
14
|
-
export
|
|
15
|
-
readonly
|
|
16
|
-
|
|
17
|
-
readonly location?: string;
|
|
18
|
-
readonly actor?: string;
|
|
19
|
-
}
|
|
17
|
+
export type EventRoles = {
|
|
18
|
+
readonly [role in EventRole]?: string;
|
|
19
|
+
};
|
|
20
20
|
export interface SinioEvent {
|
|
21
21
|
/** Identity at the source, stable over time. Part of the idempotency key. */
|
|
22
22
|
readonly sourceEventId: string;
|
|
23
23
|
/** Source-side version, kept for tracing: it plays no part in deduplication. */
|
|
24
24
|
readonly sourceVersion?: string;
|
|
25
|
-
/** Project `external_id`.
|
|
25
|
+
/** Project `external_id`. Only for organization-wide keys (issued on request), which cover several projects; a project key already carries its project. */
|
|
26
26
|
readonly project?: string;
|
|
27
27
|
/** `code` of an event type in the catalog. */
|
|
28
28
|
readonly type: string;
|
|
@@ -31,18 +31,20 @@ export interface SinioEvent {
|
|
|
31
31
|
readonly occurredAt: Timestamp;
|
|
32
32
|
readonly roles?: EventRoles;
|
|
33
33
|
/** Free-form, optional (`{}` when omitted); the readings (battery, signal, ...) are read from it as its source declares them. */
|
|
34
|
-
readonly payload?:
|
|
34
|
+
readonly payload?: JsonObject;
|
|
35
35
|
/** How many units this one event stands for (a button pressed ten times). An integer from 1 to 1,000,000; omitted = 1. */
|
|
36
36
|
readonly count?: number;
|
|
37
37
|
}
|
|
38
38
|
export interface EntityInput {
|
|
39
39
|
readonly externalId: string;
|
|
40
40
|
readonly kind: EntityKind;
|
|
41
|
+
/** Code of the entity's subkind; never empty when given. An unknown code is created undeclared. */
|
|
41
42
|
readonly subkind?: string;
|
|
42
43
|
readonly label?: string;
|
|
43
44
|
/** External id of the location the entity sits in. */
|
|
44
45
|
readonly location?: string;
|
|
45
|
-
|
|
46
|
+
/** Free-form JSON object. */
|
|
47
|
+
readonly attributes?: JsonObject;
|
|
46
48
|
}
|
|
47
49
|
export interface LocationInput {
|
|
48
50
|
readonly externalId: string;
|
|
@@ -56,24 +58,27 @@ export interface AssignmentInput {
|
|
|
56
58
|
readonly credential: string;
|
|
57
59
|
/** External id of the subject the credential is assigned to. */
|
|
58
60
|
readonly subject: string;
|
|
59
|
-
|
|
61
|
+
/** Defaults to `assigned_to`, the only relation today. */
|
|
62
|
+
readonly relation?: AssignmentRelation;
|
|
63
|
+
/** ISO 8601 with offset, or a `Date`. */
|
|
60
64
|
readonly validFrom: Timestamp;
|
|
65
|
+
/** Open-ended when omitted or `null`; otherwise strictly after `validFrom`. */
|
|
61
66
|
readonly validTo?: Timestamp | null;
|
|
62
67
|
}
|
|
63
68
|
export interface EventRejection {
|
|
64
69
|
readonly sourceEventId: string;
|
|
65
|
-
readonly code:
|
|
70
|
+
readonly code: RejectionCodeValue;
|
|
66
71
|
readonly message: string;
|
|
67
72
|
}
|
|
68
73
|
export interface ReferenceRejection {
|
|
69
74
|
readonly externalId: string;
|
|
70
|
-
readonly code:
|
|
75
|
+
readonly code: RejectionCodeValue;
|
|
71
76
|
readonly message: string;
|
|
72
77
|
}
|
|
73
78
|
export interface AssignmentRejection {
|
|
74
79
|
readonly credential: string;
|
|
75
80
|
readonly subject: string;
|
|
76
|
-
readonly code:
|
|
81
|
+
readonly code: RejectionCodeValue;
|
|
77
82
|
readonly message: string;
|
|
78
83
|
}
|
|
79
84
|
/**
|
|
@@ -82,33 +87,40 @@ export interface AssignmentRejection {
|
|
|
82
87
|
*/
|
|
83
88
|
export interface SendEventsResult {
|
|
84
89
|
/** Server-side batch ids, one per HTTP call the SDK made (batches are chunked at 500). */
|
|
85
|
-
readonly batchIds: string[];
|
|
90
|
+
readonly batchIds: readonly string[];
|
|
86
91
|
readonly accepted: number;
|
|
87
92
|
readonly pendingClassification: number;
|
|
88
93
|
readonly duplicates: number;
|
|
89
94
|
readonly revisions: number;
|
|
90
95
|
readonly rejected: number;
|
|
91
|
-
readonly rejections: EventRejection[];
|
|
96
|
+
readonly rejections: readonly EventRejection[];
|
|
92
97
|
/** The key is in test mode: its events are checked and kept apart, never used for detection. */
|
|
93
98
|
readonly testMode: boolean;
|
|
94
99
|
}
|
|
95
100
|
export interface UpsertResult {
|
|
96
101
|
readonly accepted: number;
|
|
97
102
|
readonly rejected: number;
|
|
98
|
-
readonly rejections: ReferenceRejection[];
|
|
103
|
+
readonly rejections: readonly ReferenceRejection[];
|
|
99
104
|
}
|
|
100
105
|
export interface AssignmentsResult {
|
|
101
106
|
readonly accepted: number;
|
|
102
107
|
readonly rejected: number;
|
|
103
|
-
readonly rejections: AssignmentRejection[];
|
|
108
|
+
readonly rejections: readonly AssignmentRejection[];
|
|
104
109
|
}
|
|
105
110
|
export interface SendEventsOptions {
|
|
106
111
|
/** Defaults to `webhook`. */
|
|
107
112
|
readonly mode?: IngestionMode;
|
|
108
|
-
/**
|
|
113
|
+
/**
|
|
114
|
+
* Overrides the client-level source code for this call. Only needed for keys not bound to a
|
|
115
|
+
* single source (organization-wide keys, issued on request).
|
|
116
|
+
*/
|
|
109
117
|
readonly sourceCode?: string;
|
|
110
118
|
}
|
|
111
|
-
/**
|
|
119
|
+
/**
|
|
120
|
+
* Project override, only for organization-wide API keys (issued on request) covering several
|
|
121
|
+
* projects. A project key already carries its project.
|
|
122
|
+
*/
|
|
112
123
|
export interface ProjectOptions {
|
|
124
|
+
/** Project `external_id`. */
|
|
113
125
|
readonly project?: string;
|
|
114
126
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Ingestion modes. `backfill` persists history without triggering alerts. */
|
|
2
|
+
export declare const INGESTION_MODES: readonly ["webhook", "backfill"];
|
|
3
|
+
export type IngestionMode = (typeof INGESTION_MODES)[number];
|
|
4
|
+
/** Kinds accepted by `POST /v1/entities`. */
|
|
5
|
+
export declare const ENTITY_KINDS: readonly ["device", "subject", "asset", "actor", "credential"];
|
|
6
|
+
export type EntityKind = (typeof ENTITY_KINDS)[number];
|
|
7
|
+
/** Levels accepted by `POST /v1/locations`. */
|
|
8
|
+
export declare const LOCATION_LEVELS: readonly ["site", "zone", "point"];
|
|
9
|
+
export type LocationLevel = (typeof LOCATION_LEVELS)[number];
|
|
10
|
+
/** Relations accepted by `POST /v1/assignments`; `assigned_to` when omitted. */
|
|
11
|
+
export declare const ASSIGNMENT_RELATIONS: readonly ["assigned_to"];
|
|
12
|
+
export type AssignmentRelation = (typeof ASSIGNMENT_RELATIONS)[number];
|
|
13
|
+
/** The four structural roles an event may carry in `roles`. */
|
|
14
|
+
export declare const EVENT_ROLES: readonly ["device", "subject", "location", "actor"];
|
|
15
|
+
export type EventRole = (typeof EVENT_ROLES)[number];
|
|
16
|
+
/**
|
|
17
|
+
* Codes the API returns in `rejections[].code` today. The list may grow (additive changes): a
|
|
18
|
+
* response carrying a code missing here is passed through unchanged, never refused.
|
|
19
|
+
*/
|
|
20
|
+
export declare const REJECTION_CODES: readonly ["payload_too_large", "unknown_project", "unknown_source", "archived_source", "source_mismatch", "ambiguous_source", "invalid_occurred_at", "missing_required_field", "unknown_location", "unknown_parent_location", "overlapping_assignment", "invalid_validity_period"];
|
|
21
|
+
export type RejectionCode = (typeof REJECTION_CODES)[number];
|
|
22
|
+
/** Whether a rejection code is one this SDK version knows, narrowing it to `RejectionCode`. */
|
|
23
|
+
export declare function isKnownRejectionCode(code: string): code is RejectionCode;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// The closed vocabularies of the ingestion API, each defined once: the value list (used by the local
|
|
2
|
+
// checks) and the type derived from it. They mirror the API contract exactly.
|
|
3
|
+
/** Ingestion modes. `backfill` persists history without triggering alerts. */
|
|
4
|
+
export const INGESTION_MODES = ["webhook", "backfill"];
|
|
5
|
+
/** Kinds accepted by `POST /v1/entities`. */
|
|
6
|
+
export const ENTITY_KINDS = ["device", "subject", "asset", "actor", "credential"];
|
|
7
|
+
/** Levels accepted by `POST /v1/locations`. */
|
|
8
|
+
export const LOCATION_LEVELS = ["site", "zone", "point"];
|
|
9
|
+
/** Relations accepted by `POST /v1/assignments`; `assigned_to` when omitted. */
|
|
10
|
+
export const ASSIGNMENT_RELATIONS = ["assigned_to"];
|
|
11
|
+
/** The four structural roles an event may carry in `roles`. */
|
|
12
|
+
export const EVENT_ROLES = ["device", "subject", "location", "actor"];
|
|
13
|
+
/**
|
|
14
|
+
* Codes the API returns in `rejections[].code` today. The list may grow (additive changes): a
|
|
15
|
+
* response carrying a code missing here is passed through unchanged, never refused.
|
|
16
|
+
*/
|
|
17
|
+
export const REJECTION_CODES = [
|
|
18
|
+
"payload_too_large",
|
|
19
|
+
"unknown_project",
|
|
20
|
+
"unknown_source",
|
|
21
|
+
"archived_source",
|
|
22
|
+
"source_mismatch",
|
|
23
|
+
"ambiguous_source",
|
|
24
|
+
"invalid_occurred_at",
|
|
25
|
+
"missing_required_field",
|
|
26
|
+
"unknown_location",
|
|
27
|
+
"unknown_parent_location",
|
|
28
|
+
"overlapping_assignment",
|
|
29
|
+
"invalid_validity_period",
|
|
30
|
+
];
|
|
31
|
+
/** Whether a rejection code is one this SDK version knows, narrowing it to `RejectionCode`. */
|
|
32
|
+
export function isKnownRejectionCode(code) {
|
|
33
|
+
return REJECTION_CODES.includes(code);
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=vocabularies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocabularies.js","sourceRoot":"","sources":["../../src/vocabularies.ts"],"names":[],"mappings":"AAAA,qGAAqG;AACrG,8EAA8E;AAE9E,8EAA8E;AAC9E,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,SAAS,EAAE,UAAU,CAAU,CAAC;AAGhE,6CAA6C;AAC7C,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,CAAU,CAAC;AAG3F,+CAA+C;AAC/C,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAU,CAAC;AAGlE,gFAAgF;AAChF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,aAAa,CAAU,CAAC;AAG7D,+DAA+D;AAC/D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAQ,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,CAAU,CAAC;AAG/E;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,mBAAmB;IACnB,iBAAiB;IACjB,gBAAgB;IAChB,iBAAiB;IACjB,iBAAiB;IACjB,kBAAkB;IAClB,qBAAqB;IACrB,wBAAwB;IACxB,kBAAkB;IAClB,yBAAyB;IACzB,wBAAwB;IACxB,yBAAyB;CACjB,CAAC;AAGX,+FAA+F;AAC/F,MAAM,UAAU,oBAAoB,CAAC,IAAY;IAC/C,OAAQ,eAAqC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AAC/D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinio/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Official Node.js SDK for the sinio.ai ingestion API (events, entities, locations, assignments).",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"homepage": "https://docs.sinio.ai/sdks",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/sinio-ai/sinio-node.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": "https://github.com/sinio-ai/sinio-node/issues",
|
|
6
12
|
"type": "module",
|
|
7
13
|
"main": "./dist/cjs/index.js",
|
|
8
14
|
"types": "./dist/esm/index.d.ts",
|