@warriorteam/redai-zalo-sdk 1.12.0 → 1.12.1

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.
@@ -0,0 +1,265 @@
1
+ # RedAI Zalo SDK Architecture
2
+
3
+ ## Overview
4
+
5
+ RedAI Zalo SDK là một TypeScript SDK hoàn chỉnh cho các API của Zalo, được thiết kế với kiến trúc modular và type-safe.
6
+
7
+ ## Cấu trúc thư mục
8
+
9
+ ```
10
+ redai-zalo-sdk/
11
+ ├── src/
12
+ │ ├── types/ # Type definitions
13
+ │ │ ├── common.ts # Common types và interfaces
14
+ │ │ ├── auth.ts # Authentication types
15
+ │ │ ├── oa.ts # Official Account types
16
+ │ │ ├── message.ts # Message types
17
+ │ │ └── webhook.ts # Webhook types
18
+ │ ├── clients/ # HTTP clients
19
+ │ │ ├── base-client.ts # Base HTTP client
20
+ │ │ └── zalo-client.ts # Zalo-specific client
21
+ │ ├── services/ # Business logic services
22
+ │ │ ├── auth.service.ts # Authentication service
23
+ │ │ ├── oa.service.ts # Official Account service
24
+ │ │ └── message.service.ts # Message service
25
+ │ ├── utils/ # Utility functions
26
+ │ ├── index.ts # Main export file
27
+ │ └── zalo-sdk.ts # Main SDK class
28
+ ├── examples/ # Usage examples
29
+ ├── dist/ # Build output
30
+ ├── package.json
31
+ ├── tsconfig.json
32
+ └── README.md
33
+ ```
34
+
35
+ ## Kiến trúc chính
36
+
37
+ ### 1. Layered Architecture
38
+
39
+ SDK được thiết kế theo kiến trúc phân lớp:
40
+
41
+ ```
42
+ ┌─────────────────┐
43
+ │ Main SDK │ ← ZaloSDK class (public API)
44
+ ├─────────────────┤
45
+ │ Services │ ← Business logic (AuthService, OAService, MessageService)
46
+ ├─────────────────┤
47
+ │ Clients │ ← HTTP communication (BaseClient, ZaloClient)
48
+ ├─────────────────┤
49
+ │ Types │ ← Type definitions và interfaces
50
+ └─────────────────┘
51
+ ```
52
+
53
+ ### 2. Core Components
54
+
55
+ #### ZaloSDK (Main Class)
56
+ - Entry point cho tất cả các chức năng
57
+ - Khởi tạo và quản lý các services
58
+ - Cung cấp quick methods cho các tác vụ phổ biến
59
+ - Quản lý configuration và lifecycle
60
+
61
+ #### BaseClient
62
+ - HTTP client cơ bản với axios
63
+ - Retry mechanism
64
+ - Error handling
65
+ - Request/response logging
66
+ - File upload support
67
+
68
+ #### ZaloClient
69
+ - Extends BaseClient
70
+ - Zalo-specific endpoints
71
+ - API versioning support
72
+ - OAuth và ZNS endpoint handling
73
+
74
+ #### Services
75
+ - **AuthService**: OAuth flows, token management
76
+ - **OAService**: Official Account operations
77
+ - **MessageService**: Message sending và file upload
78
+
79
+ ### 3. Type System
80
+
81
+ #### Comprehensive Types
82
+ - Tất cả API responses được type-safe
83
+ - Union types cho message types
84
+ - Enum cho constants
85
+ - Generic types cho reusability
86
+
87
+ #### Error Handling
88
+ - Custom `ZaloSDKError` class
89
+ - Structured error information
90
+ - Error code mapping
91
+
92
+ ### 4. Configuration
93
+
94
+ ```typescript
95
+ interface ZaloSDKConfig {
96
+ appId: string;
97
+ appSecret: string;
98
+ timeout?: number;
99
+ debug?: boolean;
100
+ apiBaseUrl?: string;
101
+ retry?: {
102
+ attempts?: number;
103
+ delay?: number;
104
+ };
105
+ }
106
+ ```
107
+
108
+ ## Design Patterns
109
+
110
+ ### 1. Builder Pattern
111
+ SDK configuration với default values và validation.
112
+
113
+ ### 2. Service Pattern
114
+ Mỗi service chịu trách nhiệm cho một domain cụ thể.
115
+
116
+ ### 3. Factory Pattern
117
+ Client creation và service initialization.
118
+
119
+ ### 4. Strategy Pattern
120
+ Different authentication flows (OA vs Social).
121
+
122
+ ### 5. Observer Pattern
123
+ Webhook event handling (planned).
124
+
125
+ ## Key Features
126
+
127
+ ### 1. Type Safety
128
+ - Full TypeScript support
129
+ - Comprehensive type definitions
130
+ - Generic types cho flexibility
131
+ - Union types cho message variants
132
+
133
+ ### 2. Error Handling
134
+ - Structured error responses
135
+ - Retry mechanism
136
+ - Detailed error information
137
+ - Error code mapping
138
+
139
+ ### 3. Authentication
140
+ - OAuth 2.0 flows
141
+ - PKCE support
142
+ - Token refresh
143
+ - Multiple auth scopes
144
+
145
+ ### 4. Extensibility
146
+ - Modular architecture
147
+ - Easy to add new services
148
+ - Plugin-ready design
149
+ - Custom request support
150
+
151
+ ### 5. Developer Experience
152
+ - IntelliSense support
153
+ - Comprehensive documentation
154
+ - Usage examples
155
+ - Debug logging
156
+
157
+ ## API Design Principles
158
+
159
+ ### 1. Consistency
160
+ - Consistent naming conventions
161
+ - Uniform error handling
162
+ - Standard response formats
163
+
164
+ ### 2. Simplicity
165
+ - Easy-to-use public API
166
+ - Quick methods cho common tasks
167
+ - Sensible defaults
168
+
169
+ ### 3. Flexibility
170
+ - Low-level access khi cần
171
+ - Custom request support
172
+ - Configurable behavior
173
+
174
+ ### 4. Reliability
175
+ - Retry mechanisms
176
+ - Error recovery
177
+ - Connection testing
178
+
179
+ ## Future Enhancements
180
+
181
+ ### 1. Additional Services
182
+ - ZNS Service (Zalo Notification Service)
183
+ - User Management Service
184
+ - Group Management Service
185
+ - Webhook Service
186
+
187
+ ### 2. Advanced Features
188
+ - Rate limiting
189
+ - Caching
190
+ - Batch operations
191
+ - Real-time subscriptions
192
+
193
+ ### 3. Developer Tools
194
+ - CLI tools
195
+ - Testing utilities
196
+ - Mock server
197
+ - Documentation generator
198
+
199
+ ## Testing Strategy
200
+
201
+ ### 1. Unit Tests
202
+ - Service logic testing
203
+ - Type validation
204
+ - Error handling
205
+
206
+ ### 2. Integration Tests
207
+ - API endpoint testing
208
+ - Authentication flows
209
+ - File upload/download
210
+
211
+ ### 3. E2E Tests
212
+ - Complete workflows
213
+ - Real API interactions
214
+ - Error scenarios
215
+
216
+ ## Performance Considerations
217
+
218
+ ### 1. HTTP Optimization
219
+ - Connection pooling
220
+ - Request compression
221
+ - Timeout management
222
+
223
+ ### 2. Memory Management
224
+ - Efficient data structures
225
+ - Stream processing cho large files
226
+ - Garbage collection friendly
227
+
228
+ ### 3. Network Efficiency
229
+ - Batch requests
230
+ - Caching strategies
231
+ - Retry with backoff
232
+
233
+ ## Security
234
+
235
+ ### 1. Token Management
236
+ - Secure token storage
237
+ - Automatic refresh
238
+ - Token validation
239
+
240
+ ### 2. Request Security
241
+ - HTTPS only
242
+ - Request signing
243
+ - Parameter validation
244
+
245
+ ### 3. Error Information
246
+ - Sanitized error messages
247
+ - No sensitive data in logs
248
+ - Secure debugging
249
+
250
+ ## Deployment
251
+
252
+ ### 1. Package Distribution
253
+ - NPM package
254
+ - TypeScript declarations
255
+ - Source maps
256
+
257
+ ### 2. Versioning
258
+ - Semantic versioning
259
+ - Backward compatibility
260
+ - Migration guides
261
+
262
+ ### 3. Documentation
263
+ - API documentation
264
+ - Usage examples
265
+ - Migration guides
package/CHANGELOG.md CHANGED
@@ -5,63 +5,6 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [1.12.0] - 2025-01-17
9
-
10
- ### 🎯 NEW FEATURES
11
-
12
- #### Webhook Message Classification Helpers
13
- - **ADDED**: `isUserMessageEvent()` - Phân biệt tin nhắn từ người dùng cá nhân gửi tới OA
14
- - **ADDED**: `isGroupMessageEvent()` - Phân biệt tin nhắn từ người dùng trong group
15
- - **ADDED**: `isOAToUserMessageEvent()` - Phân biệt tin nhắn OA gửi cho người dùng cá nhân
16
- - **ADDED**: `isOAToGroupMessageEvent()` - Phân biệt tin nhắn OA gửi tới group
17
- - **ADDED**: `getMessageDirection()` - Xác định hướng và đích của tin nhắn với mô tả chi tiết
18
-
19
- #### Enhanced Type Safety
20
- - **IMPROVED**: Better type guards cho webhook message classification
21
- - **ENHANCED**: Comprehensive support cho 38+ message event types
22
- - **ADDED**: Direction và target classification với Vietnamese descriptions
23
-
24
- ### 📚 DOCUMENTATION
25
-
26
- #### New Documentation Files
27
- - **ADDED**: `docs/WEBHOOK_MESSAGE_HELPERS.md` - Comprehensive guide cho message classification
28
- - **ADDED**: `examples/webhook-message-classification.ts` - Practical usage examples
29
- - **UPDATED**: README.md với message classification helpers section
30
-
31
- #### Developer Experience
32
- - **ENHANCED**: IntelliSense support với detailed JSDoc comments
33
- - **ADDED**: 13 comprehensive test cases cho message classification helpers
34
- - **IMPROVED**: Better error handling và unknown event detection
35
-
36
- ### 🔧 TECHNICAL IMPROVEMENTS
37
-
38
- #### Code Organization
39
- - **ENHANCED**: Exported helper functions từ main index.ts
40
- - **IMPROVED**: Type safety với proper TypeScript interfaces
41
- - **ADDED**: Comprehensive test coverage cho all helper functions
42
-
43
- #### Supported Event Types
44
- - **User Message Events** (12 types): `user_send_text`, `user_send_image`, etc.
45
- - **Group Message Events** (9 types): `user_send_group_text`, `user_send_group_image`, etc.
46
- - **OA to User Events** (7 types): `oa_send_text`, `oa_send_image`, etc.
47
- - **OA to Group Events** (10 types): `oa_send_group_text`, `oa_send_group_image`, etc.
48
-
49
- ## [1.11.1] - 2025-01-11
50
-
51
- ### 🔧 API IMPROVEMENTS
52
-
53
- #### Method Signature Enhancement
54
- - **IMPROVED**: `createOAAuthUrl()` method signature với better parameter order
55
- - Changed from: `(redirectUri, state?, usePkce?, pkce?)`
56
- - Changed to: `(redirectUri, state?, pkce?, usePkce?)`
57
- - **ENHANCED**: More intuitive API design với PKCE config trước usePkce flag
58
- - **UPDATED**: All examples và documentation để phù hợp với signature mới
59
- - **ADDED**: Comprehensive test coverage cho new signature
60
-
61
- #### Developer Experience
62
- - **IMPROVED**: Better IntelliSense support với clearer parameter ordering
63
- - **ENHANCED**: More logical API flow cho PKCE implementation
64
-
65
8
  ## [1.11.0] - 2025-01-11
66
9
 
67
10
  ### 🔐 SECURITY ENHANCEMENTS
package/README.md CHANGED
@@ -584,39 +584,7 @@ const handlers: WebhookHandlers = {
584
584
  - **Anonymous Events** (4 types): Anonymous chat support
585
585
  - **Shop Events** (1 type): Order management
586
586
 
587
- ### 🎯 Message Classification Helpers
588
-
589
- The SDK provides helper functions to easily classify webhook message events:
590
-
591
- ```typescript
592
- import {
593
- isUserMessageEvent,
594
- isGroupMessageEvent,
595
- isOAToUserMessageEvent,
596
- isOAToGroupMessageEvent,
597
- getMessageDirection,
598
- } from "@warriorteam/redai-zalo-sdk";
599
-
600
- function handleWebhook(event: WebhookEvent) {
601
- if (isUserMessageEvent(event)) {
602
- console.log("Message from individual user");
603
- } else if (isGroupMessageEvent(event)) {
604
- console.log("Message from group");
605
- } else if (isOAToUserMessageEvent(event)) {
606
- console.log("OA sent message to user");
607
- } else if (isOAToGroupMessageEvent(event)) {
608
- console.log("OA sent message to group");
609
- }
610
-
611
- // Or use the unified helper
612
- const { direction, target, description } = getMessageDirection(event);
613
- console.log(`${direction} message to ${target}: ${description}`);
614
- }
615
- ```
616
-
617
- For complete webhook documentation, see:
618
- - **[Webhook Events Guide](./docs/WEBHOOK_EVENTS.md)** - Complete guide for all 70+ webhook event types
619
- - **[Message Classification Helpers](./docs/WEBHOOK_MESSAGE_HELPERS.md)** - Helper functions for message classification
587
+ For complete webhook documentation, see [Webhook Events Guide](./docs/WEBHOOK_EVENTS.md).
620
588
 
621
589
  ## Changelog
622
590