fragment-ts 1.0.31 โ†’ 1.0.32

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.
Files changed (44) hide show
  1. package/API.md +752 -0
  2. package/DOCS.md +555 -0
  3. package/README.md +76 -339
  4. package/USAGE.md +309 -1306
  5. package/dist/cli/commands/init.command.js +1 -1
  6. package/dist/core/decorators/exception-filter.decorator.d.ts +5 -0
  7. package/dist/core/decorators/exception-filter.decorator.d.ts.map +1 -0
  8. package/dist/core/decorators/exception-filter.decorator.js +23 -0
  9. package/dist/core/decorators/exception-filter.decorator.js.map +1 -0
  10. package/dist/core/decorators/guard.decorator.d.ts +5 -0
  11. package/dist/core/decorators/guard.decorator.d.ts.map +1 -0
  12. package/dist/core/decorators/guard.decorator.js +23 -0
  13. package/dist/core/decorators/guard.decorator.js.map +1 -0
  14. package/dist/core/decorators/interceptor.decorator.d.ts +5 -0
  15. package/dist/core/decorators/interceptor.decorator.d.ts.map +1 -0
  16. package/dist/core/decorators/interceptor.decorator.js +23 -0
  17. package/dist/core/decorators/interceptor.decorator.js.map +1 -0
  18. package/dist/core/decorators/middleware.decorator.d.ts +8 -0
  19. package/dist/core/decorators/middleware.decorator.d.ts.map +1 -0
  20. package/dist/core/decorators/middleware.decorator.js +28 -0
  21. package/dist/core/decorators/middleware.decorator.js.map +1 -0
  22. package/dist/core/metadata/metadata-keys.d.ts +1 -0
  23. package/dist/core/metadata/metadata-keys.d.ts.map +1 -1
  24. package/dist/core/metadata/metadata-keys.js +1 -0
  25. package/dist/core/metadata/metadata-keys.js.map +1 -1
  26. package/dist/core/metadata/metadata-storage.d.ts +18 -0
  27. package/dist/core/metadata/metadata-storage.d.ts.map +1 -1
  28. package/dist/core/metadata/metadata-storage.js +82 -0
  29. package/dist/core/metadata/metadata-storage.js.map +1 -1
  30. package/dist/web/application.d.ts.map +1 -1
  31. package/dist/web/application.js +74 -5
  32. package/dist/web/application.js.map +1 -1
  33. package/dist/web/interfaces.d.ts +5 -1
  34. package/dist/web/interfaces.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/cli/commands/init.command.ts +1 -1
  37. package/src/core/decorators/exception-filter.decorator.ts +20 -0
  38. package/src/core/decorators/guard.decorator.ts +20 -0
  39. package/src/core/decorators/interceptor.decorator.ts +20 -0
  40. package/src/core/decorators/middleware.decorator.ts +25 -0
  41. package/src/core/metadata/metadata-keys.ts +1 -0
  42. package/src/core/metadata/metadata-storage.ts +114 -0
  43. package/src/web/application.ts +141 -6
  44. package/src/web/interfaces.ts +11 -2
package/README.md CHANGED
@@ -1,418 +1,155 @@
1
- ## README.md
1
+ # Fragment TS - A Modern TypeScript Framework
2
2
 
3
- ````markdown
4
- # Fragment Framework
3
+ [![npm version](https://img.shields.io/npm/v/fragment-ts)](https://www.npmjs.com/package/fragment-ts)
4
+ [![License](https://img.shields.io/npm/l/fragment-ts)](LICENSE)
5
+ [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
5
6
 
6
- A Spring Boot-style framework for TypeScript with Express and TypeORM.
7
-
8
- ## Installation
9
-
10
- ```bash
11
- npm install -g fragment-ts
12
- ```
13
- ````
14
-
15
- ## Quick Start
16
-
17
- ```bash
18
- # Create a new project (in current directory)
19
- fragment init .
20
-
21
- # Or create a new directory
22
- fragment init my-app
23
- cd my-app
24
-
25
- # Start development server with hot reload
26
- fragment serve
27
-
28
- # Build for production
29
- fragment build
30
-
31
- # Start production server
32
- npm start
33
- ```
7
+ Fragment TS is a lightweight, dependency injection-based framework for building scalable TypeScript applications with minimal configuration. Inspired by Spring and NestJS, it provides a clean architecture with powerful decorators for controllers, services, repositories, and more.
34
8
 
35
9
  ## Features
36
10
 
37
- - ๐ŸŽฏ **Decorator-based API** - Clean, intuitive syntax inspired by Spring Boot
38
- - ๐Ÿ’‰ **Dependency Injection** - Automatic resolution of dependencies
39
- - ๐Ÿ—„๏ธ **TypeORM Integration** - Full database support with migrations
40
- - ๐Ÿ” **Built-in Authentication** - JWT tokens, password hashing, role guards
41
- - ๐Ÿค– **AI Integrations** - OpenAI, Ollama, and custom AI providers
42
- - ๐Ÿ”Œ **Plugin System** - Extensible architecture
43
- - ๐Ÿงช **Testing Framework** - Built-in test runner
44
- - ๐Ÿ“ฆ **All-in-One Package** - No complex setup required
45
- - ๐Ÿ”„ **Hot Reload** - Automatic server restart on file changes
46
- - ๐ŸŽจ **Code Generation** - CLI tools for rapid development
47
-
48
- ## CLI Commands
49
-
50
- ### Project Management
51
-
52
- ```bash
53
- fragment init [dir] # Initialize new project (use . for current directory)
54
- fragment init . --features=auth # Initialize with specific features
55
- fragment serve # Start development server with hot reload
56
- fragment serve --port=4000 # Start on custom port
57
- fragment build # Build for production
58
- ```
59
-
60
- ### Code Generation
61
-
62
- ```bash
63
- fragment generate controller <name> # Generate controller
64
- fragment generate service <name> # Generate service with optional repository
65
- fragment generate entity <name> # Generate TypeORM entity
66
- fragment generate resource <name> # Generate complete CRUD resource
67
- fragment generate dto <name> # Generate Data Transfer Object
68
- fragment generate repository <name> # Generate repository
69
- ```
70
-
71
- **Examples:**
72
-
73
- ```bash
74
- fragment generate resource product # Creates controller, service, entity, DTO, repository
75
- fragment g controller users # Shorthand: g = generate
76
- ```
11
+ - ๐Ÿš€ **Dependency Injection**: Automatic wiring of components with `@Autowired` and other decorators
12
+ - ๐ŸŽฎ **REST Controllers**: Define routes with familiar decorators like `@Get()`, `@Post()`
13
+ - ๐Ÿ’พ **TypeORM Integration**: Seamless repository pattern with `@InjectRepository`
14
+ - โš™๏ธ **Modular Architecture**: Organize code into services, controllers, and repositories
15
+ - ๐Ÿ”ง **Auto-configuration**: Smart component scanning and registration
16
+ - ๐Ÿงช **Testable Design**: Easily mock dependencies for unit testing
17
+ - ๐Ÿ“ฆ **Production Ready**: Works with compiled JavaScript and TypeScript source
77
18
 
78
- ### Database Operations
79
-
80
- ```bash
81
- fragment migrate # Run all pending migrations
82
- fragment migrate:create <name> # Create a new migration
83
- fragment migrate:run # Run migrations
84
- fragment migrate:revert # Revert last migration
85
- fragment migrate:refresh # Drop all and re-run migrations
86
- fragment migrate:status # Show migration status
87
- fragment schema:sync # Sync database schema (dev only)
88
- fragment schema:drop # Drop all database tables
89
- fragment seed # Run database seeds
90
- fragment seed:create <name> # Create a new seed file
91
- ```
92
-
93
- ### Diagnostics & Debugging
19
+ ## Installation
94
20
 
95
21
  ```bash
96
- fragment routes # List all registered routes
97
- fragment routes --env=dev # List routes from TypeScript source
98
- fragment routes --env=prod # List routes from compiled dist/
99
- fragment beans # List all registered beans
100
- fragment beans --tree # Show beans as tree structure
101
- fragment info # Show application information
102
- fragment config # Show current configuration
103
- fragment version # Show Fragment version
22
+ npm install fragment-ts reflect-metadata typeorm mysql2
23
+ # or
24
+ yarn add fragment-ts reflect-metadata typeorm mysql2
104
25
  ```
105
26
 
106
- ## Project Structure
107
-
108
- ```
109
- my-app/
110
- โ”œโ”€โ”€ src/
111
- โ”‚ โ”œโ”€โ”€ controllers/ # HTTP controllers
112
- โ”‚ โ”œโ”€โ”€ services/ # Business logic
113
- โ”‚ โ”œโ”€โ”€ repositories/ # Data access layer
114
- โ”‚ โ”œโ”€โ”€ entities/ # TypeORM entities
115
- โ”‚ โ”œโ”€โ”€ dto/ # Data transfer objects
116
- โ”‚ โ”œโ”€โ”€ middlewares/ # Express middlewares
117
- โ”‚ โ”œโ”€โ”€ config/ # Configuration files
118
- โ”‚ โ””โ”€โ”€ main.ts # Application entry point
119
- โ”œโ”€โ”€ dist/ # Compiled JavaScript (after build)
120
- โ”œโ”€โ”€ test/ # Test files
121
- โ”œโ”€โ”€ fragment.json # Fragment configuration
122
- โ”œโ”€โ”€ tsconfig.json # TypeScript configuration
123
- โ””โ”€โ”€ package.json
124
- ```
125
-
126
- ## Basic Example
27
+ ## Quick Start
127
28
 
128
- ### Application Entry Point
29
+ ### 1. Create your application entry point:
129
30
 
130
31
  ```typescript
131
- // src/main.ts
32
+ // src/app.ts
132
33
  import 'reflect-metadata';
133
- import { FragmentApplication, FragmentWebApplication } from 'fragment-ts;
34
+ import { FragmentApplication, DIContainer } from 'fragment-ts';
134
35
 
135
36
  @FragmentApplication({
136
37
  port: 3000,
137
38
  autoScan: true
138
39
  })
139
- class Application {}
40
+ export class Application {
41
+ constructor() {
42
+ console.log('Application starting...');
43
+ }
44
+ }
140
45
 
141
46
  async function bootstrap() {
142
47
  const app = new FragmentWebApplication();
143
48
  await app.bootstrap(Application);
144
49
  }
145
50
 
146
- bootstrap();
51
+ bootstrap().catch(console.error);
147
52
  ```
148
53
 
149
- ### Creating a Controller
54
+ ### 2. Create a controller:
150
55
 
151
56
  ```typescript
152
- // src/controllers/users.controller.ts
153
- import { Controller, Get, Post, Put, Delete, Body, Param } from "fragment-ts";
154
- import { UserService } from "../services/user.service";
57
+ // src/controllers/user.controller.ts
58
+ import { Controller, Get, Autowired } from 'fragment-ts';
59
+ import { UserService } from '../services/user.service';
155
60
 
156
- @Controller("/users")
61
+ @Controller('/users')
157
62
  export class UserController {
158
- constructor(private userService: UserService) {}
63
+ @Autowired()
64
+ private userService!: UserService;
159
65
 
160
66
  @Get()
161
67
  async findAll() {
162
68
  return this.userService.findAll();
163
69
  }
164
-
165
- @Get("/:id")
166
- async findOne(@Param("id") id: string) {
167
- return this.userService.findOne(id);
168
- }
169
-
170
- @Post()
171
- async create(@Body() body: any) {
172
- return this.userService.create(body);
173
- }
174
-
175
- @Put("/:id")
176
- async update(@Param("id") id: string, @Body() body: any) {
177
- return this.userService.update(id, body);
178
- }
179
-
180
- @Delete("/:id")
181
- async delete(@Param("id") id: string) {
182
- return this.userService.delete(id);
183
- }
184
70
  }
185
71
  ```
186
72
 
187
- ### Creating a Service
73
+ ### 3. Create a service:
188
74
 
189
75
  ```typescript
190
76
  // src/services/user.service.ts
191
- import { Service } from "fragment-ts";
192
- import { UserRepository } from "../repositories/user.repository";
77
+ import { Service, Autowired } from 'fragment-ts';
78
+ import { UserRepository } from '../repositories/user.repository';
193
79
 
194
80
  @Service()
195
81
  export class UserService {
196
- constructor(private userRepository: UserRepository) {}
197
-
82
+ @Autowired()
83
+ private userRepository!: UserRepository;
84
+
198
85
  async findAll() {
199
86
  return this.userRepository.findAll();
200
87
  }
201
-
202
- async findOne(id: string) {
203
- return this.userRepository.findById(parseInt(id));
204
- }
205
-
206
- async create(data: any) {
207
- return this.userRepository.create(data);
208
- }
209
-
210
- async update(id: string, data: any) {
211
- return this.userRepository.update(parseInt(id), data);
212
- }
213
-
214
- async delete(id: string) {
215
- return this.userRepository.delete(parseInt(id));
216
- }
217
88
  }
218
89
  ```
219
90
 
220
- ## Authentication Example
91
+ ### 4. Create a repository:
221
92
 
222
93
  ```typescript
223
- import { Controller, Post, Body } from "fragment-ts";
224
- import { AuthModule } from "fragment-ts";
225
-
226
- @Controller("/auth")
227
- export class AuthController {
228
- @Post("/login")
229
- async login(@Body() body: any) {
230
- // Verify user credentials...
231
- const token = AuthModule.generateToken({
232
- userId: "123",
233
- email: body.email,
234
- roles: ["user"],
235
- });
236
- return { token };
237
- }
238
-
239
- @Post("/register")
240
- async register(@Body() body: any) {
241
- const hashedPassword = await AuthModule.hashPassword(body.password);
242
- // Save user...
243
- return { message: "User registered successfully" };
94
+ // src/repositories/user.repository.ts
95
+ import { Repository, InjectRepository } from 'fragment-ts';
96
+ import { User } from '../entities/user.entity';
97
+ import { Repository as TypeOrmRepository } from 'typeorm';
98
+
99
+ @Repository()
100
+ export class UserRepository {
101
+ @InjectRepository(User)
102
+ private repo!: TypeOrmRepository<User>;
103
+
104
+ async findAll() {
105
+ return this.repo.find();
244
106
  }
245
107
  }
246
108
  ```
247
109
 
248
- ## AI Integration Example
110
+ ### 5. Configure TypeORM:
249
111
 
250
112
  ```typescript
251
- import { Service } from "fragment-ts";
252
- import { AIModule } from "fragment-ts";
253
-
254
- @Service()
255
- export class ChatService {
256
- private aiModule: AIModule;
257
-
258
- constructor() {
259
- this.aiModule = new AIModule({
260
- openaiKey: process.env.OPENAI_API_KEY,
261
- });
262
- }
263
-
264
- async chat(message: string) {
265
- return this.aiModule.complete([
266
- { role: "system", content: "You are a helpful assistant." },
267
- { role: "user", content: message },
268
- ]);
269
- }
270
- }
271
- ```
272
-
273
- ## Configuration
274
-
275
- ### fragment.json
276
-
277
- ```json
113
+ // fragment.json
278
114
  {
279
- "server": {
280
- "port": 3000,
281
- "host": "0.0.0.0"
282
- },
283
- "database": {
284
- "type": "postgres",
285
- "host": "${DB_HOST}",
286
- "port": ${DB_PORT},
287
- "username": "${DB_USERNAME}",
288
- "password": "${DB_PASSWORD}",
289
- "database": "${DB_NAME}",
290
- "synchronize": false,
291
- "logging": false,
292
- "entities": ["dist/**/*.entity.js"],
293
- "migrations": ["dist/migrations/**/*.js"]
294
- }
115
+ "type": "mysql",
116
+ "host": "localhost",
117
+ "port": 3306,
118
+ "username": "root",
119
+ "password": "password",
120
+ "database": "my_db",
121
+ "entities": ["dist/entities/**/*.js"],
122
+ "synchronize": true
295
123
  }
296
-
297
- ```
298
-
299
- ### Environment Variables
300
-
301
- ```env
302
- NODE_ENV=development
303
- PORT=3000
304
- JWT_SECRET=${JWT_SECRET}
305
- DATABASE_URL=postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
306
- DB_HOST=localhost
307
- DB_PORT=5432
308
- DB_USERNAME=postgres
309
- DB_PASSWORD=password
310
- DB_NAME=myapp
311
- OPENAI_API_KEY=${OPENAI_API_KEY}
312
124
  ```
313
125
 
314
- ## Development Workflow
126
+ ## Running the Application
315
127
 
316
128
  ```bash
317
- # 1. Initialize project
318
- fragment init my-api --features=auth,database
319
- cd my-api
129
+ # Development (TypeScript)
130
+ npx ts-node src/app.ts
320
131
 
321
- # 2. Generate resources
322
- fragment generate resource user
323
- fragment generate resource product
324
-
325
- # 3. Create and run migrations
326
- fragment migrate:create InitialSchema
327
- fragment migrate:run
328
-
329
- # 4. Start development server
330
- fragment serve
331
-
332
- # 5. List routes to verify
333
- fragment routes
334
-
335
- # 6. Build for production
336
- fragment build
337
-
338
- # 7. Start production server
132
+ # Production (compiled JavaScript)
339
133
  npm start
340
134
  ```
341
135
 
342
- ## Testing
343
-
344
- ```typescript
345
- // test/user.spec.ts
346
- import { describe, it, expect } from "fragment-ts";
347
-
348
- describe("UserService", () => {
349
- it("should create a user", async () => {
350
- const user = await userService.create({
351
- email: "test@example.com",
352
- name: "Test User",
353
- });
354
-
355
- expect(user.email).toBe("test@example.com");
356
- });
357
- });
358
- ```
359
-
360
- ```bash
361
- # Run tests
362
- fragment test
363
- ```
364
-
365
- ## Production Deployment
136
+ ## Configuration
366
137
 
367
- ### Docker
138
+ Fragment automatically detects whether you're running in development (TypeScript) or production (compiled JavaScript) mode:
368
139
 
369
- ```dockerfile
370
- FROM node:18-alpine
371
- WORKDIR /app
372
- COPY package*.json ./
373
- RUN npm ci --only=production
374
- COPY . .
375
- RUN npm run build
376
- EXPOSE 3000
377
- CMD ["npm", "start"]
378
- ```
140
+ - **Development Mode**: Scans `src/` directory for `.ts` files
141
+ - **Production Mode**: Scans `dist/` directory for `.js` files
379
142
 
143
+ You can explicitly set the mode with the environment variable:
380
144
  ```bash
381
- docker build -t my-app .
382
- docker run -p 3000:3000 my-app
145
+ FRAGMENT_DEV_MODE=true node dist/app.js
383
146
  ```
384
147
 
385
- ## Examples
386
-
387
- Check out the [examples](./examples) directory for complete working applications:
388
-
389
- - REST API
390
- - Authentication System
391
- - AI Chat Application
392
- - E-commerce Backend
393
-
394
- ## Documentation
395
-
396
- For comprehensive guides and API reference, visit:
397
-
398
- - **Full Documentation**: [https://fragment.digitwhale.com](https://fragment.digitwhale.com)
399
- - **Use Cases & Examples**: [USECASES.md](./USECASES.md)
400
- - **API Reference**: [https://fragment.digitwhale.com/api](https://fragment.digitwhale.com/api)
401
-
402
- ## Community
148
+ ## Support
403
149
 
404
- - **GitHub**: [https://github.com/fragment/framework](https://github.com/fragment/framework)
405
- - **Discord**: [https://discord.gg/fragment](https://discord.gg/fragment)
406
- - **Twitter**: [@FragmentJS](https://twitter.com/FragmentJS)
407
-
408
- ## Contributing
409
-
410
- We welcome contributions! See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
150
+ For issues and feature requests, please [open an issue](https://github.com/yourusername/fragment-ts/issues).
411
151
 
412
152
  ## License
413
153
 
414
- MIT ยฉ Fragment Team
415
-
416
- ```
417
-
418
- ```
154
+ MIT ยฉ [Your Name]
155
+ ```