From 888fbc130e67773da015bf927f62b86b788bec2b Mon Sep 17 00:00:00 2001 From: AlCalzone Date: Wed, 15 Apr 2020 05:25:17 +0200 Subject: [PATCH] [iobroker] fix up some type definitions (#43832) --- types/iobroker/index.d.ts | 45 ++++++++++++++++++-------------- types/iobroker/iobroker-tests.ts | 17 ++++++++++-- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/types/iobroker/index.d.ts b/types/iobroker/index.d.ts index 410e7b19b4..210c17d6cf 100644 --- a/types/iobroker/index.d.ts +++ b/types/iobroker/index.d.ts @@ -144,10 +144,16 @@ declare global { /** The ID of this object */ _id: string; native: Record; + /** An array of `native` properties which cannot be accessed from outside the defining adapter */ + protectedNative?: string[]; + /** Like protectedNative, but the properties are also encrypted and decrypted automatically */ + encryptedNative?: string[]; enums?: Record; type: string; // specified in the derived interfaces common: StateCommon | ChannelCommon | DeviceCommon | OtherCommon; acl?: ObjectACL; + from?: string; + ts?: number; } interface StateObject extends BaseObject { @@ -155,7 +161,7 @@ declare global { common: StateCommon; acl?: StateACL; } - interface PartialStateObject extends Partial> { + interface PartialStateObject extends Partial> { common?: Partial; acl?: Partial; } @@ -165,7 +171,7 @@ declare global { common: ChannelCommon; } interface PartialChannelObject - extends Partial> { + extends Partial> { common?: Partial; } @@ -173,7 +179,7 @@ declare global { type: 'device'; common: DeviceCommon; } - interface PartialDeviceObject extends Partial> { + interface PartialDeviceObject extends Partial> { common?: Partial; } @@ -181,7 +187,7 @@ declare global { type: 'adapter' | 'config' | 'enum' | 'group' | 'host' | 'info' | 'instance' | 'meta' | 'script' | 'user'; common: OtherCommon; } - interface PartialOtherObject extends Partial> { + interface PartialOtherObject extends Partial> { common?: Partial; } @@ -328,17 +334,16 @@ declare global { /** Parameters for adapter.getObjectView */ interface GetObjectViewParams { /** First id to include in the return list */ - startkey: string; + startkey?: string; /** Last id to include in the return list */ - endkey: string; - } - - /** Parameters for @link{Objects.getObjectList} */ - interface GetObjectListParams extends GetObjectViewParams { + endkey?: string; /** Whether docs should be included in the return list */ // TODO: What are docs? include_docs?: boolean; } + /** Parameters for adapter.getObjectList */ + type GetObjectListParams = GetObjectViewParams; + type LogLevel = 'silly' | 'debug' | 'info' | 'warn' | 'error'; interface Logger { /** log message with silly level */ @@ -1717,16 +1722,16 @@ declare global { type MessageCallback = (response?: Message) => void; - type SetObjectCallback = (err: string | null, obj: { id: string }) => void; - type GetObjectCallback = (err: string | null, obj: ioBroker.Object | null | undefined) => void; - type GetEnumCallback = (err: string | null, enums: Record, requestedEnum: string) => void; + type SetObjectCallback = (err: string | null, obj?: { id: string }) => void; + type GetObjectCallback = (err: string | null, obj?: ioBroker.Object | null) => void; + type GetEnumCallback = (err: string | null, enums?: Record, requestedEnum?: string) => void; type GetEnumsCallback = ( err: string | null, - result: { + result?: { [groupName: string]: Record; }, ) => void; - type GetObjectsCallback = (err: string | null, objects: Record) => void; + type GetObjectsCallback = (err: string | null, objects?: Record) => void; type FindObjectCallback = ( /** If an error happened, this contains the message */ @@ -1783,11 +1788,11 @@ declare global { /** Whether this is a directory or a file */ isDir: boolean; /** Access rights */ - acl: EvaluatedFileACL; + acl?: EvaluatedFileACL; /** Date of last modification */ - modifiedAt: number; + modifiedAt?: number; /** Date of creation */ - createdAt: number; + createdAt?: number; } type ReadDirCallback = (err: string | null, entries?: ReadDirResult[]) => void; type ReadFileCallback = (err: string | null, file?: Buffer | string, mimeType?: string) => void; @@ -1829,8 +1834,8 @@ declare global { interface GetObjectViewItem { /** The ID of this object */ id: string; - /** A copy of the object from the DB or some aggregation result */ - value: ioBroker.Object | unknown; // TODO: find out how the non-Object return result looks like + /** A copy of the object from the DB */ + value: ioBroker.Object | null; } type GetObjectViewCallback = (err: string | null, result?: { rows: GetObjectViewItem[] }) => void; diff --git a/types/iobroker/iobroker-tests.ts b/types/iobroker/iobroker-tests.ts index b1b005f1a2..79f4ffd9ff 100644 --- a/types/iobroker/iobroker-tests.ts +++ b/types/iobroker/iobroker-tests.ts @@ -189,8 +189,21 @@ adapter.getForeignObject("obj.id", (err, obj) => { }); adapter.getObjectAsync("obj.id").then(obj => obj && obj._id.toLowerCase()); adapter.getForeignObjectAsync("obj.id").then(obj => obj && obj._id.toLowerCase()); -adapter.getForeignObjects("*", (err, objs) => objs["foo"]._id.toLowerCase()); -adapter.getForeignObjectsAsync("*").then(objs => objs["foo"]._id.toLowerCase()); +adapter.getForeignObjects("*", (err, objs) => objs!["foo"]._id.toLowerCase()); +adapter.getForeignObjectsAsync("*").then(objs => objs!["foo"]._id.toLowerCase()); + +adapter.setObject("id", { + _id: "id", + type: "device", + common: { + name: "foo" + }, + native: {}, + protectedNative: ["none"], + encryptedNative: ["none"], + from: "me", + ts: Date.now(), +}); adapter.getObjectView("system", "admin", {startkey: "foo", endkey: "bar"}, (err, docs) => { docs && docs.rows[0] && docs.rows[0].id.toLowerCase();