Skip to content

Commit f30182e

Browse files
committed
feat(api): add updateDeviceShadow
1 parent baf6b74 commit f30182e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './getAccountInfo.js'
77
export * from './deleteAccountDevice.js'
88
export * from './getDeviceShadow.js'
99
export * from './DeviceShadow.js'
10+
export * from './updateDeviceShadow.js'

src/api/updateDeviceShadow.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { slashless } from './slashless.js'
2+
3+
export const updateDeviceShadow =
4+
(
5+
{
6+
endpoint,
7+
apiKey,
8+
}: {
9+
endpoint: URL
10+
apiKey: string
11+
},
12+
fetchImplementation?: typeof fetch,
13+
) =>
14+
async (
15+
deviceId: string,
16+
state: {
17+
desired?: Record<string, any>
18+
reported?: Record<string, any>
19+
},
20+
): Promise<void> => {
21+
await (fetchImplementation ?? fetch)(
22+
new URL(
23+
`${slashless(endpoint)}/v1/devices/${encodeURIComponent(
24+
deviceId,
25+
)}/state`,
26+
),
27+
{
28+
method: 'PATCH',
29+
headers: {
30+
'Content-Type': 'application/json',
31+
Authorization: `Bearer ${apiKey}`,
32+
},
33+
body: JSON.stringify(state),
34+
},
35+
)
36+
}

0 commit comments

Comments
 (0)