@superdurable/connector-aqara 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +6 -2
- package/dex-connector.json +65 -0
- package/dist/chunk-PQNVHYKW.js +141 -0
- package/dist/connection-D6DWLUrD.d.ts +133 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.js +286 -0
- package/dist/mock.d.ts +35 -0
- package/dist/mock.js +85 -0
- package/package.json +59 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Super Durable
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Aqara TypeScript connector
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@superdurable/connector-aqara` provides the read-only `listDevices` Dex Step. It reads one bounded page of device identity and online state through Aqara's mainland China [v3 device API](https://opendoc.aqara.com/docs/%E4%BA%91%E5%BC%80%E5%8F%91/API%E6%96%87%E6%A1%A3/%E8%AE%BE%E5%A4%87%E7%AE%A1%E7%90%86%E6%8E%A5%E5%8F%A3.html).
|
|
4
|
+
|
|
5
|
+
Use the [runnable Flow and Worker](examples/device-list/) with a static `aqara` connection. In Dex Web Connections, create an approved project at [Aqara Developer Platform](https://developer.aqara.com), then enter its AppID, KeyID, AppKey, and authorized access token. The AppKey and token remain private and are resolved for each request. The connector signs requests using the documented [Aqara signature rules](https://opendoc.aqara.com/en/docs/developmanual/apiIntroduction/signGenerationRules.html). The endpoint, timeout, and response limit have manifest defaults.
|
|
6
|
+
|
|
7
|
+
The checked-in conformance scenarios replay Go-recorded requests, signatures, and outcomes. The integration test exercises a real Dex Worker and Client with a local provider that returns 503 before succeeding. Real Aqara authorization, token refresh, and device discovery remain unverified without a user-controlled account and approved project.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"connectorId": "aqara",
|
|
3
|
+
"releaseTag": "connectors/aqara/go/v0.1.0",
|
|
4
|
+
"package": {
|
|
5
|
+
"ecosystem": "npm",
|
|
6
|
+
"name": "@superdurable/connector-aqara",
|
|
7
|
+
"version": "0.1.0"
|
|
8
|
+
},
|
|
9
|
+
"factories": [
|
|
10
|
+
{
|
|
11
|
+
"name": "newListDevicesStep",
|
|
12
|
+
"kind": "query",
|
|
13
|
+
"operationId": "listDevices",
|
|
14
|
+
"inputType": "ListDevicesInput",
|
|
15
|
+
"description": "Read one bounded page of devices and online state for the authorized Aqara account or project.",
|
|
16
|
+
"fields": {
|
|
17
|
+
"stepType": "stepType",
|
|
18
|
+
"annotations": "annotations",
|
|
19
|
+
"connectionName": "connectionName",
|
|
20
|
+
"resultAttribute": "resultAttribute"
|
|
21
|
+
},
|
|
22
|
+
"branches": [
|
|
23
|
+
{
|
|
24
|
+
"id": "listed",
|
|
25
|
+
"field": "listed",
|
|
26
|
+
"optional": false
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"id": "providerRejected",
|
|
30
|
+
"field": "providerRejected",
|
|
31
|
+
"optional": true
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"id": "invalidResponse",
|
|
35
|
+
"field": "invalidResponse",
|
|
36
|
+
"optional": true
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"id": "defect",
|
|
40
|
+
"field": "defect",
|
|
41
|
+
"optional": true
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
"inputSchema": {
|
|
45
|
+
"additionalProperties": false,
|
|
46
|
+
"description": "ListDevicesInput selects one bounded page from the authorized account's devices.",
|
|
47
|
+
"properties": {
|
|
48
|
+
"pageNum": {
|
|
49
|
+
"description": "PageNum is a one-based page number; zero selects page one.",
|
|
50
|
+
"type": "integer"
|
|
51
|
+
},
|
|
52
|
+
"pageSize": {
|
|
53
|
+
"description": "PageSize is a count from 1 to 100; zero selects Aqara's documented default of 50.",
|
|
54
|
+
"type": "integer"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"required": [
|
|
58
|
+
"pageNum",
|
|
59
|
+
"pageSize"
|
|
60
|
+
],
|
|
61
|
+
"type": "object"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// src/generated/models.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { SecretString } from "@superdurable/connectors-sdk";
|
|
4
|
+
var ConfigSchema = z.object({
|
|
5
|
+
"baseUrl": z.string().optional(),
|
|
6
|
+
"timeoutMs": z.number().int().optional(),
|
|
7
|
+
"maxResponseBytes": z.number().int().optional()
|
|
8
|
+
});
|
|
9
|
+
var CredentialMaterialError = class extends TypeError {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "CredentialMaterialError";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var sdkCredentialDecodingFailureMessages = /* @__PURE__ */ new Set([
|
|
16
|
+
"project credential decoding failed",
|
|
17
|
+
"shared project credential decoding failed",
|
|
18
|
+
"project credential decoding failed: this connector version does not support shared credentials"
|
|
19
|
+
]);
|
|
20
|
+
function isCredentialMaterialError(error) {
|
|
21
|
+
return error instanceof CredentialMaterialError || error instanceof TypeError && sdkCredentialDecodingFailureMessages.has(error.message);
|
|
22
|
+
}
|
|
23
|
+
function decodeCredentials(value) {
|
|
24
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new CredentialMaterialError("credential fields are invalid");
|
|
25
|
+
const fields = value;
|
|
26
|
+
const allowed = /* @__PURE__ */ new Set(["access_token", "app_id", "app_key", "key_id"]);
|
|
27
|
+
if (Object.keys(fields).some((key) => !allowed.has(key))) throw new CredentialMaterialError("stored credential does not match the connector credential fields");
|
|
28
|
+
if (fields["access_token"] != null && !(typeof fields["access_token"] === "string")) throw new CredentialMaterialError("credential access_token has an invalid type");
|
|
29
|
+
if (fields["app_id"] != null && !(typeof fields["app_id"] === "string")) throw new CredentialMaterialError("credential app_id has an invalid type");
|
|
30
|
+
if (fields["app_key"] != null && !(typeof fields["app_key"] === "string")) throw new CredentialMaterialError("credential app_key has an invalid type");
|
|
31
|
+
if (fields["key_id"] != null && !(typeof fields["key_id"] === "string")) throw new CredentialMaterialError("credential key_id has an invalid type");
|
|
32
|
+
const credentials = {};
|
|
33
|
+
if (!fields["app_id"]) throw new CredentialMaterialError("credential app_id is required");
|
|
34
|
+
credentials.appId = fields["app_id"] ?? "";
|
|
35
|
+
if (!fields["key_id"]) throw new CredentialMaterialError("credential key_id is required");
|
|
36
|
+
credentials.keyId = fields["key_id"] ?? "";
|
|
37
|
+
if (!fields["app_key"]) throw new CredentialMaterialError("credential app_key is required");
|
|
38
|
+
credentials.appKey = new SecretString(fields["app_key"] ?? "");
|
|
39
|
+
if (!fields["access_token"]) throw new CredentialMaterialError("credential access_token is required");
|
|
40
|
+
credentials.accessToken = new SecretString(fields["access_token"] ?? "");
|
|
41
|
+
return credentials;
|
|
42
|
+
}
|
|
43
|
+
var ListDevicesInputSchema = z.strictObject({
|
|
44
|
+
"pageNum": z.number().int(),
|
|
45
|
+
"pageSize": z.number().int()
|
|
46
|
+
});
|
|
47
|
+
var DeviceSchema = z.strictObject({
|
|
48
|
+
"deviceId": z.string(),
|
|
49
|
+
"deviceName": z.string(),
|
|
50
|
+
"model": z.string(),
|
|
51
|
+
"modelType": z.number().int(),
|
|
52
|
+
"online": z.boolean(),
|
|
53
|
+
"firmwareVersion": z.string(),
|
|
54
|
+
"positionId": z.string(),
|
|
55
|
+
"parentDeviceId": z.string(),
|
|
56
|
+
"timeZone": z.string()
|
|
57
|
+
});
|
|
58
|
+
var DevicePageSchema = z.strictObject({
|
|
59
|
+
"totalCount": z.number().int(),
|
|
60
|
+
"pageNum": z.number().int(),
|
|
61
|
+
"pageSize": z.number().int(),
|
|
62
|
+
"devices": z.array(DeviceSchema).nullable()
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// src/generated/definitions.ts
|
|
66
|
+
var ConnectorID = "aqara";
|
|
67
|
+
var ProviderID = "aqara";
|
|
68
|
+
var ListDevicesBranch = {
|
|
69
|
+
Listed: "listed",
|
|
70
|
+
ProviderRejected: "providerRejected",
|
|
71
|
+
InvalidResponse: "invalidResponse",
|
|
72
|
+
Defect: "defect"
|
|
73
|
+
};
|
|
74
|
+
var ListDevicesBranchListed = ListDevicesBranch.Listed;
|
|
75
|
+
var ListDevicesBranchProviderRejected = ListDevicesBranch.ProviderRejected;
|
|
76
|
+
var ListDevicesBranchInvalidResponse = ListDevicesBranch.InvalidResponse;
|
|
77
|
+
var ListDevicesBranchDefect = ListDevicesBranch.Defect;
|
|
78
|
+
var ListDevicesDefinition = {
|
|
79
|
+
operation: { connectorId: ConnectorID, operationId: "listDevices" },
|
|
80
|
+
branches: [
|
|
81
|
+
{ id: "listed", description: "Aqara returned one page of devices, possibly empty." },
|
|
82
|
+
{ id: "providerRejected", description: "Aqara rejected authorization, permission, or device query.", optional: true },
|
|
83
|
+
{ id: "invalidResponse", description: "Aqara returned malformed or oversized data.", optional: true },
|
|
84
|
+
{ id: "defect", description: "Local input, connection configuration, or credentials are invalid.", optional: true }
|
|
85
|
+
],
|
|
86
|
+
stepDefaults: { executeMethodTimeoutMs: 3e4, executeDurability: "async" }
|
|
87
|
+
};
|
|
88
|
+
var ListDevicesMetadata = { kind: "query", idempotency: "none", authorization: "required" };
|
|
89
|
+
|
|
90
|
+
// src/generated/connection.ts
|
|
91
|
+
import { ConnectionRefSchema } from "@superdurable/connectors-sdk";
|
|
92
|
+
var Connection = class {
|
|
93
|
+
/** Stable provider and connection name. */
|
|
94
|
+
reference;
|
|
95
|
+
/** Bound provider operations. */
|
|
96
|
+
operations;
|
|
97
|
+
/** Validate a reference and bind operations. */
|
|
98
|
+
constructor(reference, operations) {
|
|
99
|
+
this.reference = ConnectionRefSchema.parse(reference);
|
|
100
|
+
if (this.reference.provider !== ProviderID) throw new TypeError("connection provider differs from the manifest");
|
|
101
|
+
if (!operations) throw new TypeError("connector operations are required");
|
|
102
|
+
this.operations = operations;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
function newConnection(client, reference) {
|
|
106
|
+
if (!client) throw new TypeError("connector client is required");
|
|
107
|
+
return new Connection(reference, client);
|
|
108
|
+
}
|
|
109
|
+
function newConnectionWithOperations(operations, reference) {
|
|
110
|
+
return new Connection(reference, operations);
|
|
111
|
+
}
|
|
112
|
+
function newProjectConnection(project, connectionName, createClient) {
|
|
113
|
+
if (!project) throw new TypeError("loaded project is required");
|
|
114
|
+
const reference = ConnectionRefSchema.parse({ provider: ProviderID, name: connectionName });
|
|
115
|
+
const selected = project.configuration.connections.find((entry) => entry.connectorId === ConnectorID && entry.connectionName === connectionName);
|
|
116
|
+
if (!selected || selected.provider !== ProviderID) throw new TypeError("project connection is not configured");
|
|
117
|
+
return newConnection(createClient(project, connectionName), reference);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export {
|
|
121
|
+
ConfigSchema,
|
|
122
|
+
CredentialMaterialError,
|
|
123
|
+
isCredentialMaterialError,
|
|
124
|
+
decodeCredentials,
|
|
125
|
+
ListDevicesInputSchema,
|
|
126
|
+
DeviceSchema,
|
|
127
|
+
DevicePageSchema,
|
|
128
|
+
ConnectorID,
|
|
129
|
+
ProviderID,
|
|
130
|
+
ListDevicesBranch,
|
|
131
|
+
ListDevicesBranchListed,
|
|
132
|
+
ListDevicesBranchProviderRejected,
|
|
133
|
+
ListDevicesBranchInvalidResponse,
|
|
134
|
+
ListDevicesBranchDefect,
|
|
135
|
+
ListDevicesDefinition,
|
|
136
|
+
ListDevicesMetadata,
|
|
137
|
+
Connection,
|
|
138
|
+
newConnection,
|
|
139
|
+
newConnectionWithOperations,
|
|
140
|
+
newProjectConnection
|
|
141
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { SecretString, CredentialProvider, Query, ConnectionRef } from '@superdurable/connectors-sdk';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
/** Connector configuration; omitted fields use manifest defaults. */
|
|
5
|
+
interface Config {
|
|
6
|
+
/** HTTPS origin for the mainland China Aqara Cloud API. Leave blank for the displayed public endpoint; override only for a trusted test server. */
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
/** Maximum duration of one device-list request as a positive Go duration, such as 10s. Leave blank for the displayed default. Runtime value in milliseconds. */
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
/** Maximum device-list JSON response in bytes. Leave blank for the displayed default; raise only for a legitimately larger page. */
|
|
11
|
+
maxResponseBytes?: number;
|
|
12
|
+
}
|
|
13
|
+
/** Runtime schema for manifest configuration. */
|
|
14
|
+
declare const ConfigSchema: z.ZodObject<{
|
|
15
|
+
baseUrl: z.ZodOptional<z.ZodString>;
|
|
16
|
+
timeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
17
|
+
maxResponseBytes: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
}, z.core.$strip>;
|
|
19
|
+
/** Private connector credentials resolved by the host. */
|
|
20
|
+
interface Credentials {
|
|
21
|
+
/** Secret accessToken from Aqara account or project authorization at https://developer.aqara.com. It expires after the authorization's chosen validity period and must be manually replaced in Dex when refreshed. Blank prevents requests. */
|
|
22
|
+
accessToken: SecretString;
|
|
23
|
+
/** Non-secret AppID from https://developer.aqara.com > Console > Project Management > approved project details. Blank prevents requests. */
|
|
24
|
+
appId: string;
|
|
25
|
+
/** Secret AppKey paired with KeyID at https://developer.aqara.com > Console > Project Management > Project Details > Key Management. Dex stores it as a credential and uses it only to sign requests. Blank prevents requests. */
|
|
26
|
+
appKey: SecretString;
|
|
27
|
+
/** Non-secret KeyID from the approved project's Key Management page at https://developer.aqara.com. Blank prevents requests. */
|
|
28
|
+
keyId: string;
|
|
29
|
+
}
|
|
30
|
+
/** Stored credential material that fails the connector credential checks, so no retry can succeed. */
|
|
31
|
+
declare class CredentialMaterialError extends TypeError {
|
|
32
|
+
constructor(message: string);
|
|
33
|
+
}
|
|
34
|
+
/** Report malformed stored credential material, also behind the SDK provider's decoding TypeError. */
|
|
35
|
+
declare function isCredentialMaterialError(error: unknown): boolean;
|
|
36
|
+
/** Decode private credential fields for the connector. */
|
|
37
|
+
declare function decodeCredentials(value: unknown): Credentials;
|
|
38
|
+
/** Host-owned source of private connector credentials. */
|
|
39
|
+
type CredentialSource = CredentialProvider<Credentials>;
|
|
40
|
+
/** ListDevicesInput selects one bounded page from the authorized account's devices. */
|
|
41
|
+
interface ListDevicesInput {
|
|
42
|
+
/** PageNum is a one-based page number; zero selects page one. */
|
|
43
|
+
"pageNum": number;
|
|
44
|
+
/** PageSize is a count from 1 to 100; zero selects Aqara's documented default of 50. */
|
|
45
|
+
"pageSize": number;
|
|
46
|
+
}
|
|
47
|
+
/** Device contains non-secret device identity and online state. */
|
|
48
|
+
interface Device {
|
|
49
|
+
/** DeviceID is Aqara's stable device ID. */
|
|
50
|
+
"deviceId": string;
|
|
51
|
+
/** DeviceName is the user-assigned display name. */
|
|
52
|
+
"deviceName": string;
|
|
53
|
+
/** Model is Aqara's device model identifier. */
|
|
54
|
+
"model": string;
|
|
55
|
+
/** ModelType is 1 or 2 for a gateway and 3 for a child device. */
|
|
56
|
+
"modelType": number;
|
|
57
|
+
/** Online reports the documented state value 1. */
|
|
58
|
+
"online": boolean;
|
|
59
|
+
/** FirmwareVersion is the installed firmware version if reported. */
|
|
60
|
+
"firmwareVersion": string;
|
|
61
|
+
/** PositionID is the provider location ID if reported. */
|
|
62
|
+
"positionId": string;
|
|
63
|
+
/** ParentDeviceID is the gateway device ID for a child device. */
|
|
64
|
+
"parentDeviceId": string;
|
|
65
|
+
/** TimeZone is the device's reported timezone if available. */
|
|
66
|
+
"timeZone": string;
|
|
67
|
+
}
|
|
68
|
+
/** DevicePage contains one page and the provider's total matching count. */
|
|
69
|
+
interface DevicePage {
|
|
70
|
+
/** TotalCount is the provider's total device count. */
|
|
71
|
+
"totalCount": number;
|
|
72
|
+
/** PageNum is the one-based page number requested. */
|
|
73
|
+
"pageNum": number;
|
|
74
|
+
/** PageSize is the page size requested. */
|
|
75
|
+
"pageSize": number;
|
|
76
|
+
/** Devices contains no more than PageSize entries. */
|
|
77
|
+
"devices": Array<Device> | null;
|
|
78
|
+
}
|
|
79
|
+
/** Runtime validator for ListDevicesInput. */
|
|
80
|
+
declare const ListDevicesInputSchema: z.ZodObject<{
|
|
81
|
+
pageNum: z.ZodNumber;
|
|
82
|
+
pageSize: z.ZodNumber;
|
|
83
|
+
}, z.core.$strict>;
|
|
84
|
+
/** Runtime validator for Device. */
|
|
85
|
+
declare const DeviceSchema: z.ZodObject<{
|
|
86
|
+
deviceId: z.ZodString;
|
|
87
|
+
deviceName: z.ZodString;
|
|
88
|
+
model: z.ZodString;
|
|
89
|
+
modelType: z.ZodNumber;
|
|
90
|
+
online: z.ZodBoolean;
|
|
91
|
+
firmwareVersion: z.ZodString;
|
|
92
|
+
positionId: z.ZodString;
|
|
93
|
+
parentDeviceId: z.ZodString;
|
|
94
|
+
timeZone: z.ZodString;
|
|
95
|
+
}, z.core.$strict>;
|
|
96
|
+
/** Runtime validator for DevicePage. */
|
|
97
|
+
declare const DevicePageSchema: z.ZodObject<{
|
|
98
|
+
totalCount: z.ZodNumber;
|
|
99
|
+
pageNum: z.ZodNumber;
|
|
100
|
+
pageSize: z.ZodNumber;
|
|
101
|
+
devices: z.ZodNullable<z.ZodArray<z.ZodObject<{
|
|
102
|
+
deviceId: z.ZodString;
|
|
103
|
+
deviceName: z.ZodString;
|
|
104
|
+
model: z.ZodString;
|
|
105
|
+
modelType: z.ZodNumber;
|
|
106
|
+
online: z.ZodBoolean;
|
|
107
|
+
firmwareVersion: z.ZodString;
|
|
108
|
+
positionId: z.ZodString;
|
|
109
|
+
parentDeviceId: z.ZodString;
|
|
110
|
+
timeZone: z.ZodString;
|
|
111
|
+
}, z.core.$strict>>>;
|
|
112
|
+
}, z.core.$strict>;
|
|
113
|
+
|
|
114
|
+
/** Typed operations supplied by a client or mock. */
|
|
115
|
+
interface Operations {
|
|
116
|
+
/** Invoke listDevices through this connection. */
|
|
117
|
+
listDevices(): Query<ListDevicesInput, DevicePage>;
|
|
118
|
+
}
|
|
119
|
+
/** Named connector connection kept outside Flow state. */
|
|
120
|
+
declare class Connection<BoundOperations = Operations> {
|
|
121
|
+
/** Stable provider and connection name. */
|
|
122
|
+
readonly reference: ConnectionRef;
|
|
123
|
+
/** Bound provider operations. */
|
|
124
|
+
readonly operations: BoundOperations;
|
|
125
|
+
/** Validate a reference and bind operations. */
|
|
126
|
+
constructor(reference: ConnectionRef, operations: BoundOperations);
|
|
127
|
+
}
|
|
128
|
+
/** Bind a client to a named connection. */
|
|
129
|
+
declare function newConnection(client: Operations, reference: ConnectionRef): Connection;
|
|
130
|
+
/** Bind scripted operations to a named connection. */
|
|
131
|
+
declare function newConnectionWithOperations(operations: Operations, reference: ConnectionRef): Connection;
|
|
132
|
+
|
|
133
|
+
export { Connection as C, type DevicePage as D, type ListDevicesInput as L, type Operations as O, type Credentials as a, type Config as b, ConfigSchema as c, CredentialMaterialError as d, type CredentialSource as e, type Device as f, DevicePageSchema as g, DeviceSchema as h, ListDevicesInputSchema as i, decodeCredentials as j, isCredentialMaterialError as k, newConnectionWithOperations as l, newConnection as n };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { L as ListDevicesInput, D as DevicePage, C as Connection, O as Operations, a as Credentials, b as Config } from './connection-D6DWLUrD.js';
|
|
2
|
+
export { c as ConfigSchema, d as CredentialMaterialError, e as CredentialSource, f as Device, g as DevicePageSchema, h as DeviceSchema, i as ListDevicesInputSchema, j as decodeCredentials, k as isCredentialMaterialError, n as newConnection, l as newConnectionWithOperations } from './connection-D6DWLUrD.js';
|
|
3
|
+
import { QueryDefinition, QueryStepConfig, Query, QueryResult, QueryStep, CredentialProvider, Call, QueryAttempt } from '@superdurable/connectors-sdk';
|
|
4
|
+
import { StepTarget } from '@superdurable/dex';
|
|
5
|
+
import { LoadedProject } from '@superdurable/connectors-sdk/projectconfig';
|
|
6
|
+
import { ProviderRequest, ProviderResponse } from '@superdurable/connectors-sdk/providerhttp';
|
|
7
|
+
import 'zod';
|
|
8
|
+
|
|
9
|
+
/** Stable connector identity. */
|
|
10
|
+
declare const ConnectorID = "aqara";
|
|
11
|
+
/** Provider identity for named connections. */
|
|
12
|
+
declare const ProviderID = "aqara";
|
|
13
|
+
/** Branch IDs for listDevices. */
|
|
14
|
+
declare const ListDevicesBranch: {
|
|
15
|
+
readonly Listed: "listed";
|
|
16
|
+
readonly ProviderRejected: "providerRejected";
|
|
17
|
+
readonly InvalidResponse: "invalidResponse";
|
|
18
|
+
readonly Defect: "defect";
|
|
19
|
+
};
|
|
20
|
+
/** Aqara returned one page of devices, possibly empty. */
|
|
21
|
+
declare const ListDevicesBranchListed: "listed";
|
|
22
|
+
/** Aqara rejected authorization, permission, or device query. */
|
|
23
|
+
declare const ListDevicesBranchProviderRejected: "providerRejected";
|
|
24
|
+
/** Aqara returned malformed or oversized data. */
|
|
25
|
+
declare const ListDevicesBranchInvalidResponse: "invalidResponse";
|
|
26
|
+
/** Local input, connection configuration, or credentials are invalid. */
|
|
27
|
+
declare const ListDevicesBranchDefect: "defect";
|
|
28
|
+
/** Manifest definition for listDevices. */
|
|
29
|
+
declare const ListDevicesDefinition: QueryDefinition;
|
|
30
|
+
/** Manifest metadata for listDevices. */
|
|
31
|
+
declare const ListDevicesMetadata: {
|
|
32
|
+
readonly kind: "query";
|
|
33
|
+
readonly idempotency: "none";
|
|
34
|
+
readonly authorization: "required";
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Configuration for the listDevices connector Step. */
|
|
38
|
+
interface ListDevicesStepConfig<StepInput, OperationInput = ListDevicesInput, OperationOutput = DevicePage> extends Omit<QueryStepConfig<StepInput, OperationInput, OperationOutput>, 'operation' | 'connection' | 'branches'> {
|
|
39
|
+
/** Named connection supplying this operation. */
|
|
40
|
+
connection: Connection<{
|
|
41
|
+
listDevices(): Query<OperationInput, OperationOutput>;
|
|
42
|
+
}>;
|
|
43
|
+
/** Name matching the bound connection. */
|
|
44
|
+
connectionName: string;
|
|
45
|
+
/** Aqara returned one page of devices, possibly empty. */
|
|
46
|
+
listed: StepTarget<QueryResult<OperationOutput>>;
|
|
47
|
+
/** Aqara rejected authorization, permission, or device query. */
|
|
48
|
+
providerRejected?: StepTarget<QueryResult<OperationOutput>>;
|
|
49
|
+
/** Aqara returned malformed or oversized data. */
|
|
50
|
+
invalidResponse?: StepTarget<QueryResult<OperationOutput>>;
|
|
51
|
+
/** Local input, connection configuration, or credentials are invalid. */
|
|
52
|
+
defect?: StepTarget<QueryResult<OperationOutput>>;
|
|
53
|
+
}
|
|
54
|
+
/** Build the listDevices Step from a named connection. */
|
|
55
|
+
declare function newListDevicesStep<StepInput, OperationInput = ListDevicesInput, OperationOutput = DevicePage>(configuration: ListDevicesStepConfig<StepInput, OperationInput, OperationOutput>): QueryStep<StepInput, OperationInput, OperationOutput>;
|
|
56
|
+
|
|
57
|
+
/** Open one named Aqara connection from Dex project configuration. */
|
|
58
|
+
declare function newProjectConnection(project: LoadedProject, connectionName: string): Connection<Operations>;
|
|
59
|
+
|
|
60
|
+
/** Send one device-list request through a caller-owned transport. */
|
|
61
|
+
type DeviceRequestSender = (request: ProviderRequest) => Promise<ProviderResponse>;
|
|
62
|
+
/** Optional request dependencies for deterministic provider tests. */
|
|
63
|
+
interface AqaraClientOptions {
|
|
64
|
+
/** Request sender; production uses the Connector SDK transport. */
|
|
65
|
+
send?: DeviceRequestSender;
|
|
66
|
+
/** Request clock; production uses the current system time. */
|
|
67
|
+
clock?: () => Date;
|
|
68
|
+
/** Sixteen random nonce bytes; production uses cryptographic randomness. */
|
|
69
|
+
nonceSource?: () => Uint8Array;
|
|
70
|
+
}
|
|
71
|
+
/** Resolve Aqara project credentials for each device query. */
|
|
72
|
+
declare class AqaraClient {
|
|
73
|
+
readonly baseUrl: string;
|
|
74
|
+
readonly timeoutMs: number;
|
|
75
|
+
readonly maxResponseBytes: number;
|
|
76
|
+
readonly credentials: CredentialProvider<Credentials>;
|
|
77
|
+
readonly send: DeviceRequestSender;
|
|
78
|
+
readonly clock: () => Date;
|
|
79
|
+
readonly nonceSource: () => Uint8Array;
|
|
80
|
+
/** Validate configuration without contacting Aqara. */
|
|
81
|
+
constructor(config: Config, credentials: CredentialProvider<Credentials>, options?: AqaraClientOptions);
|
|
82
|
+
/** Bind the one-page device Query. */
|
|
83
|
+
listDevices(): ListDevicesOperation;
|
|
84
|
+
}
|
|
85
|
+
/** Read one bounded page of authorized Aqara devices. */
|
|
86
|
+
declare class ListDevicesOperation implements Query<ListDevicesInput, DevicePage> {
|
|
87
|
+
private readonly client;
|
|
88
|
+
/** Bind an authenticated Aqara client. */
|
|
89
|
+
constructor(client: AqaraClient);
|
|
90
|
+
/** Return the stable generated Query contract. */
|
|
91
|
+
definition(): QueryDefinition;
|
|
92
|
+
/** Sign, send, and classify one device-list request. */
|
|
93
|
+
invoke(call: Call, input: ListDevicesInput): Promise<QueryAttempt<DevicePage>>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { AqaraClient, type AqaraClientOptions, Config, Connection, ConnectorID, Credentials, DevicePage, type DeviceRequestSender, ListDevicesBranch, ListDevicesBranchDefect, ListDevicesBranchInvalidResponse, ListDevicesBranchListed, ListDevicesBranchProviderRejected, ListDevicesDefinition, ListDevicesInput, ListDevicesMetadata, ListDevicesOperation, type ListDevicesStepConfig, ProviderID, newListDevicesStep, newProjectConnection };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConfigSchema,
|
|
3
|
+
Connection,
|
|
4
|
+
ConnectorID,
|
|
5
|
+
CredentialMaterialError,
|
|
6
|
+
DevicePageSchema,
|
|
7
|
+
DeviceSchema,
|
|
8
|
+
ListDevicesBranch,
|
|
9
|
+
ListDevicesBranchDefect,
|
|
10
|
+
ListDevicesBranchInvalidResponse,
|
|
11
|
+
ListDevicesBranchListed,
|
|
12
|
+
ListDevicesBranchProviderRejected,
|
|
13
|
+
ListDevicesDefinition,
|
|
14
|
+
ListDevicesInputSchema,
|
|
15
|
+
ListDevicesMetadata,
|
|
16
|
+
ProviderID,
|
|
17
|
+
decodeCredentials,
|
|
18
|
+
isCredentialMaterialError,
|
|
19
|
+
newConnection,
|
|
20
|
+
newConnectionWithOperations,
|
|
21
|
+
newProjectConnection
|
|
22
|
+
} from "./chunk-PQNVHYKW.js";
|
|
23
|
+
|
|
24
|
+
// src/generated/steps.ts
|
|
25
|
+
import { goToBranch, newQueryStep } from "@superdurable/connectors-sdk";
|
|
26
|
+
function newListDevicesStep(configuration) {
|
|
27
|
+
const { connection, connectionName, listed, providerRejected, invalidResponse, defect, ...stepConfiguration } = configuration;
|
|
28
|
+
if (!connection || connection.reference.name !== connectionName) throw new TypeError("Step connection name differs from the bound connection");
|
|
29
|
+
return newQueryStep({ ...stepConfiguration, operation: connection.operations.listDevices(), connection: connection.reference, branches: [
|
|
30
|
+
goToBranch("listed", listed),
|
|
31
|
+
...providerRejected ? [goToBranch("providerRejected", providerRejected)] : [],
|
|
32
|
+
...invalidResponse ? [goToBranch("invalidResponse", invalidResponse)] : [],
|
|
33
|
+
...defect ? [goToBranch("defect", defect)] : []
|
|
34
|
+
] });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/project.ts
|
|
38
|
+
import { SecretString as SecretString2 } from "@superdurable/connectors-sdk";
|
|
39
|
+
import { z } from "zod";
|
|
40
|
+
|
|
41
|
+
// src/client.ts
|
|
42
|
+
import { randomBytes } from "crypto";
|
|
43
|
+
import { SecretString, newQueryBranch, newQueryRetry } from "@superdurable/connectors-sdk";
|
|
44
|
+
import { BodyTooLargeError, parseRetryAfter, sendProviderRequest, validateBaseURL } from "@superdurable/connectors-sdk/providerhttp";
|
|
45
|
+
|
|
46
|
+
// src/signature.ts
|
|
47
|
+
import { createHash } from "crypto";
|
|
48
|
+
function signDeviceRequest(accessToken, appId, keyId, nonce, timestamp, appKey) {
|
|
49
|
+
const canonical = `Accesstoken=${accessToken}&Appid=${appId}&Keyid=${keyId}&Nonce=${nonce}&Time=${timestamp}${appKey}`.toLowerCase();
|
|
50
|
+
return createHash("md5").update(canonical, "utf8").digest("hex");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/client.ts
|
|
54
|
+
var emptyPage = { totalCount: 0, pageNum: 0, pageSize: 0, devices: [] };
|
|
55
|
+
var AqaraClient = class {
|
|
56
|
+
baseUrl;
|
|
57
|
+
timeoutMs;
|
|
58
|
+
maxResponseBytes;
|
|
59
|
+
credentials;
|
|
60
|
+
send;
|
|
61
|
+
clock;
|
|
62
|
+
nonceSource;
|
|
63
|
+
/** Validate configuration without contacting Aqara. */
|
|
64
|
+
constructor(config, credentials, options = {}) {
|
|
65
|
+
if (!config || !credentials) throw new TypeError("Aqara configuration and credential source are required");
|
|
66
|
+
this.baseUrl = validateBaseURL(config.baseUrl || "https://open-cn.aqara.com").replace(/\/$/, "");
|
|
67
|
+
this.timeoutMs = config.timeoutMs ?? 15e3;
|
|
68
|
+
this.maxResponseBytes = config.maxResponseBytes ?? 1048576;
|
|
69
|
+
if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0) throw new TypeError("Aqara timeout and response limit must be positive");
|
|
70
|
+
this.credentials = credentials;
|
|
71
|
+
this.send = options.send ?? sendProviderRequest;
|
|
72
|
+
this.clock = options.clock ?? (() => /* @__PURE__ */ new Date());
|
|
73
|
+
this.nonceSource = options.nonceSource ?? (() => randomBytes(16));
|
|
74
|
+
}
|
|
75
|
+
/** Bind the one-page device Query. */
|
|
76
|
+
listDevices() {
|
|
77
|
+
return new ListDevicesOperation(this);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var ListDevicesOperation = class {
|
|
81
|
+
/** Bind an authenticated Aqara client. */
|
|
82
|
+
constructor(client) {
|
|
83
|
+
this.client = client;
|
|
84
|
+
}
|
|
85
|
+
client;
|
|
86
|
+
/** Return the stable generated Query contract. */
|
|
87
|
+
definition() {
|
|
88
|
+
return ListDevicesDefinition;
|
|
89
|
+
}
|
|
90
|
+
/** Sign, send, and classify one device-list request. */
|
|
91
|
+
async invoke(call, input) {
|
|
92
|
+
const pageNum = input?.pageNum === 0 ? 1 : input?.pageNum;
|
|
93
|
+
const pageSize = input?.pageSize === 0 ? 50 : input?.pageSize;
|
|
94
|
+
if (!Number.isSafeInteger(pageNum) || !Number.isSafeInteger(pageSize) || (pageNum ?? 0) < 1 || (pageNum ?? 0) > 1e6 || (pageSize ?? 0) < 1 || (pageSize ?? 0) > 100) return branch("defect", "VALIDATION", "pageNum or pageSize is outside the supported range");
|
|
95
|
+
let credentials;
|
|
96
|
+
try {
|
|
97
|
+
credentials = await this.client.credentials.resolve(call, call.context.cancellationSignal);
|
|
98
|
+
} catch {
|
|
99
|
+
return branch("defect", "AUTHENTICATION", "Aqara connection credentials are unavailable or invalid");
|
|
100
|
+
}
|
|
101
|
+
const appId = credentials.appId;
|
|
102
|
+
const keyId = credentials.keyId;
|
|
103
|
+
const appKey = credentials.appKey instanceof SecretString ? credentials.appKey.reveal() : "";
|
|
104
|
+
const accessToken = credentials.accessToken instanceof SecretString ? credentials.accessToken.reveal() : "";
|
|
105
|
+
if (![appId, keyId, appKey, accessToken].every(isValidCredential)) return branch("defect", "AUTHENTICATION", "Aqara connection credentials are unavailable or invalid");
|
|
106
|
+
let nonce;
|
|
107
|
+
try {
|
|
108
|
+
const bytes = this.client.nonceSource();
|
|
109
|
+
if (bytes.length !== 16) throw new TypeError("nonce must contain sixteen bytes");
|
|
110
|
+
nonce = Buffer.from(bytes).toString("hex");
|
|
111
|
+
} catch {
|
|
112
|
+
return retry("AVAILABILITY", "secure nonce generation failed");
|
|
113
|
+
}
|
|
114
|
+
const timestamp = String(this.client.clock().getTime());
|
|
115
|
+
if (!/^\d+$/.test(timestamp)) return branch("defect", "VALIDATION", "device request is invalid");
|
|
116
|
+
const body = Buffer.from(JSON.stringify({ intent: "query.device.info", data: { pageNum, pageSize } }), "utf8");
|
|
117
|
+
let response;
|
|
118
|
+
try {
|
|
119
|
+
response = await this.client.send({
|
|
120
|
+
url: `${this.client.baseUrl}/v3.0/open/api`,
|
|
121
|
+
method: "POST",
|
|
122
|
+
body,
|
|
123
|
+
headers: {
|
|
124
|
+
Accept: "application/json",
|
|
125
|
+
"Content-Type": "application/json",
|
|
126
|
+
Accesstoken: accessToken,
|
|
127
|
+
Appid: appId,
|
|
128
|
+
Keyid: keyId,
|
|
129
|
+
Nonce: nonce,
|
|
130
|
+
Time: timestamp,
|
|
131
|
+
Sign: signDeviceRequest(accessToken, appId, keyId, nonce, timestamp, appKey)
|
|
132
|
+
},
|
|
133
|
+
timeoutMs: this.client.timeoutMs,
|
|
134
|
+
maxResponseBytes: this.client.maxResponseBytes,
|
|
135
|
+
addressPolicy: this.client.baseUrl === "https://open-cn.aqara.com" ? "publicHTTPS" : "privateNetworkHTTP",
|
|
136
|
+
signal: call.context.cancellationSignal
|
|
137
|
+
});
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (error instanceof BodyTooLargeError && error.status !== void 0) {
|
|
140
|
+
const early2 = classifyEarlyStatus(error.status, error.headers ?? {});
|
|
141
|
+
if (early2) return early2;
|
|
142
|
+
return branch("invalidResponse", "RESPONSE_TOO_LARGE", "Aqara response exceeded the configured limit");
|
|
143
|
+
}
|
|
144
|
+
return retry("AVAILABILITY", "Aqara request failed");
|
|
145
|
+
}
|
|
146
|
+
const early = classifyEarlyStatus(response.status, response.headers);
|
|
147
|
+
if (early) return early;
|
|
148
|
+
if (response.status !== 200) {
|
|
149
|
+
const kind = response.status === 401 ? "AUTHENTICATION" : response.status === 403 ? "AUTHORIZATION" : response.status === 400 || response.status === 404 ? "VALIDATION" : "PROVIDER_REJECTION";
|
|
150
|
+
return branch("providerRejected", kind, `Aqara rejected the query with HTTP ${response.status}`);
|
|
151
|
+
}
|
|
152
|
+
let document;
|
|
153
|
+
try {
|
|
154
|
+
document = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(response.body));
|
|
155
|
+
} catch {
|
|
156
|
+
return branch("invalidResponse", "PROTOCOL", "Aqara returned an invalid device response");
|
|
157
|
+
}
|
|
158
|
+
if (!isRecord(document) || !Number.isInteger(document.code)) return branch("invalidResponse", "PROTOCOL", "Aqara returned an invalid device response");
|
|
159
|
+
const code = document.code;
|
|
160
|
+
if (code !== 0) return classifyProviderCode(code, retryAfter(response.headers));
|
|
161
|
+
const result = document.result;
|
|
162
|
+
if (!isRecord(result) || !Array.isArray(result.data)) return branch("invalidResponse", "PROTOCOL", "Aqara returned an invalid device response");
|
|
163
|
+
const totalCount = result.totalCount === void 0 ? 0 : result.totalCount;
|
|
164
|
+
if (!Number.isSafeInteger(totalCount) || totalCount < 0 || result.data.length > pageSize) return branch("invalidResponse", "PROTOCOL", "Aqara returned an invalid device response");
|
|
165
|
+
const devices = [];
|
|
166
|
+
for (const row of result.data) {
|
|
167
|
+
if (!isRecord(row) || typeof row.did !== "string" || !row.did || typeof row.model !== "string" || !row.model || !Number.isInteger(row.state) || row.state !== 0 && row.state !== 1 || !isOptionalString(row.deviceName) || !isOptionalString(row.firmwareVersion) || !isOptionalString(row.positionId) || !isOptionalString(row.parentDid) || !isOptionalString(row.timeZone) || !isOptionalInteger(row.modelType)) return branch("invalidResponse", "PROTOCOL", "Aqara returned an invalid device response");
|
|
168
|
+
devices.push({
|
|
169
|
+
deviceId: row.did,
|
|
170
|
+
deviceName: optionalString(row.deviceName),
|
|
171
|
+
model: row.model,
|
|
172
|
+
modelType: optionalInteger(row.modelType),
|
|
173
|
+
online: row.state === 1,
|
|
174
|
+
firmwareVersion: optionalString(row.firmwareVersion),
|
|
175
|
+
positionId: optionalString(row.positionId),
|
|
176
|
+
parentDeviceId: optionalString(row.parentDid),
|
|
177
|
+
timeZone: optionalString(row.timeZone)
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return newQueryBranch("listed", { totalCount, pageNum, pageSize, devices });
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
function classifyEarlyStatus(status, headers) {
|
|
184
|
+
const delay = retryAfter(headers);
|
|
185
|
+
if (status === 429) return retry("RATE_LIMIT", "Aqara rate limited the request", delay);
|
|
186
|
+
if (status === 408 || status >= 500) return retry("AVAILABILITY", "Aqara is temporarily unavailable", delay);
|
|
187
|
+
return void 0;
|
|
188
|
+
}
|
|
189
|
+
function classifyProviderCode(code, delay) {
|
|
190
|
+
if (code === 429) return retry("RATE_LIMIT", "Aqara rate limited the request", delay);
|
|
191
|
+
if ([100, 104, 500, 501].includes(code)) return retry("AVAILABILITY", "Aqara is temporarily unavailable", delay);
|
|
192
|
+
const kind = [106, 107, 108, 109, 804, 2002, 2004, 2005].includes(code) ? "AUTHENTICATION" : [403, 603, 706, 717, 755, 756, 803, 2008, 2010, 2013, 2014].includes(code) ? "AUTHORIZATION" : [101, 102, 103, 105, 302, 303, 304, 305, 306, 604].includes(code) ? "VALIDATION" : "PROVIDER_REJECTION";
|
|
193
|
+
return branch("providerRejected", kind, `Aqara rejected the query with code ${code}`);
|
|
194
|
+
}
|
|
195
|
+
function retryAfter(headers) {
|
|
196
|
+
const value = headers["retry-after"];
|
|
197
|
+
return parseRetryAfter(Array.isArray(value) ? value[0] ?? "" : value ?? "");
|
|
198
|
+
}
|
|
199
|
+
function optionalString(value) {
|
|
200
|
+
return typeof value === "string" ? value : "";
|
|
201
|
+
}
|
|
202
|
+
function optionalInteger(value) {
|
|
203
|
+
return Number.isSafeInteger(value) ? value : 0;
|
|
204
|
+
}
|
|
205
|
+
function isOptionalString(value) {
|
|
206
|
+
return value === void 0 || value === null || typeof value === "string";
|
|
207
|
+
}
|
|
208
|
+
function isOptionalInteger(value) {
|
|
209
|
+
return value === void 0 || value === null || Number.isSafeInteger(value);
|
|
210
|
+
}
|
|
211
|
+
function isRecord(value) {
|
|
212
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
213
|
+
}
|
|
214
|
+
function isValidCredential(value) {
|
|
215
|
+
return typeof value === "string" && value.length > 0 && value.length <= 512 && /^[!-~]+$/.test(value);
|
|
216
|
+
}
|
|
217
|
+
function failure(kind, message) {
|
|
218
|
+
return { provider: "aqara", operation: "listDevices", kind, message };
|
|
219
|
+
}
|
|
220
|
+
function branch(id, kind, message) {
|
|
221
|
+
return newQueryBranch(id, emptyPage, {}, failure(kind, message));
|
|
222
|
+
}
|
|
223
|
+
function retry(kind, message, delayMs = 0) {
|
|
224
|
+
return newQueryRetry(failure(kind, message), delayMs);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// src/project.ts
|
|
228
|
+
function newProjectConnection2(project, connectionName) {
|
|
229
|
+
return newProjectConnection(project, connectionName, (loadedProject, name) => {
|
|
230
|
+
const selected = loadedProject.configuration.connections.find((entry) => entry.connectorId === "aqara" && entry.connectionName === name);
|
|
231
|
+
if (!selected) throw new TypeError("Aqara project connection is not configured");
|
|
232
|
+
const fields = z.strictObject({ baseUrl: z.string().optional(), timeout: z.union([z.string(), z.number().int()]).optional(), maxResponseBytes: z.number().int().optional() }).parse(selected.configuration);
|
|
233
|
+
const timeoutMs = typeof fields.timeout === "number" ? Math.ceil(fields.timeout / 1e6) : fields.timeout ? parseDurationMilliseconds(fields.timeout) : void 0;
|
|
234
|
+
const config = {
|
|
235
|
+
...fields.baseUrl !== void 0 ? { baseUrl: fields.baseUrl } : {},
|
|
236
|
+
...timeoutMs !== void 0 ? { timeoutMs } : {},
|
|
237
|
+
...fields.maxResponseBytes !== void 0 ? { maxResponseBytes: fields.maxResponseBytes } : {}
|
|
238
|
+
};
|
|
239
|
+
const credentials = {
|
|
240
|
+
async resolve(_call, signal) {
|
|
241
|
+
const privateFields = await loadedProject.resolveCredential("aqara", name, signal);
|
|
242
|
+
if (typeof privateFields.app_id !== "string" || typeof privateFields.key_id !== "string" || typeof privateFields.app_key !== "string" || typeof privateFields.access_token !== "string") throw new TypeError("Aqara project credentials are unavailable");
|
|
243
|
+
return { appId: privateFields.app_id, keyId: privateFields.key_id, appKey: new SecretString2(privateFields.app_key), accessToken: new SecretString2(privateFields.access_token) };
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
return new AqaraClient(config, credentials);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function parseDurationMilliseconds(value) {
|
|
250
|
+
let remaining = value;
|
|
251
|
+
let milliseconds = 0;
|
|
252
|
+
const units = { ns: 1e-6, us: 1e-3, "\xB5s": 1e-3, ms: 1, s: 1e3, m: 6e4, h: 36e5 };
|
|
253
|
+
while (remaining) {
|
|
254
|
+
const part = /^(\d+(?:\.\d+)?)(ns|us|µs|ms|s|m|h)/.exec(remaining);
|
|
255
|
+
if (!part) throw new TypeError("Aqara timeout must be a positive Go duration");
|
|
256
|
+
milliseconds += Number(part[1]) * (units[part[2] ?? ""] ?? NaN);
|
|
257
|
+
remaining = remaining.slice(part[0].length);
|
|
258
|
+
}
|
|
259
|
+
if (!Number.isSafeInteger(milliseconds) || milliseconds <= 0) throw new TypeError("Aqara timeout must resolve to positive whole milliseconds");
|
|
260
|
+
return milliseconds;
|
|
261
|
+
}
|
|
262
|
+
export {
|
|
263
|
+
AqaraClient,
|
|
264
|
+
ConfigSchema,
|
|
265
|
+
Connection,
|
|
266
|
+
ConnectorID,
|
|
267
|
+
CredentialMaterialError,
|
|
268
|
+
DevicePageSchema,
|
|
269
|
+
DeviceSchema,
|
|
270
|
+
ListDevicesBranch,
|
|
271
|
+
ListDevicesBranchDefect,
|
|
272
|
+
ListDevicesBranchInvalidResponse,
|
|
273
|
+
ListDevicesBranchListed,
|
|
274
|
+
ListDevicesBranchProviderRejected,
|
|
275
|
+
ListDevicesDefinition,
|
|
276
|
+
ListDevicesInputSchema,
|
|
277
|
+
ListDevicesMetadata,
|
|
278
|
+
ListDevicesOperation,
|
|
279
|
+
ProviderID,
|
|
280
|
+
decodeCredentials,
|
|
281
|
+
isCredentialMaterialError,
|
|
282
|
+
newConnection,
|
|
283
|
+
newConnectionWithOperations,
|
|
284
|
+
newListDevicesStep,
|
|
285
|
+
newProjectConnection2 as newProjectConnection
|
|
286
|
+
};
|
package/dist/mock.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MockQuery, Case } from '@superdurable/connectors-sdk/connectormock';
|
|
2
|
+
import { Failure } from '@superdurable/connectors-sdk';
|
|
3
|
+
import { O as Operations, C as Connection, L as ListDevicesInput, D as DevicePage, f as Device } from './connection-D6DWLUrD.js';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
/** Go zero value for ListDevicesInput in scripted cases. */
|
|
7
|
+
declare const emptyListDevicesInput: ListDevicesInput;
|
|
8
|
+
/** Go zero value for Device in scripted cases. */
|
|
9
|
+
declare const emptyDevice: Device;
|
|
10
|
+
/** Go zero value for DevicePage in scripted cases. */
|
|
11
|
+
declare const emptyDevicePage: DevicePage;
|
|
12
|
+
/** Script the listed branch. */
|
|
13
|
+
declare function listDevicesListed(value: DevicePage, failure?: Failure): Case<DevicePage>;
|
|
14
|
+
/** Script the providerRejected branch. */
|
|
15
|
+
declare function listDevicesProviderRejected(value: DevicePage, failure?: Failure): Case<DevicePage>;
|
|
16
|
+
/** Script the invalidResponse branch. */
|
|
17
|
+
declare function listDevicesInvalidResponse(value: DevicePage, failure?: Failure): Case<DevicePage>;
|
|
18
|
+
/** Script the defect branch. */
|
|
19
|
+
declare function listDevicesDefect(value: DevicePage, failure?: Failure): Case<DevicePage>;
|
|
20
|
+
/** Script a safe retry of listDevices. */
|
|
21
|
+
declare function listDevicesRetry(failure: Failure, retryAfterMs?: number): Case<DevicePage>;
|
|
22
|
+
/** Manifest scenario listedDevices for listDevices. */
|
|
23
|
+
declare function listDevicesListedDevices(): Case<DevicePage>;
|
|
24
|
+
/** Default listDevices scenario. */
|
|
25
|
+
declare function listDevicesDefault(): Case<DevicePage>;
|
|
26
|
+
/** Scripted operations and manifest defaults for this connector. */
|
|
27
|
+
declare class GeneratedMock implements Operations {
|
|
28
|
+
#private;
|
|
29
|
+
constructor(connectionName: string);
|
|
30
|
+
connection(): Connection;
|
|
31
|
+
listDevices(): MockQuery<ListDevicesInput, DevicePage>;
|
|
32
|
+
assertNoFailures(): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { GeneratedMock, GeneratedMock as Mock, emptyDevice, emptyDevicePage, emptyListDevicesInput, listDevicesDefault, listDevicesDefect, listDevicesInvalidResponse, listDevicesListed, listDevicesListedDevices, listDevicesProviderRejected, listDevicesRetry };
|
package/dist/mock.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DevicePageSchema,
|
|
3
|
+
DeviceSchema,
|
|
4
|
+
ListDevicesDefinition,
|
|
5
|
+
ListDevicesInputSchema,
|
|
6
|
+
ProviderID,
|
|
7
|
+
newConnectionWithOperations
|
|
8
|
+
} from "./chunk-PQNVHYKW.js";
|
|
9
|
+
|
|
10
|
+
// src/generated/mock.ts
|
|
11
|
+
import { branchCase, retryCase, newQuery } from "@superdurable/connectors-sdk/connectormock";
|
|
12
|
+
var emptyListDevicesInput = ListDevicesInputSchema.parse({
|
|
13
|
+
"pageNum": 0,
|
|
14
|
+
"pageSize": 0
|
|
15
|
+
});
|
|
16
|
+
var emptyDevice = DeviceSchema.parse({
|
|
17
|
+
"deviceId": "",
|
|
18
|
+
"deviceName": "",
|
|
19
|
+
"model": "",
|
|
20
|
+
"modelType": 0,
|
|
21
|
+
"online": false,
|
|
22
|
+
"firmwareVersion": "",
|
|
23
|
+
"positionId": "",
|
|
24
|
+
"parentDeviceId": "",
|
|
25
|
+
"timeZone": ""
|
|
26
|
+
});
|
|
27
|
+
var emptyDevicePage = DevicePageSchema.parse({
|
|
28
|
+
"totalCount": 0,
|
|
29
|
+
"pageNum": 0,
|
|
30
|
+
"pageSize": 0,
|
|
31
|
+
"devices": []
|
|
32
|
+
});
|
|
33
|
+
function listDevicesListed(value, failure) {
|
|
34
|
+
return branchCase("listed", value, failure);
|
|
35
|
+
}
|
|
36
|
+
function listDevicesProviderRejected(value, failure) {
|
|
37
|
+
return branchCase("providerRejected", value, failure);
|
|
38
|
+
}
|
|
39
|
+
function listDevicesInvalidResponse(value, failure) {
|
|
40
|
+
return branchCase("invalidResponse", value, failure);
|
|
41
|
+
}
|
|
42
|
+
function listDevicesDefect(value, failure) {
|
|
43
|
+
return branchCase("defect", value, failure);
|
|
44
|
+
}
|
|
45
|
+
function listDevicesRetry(failure, retryAfterMs = 0) {
|
|
46
|
+
return retryCase(failure, retryAfterMs);
|
|
47
|
+
}
|
|
48
|
+
function listDevicesListedDevices() {
|
|
49
|
+
const value = DevicePageSchema.parse({ "devices": [{ "deviceId": "lumi.07737309957642", "deviceName": "Aqara Hub", "firmwareVersion": "3.2.6", "model": "lumi.gateway.aqhm01", "modelType": 1, "online": true, "parentDeviceId": "", "positionId": "real2.730432352746111072", "timeZone": "GMT+08:00" }], "pageNum": 1, "pageSize": 50, "totalCount": 1 });
|
|
50
|
+
return branchCase("listed", value);
|
|
51
|
+
}
|
|
52
|
+
function listDevicesDefault() {
|
|
53
|
+
return listDevicesListedDevices();
|
|
54
|
+
}
|
|
55
|
+
var GeneratedMock = class {
|
|
56
|
+
#listDevices;
|
|
57
|
+
#connection;
|
|
58
|
+
constructor(connectionName) {
|
|
59
|
+
this.#listDevices = newQuery(ListDevicesDefinition).default(listDevicesDefault());
|
|
60
|
+
this.#connection = newConnectionWithOperations(this, { provider: ProviderID, name: connectionName });
|
|
61
|
+
}
|
|
62
|
+
connection() {
|
|
63
|
+
return this.#connection;
|
|
64
|
+
}
|
|
65
|
+
listDevices() {
|
|
66
|
+
return this.#listDevices;
|
|
67
|
+
}
|
|
68
|
+
assertNoFailures() {
|
|
69
|
+
this.#listDevices.assertNoFailures();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
export {
|
|
73
|
+
GeneratedMock,
|
|
74
|
+
GeneratedMock as Mock,
|
|
75
|
+
emptyDevice,
|
|
76
|
+
emptyDevicePage,
|
|
77
|
+
emptyListDevicesInput,
|
|
78
|
+
listDevicesDefault,
|
|
79
|
+
listDevicesDefect,
|
|
80
|
+
listDevicesInvalidResponse,
|
|
81
|
+
listDevicesListed,
|
|
82
|
+
listDevicesListedDevices,
|
|
83
|
+
listDevicesProviderRejected,
|
|
84
|
+
listDevicesRetry
|
|
85
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superdurable/connector-aqara",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Aqara device catalog connector for Dex TypeScript applications",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/superdurable/dex-connectors-library.git",
|
|
9
|
+
"directory": "connectors/aqara/typescript"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": "^22.0.0 || ^24.0.0"
|
|
14
|
+
},
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./mock": {
|
|
23
|
+
"types": "./dist/mock.d.ts",
|
|
24
|
+
"import": "./dist/mock.js"
|
|
25
|
+
},
|
|
26
|
+
"./dex-connector.json": "./dex-connector.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"dex-connector.json"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"prepack": "npm run build",
|
|
36
|
+
"build": "tsup src/index.ts src/mock.ts --format esm --dts --clean",
|
|
37
|
+
"typecheck": "tsc --noEmit",
|
|
38
|
+
"lint": "eslint src test examples",
|
|
39
|
+
"test": "vitest run --exclude test/integration/**",
|
|
40
|
+
"test:integration": "vitest run test/integration/*.integration.test.ts",
|
|
41
|
+
"check": "npm run typecheck && npm run lint && npm test && npm run build",
|
|
42
|
+
"example:worker": "tsx examples/device-list/worker.ts",
|
|
43
|
+
"example:start": "tsx examples/device-list/start.ts"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@superdurable/connectors-sdk": "0.11.0",
|
|
47
|
+
"@superdurable/dex": ">=1.28.0",
|
|
48
|
+
"zod": "^4.1.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@eslint/js": "^9.0.0",
|
|
52
|
+
"@types/node": "^22.0.0",
|
|
53
|
+
"eslint": "^9.0.0",
|
|
54
|
+
"tsup": "^8.0.0",
|
|
55
|
+
"tsx": "^4.23.15",
|
|
56
|
+
"typescript": "^5.9.0",
|
|
57
|
+
"typescript-eslint": "^8.0.0",
|
|
58
|
+
"vitest": "^3.0.0",
|
|
59
|
+
"yaml": "^2.9.1"
|
|
60
|
+
}
|
|
61
|
+
}
|