tsvesync 1.0.99 → 1.0.101

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.
@@ -442,9 +442,9 @@ class Helpers {
442
442
  const step2Body = this.reqBodyAuthStep2(authorizeCode, bizToken, appId, terminalId, userCountryCode);
443
443
  logger_1.logger.debug('Step 2: Logging in with authorization code...');
444
444
  const [loginResponse, loginStatus] = await this.callApi('/user/api/accountManage/v1/loginByAuthorizeCode4Vesync', 'post', step2Body, step1Headers, manager);
445
- setApiBaseUrl(originalUrl); // Restore original URL
446
445
  if (!loginResponse || loginStatus !== 200) {
447
446
  logger_1.logger.error('Step 2 failed:', loginResponse);
447
+ setApiBaseUrl(originalUrl); // Restore original URL on failure
448
448
  return [false, null, null, null];
449
449
  }
450
450
  if (loginResponse.code !== 0) {
@@ -452,29 +452,52 @@ class Helpers {
452
452
  if (loginResponse.code === exports.CROSS_REGION_ERROR_CODE) {
453
453
  logger_1.logger.debug('Cross-region error detected in Step 2, retrying with region change token...');
454
454
  const { bizToken: regionChangeToken, countryCode: newCountryCode } = loginResponse.result || {};
455
- if (regionChangeToken) {
456
- // Retry Step 2 with region change token
455
+ if (regionChangeToken && newCountryCode) {
456
+ // Retry Step 2 with region change token on the SAME endpoint
457
+ // The region change token tells the server to handle the cross-region authentication
457
458
  const retryBody = {
458
- ...step2Body,
459
+ method: 'loginByAuthorizeCode4Vesync',
460
+ authorizeCode: authorizeCode,
461
+ acceptLanguage: 'en',
462
+ clientInfo: exports.CLIENT_INFO,
463
+ clientType: 'vesyncApp',
464
+ clientVersion: exports.CLIENT_VERSION,
465
+ debugMode: false,
466
+ emailSubscriptions: false,
467
+ osInfo: exports.PHONE_OS,
468
+ terminalId: terminalId,
469
+ timeZone: exports.DEFAULT_TZ,
470
+ userCountryCode: newCountryCode,
471
+ traceId: `APP${appId}${Math.floor(Date.now() / 1000)}`,
459
472
  bizToken: regionChangeToken,
460
- regionChange: 'last_region',
461
- userCountryCode: newCountryCode || userCountryCode
473
+ regionChange: 'last_region'
462
474
  };
463
- logger_1.logger.debug('Retrying Step 2 with region change token...');
475
+ logger_1.logger.debug('Retrying Step 2 with region change token...', {
476
+ newCountryCode,
477
+ currentEndpoint: getApiBaseUrl()
478
+ });
464
479
  const [retryResponse, retryStatus] = await this.callApi('/user/api/accountManage/v1/loginByAuthorizeCode4Vesync', 'post', retryBody, step1Headers, manager);
465
480
  if (retryResponse && retryResponse.code === 0) {
466
481
  const { token, accountID, countryCode } = retryResponse.result || {};
467
482
  if (token && accountID) {
468
483
  logger_1.logger.debug('Step 2 retry successful with region change');
484
+ setApiBaseUrl(originalUrl); // Restore original URL on success
469
485
  return [true, token, accountID, countryCode || newCountryCode];
470
486
  }
471
487
  }
472
488
  logger_1.logger.error('Step 2 retry failed:', retryResponse);
489
+ setApiBaseUrl(originalUrl); // Restore original URL on failure
490
+ return [false, null, null, null];
491
+ }
492
+ else {
493
+ logger_1.logger.error('No region change token in cross-region error response');
473
494
  }
474
495
  }
475
496
  logger_1.logger.error('Step 2 error code:', loginResponse.code, 'message:', loginResponse.msg);
497
+ setApiBaseUrl(originalUrl); // Restore original URL on error
476
498
  return [false, null, null, null];
477
499
  }
500
+ setApiBaseUrl(originalUrl); // Restore original URL on success
478
501
  const { token, accountID, countryCode } = loginResponse.result || {};
479
502
  if (!token || !accountID) {
480
503
  logger_1.logger.error('Missing required fields in step 2 response:', loginResponse.result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsvesync",
3
- "version": "1.0.99",
3
+ "version": "1.0.101",
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",
@@ -570,10 +570,9 @@ export class Helpers {
570
570
  manager
571
571
  );
572
572
 
573
- setApiBaseUrl(originalUrl); // Restore original URL
574
-
575
573
  if (!loginResponse || loginStatus !== 200) {
576
574
  logger.error('Step 2 failed:', loginResponse);
575
+ setApiBaseUrl(originalUrl); // Restore original URL on failure
577
576
  return [false, null, null, null];
578
577
  }
579
578
 
@@ -584,16 +583,31 @@ export class Helpers {
584
583
 
585
584
  const { bizToken: regionChangeToken, countryCode: newCountryCode } = loginResponse.result || {};
586
585
 
587
- if (regionChangeToken) {
588
- // Retry Step 2 with region change token
586
+ if (regionChangeToken && newCountryCode) {
587
+ // Retry Step 2 with region change token on the SAME endpoint
588
+ // The region change token tells the server to handle the cross-region authentication
589
589
  const retryBody = {
590
- ...step2Body,
590
+ method: 'loginByAuthorizeCode4Vesync',
591
+ authorizeCode: authorizeCode,
592
+ acceptLanguage: 'en',
593
+ clientInfo: CLIENT_INFO,
594
+ clientType: 'vesyncApp',
595
+ clientVersion: CLIENT_VERSION,
596
+ debugMode: false,
597
+ emailSubscriptions: false,
598
+ osInfo: PHONE_OS,
599
+ terminalId: terminalId,
600
+ timeZone: DEFAULT_TZ,
601
+ userCountryCode: newCountryCode,
602
+ traceId: `APP${appId}${Math.floor(Date.now() / 1000)}`,
591
603
  bizToken: regionChangeToken,
592
- regionChange: 'last_region',
593
- userCountryCode: newCountryCode || userCountryCode
604
+ regionChange: 'last_region'
594
605
  };
595
606
 
596
- logger.debug('Retrying Step 2 with region change token...');
607
+ logger.debug('Retrying Step 2 with region change token...', {
608
+ newCountryCode,
609
+ currentEndpoint: getApiBaseUrl()
610
+ });
597
611
 
598
612
  const [retryResponse, retryStatus] = await this.callApi(
599
613
  '/user/api/accountManage/v1/loginByAuthorizeCode4Vesync',
@@ -607,18 +621,26 @@ export class Helpers {
607
621
  const { token, accountID, countryCode } = retryResponse.result || {};
608
622
  if (token && accountID) {
609
623
  logger.debug('Step 2 retry successful with region change');
624
+ setApiBaseUrl(originalUrl); // Restore original URL on success
610
625
  return [true, token, accountID, countryCode || newCountryCode];
611
626
  }
612
627
  }
613
628
 
614
629
  logger.error('Step 2 retry failed:', retryResponse);
630
+ setApiBaseUrl(originalUrl); // Restore original URL on failure
631
+ return [false, null, null, null];
632
+ } else {
633
+ logger.error('No region change token in cross-region error response');
615
634
  }
616
635
  }
617
636
 
618
637
  logger.error('Step 2 error code:', loginResponse.code, 'message:', loginResponse.msg);
638
+ setApiBaseUrl(originalUrl); // Restore original URL on error
619
639
  return [false, null, null, null];
620
640
  }
621
641
 
642
+ setApiBaseUrl(originalUrl); // Restore original URL on success
643
+
622
644
  const { token, accountID, countryCode } = loginResponse.result || {};
623
645
 
624
646
  if (!token || !accountID) {