diff --git a/.gitignore b/.gitignore index e417f7a..ad224e7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules .env coverage *.log +package-lock.json diff --git a/README.md b/README.md index 5fd76de..9c1acfd 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ const {bluetooth, destroy} = createBluetooth() ## `Bluetooth` | Method | Description | | --- | --- | -| `String[] adapters()` | List of available adapters | +| `Promise adapters()` | List of available adapters | | `Promise defaultAdapter()` | Get an available adapter | | `Promise getAdapter(String adapter)` | Get a specific adapter (one of available in `adapters()`)| @@ -148,7 +148,7 @@ const {bluetooth, destroy} = createBluetooth() | Method | Description | | --- | --- | | `Promise services()` | List of available services | -| `Promise getPrimaryService(String uuid)` | Returns a specific Primary Service | +| `Promise getPrimaryService(String uuid)` | Returns a specific Primary Service | ## `GattService` | Method | Description | @@ -166,7 +166,7 @@ const {bluetooth, destroy} = createBluetooth() | `Promise getFlags()` | Defines how the characteristic value can be used. | | `Promise isNotifying()` | True, if notifications or indications on this characteristic are currently enabled. | | `Promise readValue(Number offset = 0)` | Issues a request to read the value of the characteristic and returns the value if the operation was successful. | -| `Promise writeValue(Buffer buffer, Number offset = 0)` | Issues a request to write the value of the characteristic. | +| `Promise writeValue(Buffer buffer, Number offset = 0)` | Issues a request to write the value of the characteristic. | | `Promise startNotifications()` | Starts a notification session from this characteristic if it supports value notifications or indications. | | `Promise stopNotifications()` | This method will cancel any previous StartNotify transaction. | | `Promise toString()` | User friendly characteristic name. | diff --git a/package.json b/package.json index 8a81c17..c657307 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "author": "chrvadala", "license": "MIT", "main": "./src/index.js", + "typings": "./src/index.d.ts", "files": [ "*.md", "src", @@ -28,7 +29,8 @@ "coverage": "jest --testPathIgnorePatterns=e2e.spec.js --coverage", "test:e2e": "jest", "standard": "standard", - "ci": "npm-run-all standard coverage", + "ci": "npm-run-all standard coverage typescript", + "typescript": "tsc --strict src/index.d.ts", "build": "exit 0" }, "dependencies": { @@ -38,10 +40,12 @@ "@babel/plugin-proposal-class-properties": "^7.10.1", "@babel/plugin-proposal-decorators": "^7.10.1", "@types/jest": "^25.2.3", + "@types/node": "^14.0.13", "dotenv": "^8.2.0", "jest": "^26.0.1", "npm-run-all": "^4.1.5", - "standard": "^14.3.4" + "standard": "^14.3.4", + "typescript": "^3.9.5" }, "standard": { "ignore": [ diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..7df0863 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,81 @@ +import events = require('events'); + +declare namespace NodeBle { + interface GattCharacteristic extends events.EventEmitter { + getUUID(): Promise; + getFlags(): Promise; + isNotifying(): Promise; + readValue(offset?: number): Promise; + writeValue(buffer: Buffer, offset?: number): Promise; + startNotifications(): Promise; + stopNotifications(): Promise; + toString(): Promise; + + on(event: 'valuechanged', listener: (buffer: Buffer) => void): this; + } + + interface GattService { + isPrimary(): Promise; + getUUID(): Promise; + characteristics(): Promise; + toString(): Promise; + getCharacteristic(uuid: string): Promise; + } + + interface GattServer { + services(): Promise; + getPrimaryService(uuid: string): Promise; + } + + interface ConnectionState { + connected: boolean; + } + + interface Device extends events.EventEmitter { + getName(): Promise; + getAddress(): Promise; + getAddressType(): Promise; + getAlias(): Promise; + getRSSI(): Promise; + isPaired(): Promise; + isConnected(): Promise; + pair(): Promise; + cancelPair(): Promise; + connect(): Promise; + disconnect(): Promise; + gatt(): Promise; + toString(): Promise; + + on(event: 'connect', listener: (state: ConnectionState) => void): this; + on(event: 'disconnect', listener: (state: ConnectionState) => void): this; + } + + interface Adapter { + getAddress(): Promise; + getAddressType(): Promise; + getName(): Promise; + getAlias(): Promise; + isPowered(): Promise; + isDiscovering(): Promise; + startDiscovery(): Promise; + stopDiscovery(): Promise; + devices(): Promise; + getDevice(uuid: string): Promise; + waitDevice(uuid: string): Promise; + toString(): Promise; + } + + interface Bluetooth { + adapters(): Promise; + defaultAdapter(): Promise; + getAdapter(adapter: string): Promise; + } + + function createBluetooth(): { + destroy(): void; + bluetooth: Bluetooth; + }; +} + +export = NodeBle; +