From 6391eb64d2b65df5290cdcccc96a7b2b7f3b5b61 Mon Sep 17 00:00:00 2001 From: reluc Date: Mon, 25 Sep 2023 12:21:13 +0200 Subject: [PATCH] feat(typescript/scripting-api): use genererics to model APIs related to TD Now ExposedThing and ConsumeThing types are generic and allows as parameters of the different functions only the properties that are defined in the TD. --- typescript/scripting-api/index.d.ts | 42 ++++++++++++++--------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/typescript/scripting-api/index.d.ts b/typescript/scripting-api/index.d.ts index dfff2c55..40f122df 100644 --- a/typescript/scripting-api/index.d.ts +++ b/typescript/scripting-api/index.d.ts @@ -16,7 +16,7 @@ declare namespace WoT { * Accepts a ThingDescription and returns a ConsumedThing * @param td thing description */ - export function consume(td: ThingDescription): Promise; + export function consume(td: T): Promise>; /** * Accepts an init dictionary similar to a ThingDescription. @@ -24,7 +24,7 @@ declare namespace WoT { * * @param ptd Partial thing description */ - export function produce(init: ExposedThingInit): Promise; + export function produce(init: T): Promise>; /** @@ -104,14 +104,14 @@ declare namespace WoT { /** * The ConsumedThing interface instance represents a client API to operate a Thing. */ - export interface ConsumedThing { + export interface ConsumedThing { /** * Reads a Property value. * Takes as arguments propertyName and optionally options. * It returns a Promise that resolves with a Property value represented as an * InteractionOutput object or rejects on error. */ - readProperty(propertyName: string, options?: InteractionOptions): Promise; + readProperty(propertyName: (keyof T["properties"]) & string, options?: InteractionOptions): Promise; /** * Reads all properties of the Thing with one or multiple requests. @@ -127,14 +127,14 @@ declare namespace WoT { * It returns a Promise that resolves with a PropertyMap object that * maps keys from propertyNames to values */ - readMultipleProperties(propertyNames: string[], options?: InteractionOptions): Promise; + readMultipleProperties(propertyNames: ((keyof T["properties"]) & string)[], options?: InteractionOptions): Promise; /** * Writes a single Property. * Takes as arguments propertyName, value and optionally options. * It returns a Promise that resolves on success and rejects on failure. */ - writeProperty(propertyName: string, value: InteractionInput, options?: InteractionOptions): Promise; + writeProperty(propertyName: (keyof T["properties"]) & string, value: InteractionInput, options?: InteractionOptions): Promise; /** * Writes a multiple Property values with one request. @@ -150,26 +150,26 @@ declare namespace WoT { * It returns a Promise that resolves with the result of the Action represented * as an InteractionOutput object, or rejects with an error. */ - invokeAction(actionName: string, params?: InteractionInput, options?: InteractionOptions): Promise; + invokeAction(actionName: (keyof T["actions"]) & string, params?: InteractionInput, options?: InteractionOptions): Promise; /** * Makes a request for Property value change notifications. * Takes as arguments propertyName, listener and optionally options. * It returns a Promise that resolves on success and rejects on failure. */ - observeProperty(name: string, listener: WotListener, errorListener?: ErrorListener, options?: InteractionOptions): Promise; + observeProperty(name: (keyof T["properties"]) & string, listener: WotListener, errorListener?: ErrorListener, options?: InteractionOptions): Promise; /** * Makes a request for subscribing to Event notifications. * Takes as arguments eventName, listener and optionally options. * It returns a Promise to signal success or failure. */ - subscribeEvent(name: string, listener: WotListener, errorListener?: ErrorListener, options?: InteractionOptions): Promise; + subscribeEvent(name: (keyof T["events"]) & string, listener: WotListener, errorListener?: ErrorListener, options?: InteractionOptions): Promise; /** * Returns the the object that represents the Thing Description. */ - getThingDescription(): ThingDescription; + getThingDescription(): T; } export interface InteractionOptions { @@ -199,7 +199,7 @@ declare namespace WoT { /** * The ExposedThing interface is the server API to operate the Thing that allows defining request handlers, Property, Action, and Event interactions. **/ - export interface ExposedThing { + export interface ExposedThing { /** * Start serving external requests for the Thing, so that WoT Interactions using Properties, Actions and Events will be possible. */ @@ -216,7 +216,7 @@ declare namespace WoT { * Throws on error. * Returns a reference to the same object for supporting chaining. */ - setPropertyReadHandler(name: string, handler: PropertyReadHandler): ExposedThing; + setPropertyReadHandler(name: (keyof T["properties"]) & string, handler: PropertyReadHandler): ExposedThing; /** * Takes name as string argument and handler as argument of type PropertyWriteHandler. @@ -224,7 +224,7 @@ declare namespace WoT { * Throws on error. * Returns a reference to the same object for supporting chaining. */ - setPropertyWriteHandler(name: string, handler: PropertyWriteHandler): ExposedThing; + setPropertyWriteHandler(name: (keyof T["properties"]) & string, handler: PropertyWriteHandler): ExposedThing; /** * Takes as arguments name and handler. @@ -233,7 +233,7 @@ declare namespace WoT { * Throws on error. * Returns a reference to the same object for supporting chaining. */ - setPropertyObserveHandler(name: string, handler: PropertyReadHandler): ExposedThing; + setPropertyObserveHandler(name: (keyof T["properties"]) & string, handler: PropertyReadHandler): ExposedThing; /** * Takes as arguments name and handler. @@ -242,13 +242,13 @@ declare namespace WoT { * Throws on error. * Returns a reference to the same object for supporting chaining. */ - setPropertyUnobserveHandler(name: string, handler: PropertyReadHandler): ExposedThing; + setPropertyUnobserveHandler(name: (keyof T["properties"]) & string, handler: PropertyReadHandler): ExposedThing; /** * Takes as arguments name denoting a Property name. * Triggers emitting a notification to all observers. */ - emitPropertyChange(name: string): void; + emitPropertyChange(name: (keyof T["properties"]) & string): void; /** * Takes name as string argument and handler as argument of type ActionHandler. @@ -256,7 +256,7 @@ declare namespace WoT { * Throws on error. * Returns a reference to the same object for supporting chaining. */ - setActionHandler(name: string, handler: ActionHandler): ExposedThing; + setActionHandler(name: (keyof T["actions"]) & string, handler: ActionHandler): ExposedThing; /** * Takes as arguments name and handler. @@ -265,7 +265,7 @@ declare namespace WoT { * Throws on error. * Returns a reference to the same object for supporting chaining. */ - setEventSubscribeHandler(name: string, handler: EventSubscriptionHandler): ExposedThing; + setEventSubscribeHandler(name: (keyof T["events"]) & string, handler: EventSubscriptionHandler): ExposedThing; /** * Takes as arguments name and handler. @@ -274,18 +274,18 @@ declare namespace WoT { * Throws on error. * Returns a reference to the same object for supporting chaining. */ - setEventUnsubscribeHandler(name: string, handler: EventSubscriptionHandler): ExposedThing; + setEventUnsubscribeHandler(name: (keyof T["events"]) & string, handler: EventSubscriptionHandler): ExposedThing; /** * Takes as arguments name denoting an Event name and optionally data. * Triggers emitting the Event with optional data. */ - emitEvent(name: string, data?: InteractionInput): void; + emitEvent(name: (keyof T["events"]) & string, data?: InteractionInput): void; /** * Returns the the object that represents the Thing Description. */ - getThingDescription(): ThingDescription; + getThingDescription(): T; } export type PropertyReadHandler = (options?: InteractionOptions) => Promise;