raiton 5.9.4-beta.15 → 5.9.7-beta.18
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/build/bin/index.mjs +3 -1
- package/docs/README.md +136 -0
- package/docs/cli/README.md +81 -0
- package/docs/cli/commands.md +167 -0
- package/docs/cli/hmr.md +121 -0
- package/docs/core/README.md +67 -0
- package/docs/core/application.md +149 -0
- package/docs/core/builder-thread.md +231 -0
- package/docs/core/config.md +124 -0
- package/docs/core/controller.md +120 -0
- package/docs/core/injection.md +226 -0
- package/docs/core/middleware.md +172 -0
- package/docs/core/plugin-scope.md +171 -0
- package/docs/core/router.md +122 -0
- package/docs/framework/README.md +96 -0
- package/docs/framework/base-classes.md +214 -0
- package/docs/framework/decorators/README.md +60 -0
- package/docs/framework/decorators/controllable.md +151 -0
- package/docs/framework/decorators/injection.md +186 -0
- package/docs/framework/decorators/middleware.md +167 -0
- package/docs/framework/decorators/openapi.md +254 -0
- package/docs/framework/decorators/parametrable.md +248 -0
- package/docs/framework/decorators/socket.md +152 -0
- package/docs/framework/encryption.md +183 -0
- package/docs/framework/env.md +88 -0
- package/docs/framework/plugins/README.md +50 -0
- package/docs/framework/plugins/body-parser.md +138 -0
- package/docs/framework/plugins/openapi.md +217 -0
- package/docs/framework/plugins/security.md +159 -0
- package/docs/framework/responses.md +180 -0
- package/docs/framework/runtime.md +138 -0
- package/docs/types/README.md +206 -0
- package/package.json +1 -1
- package/source/core/application.ts +2 -0
- package/source/core/router/handler.ts +2 -0
- package/source/framework/decorators/route-interaction.decorator.ts +1 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Module Framework (`source/framework/`)
|
|
2
|
+
|
|
3
|
+
Le framework fournit les décorateurs, le runtime, les plugins intégrés, les réponses, les exceptions, les enums et les classes de base.
|
|
4
|
+
|
|
5
|
+
> **Navigation :** [← docs/](../README.md) | [↑ Index](../README.md) | [runtime.md →](runtime.md)
|
|
6
|
+
|
|
7
|
+
## Table des matières
|
|
8
|
+
|
|
9
|
+
| Fichier | Description |
|
|
10
|
+
|---------|-------------|
|
|
11
|
+
| [runtime.md](runtime.md) | Adaptateurs runtime (Bun, Node.js, Deno) |
|
|
12
|
+
| [responses.md](responses.md) | Réponses HTTP et exceptions |
|
|
13
|
+
| [encryption.md](encryption.md) | Hachage et chiffrement |
|
|
14
|
+
| [env.md](env.md) | Variables d'environnement typées |
|
|
15
|
+
| [base-classes.md](base-classes.md) | DTO, ViewModel, Service, Repository |
|
|
16
|
+
| [decorators/](decorators/README.md) | Tous les décorateurs avec exemples |
|
|
17
|
+
| [plugins/](plugins/README.md) | Plugins intégrés (body-parser, OpenAPI, sécurité) |
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
framework/
|
|
21
|
+
├── index.ts → Réexporte tout le module
|
|
22
|
+
├── controllers.ts → DelegateController (base)
|
|
23
|
+
├── services.ts → DelegateService (base)
|
|
24
|
+
├── repositories.ts → DelegateRepository (base)
|
|
25
|
+
├── view-model.ts → ViewModel (base)
|
|
26
|
+
├── data-transfer-object.ts → DataTransferObject (validation)
|
|
27
|
+
├── encryption.ts → Encryption (hashing, chiffrement)
|
|
28
|
+
├── parameter-bag.ts → ParameterBag (conteneur typé)
|
|
29
|
+
├── artifacts.ts → Artifacts (registre HMR)
|
|
30
|
+
├── env.ts → env(), envGroup()
|
|
31
|
+
├── decorators/
|
|
32
|
+
│ ├── index.ts → Réexporte tous les décorateurs
|
|
33
|
+
│ ├── controllable.decorator.ts → @Controllable
|
|
34
|
+
│ ├── routable.decorator.ts → @Get, @Post, @Put, @Patch, @Delete...
|
|
35
|
+
│ ├── parametrable.decorator.ts → @Param, @Query, @Body, @Headers...
|
|
36
|
+
│ ├── injection.decorator.ts → @Injectable, @Inject
|
|
37
|
+
│ ├── middleware.decorator.ts → @Middleware
|
|
38
|
+
│ ├── socket.decorator.ts → @Socket, @OnSocket*
|
|
39
|
+
│ ├── api.decorator.ts → @ApiTags, @ApiOperation, @ApiSecurity...
|
|
40
|
+
│ ├── api-response.decorator.ts → @ApiResponse, @ApiOkResponse, @Api*Response
|
|
41
|
+
│ └── guard.decorator.ts → Guards
|
|
42
|
+
├── plugins/
|
|
43
|
+
│ ├── index.ts → Réexporte les plugins
|
|
44
|
+
│ ├── body-parser.plugin.ts → bodyParserPlugin
|
|
45
|
+
│ ├── openapi.plugin.ts → openApiPlugin
|
|
46
|
+
│ └── security/
|
|
47
|
+
│ ├── index.ts → Security (classe statique)
|
|
48
|
+
│ ├── cors.ts → CORS middleware
|
|
49
|
+
│ ├── headers.ts → Headers sécurité
|
|
50
|
+
│ ├── rate-limit.ts → Rate limiting
|
|
51
|
+
│ ├── method-guard.ts → Méthodes HTTP autorisées
|
|
52
|
+
│ └── body-limit.ts → Taille maximale du body
|
|
53
|
+
├── runtime/
|
|
54
|
+
│ ├── index.ts → Runtime (fabrique)
|
|
55
|
+
│ ├── node/ → Adaptateur Node.js (serveur, request, reply)
|
|
56
|
+
│ ├── bun/ → Adaptateur Bun
|
|
57
|
+
│ └── deno/ → Adaptateur Deno (stub)
|
|
58
|
+
├── enums/
|
|
59
|
+
│ ├── index.ts → Réexporte les enums
|
|
60
|
+
│ ├── http-method.enum.ts → HttpMethod
|
|
61
|
+
│ ├── http-status.enum.ts → HttpStatus
|
|
62
|
+
│ ├── http-parameters.enum.ts → Parametrable
|
|
63
|
+
│ ├── runtime.enum.ts → RuntimeType
|
|
64
|
+
│ ├── encrypted.enum.ts → HashAlgoEnum, CipherAlgoEnum, PasswordAlgoEnum
|
|
65
|
+
│ ├── timestamp.enum.ts → TimestampEnum
|
|
66
|
+
│ └── event.message.enum.ts → EventMessageEnum
|
|
67
|
+
├── constants/
|
|
68
|
+
│ ├── index.ts
|
|
69
|
+
│ ├── decorators.constant.ts → METADATA_KEYS
|
|
70
|
+
│ └── microservices.constant.ts → MICROSERVICE_NAME
|
|
71
|
+
├── exceptions/
|
|
72
|
+
│ ├── index.ts
|
|
73
|
+
│ ├── throwable.ts → Throwable, throwError, throwException...
|
|
74
|
+
│ └── http-exception.ts → HttpException
|
|
75
|
+
├── responses/
|
|
76
|
+
│ ├── index.ts
|
|
77
|
+
│ ├── http.ts → HttpResponse
|
|
78
|
+
│ ├── error.ts → HttpErrorResponse
|
|
79
|
+
│ ├── http-throwable.ts → ThrowableResponse
|
|
80
|
+
│ └── helpers.ts → RaitonResponses
|
|
81
|
+
└── utilities/
|
|
82
|
+
├── index.ts
|
|
83
|
+
├── artifact.util.ts → isControllerArtifact, isSocketArtifact...
|
|
84
|
+
├── callable.util.ts → retryCallable
|
|
85
|
+
├── utilities.util.ts → getType, stabilizeJson
|
|
86
|
+
├── json.util.ts → tryParseJson, JsonUtil
|
|
87
|
+
├── path.util.ts → getDirname, getFilename, getPackageRoot
|
|
88
|
+
├── cookie.util.ts → parseCookie
|
|
89
|
+
├── ip.util.ts → getRealIp
|
|
90
|
+
├── url.ts → escapeUrl
|
|
91
|
+
└── openapi.utils.ts → generateOpenApiSpec, getSchemaFromType
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
[← docs/](../README.md) | [↑ Index](../README.md) | [runtime.md →](runtime.md)
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# Classes de Base
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/`
|
|
4
|
+
**Import :** `import { DataTransferObject, ViewModel, DelegateController, DelegateService, DelegateRepository, ParameterBag, Artifacts } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← env.md](env.md) | [↑ framework/](README.md) | [decorators/ →](decorators/README.md)
|
|
7
|
+
|
|
8
|
+
## `DataTransferObject` (DTO)
|
|
9
|
+
|
|
10
|
+
**Fichier :** `source/framework/data-transfer-object.ts`
|
|
11
|
+
|
|
12
|
+
Classe de base pour la validation des données entrantes. Utilise `class-validator`.
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { DataTransferObject } from "raiton/framework"
|
|
16
|
+
import { IsString, IsEmail, IsNotEmpty, IsNumber, Min, Max } from "class-validator"
|
|
17
|
+
|
|
18
|
+
class CreateUserDto extends DataTransferObject {
|
|
19
|
+
@IsString()
|
|
20
|
+
@IsNotEmpty()
|
|
21
|
+
name!: string
|
|
22
|
+
|
|
23
|
+
@IsEmail()
|
|
24
|
+
email!: string
|
|
25
|
+
|
|
26
|
+
@IsNumber()
|
|
27
|
+
@Min(18)
|
|
28
|
+
age!: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class UpdateUserDto extends DataTransferObject {
|
|
32
|
+
@IsString()
|
|
33
|
+
name?: string
|
|
34
|
+
|
|
35
|
+
@IsEmail()
|
|
36
|
+
email?: string
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Utilisation dans un contrôleur
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
@Controllable("/users")
|
|
44
|
+
export class UserController {
|
|
45
|
+
@Post("/")
|
|
46
|
+
create(@Body() data: CreateUserDto) {
|
|
47
|
+
// data est automatiquement validé avant l'exécution
|
|
48
|
+
// Si invalide → erreur de validation
|
|
49
|
+
return { success: true, data }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## `ViewModel`
|
|
55
|
+
|
|
56
|
+
**Fichier :** `source/framework/view-model.ts`
|
|
57
|
+
|
|
58
|
+
Classe de base pour les modèles de vue (formatage des réponses).
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { ViewModel } from "raiton/framework"
|
|
62
|
+
|
|
63
|
+
class UserViewModel extends ViewModel {
|
|
64
|
+
constructor(private user: any) {
|
|
65
|
+
super()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
toJSON() {
|
|
69
|
+
return {
|
|
70
|
+
id: this.user.id,
|
|
71
|
+
name: this.user.name,
|
|
72
|
+
email: this.user.email,
|
|
73
|
+
createdAt: this.user.created_at,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Utilisation
|
|
79
|
+
@Controllable("/users")
|
|
80
|
+
export class UserController {
|
|
81
|
+
@Get("/:id")
|
|
82
|
+
get(@Param("id") id: string) {
|
|
83
|
+
const user = { id: 1, name: "Alice", email: "alice@test.com", password: "secret" }
|
|
84
|
+
return new UserViewModel(user) // Le mot de passe est exclu
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## `DelegateController`
|
|
90
|
+
|
|
91
|
+
**Fichier :** `source/framework/controllers.ts`
|
|
92
|
+
|
|
93
|
+
Classe de base vide pour les contrôleurs. Actuellement sans fonctionnalité ajoutée.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { DelegateController } from "raiton/framework"
|
|
97
|
+
|
|
98
|
+
export class HealthController extends DelegateController {
|
|
99
|
+
// Méthodes spécifiques
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## `DelegateService`
|
|
104
|
+
|
|
105
|
+
**Fichier :** `source/framework/services.ts`
|
|
106
|
+
|
|
107
|
+
Classe de base pour les services métier. Fournit `this.repository` pour l'accès aux données.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { DelegateService, DelegateRepository, Injectable } from "raiton/framework"
|
|
111
|
+
|
|
112
|
+
class UserRepository extends DelegateRepository {
|
|
113
|
+
findAll() { return [] }
|
|
114
|
+
findById(id: number) { return { id } }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@Injectable(LifetimeEnum.SINGLETON)
|
|
118
|
+
class UserService extends DelegateService {
|
|
119
|
+
constructor() {
|
|
120
|
+
super()
|
|
121
|
+
this.repository = UserRepository // ou une instance
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
getUsers() {
|
|
125
|
+
return this.repository.findAll()
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## `DelegateRepository`
|
|
131
|
+
|
|
132
|
+
**Fichier :** `source/framework/repositories.ts`
|
|
133
|
+
|
|
134
|
+
Classe de base pour les repositories. Vérifie que le modèle est valide avant les opérations.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { DelegateRepository } from "raiton/framework"
|
|
138
|
+
|
|
139
|
+
class UserRepository extends DelegateRepository {
|
|
140
|
+
// this.model est disponible après initialisation
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## `ParameterBag`
|
|
145
|
+
|
|
146
|
+
**Fichier :** `source/framework/parameter-bag.ts`
|
|
147
|
+
|
|
148
|
+
Conteneur typé pour des paramètres dynamiques avec accès sécurisé.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { ParameterBag } from "raiton/framework"
|
|
152
|
+
|
|
153
|
+
const bag = new ParameterBag()
|
|
154
|
+
|
|
155
|
+
// Définir des valeurs
|
|
156
|
+
bag.set("name", "Alice")
|
|
157
|
+
bag.set("count", 42)
|
|
158
|
+
bag.set("active", true)
|
|
159
|
+
|
|
160
|
+
// Lire des valeurs
|
|
161
|
+
bag.get("name") // "Alice"
|
|
162
|
+
bag.get("missing") // undefined
|
|
163
|
+
bag.get("count") // 42
|
|
164
|
+
|
|
165
|
+
// Itérer
|
|
166
|
+
bag.forEach((key, value) => console.log(key, value))
|
|
167
|
+
|
|
168
|
+
// Vider
|
|
169
|
+
bag.clear()
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## `Artifacts`
|
|
173
|
+
|
|
174
|
+
**Fichier :** `source/framework/artifacts.ts`
|
|
175
|
+
|
|
176
|
+
Registre des types d'artifacts avec support HMR.
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { Artifacts } from "raiton/framework"
|
|
180
|
+
|
|
181
|
+
// Types par défaut
|
|
182
|
+
Artifacts.defaultTypes // ["controller", "socket", "service"]
|
|
183
|
+
|
|
184
|
+
// Enregistrer des types supplémentaires
|
|
185
|
+
Artifacts.registerMany("command", "event")
|
|
186
|
+
|
|
187
|
+
// Vérifier si un fichier est un artifact
|
|
188
|
+
Artifacts.is("user.controller.ts") // true (contient "controller")
|
|
189
|
+
Artifacts.is("user.service.ts") // true
|
|
190
|
+
Artifacts.is("README.md") // false
|
|
191
|
+
|
|
192
|
+
// Recharger un artifact (HMR)
|
|
193
|
+
Artifacts.reload(importedModule, filename)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Fonctions utilitaires associées
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import {
|
|
200
|
+
isControllerArtifact,
|
|
201
|
+
isSocketArtifact,
|
|
202
|
+
isServiceArtifact,
|
|
203
|
+
isArtifact,
|
|
204
|
+
} from "raiton/framework"
|
|
205
|
+
|
|
206
|
+
isControllerArtifact("user.controller.ts") // true
|
|
207
|
+
isSocketArtifact("chat.socket.ts") // true
|
|
208
|
+
isServiceArtifact("auth.service.ts") // true
|
|
209
|
+
isArtifact("user.controller.ts") // true (n'importe quel type)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
[← env.md](env.md) | [↑ framework/](README.md) | [decorators/ →](decorators/README.md)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Décorateurs
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/decorators/`
|
|
4
|
+
**Import :** `import { ... } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← framework/](../README.md) | [↑ framework/](../README.md) | [controllable.md →](controllable.md)
|
|
7
|
+
|
|
8
|
+
## Table des matières
|
|
9
|
+
|
|
10
|
+
| Fichier | Description |
|
|
11
|
+
|---------|-------------|
|
|
12
|
+
| [controllable.md](controllable.md) | `@Controllable`, `@Get`, `@Post`, `@Put`, `@Delete`... |
|
|
13
|
+
| [parametrable.md](parametrable.md) | `@Param`, `@Query`, `@Body`, `@Headers`, `@Req`... |
|
|
14
|
+
| [injection.md](injection.md) | `@Injectable`, `@Inject` |
|
|
15
|
+
| [middleware.md](middleware.md) | `@Middleware` |
|
|
16
|
+
| [socket.md](socket.md) | `@Socket`, `@OnSocketConnect`, `@OnSocketMessage`... |
|
|
17
|
+
| [openapi.md](openapi.md) | `@ApiTags`, `@ApiOperation`, `@ApiResponse`... |
|
|
18
|
+
|
|
19
|
+
Les décorateurs utilisent `reflect-metadata` et `experimentalDecorators: true` + `emitDecoratorMetadata: true` dans `tsconfig.json`.
|
|
20
|
+
|
|
21
|
+
## Tableau récapitulatif
|
|
22
|
+
|
|
23
|
+
| Décorateur | Cible | Description | Détails |
|
|
24
|
+
|-----------|-------|-------------|---------|
|
|
25
|
+
| `@Controllable` | Classe | Déclare un contrôleur REST | [Voir](controllable.md) |
|
|
26
|
+
| `@Get`/`@Post`/... | Méthode | Route HTTP | [Voir](controllable.md) |
|
|
27
|
+
| `@Param`/`@Query`/`@Body`... | Paramètre | Injection de paramètre | [Voir](parametrable.md) |
|
|
28
|
+
| `@Injectable` | Classe | Enregistrement DI | [Voir](injection.md) |
|
|
29
|
+
| `@Inject` | Constructeur/propriété | Injection de dépendance | [Voir](injection.md) |
|
|
30
|
+
| `@Middleware` | Classe/méthode | Middleware sur contrôleur/route | [Voir](middleware.md) |
|
|
31
|
+
| `@Socket` | Classe | Handler socket | [Voir](socket.md) |
|
|
32
|
+
| `@OnSocket*` | Méthode | Événement socket | [Voir](socket.md) |
|
|
33
|
+
| `@ApiTags` | Classe/méthode | Tag OpenAPI | [Voir](openapi.md) |
|
|
34
|
+
| `@ApiOperation` | Méthode | Description d'opération | [Voir](openapi.md) |
|
|
35
|
+
| `@ApiParam`/`@ApiQuery` | Méthode | Paramètre OpenAPI | [Voir](openapi.md) |
|
|
36
|
+
| `@ApiBody` | Méthode | Corps OpenAPI | [Voir](openapi.md) |
|
|
37
|
+
| `@ApiSecurity` | Classe/méthode | Sécurité OpenAPI | [Voir](openapi.md) |
|
|
38
|
+
| `@ApiResponse` | Méthode | Réponse OpenAPI | [Voir](openapi.md) |
|
|
39
|
+
| `@ApiOkResponse` | Méthode | Raccourci 200 | [Voir](openapi.md) |
|
|
40
|
+
| `@ApiCreatedResponse` | Méthode | Raccourci 201 | [Voir](openapi.md) |
|
|
41
|
+
| `@ApiBadRequestResponse` | Méthode | Raccourci 400 | [Voir](openapi.md) |
|
|
42
|
+
| `@ApiUnauthorizedResponse` | Méthode | Raccourci 401 | [Voir](openapi.md) |
|
|
43
|
+
| `@ApiForbiddenResponse` | Méthode | Raccourci 403 | [Voir](openapi.md) |
|
|
44
|
+
| `@ApiNotFoundResponse` | Méthode | Raccourci 404 | [Voir](openapi.md) |
|
|
45
|
+
| `@ApiInternalServerErrorResponse` | Méthode | Raccourci 500 | [Voir](openapi.md) |
|
|
46
|
+
| `@RouteInteraction` | Méthode | Description d'interaction | [Voir](openapi.md) |
|
|
47
|
+
|
|
48
|
+
## Règles générales
|
|
49
|
+
|
|
50
|
+
1. **`experimentalDecorators` et `emitDecoratorMetadata`** doivent être activés dans `tsconfig.json`
|
|
51
|
+
2. **`reflect-metadata`** doit être importé une fois au point d'entrée :
|
|
52
|
+
```typescript
|
|
53
|
+
import "reflect-metadata"
|
|
54
|
+
```
|
|
55
|
+
3. Les décorateurs de paramètres (`@Param`, `@Query`...) doivent être utilisés **après** le décorateur de route (`@Get`, `@Post`...)
|
|
56
|
+
4. L'ordre des décorateurs sur une méthode n'a pas d'importance
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
[← framework/](../README.md) | [↑ framework/](../README.md) | [controllable.md →](controllable.md)
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# Décorateurs de Contrôleurs et Routes
|
|
2
|
+
|
|
3
|
+
**Fichier :** `source/framework/decorators/controllable.decorator.ts`, `routable.decorator.ts`
|
|
4
|
+
**Import :** `import { Controllable, Get, Post, Put, Patch, Delete, Options, Head, Trace } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← decorators/](README.md) | [↑ decorators/](README.md) | [parametrable.md →](parametrable.md)
|
|
7
|
+
|
|
8
|
+
## `@Controllable(prefix?)`
|
|
9
|
+
|
|
10
|
+
Marque une classe comme contrôleur REST. Le préfixe est optionnel.
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
@Controllable("/users")
|
|
14
|
+
export class UserController {
|
|
15
|
+
// ...
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Fonctionnement interne :**
|
|
20
|
+
1. Appelle `@Injectable(LifetimeEnum.TRANSIENT, className)` → enregistre le contrôleur dans le DI
|
|
21
|
+
2. Crée les métadonnées via `getControllerMetadata(target.prototype)` et définit le préfixe
|
|
22
|
+
|
|
23
|
+
**Sans préfixe :**
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
@Controllable()
|
|
27
|
+
export class HealthController {
|
|
28
|
+
@Get("/health") // Route : /health
|
|
29
|
+
check() { return "ok" }
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## `@Get`, `@Post`, `@Put`, `@Patch`, `@Delete`, `@Options`, `@Head`, `@Trace`
|
|
34
|
+
|
|
35
|
+
Déclarent une route HTTP sur une méthode de contrôleur.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
@Controllable("/users")
|
|
39
|
+
export class UserController {
|
|
40
|
+
@Get("/") // GET /users
|
|
41
|
+
list() { return [] }
|
|
42
|
+
|
|
43
|
+
@Get("/:id") // GET /users/:id
|
|
44
|
+
get(@Param("id") id: string) { return { id } }
|
|
45
|
+
|
|
46
|
+
@Post("/") // POST /users
|
|
47
|
+
create(@Body() data: any) { return data }
|
|
48
|
+
|
|
49
|
+
@Put("/:id") // PUT /users/:id
|
|
50
|
+
update(@Param("id") id: string, @Body() data: any) { return { id, ...data } }
|
|
51
|
+
|
|
52
|
+
@Patch("/:id") // PATCH /users/:id
|
|
53
|
+
patch(@Param("id") id: string, @Body() data: any) { return { id, ...data } }
|
|
54
|
+
|
|
55
|
+
@Delete("/:id") // DELETE /users/:id
|
|
56
|
+
remove(@Param("id") id: string) { return { deleted: true } }
|
|
57
|
+
|
|
58
|
+
@Options("/") // OPTIONS /users
|
|
59
|
+
options() { return { methods: ["GET", "POST"] } }
|
|
60
|
+
|
|
61
|
+
@Head("/:id") // HEAD /users/:id
|
|
62
|
+
head(@Param("id") id: string) { return }
|
|
63
|
+
|
|
64
|
+
@Trace("/") // TRACE /users
|
|
65
|
+
trace() { return { message: "trace" } }
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Chemins et paramètres d'URL
|
|
70
|
+
|
|
71
|
+
Les segments `:param` dans le chemin extraient automatiquement la valeur de l'URL :
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
@Controllable("/api")
|
|
75
|
+
export class ApiController {
|
|
76
|
+
@Get("/users/:userId/posts/:postId")
|
|
77
|
+
getPost(
|
|
78
|
+
@Param("userId") userId: string,
|
|
79
|
+
@Param("postId") postId: string,
|
|
80
|
+
) {
|
|
81
|
+
return { userId, postId }
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Routes avec version
|
|
87
|
+
|
|
88
|
+
Le paramètre `version` est supporté via `compileController` → `app.route()` mais pas directement dans les décorateurs de route. Pour versionner :
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
@Controllable("/v1/users")
|
|
92
|
+
export class UserV1Controller { /* ... */ }
|
|
93
|
+
|
|
94
|
+
@Controllable("/v2/users")
|
|
95
|
+
export class UserV2Controller { /* ... */ }
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Exemple complet
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { Controllable, Get, Post, Put, Delete, Param, Body } from "raiton/framework"
|
|
102
|
+
|
|
103
|
+
interface User {
|
|
104
|
+
id: number
|
|
105
|
+
name: string
|
|
106
|
+
email: string
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let users: User[] = [{ id: 1, name: "Alice", email: "alice@example.com" }]
|
|
110
|
+
let nextId = 2
|
|
111
|
+
|
|
112
|
+
@Controllable("/users")
|
|
113
|
+
export class UserController {
|
|
114
|
+
@Get("/")
|
|
115
|
+
list(): User[] {
|
|
116
|
+
return users
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Get("/:id")
|
|
120
|
+
get(@Param("id") id: string): User | undefined {
|
|
121
|
+
return users.find(u => u.id === Number(id))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@Post("/")
|
|
125
|
+
create(@Body() data: Omit<User, "id">): User {
|
|
126
|
+
const user = { id: nextId++, ...data }
|
|
127
|
+
users.push(user)
|
|
128
|
+
return user
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@Put("/:id")
|
|
132
|
+
update(@Param("id") id: string, @Body() data: Partial<User>): User | undefined {
|
|
133
|
+
const index = users.findIndex(u => u.id === Number(id))
|
|
134
|
+
if (index === -1) return undefined
|
|
135
|
+
users[index] = { ...users[index], ...data }
|
|
136
|
+
return users[index]
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@Delete("/:id")
|
|
140
|
+
remove(@Param("id") id: string): { deleted: boolean } {
|
|
141
|
+
const index = users.findIndex(u => u.id === Number(id))
|
|
142
|
+
if (index !== -1) users.splice(index, 1)
|
|
143
|
+
return { deleted: index !== -1 }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
[← decorators/](README.md) | [↑ decorators/](README.md) | [parametrable.md →](parametrable.md)
|
|
151
|
+
```
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# Décorateurs d'Injection
|
|
2
|
+
|
|
3
|
+
**Fichier :** `source/framework/decorators/injection.decorator.ts`
|
|
4
|
+
**Import :** `import { Injectable, Inject } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← parametrable.md](parametrable.md) | [↑ decorators/](README.md) | [middleware.md →](middleware.md)
|
|
7
|
+
|
|
8
|
+
## `@Injectable(lifetime?, name?, scope?)`
|
|
9
|
+
|
|
10
|
+
Enregistre une classe dans le conteneur DI.
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
import { Injectable } from "raiton/framework"
|
|
14
|
+
import { LifetimeEnum } from "@protorians/core"
|
|
15
|
+
|
|
16
|
+
// Singleton — une seule instance pour toute l'application
|
|
17
|
+
@Injectable(LifetimeEnum.SINGLETON)
|
|
18
|
+
class DatabaseService {
|
|
19
|
+
connect() { /* ... */ }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Transient — nouvelle instance à chaque injection
|
|
23
|
+
@Injectable(LifetimeEnum.TRANSIENT)
|
|
24
|
+
class MailService {
|
|
25
|
+
send(email: string) { /* ... */ }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Avec nom personnalisé
|
|
29
|
+
@Injectable(LifetimeEnum.SINGLETON, "cache")
|
|
30
|
+
class RedisCache {
|
|
31
|
+
get(key: string) { /* ... */ }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Avec scope symbol (isolation)
|
|
35
|
+
const adminScope = Symbol("admin")
|
|
36
|
+
@Injectable(LifetimeEnum.SINGLETON, "adminConfig", adminScope)
|
|
37
|
+
class AdminConfig {
|
|
38
|
+
// Accessible uniquement avec le scope admin
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Fonctionnement interne :**
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
// Ce que @Injectable(LifetimeEnum.SINGLETON, "db") produit :
|
|
46
|
+
const metadata = {
|
|
47
|
+
name: "db",
|
|
48
|
+
construct: DatabaseService,
|
|
49
|
+
lifetime: LifetimeEnum.SINGLETON,
|
|
50
|
+
scope: undefined,
|
|
51
|
+
}
|
|
52
|
+
Reflect.defineMetadata(METADATA_KEYS.CONTAINER, metadata, target)
|
|
53
|
+
Injection.registry("db", DatabaseService, LifetimeEnum.SINGLETON)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## `@Inject(token?)`
|
|
57
|
+
|
|
58
|
+
Injecte une dépendance dans un paramètre du constructeur ou une propriété.
|
|
59
|
+
|
|
60
|
+
### Injection dans le constructeur
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
@Injectable(LifetimeEnum.TRANSIENT)
|
|
64
|
+
class UserService {
|
|
65
|
+
constructor(
|
|
66
|
+
@Inject() private db: DatabaseService, // Résolution par type
|
|
67
|
+
@Inject("cache") private cache: RedisCache, // Résolution par nom
|
|
68
|
+
@Inject() private config: AppConfig,
|
|
69
|
+
) {}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Injection dans une propriété
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
@Injectable(LifetimeEnum.TRANSIENT)
|
|
77
|
+
class UserController {
|
|
78
|
+
@Inject() private userService!: UserService
|
|
79
|
+
|
|
80
|
+
@Inject("cache")
|
|
81
|
+
private cache!: RedisCache
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Résolution sans argument
|
|
86
|
+
|
|
87
|
+
Quand `@Inject()` est utilisé sans argument, le type est déduit automatiquement :
|
|
88
|
+
|
|
89
|
+
- **Constructeur** : via `design:paramtypes` (TypeScript émet le type natif)
|
|
90
|
+
- **Propriété** : via `design:type` (nécessite `emitDecoratorMetadata: true`)
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
// Avec emitDecoratorMetadata: true
|
|
94
|
+
class UserController {
|
|
95
|
+
constructor(
|
|
96
|
+
@Inject() private db: DatabaseService, // design:paramtypes[0] = DatabaseService
|
|
97
|
+
) {}
|
|
98
|
+
|
|
99
|
+
@Inject()
|
|
100
|
+
private repo!: UserRepository // design:type = UserRepository
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Cycle de vie complet
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
@Injectable(LifetimeEnum.SINGLETON)
|
|
108
|
+
class DatabaseService {
|
|
109
|
+
private connection: any
|
|
110
|
+
|
|
111
|
+
// Appelé après la création
|
|
112
|
+
onInit() {
|
|
113
|
+
console.log("Initialisation...")
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Appelé après injection des propriétés
|
|
117
|
+
onMount() {
|
|
118
|
+
this.connection = createConnection()
|
|
119
|
+
console.log("Prêt")
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Appelé lors de Injection.shutdown()
|
|
123
|
+
async onUnmount() {
|
|
124
|
+
await this.connection.close()
|
|
125
|
+
console.log("Fermé")
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@Injectable(LifetimeEnum.TRANSIENT)
|
|
130
|
+
class UserService {
|
|
131
|
+
constructor(@Inject() private db: DatabaseService) {}
|
|
132
|
+
|
|
133
|
+
getUsers() {
|
|
134
|
+
return this.db.query("SELECT * FROM users")
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Exemple : service + contrôleur
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { Injectable, Inject, Controllable, Get, Param } from "raiton/framework"
|
|
143
|
+
|
|
144
|
+
// Service (singleton)
|
|
145
|
+
@Injectable(LifetimeEnum.SINGLETON)
|
|
146
|
+
class TodoService {
|
|
147
|
+
private todos = [{ id: 1, title: "Apprendre Raiton" }]
|
|
148
|
+
private nextId = 2
|
|
149
|
+
|
|
150
|
+
findAll() { return this.todos }
|
|
151
|
+
findById(id: number) { return this.todos.find(t => t.id === id) }
|
|
152
|
+
create(title: string) {
|
|
153
|
+
const todo = { id: this.nextId++, title }
|
|
154
|
+
this.todos.push(todo)
|
|
155
|
+
return todo
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Contrôleur (transient — nouvelle instance par requête)
|
|
160
|
+
@Controllable("/todos")
|
|
161
|
+
export class TodoController {
|
|
162
|
+
constructor(@Inject() private service: TodoService) {}
|
|
163
|
+
|
|
164
|
+
@Get("/")
|
|
165
|
+
list() {
|
|
166
|
+
return this.service.findAll()
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
@Get("/:id")
|
|
170
|
+
get(@Param("id") id: string) {
|
|
171
|
+
return this.service.findById(Number(id))
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Règles importantes
|
|
177
|
+
|
|
178
|
+
1. **Toujours importer `reflect-metadata`** au point d'entrée de l'application
|
|
179
|
+
2. **`emitDecoratorMetadata: true`** requis dans `tsconfig.json` pour l'inférence de type automatique
|
|
180
|
+
3. Si l'inférence échoue, utilisez le token explicite : `@Inject("nomDuService")`
|
|
181
|
+
4. Le nom par défaut est le nom de la classe en **camelCase** : `DatabaseService` → `databaseService`
|
|
182
|
+
5. Les dépendances circulaires sont détectées et lèvent une `Throwable`
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
[← parametrable.md](parametrable.md) | [↑ decorators/](README.md) | [middleware.md →](middleware.md)
|