@bts-soft/core 1.9.2 → 1.9.4
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 +361 -34
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -1,67 +1,394 @@
|
|
|
1
|
-
|
|
2
1
|
# @bts-soft/core
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The `@bts-soft/core` package is a comprehensive collection of essential modules and utilities for NestJS applications. It serves as the foundation for building scalable, maintainable, and feature-rich backend systems by providing a unified interface to multiple specialized packages.
|
|
6
|
+
|
|
7
|
+
This core package bundles together notification systems, caching, file uploads, validation, and common utilities - all optimized for production use.
|
|
6
8
|
|
|
7
9
|
---
|
|
8
10
|
|
|
9
|
-
##
|
|
11
|
+
## Package Contents
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
-
|
|
13
|
+
| Package | Version | Description |
|
|
14
|
+
|---------|---------|-------------|
|
|
15
|
+
| `@bts-soft/notifications` | Latest | Multi-channel notification system with queue processing |
|
|
16
|
+
| `@bts-soft/cache` | Latest | Redis-based caching with advanced data structures |
|
|
17
|
+
| `@bts-soft/upload` | Latest | File upload system with cloud storage support |
|
|
18
|
+
| `@bts-soft/validation` | Latest | Comprehensive validation decorators and utilities |
|
|
19
|
+
| `@bts-soft/common` | Latest | Shared modules, interceptors, and base classes |
|
|
14
20
|
|
|
15
21
|
---
|
|
16
22
|
|
|
17
|
-
##
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
### **Multi-Channel Notifications**
|
|
26
|
+
- Support for 8+ messaging channels (Email, SMS, WhatsApp, Telegram, etc.)
|
|
27
|
+
- Queue-based processing with BullMQ and Redis
|
|
28
|
+
- Unified API across all notification types
|
|
29
|
+
- Automatic retry mechanisms with exponential backoff
|
|
30
|
+
|
|
31
|
+
### **Advanced Caching**
|
|
32
|
+
- Redis-based caching with TTL support
|
|
33
|
+
- Distributed locking for concurrency control
|
|
34
|
+
- Support for advanced data structures (Hashes, Sorted Sets, Pub/Sub)
|
|
35
|
+
- Geospatial operations and real-time messaging
|
|
36
|
+
|
|
37
|
+
### **File Upload System**
|
|
38
|
+
- Support for both REST and GraphQL APIs
|
|
39
|
+
- Cloudinary integration with strategy pattern
|
|
40
|
+
- Event-driven architecture with observer pattern
|
|
41
|
+
- Configurable file size and quantity limits
|
|
18
42
|
|
|
19
|
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
43
|
+
### **Robust Validation**
|
|
44
|
+
- Comprehensive validation decorators for common fields
|
|
45
|
+
- SQL injection protection built-in
|
|
46
|
+
- GraphQL and REST compatibility
|
|
47
|
+
- Automatic text transformation utilities
|
|
48
|
+
|
|
49
|
+
### **Common Utilities**
|
|
50
|
+
- Standardized API response formatting
|
|
51
|
+
- Internationalization (i18n) support
|
|
52
|
+
- Rate limiting and security interceptors
|
|
53
|
+
- Production-ready configuration modules
|
|
23
54
|
|
|
24
55
|
---
|
|
25
56
|
|
|
26
|
-
##
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install @bts-soft/core
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Peer Dependencies
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install @nestjs/common @nestjs/core @nestjs/graphql bullmq @nestjs/bullmq
|
|
67
|
+
npm install redis nodemailer twilio firebase-admin axios
|
|
68
|
+
npm install node-telegram-bot-api graphql-upload class-validator class-transformer
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
27
72
|
|
|
28
|
-
|
|
73
|
+
## Quick Start
|
|
74
|
+
|
|
75
|
+
### 1. Basic Setup
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
29
78
|
import { Module } from '@nestjs/common';
|
|
30
|
-
import {
|
|
79
|
+
import {
|
|
80
|
+
NotificationModule,
|
|
81
|
+
CacheModule,
|
|
82
|
+
UploadModule,
|
|
83
|
+
ConfigModule,
|
|
84
|
+
GraphqlModule
|
|
85
|
+
} from '@bts-soft/core';
|
|
31
86
|
|
|
32
87
|
@Module({
|
|
33
|
-
imports: [
|
|
88
|
+
imports: [
|
|
89
|
+
ConfigModule,
|
|
90
|
+
CacheModule,
|
|
91
|
+
NotificationModule,
|
|
92
|
+
UploadModule,
|
|
93
|
+
GraphqlModule,
|
|
94
|
+
],
|
|
34
95
|
})
|
|
35
96
|
export class AppModule {}
|
|
36
|
-
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 2. Environment Configuration
|
|
100
|
+
|
|
101
|
+
```env
|
|
102
|
+
# Redis Configuration
|
|
103
|
+
REDIS_HOST=localhost
|
|
104
|
+
REDIS_PORT=6379
|
|
105
|
+
|
|
106
|
+
# Cloudinary Configuration
|
|
107
|
+
CLOUDINARY_CLOUD_NAME=your-cloud-name
|
|
108
|
+
CLOUDINARY_API_KEY=your-api-key
|
|
109
|
+
CLOUDINARY_API_SECRET=your-api-secret
|
|
110
|
+
|
|
111
|
+
# Email Configuration
|
|
112
|
+
EMAIL_HOST=smtp.gmail.com
|
|
113
|
+
EMAIL_USER=your-email@gmail.com
|
|
114
|
+
EMAIL_PASS=your-app-password
|
|
115
|
+
|
|
116
|
+
# Twilio Configuration
|
|
117
|
+
TWILIO_ACCOUNT_SID=your_account_sid
|
|
118
|
+
TWILIO_AUTH_TOKEN=your_auth_token
|
|
119
|
+
TWILIO_SMS_NUMBER=+1234567890
|
|
120
|
+
|
|
121
|
+
# Telegram Configuration
|
|
122
|
+
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 3. Usage Examples
|
|
126
|
+
|
|
127
|
+
#### Notifications
|
|
128
|
+
```typescript
|
|
129
|
+
import { NotificationService, ChannelType } from '@bts-soft/core';
|
|
130
|
+
|
|
131
|
+
@Injectable()
|
|
132
|
+
export class UserService {
|
|
133
|
+
constructor(private notificationService: NotificationService) {}
|
|
134
|
+
|
|
135
|
+
async sendWelcome(userEmail: string, userName: string) {
|
|
136
|
+
await this.notificationService.send(ChannelType.EMAIL, {
|
|
137
|
+
recipientId: userEmail,
|
|
138
|
+
subject: 'Welcome',
|
|
139
|
+
body: `Hello ${userName}, welcome to our platform!`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Caching
|
|
146
|
+
```typescript
|
|
147
|
+
import { RedisService } from '@bts-soft/core';
|
|
148
|
+
|
|
149
|
+
@Injectable()
|
|
150
|
+
export class ProductService {
|
|
151
|
+
constructor(private redisService: RedisService) {}
|
|
152
|
+
|
|
153
|
+
async getProducts(): Promise<any[]> {
|
|
154
|
+
const cached = await this.redisService.get('products');
|
|
155
|
+
if (cached) return cached;
|
|
156
|
+
|
|
157
|
+
const products = await this.fetchFromDB();
|
|
158
|
+
await this.redisService.set('products', products, 3600);
|
|
159
|
+
return products;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### File Upload
|
|
165
|
+
```typescript
|
|
166
|
+
import { UploadService } from '@bts-soft/core';
|
|
167
|
+
|
|
168
|
+
@Resolver()
|
|
169
|
+
export class UserResolver {
|
|
170
|
+
constructor(private uploadService: UploadService) {}
|
|
171
|
+
|
|
172
|
+
@Mutation()
|
|
173
|
+
async uploadAvatar(@Args('file') file: Promise<FileUpload>) {
|
|
174
|
+
return this.uploadService.uploadImage(file);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### Validation
|
|
180
|
+
```typescript
|
|
181
|
+
import { EmailField, PasswordField, PhoneField } from '@bts-soft/core';
|
|
182
|
+
|
|
183
|
+
export class CreateUserDto {
|
|
184
|
+
@EmailField()
|
|
185
|
+
email: string;
|
|
186
|
+
|
|
187
|
+
@PasswordField()
|
|
188
|
+
password: string;
|
|
189
|
+
|
|
190
|
+
@PhoneField()
|
|
191
|
+
phone: string;
|
|
192
|
+
}
|
|
193
|
+
```
|
|
37
194
|
|
|
38
195
|
---
|
|
39
196
|
|
|
40
|
-
##
|
|
197
|
+
## Architecture
|
|
41
198
|
|
|
42
|
-
|
|
199
|
+
### Design Patterns
|
|
43
200
|
|
|
44
|
-
-
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
|
|
48
|
-
-
|
|
49
|
-
|
|
50
|
-
- [cache Documentation]([@bts-soft/cache - npm](https://www.npmjs.com/package/@bts-soft/cache))
|
|
51
|
-
|
|
201
|
+
- **Strategy Pattern**: Switch between cloud providers and notification channels
|
|
202
|
+
- **Command Pattern**: Encapsulate upload and delete operations
|
|
203
|
+
- **Observer Pattern**: Event-driven reactions to system events
|
|
204
|
+
- **Factory Pattern**: Dynamic creation of notification channels
|
|
205
|
+
- **Repository Pattern**: Consistent data access across modules
|
|
52
206
|
|
|
53
|
-
|
|
207
|
+
### Module Structure
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
@bts-soft/core/
|
|
211
|
+
├── notifications/ # Multi-channel notification system
|
|
212
|
+
├── cache/ # Redis caching with advanced features
|
|
213
|
+
├── upload/ # File upload and management
|
|
214
|
+
├── validation/ # Validation decorators and utilities
|
|
215
|
+
└── common/ # Shared modules and base classes
|
|
216
|
+
```
|
|
54
217
|
|
|
218
|
+
---
|
|
55
219
|
|
|
56
|
-
|
|
220
|
+
## Advanced Configuration
|
|
57
221
|
|
|
58
|
-
|
|
222
|
+
### Custom Notification Channels
|
|
59
223
|
|
|
60
|
-
|
|
224
|
+
```typescript
|
|
225
|
+
const customConfig = {
|
|
226
|
+
email: {
|
|
227
|
+
service: 'gmail',
|
|
228
|
+
user: 'custom@gmail.com',
|
|
229
|
+
pass: 'custom-password',
|
|
230
|
+
sender: 'no-reply@custom.com'
|
|
231
|
+
},
|
|
232
|
+
sms: {
|
|
233
|
+
accountSid: 'your-sid',
|
|
234
|
+
authToken: 'your-token',
|
|
235
|
+
number: '+1234567890'
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
```
|
|
61
239
|
|
|
62
|
-
|
|
240
|
+
### Distributed Locking
|
|
63
241
|
|
|
64
|
-
|
|
242
|
+
```typescript
|
|
243
|
+
async processCriticalTask(taskId: string): Promise<void> {
|
|
244
|
+
const lockKey = `task:${taskId}`;
|
|
245
|
+
const lock = await this.redisService.acquireLock(lockKey, 10000);
|
|
246
|
+
|
|
247
|
+
if (!lock) throw new Error('Could not acquire lock');
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
// Process task exclusively
|
|
251
|
+
} finally {
|
|
252
|
+
await this.redisService.releaseLock(lockKey);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Event Observers
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
// Custom observer for upload events
|
|
261
|
+
export class AnalyticsObserver implements IUploadObserver {
|
|
262
|
+
async update(event: string, data: any): Promise<void> {
|
|
263
|
+
// Track upload events in analytics system
|
|
264
|
+
await this.analyticsService.track('file_upload', data);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
## Error Handling
|
|
272
|
+
|
|
273
|
+
All modules include comprehensive error handling:
|
|
65
274
|
|
|
275
|
+
- **Validation Errors**: Descriptive messages with field-level details
|
|
276
|
+
- **Network Errors**: Automatic retry with exponential backoff
|
|
277
|
+
- **Configuration Errors**: Early validation with helpful messages
|
|
278
|
+
- **Runtime Errors**: Structured logging and graceful degradation
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## Performance Features
|
|
283
|
+
|
|
284
|
+
- **Queue Processing**: Non-blocking notification delivery
|
|
285
|
+
- **Connection Pooling**: Optimized Redis and database connections
|
|
286
|
+
- **Memory Management**: Efficient file streaming and buffer handling
|
|
287
|
+
- **Caching Strategies**: Multi-level caching with intelligent invalidation
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## Production Readiness
|
|
292
|
+
|
|
293
|
+
### Security
|
|
294
|
+
- SQL injection prevention
|
|
295
|
+
- Rate limiting on APIs
|
|
296
|
+
- Secure file type validation
|
|
297
|
+
- Environment-based configuration
|
|
298
|
+
|
|
299
|
+
### Monitoring
|
|
300
|
+
- Structured logging across all modules
|
|
301
|
+
- Performance metrics collection
|
|
302
|
+
- Health check endpoints
|
|
303
|
+
- Error tracking and reporting
|
|
304
|
+
|
|
305
|
+
### Scalability
|
|
306
|
+
- Horizontal scaling support
|
|
307
|
+
- Stateless service design
|
|
308
|
+
- Database connection pooling
|
|
309
|
+
- Message queue integration
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## API Reference
|
|
314
|
+
|
|
315
|
+
### Core Services
|
|
316
|
+
|
|
317
|
+
| Service | Description | Methods |
|
|
318
|
+
|---------|-------------|---------|
|
|
319
|
+
| `NotificationService` | Multi-channel messaging | `send()`, `queue()` |
|
|
320
|
+
| `RedisService` | Caching and data storage | `get()`, `set()`, `hGetAll()`, `zAdd()` |
|
|
321
|
+
| `UploadService` | File management | `uploadImage()`, `uploadVideo()`, `deleteFile()` |
|
|
322
|
+
| `Validation` | Input validation | Decorators for fields and DTOs |
|
|
323
|
+
|
|
324
|
+
### Common Modules
|
|
325
|
+
|
|
326
|
+
| Module | Purpose | Features |
|
|
327
|
+
|--------|---------|----------|
|
|
328
|
+
| `ConfigModule` | Environment configuration | Dynamic .env loading, validation |
|
|
329
|
+
| `GraphqlModule` | GraphQL setup | Apollo server, error handling, subscriptions |
|
|
330
|
+
| `TranslationModule` | Internationalization | Multi-language support, header detection |
|
|
331
|
+
| `ThrottlerModule` | Rate limiting | Multiple strategy support |
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
## Migration Guide
|
|
336
|
+
|
|
337
|
+
### From Individual Packages
|
|
338
|
+
|
|
339
|
+
If you're migrating from individual `@bts-soft` packages:
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
// Before
|
|
343
|
+
import { NotificationService } from '@bts-soft/notifications';
|
|
344
|
+
import { RedisService } from '@bts-soft/cache';
|
|
345
|
+
import { UploadService } from '@bts-soft/upload';
|
|
346
|
+
|
|
347
|
+
// After
|
|
348
|
+
import {
|
|
349
|
+
NotificationService,
|
|
350
|
+
RedisService,
|
|
351
|
+
UploadService
|
|
352
|
+
} from '@bts-soft/core';
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
All APIs remain compatible - the core package simply bundles the individual packages together.
|
|
356
|
+
|
|
357
|
+
---
|
|
358
|
+
|
|
359
|
+
## Support
|
|
360
|
+
|
|
361
|
+
### Documentation
|
|
362
|
+
- Full API documentation available at [bts-soft.dev/docs/core]([@bts-soft/core - npm](https://www.npmjs.com/package/@bts-soft/core))
|
|
363
|
+
- Examples and tutorials in the GitHub repository
|
|
364
|
+
|
|
365
|
+
### Community
|
|
366
|
+
- GitHub Issues for bug reports and feature requests
|
|
367
|
+
- Discord community for real-time support
|
|
368
|
+
- Stack Overflow for technical questions
|
|
369
|
+
|
|
370
|
+
### Enterprise
|
|
371
|
+
Dedicated support and custom implementations available for enterprise customers.
|
|
372
|
+
|
|
373
|
+
---
|
|
374
|
+
|
|
375
|
+
## License
|
|
376
|
+
|
|
377
|
+
This package is part of the `@bts-soft` ecosystem. All rights reserved © BTS Soft.
|
|
378
|
+
|
|
379
|
+
Individual packages may have their own license terms - please refer to each package's documentation for specific licensing information.
|
|
380
|
+
|
|
381
|
+
---
|
|
382
|
+
|
|
383
|
+
## Contact
|
|
384
|
+
|
|
385
|
+
**Author:** Omar Sabry
|
|
386
|
+
**Email:** [omar.sabry.dev@gmail.com](mailto:omar.sabry.dev@gmail.com)
|
|
387
|
+
**LinkedIn:** [Omar Sabry](https://www.linkedin.com/in/omarsa6ry/)
|
|
388
|
+
**Portfolio:** [Portfolio](https://omarsabry.netlify.app/)
|
|
389
|
+
|
|
390
|
+
---
|
|
391
|
+
|
|
392
|
+
## Repository
|
|
66
393
|
|
|
67
|
-
**GitHub:**
|
|
394
|
+
**GitHub:** [https://github.com/Omar-Sa6ry/bts-soft/tree/main/packages/core](https://github.com/Omar-Sa6ry/bts-soft/tree/main/packages/core)
|