tsvesync 1.0.98 → 1.0.99

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.
@@ -117,7 +117,7 @@ export declare class Helpers {
117
117
  /**
118
118
  * Build request body for second authentication step (login with authorize code)
119
119
  */
120
- static reqBodyAuthStep2(authorizeCode: string, bizToken: string | null, appId: string, terminalId: string, userCountryCode?: string): Record<string, any>;
120
+ static reqBodyAuthStep2(authorizeCode: string, bizToken: string | null, appId: string, terminalId: string, userCountryCode?: string, regionChange?: string): Record<string, any>;
121
121
  /**
122
122
  * Builder for body of api requests
123
123
  */
@@ -194,7 +194,7 @@ class Helpers {
194
194
  /**
195
195
  * Build request body for second authentication step (login with authorize code)
196
196
  */
197
- static reqBodyAuthStep2(authorizeCode, bizToken, appId, terminalId, userCountryCode) {
197
+ static reqBodyAuthStep2(authorizeCode, bizToken, appId, terminalId, userCountryCode, regionChange) {
198
198
  const body = {
199
199
  'method': 'loginByAuthorizeCode4Vesync',
200
200
  'authorizeCode': authorizeCode,
@@ -214,6 +214,10 @@ class Helpers {
214
214
  if (bizToken) {
215
215
  body.bizToken = bizToken;
216
216
  }
217
+ // Only include regionChange if provided
218
+ if (regionChange) {
219
+ body.regionChange = regionChange;
220
+ }
217
221
  return body;
218
222
  }
219
223
  /**
@@ -444,6 +448,30 @@ class Helpers {
444
448
  return [false, null, null, null];
445
449
  }
446
450
  if (loginResponse.code !== 0) {
451
+ // Handle cross-region error with retry (like pyvesync PR #340)
452
+ if (loginResponse.code === exports.CROSS_REGION_ERROR_CODE) {
453
+ logger_1.logger.debug('Cross-region error detected in Step 2, retrying with region change token...');
454
+ const { bizToken: regionChangeToken, countryCode: newCountryCode } = loginResponse.result || {};
455
+ if (regionChangeToken) {
456
+ // Retry Step 2 with region change token
457
+ const retryBody = {
458
+ ...step2Body,
459
+ bizToken: regionChangeToken,
460
+ regionChange: 'last_region',
461
+ userCountryCode: newCountryCode || userCountryCode
462
+ };
463
+ logger_1.logger.debug('Retrying Step 2 with region change token...');
464
+ const [retryResponse, retryStatus] = await this.callApi('/user/api/accountManage/v1/loginByAuthorizeCode4Vesync', 'post', retryBody, step1Headers, manager);
465
+ if (retryResponse && retryResponse.code === 0) {
466
+ const { token, accountID, countryCode } = retryResponse.result || {};
467
+ if (token && accountID) {
468
+ logger_1.logger.debug('Step 2 retry successful with region change');
469
+ return [true, token, accountID, countryCode || newCountryCode];
470
+ }
471
+ }
472
+ logger_1.logger.error('Step 2 retry failed:', retryResponse);
473
+ }
474
+ }
447
475
  logger_1.logger.error('Step 2 error code:', loginResponse.code, 'message:', loginResponse.msg);
448
476
  return [false, null, null, null];
449
477
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsvesync",
3
- "version": "1.0.98",
3
+ "version": "1.0.99",
4
4
  "description": "A TypeScript library for interacting with VeSync smart home devices",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -264,7 +264,7 @@ export class Helpers {
264
264
  /**
265
265
  * Build request body for second authentication step (login with authorize code)
266
266
  */
267
- static reqBodyAuthStep2(authorizeCode: string, bizToken: string | null, appId: string, terminalId: string, userCountryCode?: string): Record<string, any> {
267
+ static reqBodyAuthStep2(authorizeCode: string, bizToken: string | null, appId: string, terminalId: string, userCountryCode?: string, regionChange?: string): Record<string, any> {
268
268
  const body: Record<string, any> = {
269
269
  'method': 'loginByAuthorizeCode4Vesync',
270
270
  'authorizeCode': authorizeCode,
@@ -285,6 +285,11 @@ export class Helpers {
285
285
  if (bizToken) {
286
286
  body.bizToken = bizToken;
287
287
  }
288
+
289
+ // Only include regionChange if provided
290
+ if (regionChange) {
291
+ body.regionChange = regionChange;
292
+ }
288
293
 
289
294
  return body;
290
295
  }
@@ -573,6 +578,43 @@ export class Helpers {
573
578
  }
574
579
 
575
580
  if (loginResponse.code !== 0) {
581
+ // Handle cross-region error with retry (like pyvesync PR #340)
582
+ if (loginResponse.code === CROSS_REGION_ERROR_CODE) {
583
+ logger.debug('Cross-region error detected in Step 2, retrying with region change token...');
584
+
585
+ const { bizToken: regionChangeToken, countryCode: newCountryCode } = loginResponse.result || {};
586
+
587
+ if (regionChangeToken) {
588
+ // Retry Step 2 with region change token
589
+ const retryBody = {
590
+ ...step2Body,
591
+ bizToken: regionChangeToken,
592
+ regionChange: 'last_region',
593
+ userCountryCode: newCountryCode || userCountryCode
594
+ };
595
+
596
+ logger.debug('Retrying Step 2 with region change token...');
597
+
598
+ const [retryResponse, retryStatus] = await this.callApi(
599
+ '/user/api/accountManage/v1/loginByAuthorizeCode4Vesync',
600
+ 'post',
601
+ retryBody,
602
+ step1Headers,
603
+ manager
604
+ );
605
+
606
+ if (retryResponse && retryResponse.code === 0) {
607
+ const { token, accountID, countryCode } = retryResponse.result || {};
608
+ if (token && accountID) {
609
+ logger.debug('Step 2 retry successful with region change');
610
+ return [true, token, accountID, countryCode || newCountryCode];
611
+ }
612
+ }
613
+
614
+ logger.error('Step 2 retry failed:', retryResponse);
615
+ }
616
+ }
617
+
576
618
  logger.error('Step 2 error code:', loginResponse.code, 'message:', loginResponse.msg);
577
619
  return [false, null, null, null];
578
620
  }