yoto-nodejs-client 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -40
- package/bin/auth.js +4 -3
- package/bin/device-model.js +25 -5
- package/bin/device-tui.js +25 -3
- package/bin/devices.js +25 -9
- package/bin/lib/cli-helpers.d.ts.map +1 -1
- package/bin/lib/cli-helpers.js +3 -1
- package/bin/lib/token-helpers.d.ts +4 -2
- package/bin/lib/token-helpers.d.ts.map +1 -1
- package/bin/lib/token-helpers.js +9 -8
- package/bin/refresh-token.js +4 -2
- package/bin/token-info.js +2 -2
- package/lib/api-client.d.ts +11 -10
- package/lib/api-client.d.ts.map +1 -1
- package/lib/api-client.js +12 -14
- package/lib/api-endpoints/auth.test.js +4 -4
- package/lib/api-endpoints/content.test.js +32 -32
- package/lib/api-endpoints/devices.d.ts +4 -4
- package/lib/api-endpoints/devices.js +2 -2
- package/lib/api-endpoints/devices.test.js +45 -45
- package/lib/api-endpoints/endpoint-test-helpers.d.ts +3 -4
- package/lib/api-endpoints/endpoint-test-helpers.d.ts.map +1 -1
- package/lib/api-endpoints/endpoint-test-helpers.js +21 -5
- package/lib/api-endpoints/family-library-groups.d.ts +3 -3
- package/lib/api-endpoints/family-library-groups.d.ts.map +1 -1
- package/lib/api-endpoints/family-library-groups.js +3 -3
- package/lib/api-endpoints/family-library-groups.test.js +29 -29
- package/lib/api-endpoints/family.test.js +11 -11
- package/lib/api-endpoints/icons.test.js +14 -14
- package/lib/mqtt/client.d.ts +123 -48
- package/lib/mqtt/client.d.ts.map +1 -1
- package/lib/mqtt/client.js +131 -49
- package/lib/mqtt/factory.d.ts +12 -5
- package/lib/mqtt/factory.d.ts.map +1 -1
- package/lib/mqtt/factory.js +39 -11
- package/lib/mqtt/index.js +2 -1
- package/lib/mqtt/mqtt.test.js +25 -22
- package/lib/test-helpers/device-model-test-helpers.d.ts +29 -0
- package/lib/test-helpers/device-model-test-helpers.d.ts.map +1 -0
- package/lib/test-helpers/device-model-test-helpers.js +116 -0
- package/lib/token.d.ts +44 -2
- package/lib/token.d.ts.map +1 -1
- package/lib/token.js +142 -2
- package/lib/yoto-account.d.ts +339 -9
- package/lib/yoto-account.d.ts.map +1 -1
- package/lib/yoto-account.js +411 -39
- package/lib/yoto-account.test.js +139 -0
- package/lib/yoto-device.d.ts +418 -30
- package/lib/yoto-device.d.ts.map +1 -1
- package/lib/yoto-device.js +670 -104
- package/lib/yoto-device.test.js +88 -0
- package/package.json +1 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/** @import { YotoDevice } from './api-endpoints/devices.js' */
|
|
2
|
+
|
|
3
|
+
import test from 'node:test'
|
|
4
|
+
import assert from 'node:assert/strict'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { YotoClient } from './api-client.js'
|
|
7
|
+
import { YotoDeviceModel } from './yoto-device.js'
|
|
8
|
+
import { loadTestTokens } from './api-endpoints/endpoint-test-helpers.js'
|
|
9
|
+
import { saveTokensToEnv } from '../bin/lib/token-helpers.js'
|
|
10
|
+
import {
|
|
11
|
+
assertConfigShape,
|
|
12
|
+
assertPlaybackShape,
|
|
13
|
+
toLower,
|
|
14
|
+
waitForModelReady
|
|
15
|
+
} from './test-helpers/device-model-test-helpers.js'
|
|
16
|
+
|
|
17
|
+
const envPath = join(import.meta.dirname, '..', '.env')
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @returns {YotoClient}
|
|
21
|
+
*/
|
|
22
|
+
function createTestClient () {
|
|
23
|
+
const clientId = process.env['YOTO_CLIENT_ID']
|
|
24
|
+
const refreshToken = process.env['YOTO_REFRESH_TOKEN']
|
|
25
|
+
const accessToken = process.env['YOTO_ACCESS_TOKEN']
|
|
26
|
+
|
|
27
|
+
assert.ok(clientId, 'YOTO_CLIENT_ID is required')
|
|
28
|
+
assert.ok(refreshToken, 'YOTO_REFRESH_TOKEN is required')
|
|
29
|
+
assert.ok(accessToken, 'YOTO_ACCESS_TOKEN is required')
|
|
30
|
+
|
|
31
|
+
return new YotoClient({
|
|
32
|
+
clientId,
|
|
33
|
+
refreshToken,
|
|
34
|
+
accessToken,
|
|
35
|
+
onTokenRefresh: async (tokens) => {
|
|
36
|
+
const { resolvedPath } = await saveTokensToEnv(envPath, {
|
|
37
|
+
access_token: tokens.updatedAccessToken,
|
|
38
|
+
refresh_token: tokens.updatedRefreshToken,
|
|
39
|
+
token_type: 'Bearer',
|
|
40
|
+
expires_in: tokens.updatedExpiresAt - Math.floor(Date.now() / 1000)
|
|
41
|
+
}, tokens.clientId)
|
|
42
|
+
console.log(`Token Refreshed: ${resolvedPath}`)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {YotoClient} client
|
|
49
|
+
* @param {YotoDevice} device
|
|
50
|
+
* @returns {Promise<void>}
|
|
51
|
+
*/
|
|
52
|
+
async function assertDeviceModel (client, device) {
|
|
53
|
+
const model = new YotoDeviceModel(client, device)
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
await waitForModelReady(model)
|
|
57
|
+
|
|
58
|
+
assertConfigShape(model.config)
|
|
59
|
+
assertPlaybackShape(model.playback)
|
|
60
|
+
|
|
61
|
+
// TODO: Add mutation coverage (updateConfig, startCard, pauseCard, etc.)
|
|
62
|
+
} finally {
|
|
63
|
+
await model.stop()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
test('YotoDeviceModel - online devices', async (t) => {
|
|
68
|
+
loadTestTokens()
|
|
69
|
+
const client = createTestClient()
|
|
70
|
+
const response = await client.getDevices()
|
|
71
|
+
|
|
72
|
+
const onlineMini = response.devices.find(device =>
|
|
73
|
+
device.online && (toLower(device.deviceFamily) === 'mini' || toLower(device.deviceType).includes('mini'))
|
|
74
|
+
)
|
|
75
|
+
const onlineV3 = response.devices.find(device =>
|
|
76
|
+
device.online && (toLower(device.deviceFamily) === 'v3' || toLower(device.deviceType).includes('v3'))
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
await t.test('online mini', { skip: !onlineMini }, async () => {
|
|
80
|
+
assert.ok(onlineMini, 'No online mini device found')
|
|
81
|
+
await assertDeviceModel(client, onlineMini)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
await t.test('online v3', { skip: !onlineV3 }, async () => {
|
|
85
|
+
assert.ok(onlineV3, 'No online v3 device found')
|
|
86
|
+
await assertDeviceModel(client, onlineV3)
|
|
87
|
+
})
|
|
88
|
+
})
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yoto-nodejs-client",
|
|
3
3
|
"description": "(Unofficial) Node.js client for the Yoto API with automatic token refresh, MQTT device communication, and TypeScript support",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.7",
|
|
5
5
|
"author": "Bret Comnes <bcomnes@gmail.com> (https://bret.io)",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/bcomnes/yoto-nodejs-client/issues"
|