@triproject/nestjs-core 1.0.5 → 1.0.6
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 +297 -38
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
-
# @triproject/nestjs
|
|
1
|
+
# @triproject/nestjs-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A collection of NestJS modules and utilities to speed up development with built-in support for caching, database access, encryption, logging, mail, notifications, queues, and more.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@triproject/nestjs-core)
|
|
6
|
+
[](https://opensource.org/licenses/ISC)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🚀 **Quick Bootstrap** - Preconfigured NestJS application setup
|
|
11
|
+
- 🔐 **Encryption & Authentication** - JWT, password hashing (PBKDF2), AES-256-GCM encryption, TOTP/2FA
|
|
12
|
+
- 💾 **Database** - Sequelize ORM with TypeScript support
|
|
13
|
+
- 🗄️ **Caching** - Redis integration with helpers
|
|
14
|
+
- 📧 **Email** - Nodemailer with queue support
|
|
15
|
+
- 🔔 **Notifications** - Push notifications and Slack integration
|
|
16
|
+
- ⚡ **Queue Management** - Bull and BullMQ support
|
|
17
|
+
- 📝 **Logging** - Structured logging with Slack alerts
|
|
18
|
+
- 🎨 **Swagger** - API documentation helpers
|
|
19
|
+
- 🔌 **Modular** - Import only what you need
|
|
4
20
|
|
|
5
21
|
## Installation
|
|
6
22
|
|
|
7
23
|
```bash
|
|
8
|
-
npm install @triproject/nestjs
|
|
24
|
+
npm install @triproject/nestjs-core
|
|
9
25
|
```
|
|
10
26
|
|
|
11
27
|
## Modular Imports
|
|
@@ -15,36 +31,82 @@ This package uses subpath exports to avoid loading unnecessary dependencies. Imp
|
|
|
15
31
|
### Bootstrap (Core)
|
|
16
32
|
|
|
17
33
|
```typescript
|
|
18
|
-
import { bootstrap } from '@triproject/nestjs/bootstrap';
|
|
19
|
-
import { APP_PORT, APP_KEY } from '@triproject/nestjs/config';
|
|
34
|
+
import { bootstrap } from '@triproject/nestjs-core/bootstrap';
|
|
35
|
+
import { APP_PORT, APP_KEY } from '@triproject/nestjs-core/config';
|
|
20
36
|
```
|
|
21
37
|
|
|
22
|
-
Required peer dependencies
|
|
38
|
+
**Required peer dependencies:**
|
|
39
|
+
|
|
40
|
+
- `@nestjs/common` (^11.0.0)
|
|
41
|
+
- `@nestjs/core` (^11.0.0)
|
|
42
|
+
- `express` (^4.0.0 || ^5.0.0)
|
|
43
|
+
- `helmet` (^7.0.0 || ^8.0.0)
|
|
44
|
+
- `cookie-parser` (^1.4.0)
|
|
45
|
+
- `class-validator` (^0.14.0)
|
|
46
|
+
- `dotenv` (^16.0.0 || ^17.0.0)
|
|
47
|
+
|
|
48
|
+
### Configuration
|
|
23
49
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
50
|
+
```typescript
|
|
51
|
+
import {
|
|
52
|
+
APP_NAME,
|
|
53
|
+
APP_PORT,
|
|
54
|
+
APP_KEY,
|
|
55
|
+
DB_HOST,
|
|
56
|
+
DB_PORT,
|
|
57
|
+
REDIS_HOST,
|
|
58
|
+
REDIS_PORT,
|
|
59
|
+
// ... and more
|
|
60
|
+
} from '@triproject/nestjs-core/config';
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
All environment variables are pre-configured with sensible defaults.
|
|
30
64
|
|
|
31
65
|
### Cache (Redis)
|
|
32
66
|
|
|
33
67
|
```typescript
|
|
34
|
-
import { AppCache, Redis, CacheModule } from '@triproject/nestjs/cache';
|
|
68
|
+
import { AppCache, Redis, CacheModule } from '@triproject/nestjs-core/cache';
|
|
69
|
+
|
|
70
|
+
// In your module
|
|
71
|
+
@Module({
|
|
72
|
+
imports: [CacheModule],
|
|
73
|
+
})
|
|
74
|
+
export class AppModule {}
|
|
75
|
+
|
|
76
|
+
// In your service
|
|
77
|
+
constructor(private cache: AppCache) {}
|
|
78
|
+
|
|
79
|
+
async cacheData() {
|
|
80
|
+
await this.cache.set('key', { data: 'value' }, 3600);
|
|
81
|
+
const data = await this.cache.get<{ data: string }>('key');
|
|
82
|
+
await this.cache.delete('key');
|
|
83
|
+
}
|
|
35
84
|
```
|
|
36
85
|
|
|
37
|
-
Required peer dependencies
|
|
86
|
+
**Required peer dependencies:**
|
|
38
87
|
|
|
39
88
|
- `redis` (^4.0.0 || ^5.0.0)
|
|
40
89
|
|
|
41
90
|
### Database (Sequelize)
|
|
42
91
|
|
|
43
92
|
```typescript
|
|
44
|
-
import { DatabaseModule, Repository } from '@triproject/nestjs/db';
|
|
93
|
+
import { DatabaseModule, Repository } from '@triproject/nestjs-core/db';
|
|
94
|
+
|
|
95
|
+
// In your module
|
|
96
|
+
@Module({
|
|
97
|
+
imports: [DatabaseModule],
|
|
98
|
+
})
|
|
99
|
+
export class AppModule {}
|
|
100
|
+
|
|
101
|
+
// Base repository with pagination and common queries
|
|
102
|
+
export class UserRepository extends Repository<UserModel> {
|
|
103
|
+
constructor() {
|
|
104
|
+
super(UserModel);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
45
107
|
```
|
|
46
108
|
|
|
47
|
-
Required peer dependencies
|
|
109
|
+
**Required peer dependencies:**
|
|
48
110
|
|
|
49
111
|
- `sequelize` (^6.0.0)
|
|
50
112
|
- `sequelize-typescript` (^2.0.0)
|
|
@@ -53,28 +115,73 @@ Required peer dependencies:
|
|
|
53
115
|
### Encryption & JWT
|
|
54
116
|
|
|
55
117
|
```typescript
|
|
56
|
-
import { PasswordHash, Jwt, EncryptionModule } from '@triproject/nestjs/encryption';
|
|
118
|
+
import { PasswordHash, Jwt, Encryption, EncryptionModule } from '@triproject/nestjs-core/encryption';
|
|
119
|
+
|
|
120
|
+
// Password hashing
|
|
121
|
+
const hash = passwordHash.sign('myPassword');
|
|
122
|
+
const isValid = passwordHash.verify('myPassword', hash);
|
|
123
|
+
|
|
124
|
+
// JWT tokens
|
|
125
|
+
const token = jwt.sign({ userId: 123 }, 3600);
|
|
126
|
+
const decoded = jwt.verify<{ userId: number }>(token);
|
|
127
|
+
|
|
128
|
+
// AES-256-GCM encryption
|
|
129
|
+
const encrypted = encryption.encrypt('sensitive data');
|
|
130
|
+
const decrypted = encryption.decrypt(encrypted);
|
|
57
131
|
```
|
|
58
132
|
|
|
59
|
-
Required peer dependencies
|
|
133
|
+
**Required peer dependencies:**
|
|
60
134
|
|
|
61
135
|
- `jsonwebtoken` (^9.0.0)
|
|
62
136
|
|
|
137
|
+
**Features:**
|
|
138
|
+
|
|
139
|
+
- PBKDF2 password hashing with SHA-512
|
|
140
|
+
- RS512 JWT tokens
|
|
141
|
+
- AES-256-GCM encryption
|
|
142
|
+
- Native Node.js crypto (no external dependencies)
|
|
143
|
+
|
|
63
144
|
### Logger
|
|
64
145
|
|
|
65
146
|
```typescript
|
|
66
|
-
import { AppLogger, SlackLogger } from '@triproject/nestjs/logger';
|
|
147
|
+
import { AppLogger, SlackLogger } from '@triproject/nestjs-core/logger';
|
|
148
|
+
|
|
149
|
+
const logger = new AppLogger('MyService');
|
|
150
|
+
logger.log('Info message');
|
|
151
|
+
logger.error('Error message', error);
|
|
152
|
+
logger.warn('Warning message');
|
|
153
|
+
logger.debug('Debug message');
|
|
154
|
+
|
|
155
|
+
// Slack integration
|
|
156
|
+
const slackLogger = new SlackLogger();
|
|
157
|
+
await slackLogger.error('Critical error', error);
|
|
67
158
|
```
|
|
68
159
|
|
|
69
|
-
|
|
160
|
+
**Required peer dependencies:**
|
|
161
|
+
|
|
162
|
+
- `axios` (^1.0.0)
|
|
163
|
+
- `lodash` (^4.17.0)
|
|
164
|
+
- `moment` (^2.30.0)
|
|
70
165
|
|
|
71
166
|
### Mail
|
|
72
167
|
|
|
73
168
|
```typescript
|
|
74
|
-
import { MailModule, Mailer } from '@triproject/nestjs/mail';
|
|
169
|
+
import { MailModule, Mailer } from '@triproject/nestjs-core/mail';
|
|
170
|
+
|
|
171
|
+
@Module({
|
|
172
|
+
imports: [MailModule],
|
|
173
|
+
})
|
|
174
|
+
export class AppModule {}
|
|
175
|
+
|
|
176
|
+
// Send email
|
|
177
|
+
await mailer.send({
|
|
178
|
+
to: 'user@example.com',
|
|
179
|
+
subject: 'Welcome',
|
|
180
|
+
html: '<p>Welcome to our app!</p>',
|
|
181
|
+
});
|
|
75
182
|
```
|
|
76
183
|
|
|
77
|
-
Required peer dependencies
|
|
184
|
+
**Required peer dependencies:**
|
|
78
185
|
|
|
79
186
|
- `nodemailer` (^6.0.0 || ^7.0.0)
|
|
80
187
|
- `@nestjs/bull` (^11.0.0)
|
|
@@ -83,10 +190,22 @@ Required peer dependencies:
|
|
|
83
190
|
### Notifications
|
|
84
191
|
|
|
85
192
|
```typescript
|
|
86
|
-
import { NotificationModule, PushNotification } from '@triproject/nestjs/notifications';
|
|
193
|
+
import { NotificationModule, PushNotification } from '@triproject/nestjs-core/notifications';
|
|
194
|
+
|
|
195
|
+
@Module({
|
|
196
|
+
imports: [NotificationModule],
|
|
197
|
+
})
|
|
198
|
+
export class AppModule {}
|
|
199
|
+
|
|
200
|
+
// Send push notification
|
|
201
|
+
await pushNotification.send({
|
|
202
|
+
playerIds: ['user-id'],
|
|
203
|
+
title: 'New Message',
|
|
204
|
+
message: 'You have a new message',
|
|
205
|
+
});
|
|
87
206
|
```
|
|
88
207
|
|
|
89
|
-
Required peer dependencies
|
|
208
|
+
**Required peer dependencies:**
|
|
90
209
|
|
|
91
210
|
- `@nestjs/bull` (^11.0.0)
|
|
92
211
|
- `bull` (^4.0.0)
|
|
@@ -94,10 +213,24 @@ Required peer dependencies:
|
|
|
94
213
|
### Queues (BullMQ)
|
|
95
214
|
|
|
96
215
|
```typescript
|
|
97
|
-
import { QueueModule, AppQueue } from '@triproject/nestjs/queues';
|
|
216
|
+
import { QueueModule, AppQueue } from '@triproject/nestjs-core/queues';
|
|
217
|
+
|
|
218
|
+
@Module({
|
|
219
|
+
imports: [QueueModule],
|
|
220
|
+
})
|
|
221
|
+
export class AppModule {}
|
|
222
|
+
|
|
223
|
+
// Create queue processor
|
|
224
|
+
@Processor('myQueue')
|
|
225
|
+
export class MyQueue extends AppQueue {
|
|
226
|
+
@Process('job-name')
|
|
227
|
+
async processJob(job: Job) {
|
|
228
|
+
// Process job
|
|
229
|
+
}
|
|
230
|
+
}
|
|
98
231
|
```
|
|
99
232
|
|
|
100
|
-
Required peer dependencies
|
|
233
|
+
**Required peer dependencies:**
|
|
101
234
|
|
|
102
235
|
- `@nestjs/bullmq` (^11.0.0)
|
|
103
236
|
- `bullmq` (^5.0.0)
|
|
@@ -105,36 +238,162 @@ Required peer dependencies:
|
|
|
105
238
|
### Helpers
|
|
106
239
|
|
|
107
240
|
```typescript
|
|
108
|
-
import {
|
|
241
|
+
import {
|
|
242
|
+
CatchError,
|
|
243
|
+
TransformResponseInterceptor,
|
|
244
|
+
totp,
|
|
245
|
+
SwaggerApiPaginateResponse,
|
|
246
|
+
} from '@triproject/nestjs-core/helpers';
|
|
247
|
+
|
|
248
|
+
// Error handling decorator
|
|
249
|
+
class MyService {
|
|
250
|
+
@CatchError('Operation failed')
|
|
251
|
+
async myMethod() {
|
|
252
|
+
// Automatically catches and transforms errors
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// TOTP/2FA
|
|
257
|
+
const { secret, qrCodeBase64 } = await totp.generateSecret({
|
|
258
|
+
email: 'user@example.com',
|
|
259
|
+
issuer: 'MyApp',
|
|
260
|
+
});
|
|
261
|
+
const isValid = totp.verify(secret, '123456');
|
|
262
|
+
|
|
263
|
+
// Swagger decorators
|
|
264
|
+
@SwaggerApiPaginateResponse(UserDto)
|
|
265
|
+
async getUsers() {
|
|
266
|
+
// Returns paginated response with Swagger docs
|
|
267
|
+
}
|
|
109
268
|
```
|
|
110
269
|
|
|
111
|
-
Required peer dependencies for TOTP
|
|
270
|
+
**Required peer dependencies (for TOTP):**
|
|
112
271
|
|
|
113
272
|
- `speakeasy` (^2.0.0)
|
|
114
273
|
- `qrcode` (^1.5.0)
|
|
115
274
|
|
|
116
|
-
## Usage Example
|
|
275
|
+
## Complete Usage Example
|
|
117
276
|
|
|
118
277
|
```typescript
|
|
278
|
+
// main.ts
|
|
279
|
+
import { bootstrap } from '@triproject/nestjs-core/bootstrap';
|
|
280
|
+
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
281
|
+
import { AppModule } from './app.module';
|
|
282
|
+
|
|
283
|
+
const setupSwagger = (app) => {
|
|
284
|
+
const config = new DocumentBuilder().setTitle('My API').setVersion('1.0').build();
|
|
285
|
+
const document = SwaggerModule.createDocument(app, config);
|
|
286
|
+
SwaggerModule.setup('docs', app, document);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
bootstrap(AppModule, setupSwagger);
|
|
290
|
+
|
|
119
291
|
// app.module.ts
|
|
120
292
|
import { Module } from '@nestjs/common';
|
|
121
|
-
import { CacheModule } from '@triproject/nestjs/cache';
|
|
122
|
-
import { DatabaseModule } from '@triproject/nestjs/db';
|
|
123
|
-
import { EncryptionModule } from '@triproject/nestjs/encryption';
|
|
293
|
+
import { CacheModule } from '@triproject/nestjs-core/cache';
|
|
294
|
+
import { DatabaseModule } from '@triproject/nestjs-core/db';
|
|
295
|
+
import { EncryptionModule } from '@triproject/nestjs-core/encryption';
|
|
296
|
+
import { MailModule } from '@triproject/nestjs-core/mail';
|
|
124
297
|
|
|
125
298
|
@Module({
|
|
126
|
-
imports: [CacheModule,
|
|
299
|
+
imports: [DatabaseModule, CacheModule, EncryptionModule, MailModule],
|
|
127
300
|
})
|
|
128
301
|
export class AppModule {}
|
|
129
302
|
|
|
130
|
-
//
|
|
131
|
-
import {
|
|
132
|
-
import {
|
|
133
|
-
import {
|
|
303
|
+
// user.service.ts
|
|
304
|
+
import { Injectable } from '@nestjs/common';
|
|
305
|
+
import { AppCache } from '@triproject/nestjs-core/cache';
|
|
306
|
+
import { PasswordHash, Jwt } from '@triproject/nestjs-core/encryption';
|
|
134
307
|
|
|
135
|
-
|
|
308
|
+
@Injectable()
|
|
309
|
+
export class UserService {
|
|
310
|
+
constructor(private cache: AppCache, private passwordHash: PasswordHash, private jwt: Jwt) {}
|
|
311
|
+
|
|
312
|
+
async login(email: string, password: string) {
|
|
313
|
+
// Get user from cache or database
|
|
314
|
+
const user = await this.cache.get(`user:${email}`);
|
|
315
|
+
|
|
316
|
+
// Verify password
|
|
317
|
+
const isValid = this.passwordHash.verify(password, user.password);
|
|
318
|
+
if (!isValid) throw new Error('Invalid credentials');
|
|
319
|
+
|
|
320
|
+
// Generate JWT token
|
|
321
|
+
const token = this.jwt.sign({ userId: user.id }, 86400);
|
|
322
|
+
|
|
323
|
+
return { token };
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
## Environment Variables
|
|
329
|
+
|
|
330
|
+
All configuration is done through environment variables. Create a `.env` file:
|
|
331
|
+
|
|
332
|
+
```env
|
|
333
|
+
# App
|
|
334
|
+
APP_NAME=MyApp
|
|
335
|
+
APP_PORT=3000
|
|
336
|
+
APP_KEY=your-app-secret-key
|
|
337
|
+
|
|
338
|
+
# Database
|
|
339
|
+
DB_HOST=localhost
|
|
340
|
+
DB_PORT=5432
|
|
341
|
+
DB_USER=postgres
|
|
342
|
+
DB_PASSWORD=password
|
|
343
|
+
DB_NAME=mydb
|
|
344
|
+
|
|
345
|
+
# Redis
|
|
346
|
+
REDIS_HOST=localhost
|
|
347
|
+
REDIS_PORT=6379
|
|
348
|
+
REDIS_PASSWORD=
|
|
349
|
+
|
|
350
|
+
# JWT
|
|
351
|
+
JWT_PUBLIC_KEY=your-rsa-public-key
|
|
352
|
+
JWT_PRIVATE_KEY=your-rsa-private-key
|
|
353
|
+
|
|
354
|
+
# Encryption
|
|
355
|
+
PASSWORD_HASH_SECRET=your-hash-secret
|
|
356
|
+
ENCRYPTION_KEY=your-aes-256-key
|
|
357
|
+
|
|
358
|
+
# Mail
|
|
359
|
+
MAIL_HOST=smtp.gmail.com
|
|
360
|
+
MAIL_PORT=587
|
|
361
|
+
MAIL_USERNAME=your-email
|
|
362
|
+
MAIL_PASSWORD=your-password
|
|
363
|
+
|
|
364
|
+
# Slack
|
|
365
|
+
SLACK_ERROR_WEBHOOK_URL=your-slack-webhook
|
|
366
|
+
|
|
367
|
+
# OneSignal
|
|
368
|
+
ONESIGNAL_APP_ID=your-app-id
|
|
369
|
+
ONESIGNAL_REST_API_KEY=your-api-key
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
## Testing
|
|
373
|
+
|
|
374
|
+
This package includes comprehensive test coverage:
|
|
375
|
+
|
|
376
|
+
```bash
|
|
377
|
+
npm test
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Development
|
|
381
|
+
|
|
382
|
+
```bash
|
|
383
|
+
# Install dependencies
|
|
384
|
+
npm install
|
|
385
|
+
|
|
386
|
+
# Build
|
|
387
|
+
npm run build
|
|
388
|
+
|
|
389
|
+
# Run tests
|
|
390
|
+
npm test
|
|
136
391
|
```
|
|
137
392
|
|
|
138
393
|
## License
|
|
139
394
|
|
|
140
395
|
ISC
|
|
396
|
+
|
|
397
|
+
## Contributing
|
|
398
|
+
|
|
399
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|