@redonvn/zalo-ads-sdk 0.2.16 → 0.2.18

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
  */
@@ -562,6 +570,13 @@ var ZaloAuthManager = class {
562
570
  if (session.zpsidFingerprint && session.zpsidFingerprint !== currentFingerprint) {
563
571
  return false;
564
572
  }
573
+ if (session.domain === "ads.zalo.me" && this.accountScope?.memberId && String(this.accountScope.memberId) !== "0" && !this.accountScope.isRoot) {
574
+ const expectedMemberId = String(this.accountScope.memberId);
575
+ const activeMemberId = String(session.playSessionPayload?.data?.id ?? "");
576
+ if (activeMemberId && activeMemberId !== expectedMemberId) {
577
+ return false;
578
+ }
579
+ }
565
580
  return true;
566
581
  }
567
582
  async resolveZpsid() {
@@ -637,11 +652,64 @@ var ZaloAuthManager = class {
637
652
  const whoamiJson = await whoamiRes.json().catch(() => null);
638
653
  if (whoamiJson?.data) {
639
654
  adsProfile = whoamiJson.data;
640
- if (adsProfile.id) {
641
- cookies["ads_user_id"] = String(adsProfile.id);
655
+ }
656
+ const currentActiveId = String(
657
+ decodePlaySessionJwt(cookies["PLAY_SESSION"] || "")?.data?.id ?? ""
658
+ );
659
+ const shouldSwitchContext = Boolean(
660
+ this.accountScope?.memberId && String(this.accountScope.memberId) !== "0" && !this.accountScope.isRoot && (!explicitPlaySession || currentActiveId && currentActiveId !== String(this.accountScope.memberId))
661
+ );
662
+ if (shouldSwitchContext) {
663
+ const memberIdStr = String(this.accountScope.memberId);
664
+ const switchUrls = [];
665
+ if (this.accountScope.accountType === "ACCESSOR") {
666
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
667
+ } else if (this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || this.accountScope.accountType === "AGENCY_SUB_ACCOUNT") {
668
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
669
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
670
+ } else {
671
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
672
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
642
673
  }
674
+ for (const switchUrl of switchUrls) {
675
+ try {
676
+ const switchRes = await this.fetchImpl(switchUrl, {
677
+ method: "GET",
678
+ headers: {
679
+ Cookie: this.formatCookieHeader(cookies),
680
+ "User-Agent": this.userAgent,
681
+ Accept: "application/json, text/plain, */*"
682
+ },
683
+ redirect: "manual"
684
+ });
685
+ this.mergeSetCookies(switchRes, cookies);
686
+ const location = switchRes.headers.get("location") || "";
687
+ if (location && !location.includes("/client/denied")) {
688
+ const switchedWhoamiRes = await this.fetchImpl("https://ads.zalo.me/api/whoami", {
689
+ method: "GET",
690
+ headers: {
691
+ Cookie: this.formatCookieHeader(cookies),
692
+ "User-Agent": this.userAgent,
693
+ Accept: "application/json"
694
+ }
695
+ });
696
+ this.mergeSetCookies(switchedWhoamiRes, cookies);
697
+ const switchedJson = await switchedWhoamiRes.json().catch(() => null);
698
+ if (switchedJson?.data) {
699
+ adsProfile = switchedJson.data;
700
+ }
701
+ break;
702
+ }
703
+ } catch {
704
+ }
705
+ }
706
+ }
707
+ if (this.accountScope?.memberId && String(this.accountScope.memberId) !== "0") {
708
+ cookies["ads_user_id"] = String(this.accountScope.memberId);
643
709
  } else if (this.extraCookies?.ads_user_id) {
644
- cookies["ads_user_id"] = this.extraCookies.ads_user_id;
710
+ cookies["ads_user_id"] = String(this.extraCookies.ads_user_id);
711
+ } else if (adsProfile?.id) {
712
+ cookies["ads_user_id"] = String(adsProfile.id);
645
713
  }
646
714
  } else if (domain === "page.ads.zalo.me") {
647
715
  await this.followOAuthRedirects("https://page.ads.zalo.me/api/login", cookies, false);
@@ -843,22 +911,31 @@ var ZaloSdkHttpClient = class {
843
911
  authByHost = /* @__PURE__ */ new Map();
844
912
  authManagerInstance;
845
913
  autoRetryOn401;
914
+ accountScope;
846
915
  constructor(options = {}) {
847
916
  this.fetchImpl = options.fetch ?? globalThis.fetch;
848
917
  if (!this.fetchImpl) throw new Error("A Fetch implementation is required (Node.js 18+ provides global fetch).");
849
918
  this.baseUrls = { ...DEFAULT_BASE_URLS, ...options.baseUrls ?? {} };
850
919
  this.autoRetryOn401 = options.autoRetryOn401 ?? true;
920
+ this.accountScope = options.accountScope;
851
921
  if (options.authManager) {
852
922
  this.authManagerInstance = options.authManager;
923
+ if (options.accountScope && !this.authManagerInstance.getAccountScope()) {
924
+ this.authManagerInstance.setAccountScope(options.accountScope);
925
+ }
853
926
  } else if (options.zpsid || options.ssoAuth) {
854
927
  const extraCookies = {
855
928
  ...options.ssoAuth?.extraCookies ?? {},
856
929
  ...options.zoawSek ? { zoaw_sek: options.zoawSek } : {}
857
930
  };
931
+ if (options.accountScope?.memberId && String(options.accountScope.memberId) !== "0") {
932
+ extraCookies["ads_user_id"] = String(options.accountScope.memberId);
933
+ }
858
934
  this.authManagerInstance = new ZaloAuthManager({
859
935
  fetch: this.fetchImpl,
860
936
  integrationId: options.integrationId,
861
937
  accountKey: options.accountKey,
938
+ accountScope: options.accountScope ?? options.ssoAuth?.accountScope,
862
939
  sessionStore: options.sessionStore,
863
940
  redis: options.redis,
864
941
  redisKeyPrefix: options.redisKeyPrefix,
@@ -975,6 +1052,16 @@ var ZaloSdkHttpClient = class {
975
1052
  add("header_param", operation.headerFields);
976
1053
  add("cookie_param", operation.cookieFields);
977
1054
  add("body", operation.bodyFields);
1055
+ if (this.accountScope && output.query_param) {
1056
+ const query = output.query_param;
1057
+ const isAgencyContext = this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" || this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || Boolean(this.accountScope.agencyId);
1058
+ if (operation.queryFields.includes("agencyId") && query.agencyId === void 0 && isAgencyContext && this.accountScope.agencyId) {
1059
+ query.agencyId = Number(this.accountScope.agencyId);
1060
+ }
1061
+ if (operation.queryFields.includes("createdBy") && query.createdBy === void 0 && this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" && this.accountScope.memberId && String(this.accountScope.memberId) !== "0") {
1062
+ query.createdBy = Number(this.accountScope.memberId);
1063
+ }
1064
+ }
978
1065
  return output;
979
1066
  }
980
1067
  async resolveFile(input) {
@@ -3881,7 +3968,6 @@ var ZaloReportsService = class {
3881
3968
  getReportapiV3Campaigns(request, options) {
3882
3969
  const normalized = {
3883
3970
  filter: "[]",
3884
- agencyId: 0,
3885
3971
  ...request
3886
3972
  };
3887
3973
  return this.client.requestWithoutBody(OP_ZaloAdsGetReportapiV3Campaigns, this.client.toTransportInput(OP_ZaloAdsGetReportapiV3Campaigns, normalized), options);
@@ -7093,6 +7179,21 @@ var ZaloAdsSdk = class {
7093
7179
  get authManager() {
7094
7180
  return this.client.authManager;
7095
7181
  }
7182
+ /**
7183
+ * Phạm vi tài khoản quảng cáo cô lập (Isolated Workspace) đang gắn với instance SDK.
7184
+ */
7185
+ get accountScope() {
7186
+ return this.client.accountScope;
7187
+ }
7188
+ /**
7189
+ * Cập nhật phạm vi tài khoản quảng cáo cô lập.
7190
+ */
7191
+ setAccountScope(scope) {
7192
+ this.client.accountScope = scope;
7193
+ if (this.client.authManager) {
7194
+ this.client.authManager.setAccountScope(scope);
7195
+ }
7196
+ }
7096
7197
  /**
7097
7198
  * 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
7199
  */
@@ -7172,6 +7273,9 @@ var ZaloAdsIntegrationManager = class {
7172
7273
  existing.sdk.setZpsid(request.zpsid);
7173
7274
  existing.zpsid = cleanZpsid;
7174
7275
  }
7276
+ if (request.accountScope) {
7277
+ existing.sdk.setAccountScope(request.accountScope);
7278
+ }
7175
7279
  this.sdkPool.set(integrationId, existing);
7176
7280
  return existing.sdk;
7177
7281
  }
@@ -7186,15 +7290,20 @@ var ZaloAdsIntegrationManager = class {
7186
7290
  ...request.extraCookies ?? {},
7187
7291
  ...request.zoawSek ? { zoaw_sek: request.zoawSek } : {}
7188
7292
  };
7293
+ if (request.accountScope?.memberId && String(request.accountScope.memberId) !== "0") {
7294
+ extraCookies.ads_user_id = String(request.accountScope.memberId);
7295
+ }
7189
7296
  const sdk = new ZaloAdsSdk({
7190
7297
  integrationId,
7191
7298
  zpsid: cleanZpsid,
7299
+ accountScope: request.accountScope,
7192
7300
  sessionStore: this.sessionStore,
7193
7301
  fetch: this.fetchImpl,
7194
7302
  baseUrls: this.baseUrls,
7195
7303
  ssoAuth: {
7196
7304
  integrationId,
7197
7305
  zpsid: cleanZpsid,
7306
+ accountScope: request.accountScope,
7198
7307
  extraCookies,
7199
7308
  sessionStore: this.sessionStore,
7200
7309
  sessionTtlMs: this.sessionTtlMs,
@@ -7278,7 +7387,8 @@ var ZaloAdsIntegrationManager = class {
7278
7387
  integrationId: accountKey,
7279
7388
  zpsid: params.zpsid,
7280
7389
  zoawSek: params.zoawSek,
7281
- extraCookies: params.extraCookies
7390
+ extraCookies: params.extraCookies,
7391
+ accountScope: params.accountScope
7282
7392
  });
7283
7393
  const sessions = await sdk.authenticateAll([
7284
7394
  "ads.zalo.me",