@redonvn/zalo-ads-sdk 0.2.16 → 0.2.17

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/dist/index.mjs CHANGED
@@ -284,6 +284,7 @@ function parseZaloCookieInput(rawInput) {
284
284
  }
285
285
  var ZaloAuthManager = class {
286
286
  explicitAccountKey;
287
+ accountScope;
287
288
  zpsid;
288
289
  getZpsidFn;
289
290
  extraCookies;
@@ -301,6 +302,7 @@ var ZaloAuthManager = class {
301
302
  throw new Error("A Fetch implementation is required (Node.js 18+ provides global fetch).");
302
303
  }
303
304
  this.explicitAccountKey = normalizedOptions.integrationId ?? normalizedOptions.accountKey;
305
+ this.accountScope = normalizedOptions.accountScope;
304
306
  this.extraCookies = { ...normalizedOptions.extraCookies ?? {} };
305
307
  if (normalizedOptions.zpsid) {
306
308
  const parsed = parseZaloCookieInput(normalizedOptions.zpsid);
@@ -348,6 +350,12 @@ var ZaloAuthManager = class {
348
350
  getSessionStore() {
349
351
  return this.sessionStore;
350
352
  }
353
+ getAccountScope() {
354
+ return this.accountScope;
355
+ }
356
+ setAccountScope(scope) {
357
+ this.accountScope = scope;
358
+ }
351
359
  /**
352
360
  * Cập nhật `zpsid` mới và xóa toàn bộ cache phiên cũ trên cả RAM lẫn Redis.
353
361
  */
@@ -637,11 +645,58 @@ var ZaloAuthManager = class {
637
645
  const whoamiJson = await whoamiRes.json().catch(() => null);
638
646
  if (whoamiJson?.data) {
639
647
  adsProfile = whoamiJson.data;
640
- if (adsProfile.id) {
641
- cookies["ads_user_id"] = String(adsProfile.id);
648
+ }
649
+ if (this.accountScope?.memberId && String(this.accountScope.memberId) !== "0" && !this.accountScope.isRoot && !explicitPlaySession) {
650
+ const memberIdStr = String(this.accountScope.memberId);
651
+ const switchUrls = [];
652
+ if (this.accountScope.accountType === "ACCESSOR") {
653
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
654
+ } else if (this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || this.accountScope.accountType === "AGENCY_SUB_ACCOUNT") {
655
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
656
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
657
+ } else {
658
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
659
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
660
+ }
661
+ for (const switchUrl of switchUrls) {
662
+ try {
663
+ const switchRes = await this.fetchImpl(switchUrl, {
664
+ method: "GET",
665
+ headers: {
666
+ Cookie: this.formatCookieHeader(cookies),
667
+ "User-Agent": this.userAgent,
668
+ Accept: "application/json, text/plain, */*"
669
+ },
670
+ redirect: "manual"
671
+ });
672
+ this.mergeSetCookies(switchRes, cookies);
673
+ const location = switchRes.headers.get("location") || "";
674
+ if (location && !location.includes("/client/denied")) {
675
+ const switchedWhoamiRes = await this.fetchImpl("https://ads.zalo.me/api/whoami", {
676
+ method: "GET",
677
+ headers: {
678
+ Cookie: this.formatCookieHeader(cookies),
679
+ "User-Agent": this.userAgent,
680
+ Accept: "application/json"
681
+ }
682
+ });
683
+ this.mergeSetCookies(switchedWhoamiRes, cookies);
684
+ const switchedJson = await switchedWhoamiRes.json().catch(() => null);
685
+ if (switchedJson?.data) {
686
+ adsProfile = switchedJson.data;
687
+ }
688
+ break;
689
+ }
690
+ } catch {
691
+ }
642
692
  }
693
+ }
694
+ if (this.accountScope?.memberId && String(this.accountScope.memberId) !== "0") {
695
+ cookies["ads_user_id"] = String(this.accountScope.memberId);
643
696
  } else if (this.extraCookies?.ads_user_id) {
644
- cookies["ads_user_id"] = this.extraCookies.ads_user_id;
697
+ cookies["ads_user_id"] = String(this.extraCookies.ads_user_id);
698
+ } else if (adsProfile?.id) {
699
+ cookies["ads_user_id"] = String(adsProfile.id);
645
700
  }
646
701
  } else if (domain === "page.ads.zalo.me") {
647
702
  await this.followOAuthRedirects("https://page.ads.zalo.me/api/login", cookies, false);
@@ -843,22 +898,31 @@ var ZaloSdkHttpClient = class {
843
898
  authByHost = /* @__PURE__ */ new Map();
844
899
  authManagerInstance;
845
900
  autoRetryOn401;
901
+ accountScope;
846
902
  constructor(options = {}) {
847
903
  this.fetchImpl = options.fetch ?? globalThis.fetch;
848
904
  if (!this.fetchImpl) throw new Error("A Fetch implementation is required (Node.js 18+ provides global fetch).");
849
905
  this.baseUrls = { ...DEFAULT_BASE_URLS, ...options.baseUrls ?? {} };
850
906
  this.autoRetryOn401 = options.autoRetryOn401 ?? true;
907
+ this.accountScope = options.accountScope;
851
908
  if (options.authManager) {
852
909
  this.authManagerInstance = options.authManager;
910
+ if (options.accountScope && !this.authManagerInstance.getAccountScope()) {
911
+ this.authManagerInstance.setAccountScope(options.accountScope);
912
+ }
853
913
  } else if (options.zpsid || options.ssoAuth) {
854
914
  const extraCookies = {
855
915
  ...options.ssoAuth?.extraCookies ?? {},
856
916
  ...options.zoawSek ? { zoaw_sek: options.zoawSek } : {}
857
917
  };
918
+ if (options.accountScope?.memberId && String(options.accountScope.memberId) !== "0") {
919
+ extraCookies["ads_user_id"] = String(options.accountScope.memberId);
920
+ }
858
921
  this.authManagerInstance = new ZaloAuthManager({
859
922
  fetch: this.fetchImpl,
860
923
  integrationId: options.integrationId,
861
924
  accountKey: options.accountKey,
925
+ accountScope: options.accountScope ?? options.ssoAuth?.accountScope,
862
926
  sessionStore: options.sessionStore,
863
927
  redis: options.redis,
864
928
  redisKeyPrefix: options.redisKeyPrefix,
@@ -975,6 +1039,16 @@ var ZaloSdkHttpClient = class {
975
1039
  add("header_param", operation.headerFields);
976
1040
  add("cookie_param", operation.cookieFields);
977
1041
  add("body", operation.bodyFields);
1042
+ if (this.accountScope && output.query_param) {
1043
+ const query = output.query_param;
1044
+ const isAgencyContext = this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" || this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || Boolean(this.accountScope.agencyId);
1045
+ if (operation.queryFields.includes("agencyId") && query.agencyId === void 0 && isAgencyContext && this.accountScope.agencyId) {
1046
+ query.agencyId = Number(this.accountScope.agencyId);
1047
+ }
1048
+ if (operation.queryFields.includes("createdBy") && query.createdBy === void 0 && this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" && this.accountScope.memberId && String(this.accountScope.memberId) !== "0") {
1049
+ query.createdBy = Number(this.accountScope.memberId);
1050
+ }
1051
+ }
978
1052
  return output;
979
1053
  }
980
1054
  async resolveFile(input) {
@@ -3881,7 +3955,6 @@ var ZaloReportsService = class {
3881
3955
  getReportapiV3Campaigns(request, options) {
3882
3956
  const normalized = {
3883
3957
  filter: "[]",
3884
- agencyId: 0,
3885
3958
  ...request
3886
3959
  };
3887
3960
  return this.client.requestWithoutBody(OP_ZaloAdsGetReportapiV3Campaigns, this.client.toTransportInput(OP_ZaloAdsGetReportapiV3Campaigns, normalized), options);
@@ -7093,6 +7166,21 @@ var ZaloAdsSdk = class {
7093
7166
  get authManager() {
7094
7167
  return this.client.authManager;
7095
7168
  }
7169
+ /**
7170
+ * Phạm vi tài khoản quảng cáo cô lập (Isolated Workspace) đang gắn với instance SDK.
7171
+ */
7172
+ get accountScope() {
7173
+ return this.client.accountScope;
7174
+ }
7175
+ /**
7176
+ * Cập nhật phạm vi tài khoản quảng cáo cô lập.
7177
+ */
7178
+ setAccountScope(scope) {
7179
+ this.client.accountScope = scope;
7180
+ if (this.client.authManager) {
7181
+ this.client.authManager.setAccountScope(scope);
7182
+ }
7183
+ }
7096
7184
  /**
7097
7185
  * Cập nhật cookie `zpsid` gốc (hoặc chuỗi Cookie trình duyệt) để tự động xác thực cho toàn bộ các domain Zalo Ads.
7098
7186
  */
@@ -7172,6 +7260,9 @@ var ZaloAdsIntegrationManager = class {
7172
7260
  existing.sdk.setZpsid(request.zpsid);
7173
7261
  existing.zpsid = cleanZpsid;
7174
7262
  }
7263
+ if (request.accountScope) {
7264
+ existing.sdk.setAccountScope(request.accountScope);
7265
+ }
7175
7266
  this.sdkPool.set(integrationId, existing);
7176
7267
  return existing.sdk;
7177
7268
  }
@@ -7186,15 +7277,20 @@ var ZaloAdsIntegrationManager = class {
7186
7277
  ...request.extraCookies ?? {},
7187
7278
  ...request.zoawSek ? { zoaw_sek: request.zoawSek } : {}
7188
7279
  };
7280
+ if (request.accountScope?.memberId && String(request.accountScope.memberId) !== "0") {
7281
+ extraCookies.ads_user_id = String(request.accountScope.memberId);
7282
+ }
7189
7283
  const sdk = new ZaloAdsSdk({
7190
7284
  integrationId,
7191
7285
  zpsid: cleanZpsid,
7286
+ accountScope: request.accountScope,
7192
7287
  sessionStore: this.sessionStore,
7193
7288
  fetch: this.fetchImpl,
7194
7289
  baseUrls: this.baseUrls,
7195
7290
  ssoAuth: {
7196
7291
  integrationId,
7197
7292
  zpsid: cleanZpsid,
7293
+ accountScope: request.accountScope,
7198
7294
  extraCookies,
7199
7295
  sessionStore: this.sessionStore,
7200
7296
  sessionTtlMs: this.sessionTtlMs,
@@ -7278,7 +7374,8 @@ var ZaloAdsIntegrationManager = class {
7278
7374
  integrationId: accountKey,
7279
7375
  zpsid: params.zpsid,
7280
7376
  zoawSek: params.zoawSek,
7281
- extraCookies: params.extraCookies
7377
+ extraCookies: params.extraCookies,
7378
+ accountScope: params.accountScope
7282
7379
  });
7283
7380
  const sessions = await sdk.authenticateAll([
7284
7381
  "ads.zalo.me",