@redonvn/zalo-ads-sdk 0.0.0-stage → 0.2.0
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/README.md +89 -2
- package/dist/index.d.mts +3477 -0
- package/dist/index.d.ts +3477 -0
- package/dist/index.js +3451 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3391 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +53 -4
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @redonvn/zalo-ads-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
SDK TypeScript cho toàn bộ hệ sinh thái Zalo Ads (`ads.zalo.me`, `page.ads.zalo.me`, `form.ads.zalo.me`, `profile.ads.zalo.me`, `api.widget.zalo.me`, `widget.chat.zalo.me`).
|
|
4
|
+
|
|
5
|
+
Hỗ trợ:
|
|
6
|
+
- **Silent OAuth2 PKCE SSO (`ZaloAuthManager`)**: Chỉ cần truyền duy nhất cookie `zpsid` (hoặc nguyên chuỗi Cookie trình duyệt có chứa `zpsid=...`), SDK tự động đổi ra `PLAY_SESSION`, `XSRF-TOKEN`, `zf_id` và `Authorization` token cho từng domain.
|
|
7
|
+
- **Lưu trữ phiên trên RAM hoặc Redis (`ZaloSessionStore`)**:
|
|
8
|
+
- `MemoryZaloSessionStore`: L1 RAM cache có giới hạn số lượng tài khoản theo cơ chế LRU (`maxAccounts`).
|
|
9
|
+
- `RedisZaloSessionStore`: L2 Redis shared cache kèm Distributed Lock (`setNX`), tương thích trực tiếp với `RedisService` của `redai-v201-be-app-staging` và `ioredis`.
|
|
10
|
+
- `TieredZaloSessionStore`: Kết hợp 2 tầng **L1 RAM (0ms) + L2 Redis (chia sẻ giữa nhiều worker/pod + giữ phiên khi restart server)**.
|
|
11
|
+
- **Quản lý Đa Tài Khoản / Đa Integration (`ZaloAdsIntegrationManager`)**: Cô lập phiên theo `integrationId` (hoặc tự động tách `accountKey` từ `zpsid`), tự động phát hiện khi người dùng cập nhật `zpsid` mới (`zpsidFingerprint`) để làm mới phiên.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 1. Sử dụng đơn lẻ (Mặc định tự cache trong RAM theo tài khoản)
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { ZaloAdsSdk } from "@redonvn/zalo-ads-sdk";
|
|
19
|
+
|
|
20
|
+
const sdk = new ZaloAdsSdk({
|
|
21
|
+
zpsid: "g_kQ.330114198.434.TmeTxcUlEkNnYvVsQwywvHBSIDPKdmlKKvu3tSnRUwvf5VqMPj4FAmslEkK",
|
|
22
|
+
// Tùy chọn: truyền thêm integrationId và redis nếu muốn lưu thẳng xuống Redis
|
|
23
|
+
// integrationId: "eb18102a-...",
|
|
24
|
+
// redis: this.redisService,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const whoami = await sdk.ads.getApiWhoami();
|
|
28
|
+
const rules = await sdk.ads.listAutomatedRules({ ruleName: "" });
|
|
29
|
+
const oas = await sdk.instantPages.listOfficialAccounts();
|
|
30
|
+
const formSession = await sdk.forms.getAuthSession();
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 2. Quản lý nhiều Integration trên Backend (`redai-v201-be-app-staging`) với RAM + Redis
|
|
36
|
+
|
|
37
|
+
Trên `redai-v201-be-app-staging`, mỗi người dùng có thể kết nối nhiều Zalo Ads Integration (`integrationId`). Sử dụng `ZaloAdsIntegrationManager` để quản lý tập trung:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { Injectable } from "@nestjs/common";
|
|
41
|
+
import { RedisService } from "@shared/services/redis.service";
|
|
42
|
+
import { GenericIntegrationCredentialService } from "@modules/integration/shared/credentials/generic-integration-credential.service";
|
|
43
|
+
import { ZaloAdsIntegrationManager, ZaloAdsSdk } from "@redonvn/zalo-ads-sdk";
|
|
44
|
+
|
|
45
|
+
@Injectable()
|
|
46
|
+
export class ZaloAdsClientFactoryService {
|
|
47
|
+
public readonly manager: ZaloAdsIntegrationManager;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
private readonly redisService: RedisService,
|
|
51
|
+
private readonly credentialService: GenericIntegrationCredentialService,
|
|
52
|
+
) {
|
|
53
|
+
this.manager = new ZaloAdsIntegrationManager({
|
|
54
|
+
// Truyền trực tiếp RedisService của backend -> Tự động bật Tiered Store (L1 RAM + L2 Redis + Distributed Lock)
|
|
55
|
+
// Nếu không truyền `redis`, Manager tự động dùng MemoryZaloSessionStore (RAM LRU).
|
|
56
|
+
redis: this.redisService,
|
|
57
|
+
redisKeyPrefix: "zalo_ads:sso",
|
|
58
|
+
maxAccountsInMemory: 500,
|
|
59
|
+
|
|
60
|
+
// Tự động lấy zpsid đã giải mã từ Credential Vault theo integrationId
|
|
61
|
+
resolveCredentials: async (integrationId: string) => {
|
|
62
|
+
const bound = await this.credentialService.resolveBound(integrationId);
|
|
63
|
+
const creds = bound.credentials as Record<string, string>;
|
|
64
|
+
return {
|
|
65
|
+
zpsid: creds.zpsid || creds.cookie,
|
|
66
|
+
zoawSek: creds.zoawSek,
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Lấy SDK thực thi cho bất kỳ integrationId nào của người dùng */
|
|
73
|
+
async getSdkForIntegration(integrationId: string): Promise<ZaloAdsSdk> {
|
|
74
|
+
return this.manager.forIntegration(integrationId);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Dùng khi người dùng bấm "Kết nối / Kiểm tra kết nối" (Auth Connect Flow) */
|
|
78
|
+
async verifyAndWarmup(integrationId: string, zpsidOrCookie: string) {
|
|
79
|
+
return this.manager.verifyIntegration({
|
|
80
|
+
integrationId,
|
|
81
|
+
zpsid: zpsidOrCookie,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Xóa sạch phiên trong RAM + Redis khi người dùng sửa/xóa Integration */
|
|
86
|
+
async invalidate(integrationId: string): Promise<void> {
|
|
87
|
+
await this.manager.invalidateIntegration(integrationId);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|