From 31b4a2e149ef8ad52878fb3bb17a9303799eee1b Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Fri, 30 Aug 2024 18:21:30 +0300 Subject: [PATCH] fix: Delta typing Fix Delta typing to allow meta and values in updates at the same time. --- packages/server-api/src/deltas.test.ts | 59 ++++++++++++++++++++++++++ packages/server-api/src/deltas.ts | 35 +++++++++------ packages/server-api/tsconfig.json | 3 +- 3 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 packages/server-api/src/deltas.test.ts diff --git a/packages/server-api/src/deltas.test.ts b/packages/server-api/src/deltas.test.ts new file mode 100644 index 000000000..535231765 --- /dev/null +++ b/packages/server-api/src/deltas.test.ts @@ -0,0 +1,59 @@ +import { Delta, Path } from './deltas' + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const metaDelta: Delta = { + updates: [ + { + meta: [ + { + path: 'foo.bar' as Path, + value: { + displayName: 'Foo Bar' + } + } + ] + } + ] +} + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const valuesDelta: Delta = { + updates: [ + { + values: [ + { + path: 'foo.bar' as Path, + value: { + displayName: 'Foo Bar' + } + } + ] + } + ] +} + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const combinedDelta: Delta = { + updates: [ + { + meta: [ + { + path: 'foo.bar' as Path, + value: { + displayName: 'Foo Bar' + } + } + ] + }, + { + values: [ + { + path: 'foo.bar' as Path, + value: { + displayName: 'Foo Bar' + } + } + ] + } + ] +} diff --git a/packages/server-api/src/deltas.ts b/packages/server-api/src/deltas.ts index 05466bc76..e8e5320ff 100644 --- a/packages/server-api/src/deltas.ts +++ b/packages/server-api/src/deltas.ts @@ -32,25 +32,25 @@ export interface DeltaSubscription { }> } -// "Classic" Delta with values -export interface ValuesDelta { +export interface Delta { context?: Context updates: Update[] } -export interface MetaDelta { - metas: Array<{ values: Meta[] }> -} - -// Delta Message -export type Delta = ValuesDelta | MetaDelta +/** + * @deprecated earlier mistake assumed ValuesDelta and MetaDelta were separate + */ +export type ValuesDelta = Delta +/** + * @deprecated earlier mistake assumed ValuesDelta and MetaDelta were separate + */ +export type MetaDelta = Delta -export interface Update { +export type Update = { timestamp?: Timestamp source?: Source $source?: SourceRef - values: PathValue[] -} +} & ({ values: PathValue[] } | { meta: Meta[] }) // require either values or meta or both // Update delta export interface PathValue { @@ -67,7 +67,7 @@ export interface Notification { // MetaMessage export interface Meta { - path: string + path: Path value: MetaValue } @@ -76,6 +76,17 @@ export interface MetaValue { description?: string units?: string example?: string + timeout?: number + displayName?: string + displayScale?: { + lower: number + upper: number + } + zones?: { + upper: number + lower: number + state: string + }[] } // Notification attribute types diff --git a/packages/server-api/tsconfig.json b/packages/server-api/tsconfig.json index 5023d43d6..0bad7084f 100644 --- a/packages/server-api/tsconfig.json +++ b/packages/server-api/tsconfig.json @@ -69,5 +69,6 @@ }, "ts-node": { "files": true - } + }, + "exclude": ["src/**/*.test.ts"] }