This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi.ts
218 lines (173 loc) · 6.16 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as crypto from 'crypto';
import got, {Got, Response} from 'got';
import {Device} from './device';
import {
DevicesResponse,
DeviceTokenResponse,
PairingResultResponse,
TokenResponse,
TuyaResponse,
UsersResponse
} from './responses';
// Re-export common types for further manipulation by consumers
export {Device, Status} from './device';
export class OpenAPI {
public tokenAccess: string;
public tokenRefresh: string;
public tokenExpiresAt: Date;
public schema: string;
public handleToken: boolean;
private readonly _client: Got;
private readonly _key: string;
private readonly _secret: string;
constructor({key, secret, schema, region = 'us', handleToken = false}: {key: string; secret: string; schema: string; region: string; handleToken: boolean}) {
this.tokenAccess = '';
this.tokenRefresh = '';
this.tokenExpiresAt = new Date();
this._key = key;
this._secret = secret;
this.schema = schema;
this.handleToken = handleToken;
this._client = got.extend({
responseType: 'json',
prefixUrl: `https://openapi.tuya${region}.com/v1.0/`,
headers: {
client_id: this._key,
sign_method: 'HMAC-SHA256'
},
hooks: {
beforeRequest: [
async options => {
const isTokenUrl: boolean = options.url.toString().includes('token');
if (!isTokenUrl && this.tokenAccess === '' && this.handleToken) {
await this.getToken();
}
if (!isTokenUrl && this.isTokenExpired()) {
await this.refreshToken();
}
const now = Date.now();
options.headers.t = now.toString();
// Calculate signature
let sign = '';
if (isTokenUrl) {
sign = crypto
.createHmac('sha256', this._secret)
.update(this._key + now.toString())
.digest('hex').toUpperCase();
} else {
sign = crypto
.createHmac('sha256', this._secret)
.update(`${this._key}${this.tokenAccess}${now}`)
.digest('hex').toUpperCase();
options.headers.access_token = this.tokenAccess;
}
options.headers.sign = sign;
}
],
afterResponse: [
(response: Response) => {
const body = response.body as TuyaResponse<unknown>;
if (!body.success) {
throw new Error(body.msg);
}
response.body = body.result;
return response;
}
]
}
});
}
// Authorization methods
isTokenExpired(): boolean {
return Date.now() > this.tokenExpiresAt.getTime();
}
async getToken(): Promise<void> {
if (this.handleToken) {
throw new HandleTokenError();
}
const {body: {access_token, refresh_token, expire_time}} = await this._client.get<TokenResponse>('token?grant_type=1');
this.tokenAccess = access_token;
this.tokenRefresh = refresh_token;
this.tokenExpiresAt = new Date(Date.now() + (expire_time * 1000));
}
async refreshToken(): Promise<void> {
const {body: {access_token, refresh_token, expire_time}} = await this._client.get<TokenResponse>(`token/${this.tokenRefresh}`);
this.tokenAccess = access_token;
this.tokenRefresh = refresh_token;
this.tokenExpiresAt = new Date(Date.now() + (expire_time * 1000));
}
// API methods
async putUser({countryCode, username, password, usernameType, nickname}: {countryCode: string; username: string; password: string; usernameType: string; nickname: string}): Promise<string> {
const request = {
schema: this.schema,
country_code: countryCode,
username,
password,
username_type: usernameType,
nick_name: nickname
};
interface UserResponse {
uid: string;
}
const response = await this._client.post<UserResponse>(`apps/${this.schema}/user`, {
json: request
});
return response.body.uid;
}
async getUsers({pageNumber, pageSize}: {pageNumber: number; pageSize: number} = {pageNumber: 1, pageSize: 100}): Promise<UsersResponse> {
const response = await this._client.get<UsersResponse>(`apps/${this.schema}/users`, {
searchParams: {
page_no: pageNumber,
page_size: pageSize
}
});
return response.body;
}
async getDeviceToken({uid, timezone}: {uid: string; timezone: string}): Promise<DeviceTokenResponse> {
const response = await this._client.post<DeviceTokenResponse>('devices/token', {
json: {
uid,
timeZoneId: timezone
}
});
return response.body;
}
async getDevicesByToken(token: string): Promise<PairingResultResponse> {
const response = await this._client.get<PairingResultResponse>(`devices/tokens/${token}`);
return response.body;
}
async getDevicesByUser(uid: string): Promise<Device[]> {
const response = await this._client.get<Device[]>(`users/${uid}/devices`);
return response.body;
}
async getDevices({ids, pageNumber = 0, pageSize = 100}: {ids?: string[]; pageNumber: number; pageSize: number} = {pageNumber: 0, pageSize: 100}): Promise<DevicesResponse> {
const searchParameters: any = {
schema: this.schema,
page_no: pageNumber,
page_size: pageSize
};
if (ids) {
searchParameters.device_ids = ids.toString();
}
const response = await this._client.get<DevicesResponse>('devices', {searchParams: searchParameters});
return response.body;
}
async getDevice(deviceId: string): Promise<Device> {
const response = await this._client.get<Device>(`devices/${deviceId}`);
return response.body;
}
async getDeviceStatus(deviceId: string): Promise<Device['status']> {
const response = await this._client.get<Device['status']>(`devices/${deviceId}/status`);
return response.body;
}
async getSubDevicesOfZigbeeGateway(deviceId: string): Promise<Device> {
const response = await this._client.get<Device>(`devices/${deviceId}/sub-devices`);
return response.body;
}
}
class HandleTokenError extends Error {
constructor() {
super('Token acquisition is automatically handled.');
}
}
module.exports = OpenAPI;