diff --git a/package-lock.json b/package-lock.json index ef83275022a286d6a3bc899a08073a8d6df283cf..473422b1034c87cabb4164eb4b63c06b926258db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "usp-js", - "version": "0.0.7", + "version": "0.0.11", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 49db70fd11a915fc2e15d68cf8f7e4e0f90f1686..0aab3397bfedd0f240564e087f57c1c903900823 100644 --- a/package.json +++ b/package.json @@ -25,5 +25,5 @@ "typescript": "^4.0.5" }, "type": "commonjs", - "types": "build/src/index.d.ts" + "types": "build/src/types.d.ts" } diff --git a/src/index.ts b/src/index.ts index a5532e18dd2762f9d4cae132e12e51e17734f89f..b879b147b8be869686b670a8c246dbf4654809ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import use from "./protocol"; import config from "./config.json"; import { makeRouter, timer, loopTimer } from "./lib"; +import { JSType, USPMessage, ErrorMessage, PromiseResult, RejectFn, SendFn, JSObject, OperateOptions, OperateFunction, OperateCleanupFunction, ConnectOptions, ConnectEvents, Device, Role } from "./types"; /** * Add dot to the end of a string if it does not have one @@ -237,7 +238,7 @@ const connect = async ( // Initialize connection await protocol .init() - .then(({ timeout }) => { + .then(({ timeout }: { timeout: number }) => { stopTimer(); if (events && events.onTimeout) restartTimeout = loopTimer(timeout, () => { diff --git a/src/lib.ts b/src/lib.ts index d85f9aa8952c1f8c5e03a88d60b6fa1ed4bffd95..a445bf32398616a93eeebe923b39a9cc546de84b 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1,3 +1,5 @@ +import { Router, PromiseResult } from "./types"; + /** * Creates a timer that exectues the callback unless the returned function is used */ diff --git a/src/protocol/index.ts b/src/protocol/index.ts index 376a45259660d9b6c264ec2809f79164e8ef5590..fadb3458ff725533407bc1f373008b9a550c7bd6 100644 --- a/src/protocol/index.ts +++ b/src/protocol/index.ts @@ -1,4 +1,5 @@ import mqttAsync from "async-mqtt"; +import { AddMessage, BaseMessage, CommandType, ConnectOptions, DeleteMessage, EncodeArgs, ErrorMessage, GetMessage, InitResult, JSObject, MessageType, MQTTRequest, NotifyMessage, Protocol, ProtocolEvents, SetMessage, USPMessage } from "../types"; import * as messages from "./js-usp-protobuf/protoMessage"; import { search, searchAll, unflatten, unwrapArray, unwrapObject } from "./lib"; diff --git a/src/types.d.ts b/src/types.ts similarity index 75% rename from src/types.d.ts rename to src/types.ts index a360532194a39f30b0125d8dede281dbd009b3ca..b8556d45024bbd6bf097c9616d82b1ca52e1d959 100644 --- a/src/types.d.ts +++ b/src/types.ts @@ -1,6 +1,7 @@ // import { IClientOptions } from "async-mqtt"; -declare type IClientOptions = import("async-mqtt").IClientOptions; -interface ConnectOptions extends IClientOptions { +import { IClientOptions } from "async-mqtt"; + +export interface ConnectOptions extends IClientOptions { toId: string; fromId: string; publishEndpoint?: string; @@ -8,11 +9,11 @@ interface ConnectOptions extends IClientOptions { } /** Stores promise resolve and reject responses */ -type PromiseResult = { resolve; reject }; +export type PromiseResult = { resolve; reject }; /** Routes messages */ -type Router = Map<string, PromiseResult>; +export type Router = Map<string, PromiseResult>; /** Send Function */ -type SendFn = ( +export type SendFn = ( command: CommandType, args: Partial<EncodeArgs> ) => Promise<USPMessage>; @@ -20,12 +21,12 @@ type SendFn = ( /** * Connection lifecycle event handlers */ -interface ConnectEvents { +export interface ConnectEvents { /** Handler for idle timeout */ onTimeout?: () => void; } /** Device API */ -interface Device { +export interface Device { /** * Get value at path * @param path Location of value (e.g. "Device.DeviceInfo.") @@ -93,7 +94,7 @@ interface Device { * await usp.get("Device.WiFi.Radio.1.").then(device.resolve) * ``` */ - resolve: (msg: JSType, level: number = 1) => Promise<JSType>; + resolve: (msg: JSType, level: number) => Promise<JSType>; /** * Disconenct from device @@ -111,7 +112,7 @@ interface Device { } /** Options for Operate Subscription */ -interface OperateOptions { +export interface OperateOptions { /** Subscription ID (optional) */ ID?: string; /** Should subscription remain through sessions (optional) */ @@ -119,12 +120,12 @@ interface OperateOptions { } /** Executes a command */ -type OperateFunction = (input?: JSObject) => Promise<JSType>; +export type OperateFunction = (input?: JSObject) => Promise<JSType>; /** Cleans up command subscription */ -type OperateCleanupFunction = () => Promise<void>; +export type OperateCleanupFunction = () => Promise<void>; /** Arguments for encoding commands */ -interface EncodeArgs { +export interface EncodeArgs { path: string; paths: string[]; value: JSType; @@ -134,64 +135,64 @@ interface EncodeArgs { } /** Session role */ -type Role = "admin" | "user" | "none"; +export type Role = "admin" | "user" | "none"; /** Representation of JS object */ -type JSObject = { [key: string]: JSType }; +export type JSObject = { [key: string]: JSType }; /** USP Commands */ -type CommandType = "get" | "set" | "operate" | "add" | "delete" | "notify"; +export type CommandType = "get" | "set" | "operate" | "add" | "delete" | "notify"; /** Message types */ -type MessageType = CommandType | "error"; +export type MessageType = CommandType | "error"; /** Represents js types */ -type JSType = number | boolean | string | JSObject; +export type JSType = number | boolean | string | JSObject; /** User error message */ -interface DeviceError { +export interface DeviceError { type: "error"; reason: string; path?: string; } /** Base Message passed between protocol and api */ -interface BaseMessage { +export interface BaseMessage { id: string; type: MessageType; path: string; data: {}; } /** Error message */ -interface ErrorMessage extends BaseMessage { +export interface ErrorMessage extends BaseMessage { type: "error"; data: { reason: string; }; } /** Get Message */ -interface GetMessage extends BaseMessage { +export interface GetMessage extends BaseMessage { type: "get"; data: JSType; } /** Set Message */ -interface SetMessage extends BaseMessage { +export interface SetMessage extends BaseMessage { type: "set"; data: { updated: true; }; } /** Operate Message */ -interface OperateMessage extends BaseMessage { +export interface OperateMessage extends BaseMessage { type: "operate"; - data: JSValue; + data: JSType; } /** Notify Message */ -interface NotifyMessage extends BaseMessage { +export interface NotifyMessage extends BaseMessage { type: "notify"; - data: JSValue; + data: JSType; } /** Add Message */ -interface AddMessage extends BaseMessage { +export interface AddMessage extends BaseMessage { type: "add"; data: string; } /** Delete Message */ -interface DeleteMessage extends BaseMessage { +export interface DeleteMessage extends BaseMessage { type: "delete"; data: { deleted: true; @@ -199,7 +200,7 @@ interface DeleteMessage extends BaseMessage { } /** Union of Messages */ -type USPMessage = +export type USPMessage = | GetMessage | SetMessage | OperateMessage @@ -209,19 +210,19 @@ type USPMessage = | ErrorMessage; /** Protocol event handlers */ -interface ProtocolArgs { +export interface ProtocolArgs { onMessage: (msg: USPMessage) => void; onError: (err: any) => void; } /** Results of initialiazing protocol */ -interface InitResult { +export interface InitResult { /** Session timeout in seconds */ timeout: number; } /** Protocol interface */ -interface Protocol { +export interface Protocol { /** Initialize protocol */ init: () => Promise<InitResult>; /** Close connection */ @@ -236,17 +237,17 @@ interface Protocol { } /** Protocol event handlers */ -interface ProtocolEvents { +export interface ProtocolEvents { /** Handler for message event */ onMessage: (resp: USPMessage) => void; } /** Mqtt message structure */ -interface MQTTRequest { +export interface MQTTRequest { /** Message id */ id: string; /** Message data */ data: any; } -type RejectFn = (reason?: any) => void \ No newline at end of file +export type RejectFn = (reason?: any) => void \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 980f171f501e31f4fe2de90b1f75349f240ecc01..a79a2283b3ce7e9c3e6725df253346d05ac72c0b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ // "rootDir": "src", "strict": true, - "noImplicitAny": true, + "noImplicitAny": false, "esModuleInterop": true, "resolveJsonModule": true, @@ -17,7 +17,7 @@ "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "sourceMap": true, - "declaration": true + "declaration": true, }, "compileOnSave": true, "typedocOptions": {