@onivoro/isomorphic-common 24.0.0 → 24.0.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.
@@ -1,10 +0,0 @@
1
- {
2
- "name": "@onivoro/isomorphic-common",
3
- "version": "23.0.0",
4
- "type": "commonjs",
5
- "main": "./src/index.js",
6
- "types": "./src/index.d.ts",
7
- "dependencies": {
8
- "tslib": "^2.3.0"
9
- }
10
- }
@@ -1,322 +0,0 @@
1
- # @onivoro/server-common
2
-
3
- A comprehensive collection of common server utilities, DTOs, decorators, pipes, and functions for building robust NestJS applications. This library provides foundational components used across enterprise-scale server applications.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- npm install @onivoro/server-common
9
- ```
10
-
11
- ## Features
12
-
13
- - **NestJS Module**: Ready-to-import server common module
14
- - **Error Handling**: Advanced error filters for TypeORM and general exceptions
15
- - **API Decorators**: OpenAPI documentation decorators and response formatters
16
- - **DTOs**: Common data transfer objects for typical API operations
17
- - **Validation Pipes**: Custom pipes for data parsing and validation
18
- - **Utility Functions**: Helper functions for API creation, encoding, file operations, and more
19
- - **Environment Management**: Environment variable handling and configuration
20
- - **Application Bootstrap**: Functions for creating and configuring API applications
21
-
22
- ## Quick Start
23
-
24
- ### Import the Module
25
-
26
- ```typescript
27
- import { ServerCommonModule } from '@onivoro/server-common';
28
-
29
- @Module({
30
- imports: [ServerCommonModule],
31
- // ...
32
- })
33
- export class AppModule {}
34
- ```
35
-
36
- ### Using API Configuration
37
-
38
- ```typescript
39
- import { createApiApp, configureApiApp } from '@onivoro/server-common';
40
-
41
- async function bootstrap() {
42
- const app = await createApiApp(AppModule);
43
- await configureApiApp(app, {
44
- port: 3000,
45
- title: 'My API',
46
- version: '1.0.0'
47
- });
48
- }
49
- ```
50
-
51
- ### Environment Configuration
52
-
53
- ```typescript
54
- import { EnvironmentClass } from '@onivoro/server-common';
55
-
56
- @EnvironmentClass()
57
- export class AppConfig {
58
- @EnvironmentVariable('DATABASE_URL')
59
- databaseUrl: string;
60
-
61
- @EnvironmentVariable('PORT', '3000')
62
- port: number;
63
- }
64
- ```
65
-
66
- ## Configuration
67
-
68
- The library provides several configuration utilities:
69
-
70
- ### Environment Variables
71
-
72
- ```typescript
73
- import { loadDotEnvForKey } from '@onivoro/server-common';
74
-
75
- // Load environment variables for a specific key
76
- loadDotEnvForKey('development');
77
- ```
78
-
79
- ### Version Management
80
-
81
- ```typescript
82
- import { getPackageVersion, VERSION_PROVIDER_TOKEN } from '@onivoro/server-common';
83
-
84
- // Get version from package.json
85
- const version = getPackageVersion();
86
-
87
- // Use as provider
88
- @Module({
89
- providers: [
90
- {
91
- provide: VERSION_PROVIDER_TOKEN,
92
- useValue: getPackageVersion()
93
- }
94
- ]
95
- })
96
- export class AppModule {}
97
- ```
98
-
99
- ## Usage Examples
100
-
101
- ### Error Handling
102
-
103
- ```typescript
104
- import { ErrorFilter, TypeormErrorFilter } from '@onivoro/server-common';
105
-
106
- @UseFilters(new ErrorFilter(), new TypeormErrorFilter())
107
- @Controller('api')
108
- export class ApiController {
109
- // Your controller methods
110
- }
111
- ```
112
-
113
- ### API Documentation Decorators
114
-
115
- ```typescript
116
- import {
117
- DefaultApiController,
118
- ApiResponsePaged,
119
- ApiQueryPagedParams
120
- } from '@onivoro/server-common';
121
-
122
- @DefaultApiController('users')
123
- export class UsersController {
124
- @Get()
125
- @ApiQueryPagedParams()
126
- @ApiResponsePaged(UserDto)
127
- async findAll(@QueryPagedParams() params: PageParams) {
128
- // Implementation
129
- }
130
- }
131
- ```
132
-
133
- ### Common DTOs
134
-
135
- ```typescript
136
- import {
137
- PagedResponseDto,
138
- SuccessDto,
139
- HealthDto,
140
- EmailDto
141
- } from '@onivoro/server-common';
142
-
143
- @Get('health')
144
- async getHealth(): Promise<HealthDto> {
145
- return { status: 'ok', timestamp: new Date() };
146
- }
147
-
148
- @Post('send-email')
149
- async sendEmail(@Body() emailDto: EmailDto): Promise<SuccessDto> {
150
- // Send email logic
151
- return { success: true };
152
- }
153
- ```
154
-
155
- ### Validation Pipes
156
-
157
- ```typescript
158
- import {
159
- ParseDateOptionalPipe,
160
- ParseUuidOptionalPipe,
161
- ZodValidationPipe
162
- } from '@onivoro/server-common';
163
-
164
- @Get('events')
165
- async getEvents(
166
- @Query('date', ParseDateOptionalPipe) date?: Date,
167
- @Query('userId', ParseUuidOptionalPipe) userId?: string
168
- ) {
169
- // Implementation
170
- }
171
- ```
172
-
173
- ### Utility Functions
174
-
175
- ```typescript
176
- import {
177
- generateUniqueCode,
178
- getRandomString,
179
- encode,
180
- decode,
181
- shell,
182
- tryCatch
183
- } from '@onivoro/server-common';
184
-
185
- // Generate unique codes
186
- const code = generateUniqueCode(8);
187
-
188
- // Random string generation
189
- const randomStr = getRandomString(16);
190
-
191
- // Encoding/decoding
192
- const encoded = encode('sensitive data');
193
- const decoded = decode(encoded);
194
-
195
- // Shell command execution
196
- const result = await shell('ls -la');
197
-
198
- // Safe function execution
199
- const [error, result] = await tryCatch(async () => {
200
- return await someAsyncOperation();
201
- });
202
- ```
203
-
204
- ### File Operations
205
-
206
- ```typescript
207
- import {
208
- readFileAsJson,
209
- saveFileAsJson,
210
- parsePackageJson
211
- } from '@onivoro/server-common';
212
-
213
- // Read JSON file
214
- const config = await readFileAsJson('./config.json');
215
-
216
- // Save JSON file
217
- await saveFileAsJson('./output.json', { data: 'example' });
218
-
219
- // Parse package.json
220
- const packageInfo = parsePackageJson('./package.json');
221
- ```
222
-
223
- ### Application Metadata
224
-
225
- ```typescript
226
- import { generateAppMetadata } from '@onivoro/server-common';
227
-
228
- const metadata = generateAppMetadata({
229
- name: 'My API',
230
- version: '1.0.0',
231
- description: 'Example API'
232
- });
233
- ```
234
-
235
- ## API Reference
236
-
237
- ### Core Functions
238
-
239
- - **createApiApp(module)**: Create NestJS application
240
- - **configureApiApp(app, options)**: Configure API with OpenAPI, CORS, etc.
241
- - **moduleFactory(module, options)**: Create dynamic modules
242
- - **initOpenapi(app, config)**: Initialize OpenAPI documentation
243
-
244
- ### Utility Functions
245
-
246
- - **encode(data)**: Encode sensitive data
247
- - **decode(data)**: Decode sensitive data
248
- - **generateUniqueCode(length)**: Generate unique alphanumeric codes
249
- - **getRandomString(length)**: Generate random strings
250
- - **shell(command)**: Execute shell commands
251
- - **tryCatch(fn)**: Safe async function execution
252
-
253
- ### File Operations
254
-
255
- - **readFileAsJson(path)**: Read and parse JSON files
256
- - **saveFileAsJson(path, data)**: Save data as JSON file
257
- - **parsePackageJson(path)**: Parse package.json files
258
-
259
- ### Environment
260
-
261
- - **loadDotEnvForKey(key)**: Load environment variables
262
- - **getPackageVersion()**: Get current package version
263
- - **isPortInUse(port)**: Check if port is available
264
-
265
- ### Memory and Performance
266
-
267
- - **getMemoryStats()**: Get Node.js memory statistics
268
-
269
- ## DTOs
270
-
271
- ### Common DTOs
272
-
273
- - **SuccessDto**: Standard success response
274
- - **HealthDto**: Health check response
275
- - **EmailDto**: Email data transfer object
276
- - **PagedResponseDto**: Paginated response wrapper
277
- - **LookupDto**: Key-value lookup objects
278
-
279
- ### Request DTOs
280
-
281
- - **BodyDto**: Generic request body
282
- - **UserIdDto**: User ID parameter
283
- - **ValueDto/ValuesDto**: Generic value containers
284
-
285
- ## Decorators
286
-
287
- ### API Documentation
288
-
289
- - **@DefaultApiController**: Standard API controller decorator
290
- - **@ApiResponsePaged**: Paginated response documentation
291
- - **@ApiQueryPagedParams**: Paginated query parameters
292
- - **@ApiBodyUnspecified**: Unspecified request body
293
- - **@ApiResponseUnspecified**: Unspecified response
294
-
295
- ### Environment
296
-
297
- - **@EnvironmentClass**: Mark classes for environment variable injection
298
-
299
- ### Query Parameters
300
-
301
- - **@QueryPagedParams**: Extract pagination parameters
302
-
303
- ## Error Handling
304
-
305
- The library provides comprehensive error handling:
306
-
307
- - **ErrorFilter**: General application error filter
308
- - **TypeormErrorFilter**: Database-specific error handling
309
- - **Structured Error Responses**: Consistent error response format
310
-
311
- ## Best Practices
312
-
313
- 1. **Use Type Safety**: Leverage TypeScript for all DTOs and interfaces
314
- 2. **Environment Configuration**: Use `@EnvironmentClass` for configuration
315
- 3. **Error Handling**: Apply error filters at the controller level
316
- 4. **API Documentation**: Use provided decorators for OpenAPI documentation
317
- 5. **Validation**: Use custom pipes for input validation
318
- 6. **Pagination**: Use provided pagination utilities for list endpoints
319
-
320
- ## License
321
-
322
- This library is part of the Onivoro monorepo ecosystem.
@@ -1,12 +0,0 @@
1
- {
2
- "name": "@onivoro/server-common",
3
- "version": "23.0.0",
4
- "type": "commonjs",
5
- "main": "./src/index.js",
6
- "types": "./src/index.d.ts",
7
- "dependencies": {
8
- "@onivoro/isomorphic-common": "23.0.0",
9
- "@onivoro/server-b2b-orm": "23.0.0",
10
- "tslib": "^2.3.0"
11
- }
12
- }