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,167 @@
|
|
|
1
|
+
# Décorateur `@Middleware`
|
|
2
|
+
|
|
3
|
+
**Fichier :** `source/framework/decorators/middleware.decorator.ts`
|
|
4
|
+
**Import :** `import { Middleware } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← injection.md](injection.md) | [↑ decorators/](README.md) | [socket.md →](socket.md)
|
|
7
|
+
|
|
8
|
+
Attache un middleware à un contrôleur (toutes ses routes) ou à une route spécifique.
|
|
9
|
+
|
|
10
|
+
```typescript
|
|
11
|
+
function Middleware(middleware: MiddlewareCallable)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Signature du middleware
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
type MiddlewareCallable = (params: MiddlewareParametersInterface) => any
|
|
18
|
+
|
|
19
|
+
interface MiddlewareParametersInterface {
|
|
20
|
+
context: RequestContext
|
|
21
|
+
next: () => Promise<any>
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## `@Middleware` sur une méthode (route spécifique)
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Controllable, Get, Middleware, Param } from "raiton/framework"
|
|
29
|
+
|
|
30
|
+
const logger = async ({ context, next }: MiddlewareParametersInterface) => {
|
|
31
|
+
console.log(`Avant: ${context.req.url}`)
|
|
32
|
+
await next()
|
|
33
|
+
console.log(`Après: ${context.req.url}`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@Controllable("/users")
|
|
37
|
+
export class UserController {
|
|
38
|
+
@Get("/")
|
|
39
|
+
list() {
|
|
40
|
+
return []
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@Get("/:id")
|
|
44
|
+
@Middleware(logger) // ← middleware uniquement sur cette route
|
|
45
|
+
get(@Param("id") id: string) {
|
|
46
|
+
return { id }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## `@Middleware` sur une classe (tout le contrôleur)
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Controllable, Get, Post, Middleware, Body } from "raiton/framework"
|
|
55
|
+
|
|
56
|
+
const auth = async ({ context, next }: MiddlewareParametersInterface) => {
|
|
57
|
+
const token = context.req.headers.get("authorization")
|
|
58
|
+
if (!token) {
|
|
59
|
+
context.reply.status(401)
|
|
60
|
+
return context.reply.send({ error: "Non autorisé" })
|
|
61
|
+
}
|
|
62
|
+
context.state.user = { id: 1, role: "admin" }
|
|
63
|
+
await next()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@Controllable("/admin")
|
|
67
|
+
@Middleware(auth) // ← middleware sur TOUTES les routes du contrôleur
|
|
68
|
+
export class AdminController {
|
|
69
|
+
@Get("/dashboard")
|
|
70
|
+
dashboard() {
|
|
71
|
+
return { data: "sensible" }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@Post("/actions")
|
|
75
|
+
action(@Body() data: any) {
|
|
76
|
+
return { success: true, data }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Combinaison classe + méthode
|
|
82
|
+
|
|
83
|
+
Les middlewares de classe s'exécutent avant ceux de méthode.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const auth = async ({ context, next }) => {
|
|
87
|
+
context.state.user = { id: 1 }
|
|
88
|
+
await next()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const adminOnly = async ({ context, next }) => {
|
|
92
|
+
if (context.state.user.role !== "admin") {
|
|
93
|
+
context.reply.status(403)
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
await next()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Controllable("/admin")
|
|
100
|
+
@Middleware(auth) // 1er : authentification
|
|
101
|
+
export class AdminController {
|
|
102
|
+
@Get("/")
|
|
103
|
+
list() { return [] }
|
|
104
|
+
|
|
105
|
+
@Delete("/:id")
|
|
106
|
+
@Middleware(adminOnly) // 2e : vérification admin (après auth)
|
|
107
|
+
remove(@Param("id") id: string) {
|
|
108
|
+
return { deleted: true }
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Ordre d'exécution
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
1. Middlewares globaux (app.use())
|
|
117
|
+
2. @Middleware sur la classe du contrôleur
|
|
118
|
+
3. @Middleware sur la méthode de la route
|
|
119
|
+
4. Handler de la route
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Utilisation avec `@Injectable` et injection de dépendances
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
@Injectable(LifetimeEnum.SINGLETON)
|
|
126
|
+
class AuthMiddleware {
|
|
127
|
+
async handle({ context, next }: MiddlewareParametersInterface) {
|
|
128
|
+
const token = context.req.headers.get("authorization")
|
|
129
|
+
context.state.user = await this.verify(token)
|
|
130
|
+
await next()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private async verify(token: string) {
|
|
134
|
+
// Vérification du token...
|
|
135
|
+
return { id: 1 }
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@Controllable("/protected")
|
|
140
|
+
@Middleware(async (params) => {
|
|
141
|
+
const middleware = Injection.resolve(AuthMiddleware)
|
|
142
|
+
return middleware.handle(params)
|
|
143
|
+
})
|
|
144
|
+
export class ProtectedController {
|
|
145
|
+
@Get("/")
|
|
146
|
+
get() {
|
|
147
|
+
return { secret: true }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## `createMiddlewareDecoration`
|
|
153
|
+
|
|
154
|
+
Fonction utilitaire pour créer des décorateurs de middleware personnalisés :
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { createMiddlewareDecoration } from "raiton/framework"
|
|
158
|
+
|
|
159
|
+
const auth = createMiddlewareDecoration(async ({ context, next }) => {
|
|
160
|
+
// ...
|
|
161
|
+
})
|
|
162
|
+
// auth est utilisable comme @auth
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
[← injection.md](injection.md) | [↑ decorators/](README.md) | [socket.md →](socket.md)
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# Décorateurs OpenAPI
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/decorators/api.decorator.ts`, `api-response.decorator.ts`, `route-interaction.decorator.ts`
|
|
4
|
+
**Import :** `import { ApiTags, ApiOperation, ApiSecurity, ApiBody, ApiParam, ApiQuery, ApiResponse, ApiOkResponse, ApiCreatedResponse, ApiBadRequestResponse, ApiUnauthorizedResponse, ApiForbiddenResponse, ApiNotFoundResponse, ApiInternalServerErrorResponse, RouteInteraction } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← socket.md](socket.md) | [↑ decorators/](README.md) | [plugins/ →](../plugins/README.md)
|
|
7
|
+
|
|
8
|
+
Ces décorateurs ajoutent des métadonnées utilisées par `openApiPlugin()` pour générer une spécification OpenAPI 3.0.
|
|
9
|
+
|
|
10
|
+
## Configuration du tag et sécurité
|
|
11
|
+
|
|
12
|
+
### `@ApiTags(...tags)`
|
|
13
|
+
|
|
14
|
+
Ajoute des tags de regroupement (classe ou méthode) :
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
@ApiTags("Utilisateurs")
|
|
18
|
+
@Controllable("/users")
|
|
19
|
+
export class UserController {
|
|
20
|
+
@Get("/")
|
|
21
|
+
@ApiTags("Liste") // Tags additionnels pour cette route
|
|
22
|
+
list() { return [] }
|
|
23
|
+
|
|
24
|
+
@Get("/:id")
|
|
25
|
+
get(@Param("id") id: string) { return { id } }
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### `@ApiSecurity({name, scopes?})`
|
|
30
|
+
|
|
31
|
+
Définit le schéma de sécurité (classe ou méthode) :
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
@ApiSecurity({ name: "bearerAuth" }) // Appliqué à toutes les routes
|
|
35
|
+
@Controllable("/admin")
|
|
36
|
+
export class AdminController {
|
|
37
|
+
@Get("/")
|
|
38
|
+
list() { return [] }
|
|
39
|
+
|
|
40
|
+
@Get("/public")
|
|
41
|
+
@ApiSecurity({ name: "none" }) // Surcharge pour cette route
|
|
42
|
+
publicInfo() { return { info: "public" } }
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Définition de l'opération
|
|
47
|
+
|
|
48
|
+
### `@ApiOperation({summary, description, operationId, deprecated})`
|
|
49
|
+
|
|
50
|
+
Métadonnées de l'opération OpenAPI :
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
@Controllable("/users")
|
|
54
|
+
export class UserController {
|
|
55
|
+
@Get("/:id")
|
|
56
|
+
@ApiOperation({
|
|
57
|
+
summary: "Récupère un utilisateur",
|
|
58
|
+
description: "Retourne les détails d'un utilisateur par son ID",
|
|
59
|
+
operationId: "getUserById",
|
|
60
|
+
deprecated: false,
|
|
61
|
+
})
|
|
62
|
+
get(@Param("id") id: string) { return { id } }
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Paramètres
|
|
67
|
+
|
|
68
|
+
### `@ApiBody({description, required, type})`
|
|
69
|
+
|
|
70
|
+
Définit le corps de la requête :
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
@Controllable("/users")
|
|
74
|
+
export class UserController {
|
|
75
|
+
@Post("/")
|
|
76
|
+
@ApiBody({
|
|
77
|
+
description: "Données de l'utilisateur",
|
|
78
|
+
required: true,
|
|
79
|
+
type: Object,
|
|
80
|
+
})
|
|
81
|
+
create(@Body() data: any) { return data }
|
|
82
|
+
|
|
83
|
+
@Post("/batch")
|
|
84
|
+
@ApiBody({
|
|
85
|
+
description: "Liste d'utilisateurs",
|
|
86
|
+
required: true,
|
|
87
|
+
type: Array,
|
|
88
|
+
})
|
|
89
|
+
createBatch(@Body() data: any[]) { return data }
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `@ApiParam(name, options)`
|
|
94
|
+
|
|
95
|
+
Définit un paramètre de chemin dans la spec OpenAPI :
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
@Controllable("/users")
|
|
99
|
+
export class UserController {
|
|
100
|
+
@Get("/:id")
|
|
101
|
+
@ApiParam("id", {
|
|
102
|
+
description: "ID de l'utilisateur",
|
|
103
|
+
required: true,
|
|
104
|
+
type: Number,
|
|
105
|
+
})
|
|
106
|
+
get(@Param("id") id: string) { return { id } }
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### `@ApiQuery(name, options)`
|
|
111
|
+
|
|
112
|
+
Définit un paramètre de query string :
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
@Controllable("/users")
|
|
116
|
+
export class UserController {
|
|
117
|
+
@Get("/")
|
|
118
|
+
@ApiQuery("page", { description: "Numéro de page", required: false, type: Number })
|
|
119
|
+
@ApiQuery("limit", { description: "Résultats par page", required: false, type: Number })
|
|
120
|
+
@ApiQuery("search", { description: "Recherche", required: false, type: String })
|
|
121
|
+
list(
|
|
122
|
+
@Query("page") page: string = "1",
|
|
123
|
+
@Query("limit") limit: string = "10",
|
|
124
|
+
@Query("search") search?: string,
|
|
125
|
+
) {
|
|
126
|
+
return { page, limit, search }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Réponses
|
|
132
|
+
|
|
133
|
+
### `@ApiResponse({status, description, type, isArray})`
|
|
134
|
+
|
|
135
|
+
Définit une réponse OpenAPI générique :
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
@Controllable("/users")
|
|
139
|
+
export class UserController {
|
|
140
|
+
@Get("/:id")
|
|
141
|
+
@ApiResponse({ status: 200, description: "Utilisateur trouvé", type: Object })
|
|
142
|
+
@ApiResponse({ status: 404, description: "Utilisateur non trouvé" })
|
|
143
|
+
get(@Param("id") id: string) {
|
|
144
|
+
const user = users.find(u => u.id === Number(id))
|
|
145
|
+
if (!user) throw new HttpException(404, "Non trouvé")
|
|
146
|
+
return user
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Raccourcis de réponses
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
@Controllable("/users")
|
|
155
|
+
export class UserController {
|
|
156
|
+
@Post("/")
|
|
157
|
+
@ApiCreatedResponse("Utilisateur créé avec succès", Object)
|
|
158
|
+
@ApiBadRequestResponse("Données invalides")
|
|
159
|
+
create(@Body() data: any) { return data }
|
|
160
|
+
|
|
161
|
+
@Get("/:id")
|
|
162
|
+
@ApiOkResponse("Détails de l'utilisateur", Object)
|
|
163
|
+
@ApiNotFoundResponse("Utilisateur introuvable")
|
|
164
|
+
@ApiUnauthorizedResponse("Non authentifié")
|
|
165
|
+
@ApiForbiddenResponse("Accès refusé")
|
|
166
|
+
@ApiInternalServerErrorResponse("Erreur serveur")
|
|
167
|
+
get(@Param("id") id: string) {
|
|
168
|
+
return { id }
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### `@RouteInteraction(description)`
|
|
174
|
+
|
|
175
|
+
Décrit une interaction de route (utilisé pour la documentation) :
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
@Controllable("/users")
|
|
179
|
+
export class UserController {
|
|
180
|
+
@Get("/:id")
|
|
181
|
+
@RouteInteraction("Récupère un utilisateur par son identifiant unique")
|
|
182
|
+
get(@Param("id") id: string) { return { id } }
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Exemple complet
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import {
|
|
190
|
+
Controllable, Get, Post, Param, Body, Query,
|
|
191
|
+
ApiTags, ApiOperation, ApiParam, ApiQuery, ApiBody,
|
|
192
|
+
ApiOkResponse, ApiCreatedResponse, ApiBadRequestResponse, ApiNotFoundResponse,
|
|
193
|
+
} from "raiton/framework"
|
|
194
|
+
|
|
195
|
+
@ApiTags("Produits")
|
|
196
|
+
@Controllable("/products")
|
|
197
|
+
export class ProductController {
|
|
198
|
+
@Get("/")
|
|
199
|
+
@ApiOperation({ summary: "Liste tous les produits", description: "Retourne une liste paginée de produits" })
|
|
200
|
+
@ApiQuery("page", { description: "Page", required: false, type: Number })
|
|
201
|
+
@ApiQuery("category", { description: "Filtre par catégorie", required: false, type: String })
|
|
202
|
+
@ApiOkResponse("Liste des produits", Array, true)
|
|
203
|
+
list(
|
|
204
|
+
@Query("page") page: string = "1",
|
|
205
|
+
@Query("category") category?: string,
|
|
206
|
+
) {
|
|
207
|
+
return []
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
@Get("/:id")
|
|
211
|
+
@ApiOperation({ summary: "Détail d'un produit" })
|
|
212
|
+
@ApiParam("id", { description: "ID du produit", required: true, type: Number })
|
|
213
|
+
@ApiOkResponse("Produit trouvé", Object)
|
|
214
|
+
@ApiNotFoundResponse("Produit introuvable")
|
|
215
|
+
get(@Param("id") id: string) {
|
|
216
|
+
return { id, name: "Produit " + id }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
@Post("/")
|
|
220
|
+
@ApiOperation({ summary: "Crée un nouveau produit" })
|
|
221
|
+
@ApiBody({ description: "Données du produit", required: true, type: Object })
|
|
222
|
+
@ApiCreatedResponse("Produit créé", Object)
|
|
223
|
+
@ApiBadRequestResponse("Données invalides")
|
|
224
|
+
create(@Body() data: any) {
|
|
225
|
+
return { id: 1, ...data }
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Utilisation avec `openApiPlugin()`
|
|
231
|
+
|
|
232
|
+
Les métadonnées accumulées par ces décorateurs sont exploitées par le plugin OpenAPI :
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
import { openApiPlugin } from "raiton/framework"
|
|
236
|
+
|
|
237
|
+
app.register(openApiPlugin({
|
|
238
|
+
title: "Catalogue API",
|
|
239
|
+
version: "2.0.0",
|
|
240
|
+
description: "API de gestion de catalogue produits",
|
|
241
|
+
endpoint: "/docs/json",
|
|
242
|
+
}))
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Notes
|
|
246
|
+
|
|
247
|
+
- Les décorateurs fonctionnent avec `reflect-metadata` — l'import est requis au point d'entrée
|
|
248
|
+
- Les données sont stockées via `METADATA_KEYS` (symboles) dans `source/framework/constants/decorators.constant.ts`
|
|
249
|
+
- La génération du schéma utilise `generateOpenApiSpec(scope, opts)` de `source/framework/utilities/openapi.utils.ts`
|
|
250
|
+
- Les types supportés : `String`, `Number`, `Boolean`, `Object`, `Array`, `Date`
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
[← socket.md](socket.md) | [↑ decorators/](README.md) | [plugins/ →](../plugins/README.md)
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# Décorateurs de Paramètres
|
|
2
|
+
|
|
3
|
+
**Fichier :** `source/framework/decorators/parametrable.decorator.ts`
|
|
4
|
+
**Import :** `import { Param, Query, Body, Headers, Cookie, Req, Reply, UploadedFile, Parametrable } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← controllable.md](controllable.md) | [↑ decorators/](README.md) | [injection.md →](injection.md)
|
|
7
|
+
|
|
8
|
+
Ces décorateurs injectent des parties de la requête dans les paramètres de la méthode du contrôleur.
|
|
9
|
+
|
|
10
|
+
## `@Param(name?)`
|
|
11
|
+
|
|
12
|
+
Extrait un paramètre d'URL (segment dynamique dans le chemin) :
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
@Controllable("/users")
|
|
16
|
+
export class UserController {
|
|
17
|
+
@Get("/:id")
|
|
18
|
+
get(@Param("id") id: string) {
|
|
19
|
+
return { id }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@Get("/:organization/:project")
|
|
23
|
+
getProject(
|
|
24
|
+
@Param("organization") org: string,
|
|
25
|
+
@Param("project") project: string,
|
|
26
|
+
) {
|
|
27
|
+
return { org, project }
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## `@Query(name?)`
|
|
33
|
+
|
|
34
|
+
Extrait un paramètre de la query string (`?key=value`) :
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
@Controllable("/users")
|
|
38
|
+
export class UserController {
|
|
39
|
+
@Get("/")
|
|
40
|
+
list(
|
|
41
|
+
@Query("page") page: string = "1",
|
|
42
|
+
@Query("limit") limit: string = "10",
|
|
43
|
+
@Query("search") search?: string,
|
|
44
|
+
) {
|
|
45
|
+
// GET /users?page=2&limit=20&search=alice
|
|
46
|
+
// → page="2", limit="20", search="alice"
|
|
47
|
+
return { page, limit, search }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Sans nom explicite** — reçoit l'objet complet :
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
@Controllable("/search")
|
|
56
|
+
export class SearchController {
|
|
57
|
+
@Get("/")
|
|
58
|
+
search(@Query() query: Record<string, string>) {
|
|
59
|
+
// GET /search?q=hello&lang=fr
|
|
60
|
+
// → query = { q: "hello", lang: "fr" }
|
|
61
|
+
return query
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## `@Body()`
|
|
67
|
+
|
|
68
|
+
Injecte le corps de la requête. Dépend du `bodyParserPlugin` (activé automatiquement).
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
@Controllable("/users")
|
|
72
|
+
export class UserController {
|
|
73
|
+
@Post("/")
|
|
74
|
+
create(@Body() data: { name: string; email: string }) {
|
|
75
|
+
// POST /users avec JSON {"name": "Alice", "email": "alice@example.com"}
|
|
76
|
+
// → data = { name: "Alice", email: "alice@example.com" }
|
|
77
|
+
return { success: true, data }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Avec DTO pour validation automatique :
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { DataTransferObject } from "raiton/framework"
|
|
86
|
+
import { IsString, IsEmail, IsNotEmpty } from "class-validator"
|
|
87
|
+
|
|
88
|
+
class CreateUserDto extends DataTransferObject {
|
|
89
|
+
@IsString() @IsNotEmpty()
|
|
90
|
+
name!: string
|
|
91
|
+
|
|
92
|
+
@IsEmail()
|
|
93
|
+
email!: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Controllable("/users")
|
|
97
|
+
export class UserController {
|
|
98
|
+
@Post("/")
|
|
99
|
+
create(@Body() data: CreateUserDto) {
|
|
100
|
+
// data est automatiquement validé via class-validator
|
|
101
|
+
return { success: true, data }
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## `@Headers(name?)`
|
|
107
|
+
|
|
108
|
+
Extrait un en-tête HTTP :
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
@Controllable("/auth")
|
|
112
|
+
export class AuthController {
|
|
113
|
+
@Get("/me")
|
|
114
|
+
me(@Headers("authorization") token: string) {
|
|
115
|
+
// GET /auth/me avec en-tête "Authorization: Bearer xxx"
|
|
116
|
+
// → token = "Bearer xxx"
|
|
117
|
+
return { token }
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Sans nom explicite** — reçoit tous les en-têtes :
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
@Controllable("/debug")
|
|
126
|
+
export class DebugController {
|
|
127
|
+
@Get("/headers")
|
|
128
|
+
headers(@Headers() headers: Record<string, string>) {
|
|
129
|
+
return headers
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## `@Cookie(name?)`
|
|
135
|
+
|
|
136
|
+
Extrait un cookie spécifique :
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
@Controllable("/session")
|
|
140
|
+
export class SessionController {
|
|
141
|
+
@Get("/")
|
|
142
|
+
get(@Cookie("sessionId") sessionId: string) {
|
|
143
|
+
return { sessionId }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## `@Req()`
|
|
149
|
+
|
|
150
|
+
Injecte l'objet requête brut (wrapper natif) :
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import type { RequestInterface } from "raiton/types"
|
|
154
|
+
|
|
155
|
+
@Controllable("/debug")
|
|
156
|
+
export class DebugController {
|
|
157
|
+
@Get("/req")
|
|
158
|
+
debug(@Req() req: RequestInterface) {
|
|
159
|
+
return {
|
|
160
|
+
method: req.method,
|
|
161
|
+
url: req.url,
|
|
162
|
+
headers: Object.fromEntries(req.headers),
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## `@Reply()`
|
|
169
|
+
|
|
170
|
+
Injecte l'objet réponse brut (wrapper natif) :
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import type { ReplyInterface } from "raiton/types"
|
|
174
|
+
|
|
175
|
+
@Controllable("/custom")
|
|
176
|
+
export class CustomController {
|
|
177
|
+
@Get("/download")
|
|
178
|
+
download(@Reply() reply: ReplyInterface) {
|
|
179
|
+
reply.type("application/pdf")
|
|
180
|
+
reply.header("Content-Disposition", "attachment; filename=doc.pdf")
|
|
181
|
+
reply.send(Buffer.from("PDF content"))
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## `@UploadedFile(name?)`
|
|
187
|
+
|
|
188
|
+
Extrait un fichier uploadé (via `multipart/form-data`) :
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
@Controllable("/files")
|
|
192
|
+
export class FileController {
|
|
193
|
+
@Post("/upload")
|
|
194
|
+
upload(@UploadedFile("avatar") file: { name: string; type: string; size: number; buffer: Buffer }) {
|
|
195
|
+
// POST /files/upload avec FormData { avatar: <fichier> }
|
|
196
|
+
return {
|
|
197
|
+
name: file.name,
|
|
198
|
+
type: file.type,
|
|
199
|
+
size: file.size,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
@Post("/upload-multiple")
|
|
204
|
+
uploadMultiple(@UploadedFile() files: Record<string, any>) {
|
|
205
|
+
// Tous les fichiers uploadés
|
|
206
|
+
return files
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Utilisation combinée
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
@Controllable("/posts")
|
|
215
|
+
export class PostController {
|
|
216
|
+
@Post("/:id/comment")
|
|
217
|
+
addComment(
|
|
218
|
+
@Param("id") postId: string,
|
|
219
|
+
@Body() comment: { text: string },
|
|
220
|
+
@Query("notify") notify?: string,
|
|
221
|
+
@Headers("authorization") auth?: string,
|
|
222
|
+
@Cookie("locale") locale?: string,
|
|
223
|
+
) {
|
|
224
|
+
return { postId, comment, notify, auth, locale }
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Enum `Parametrable`
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
enum Parametrable {
|
|
233
|
+
PARAM = "param",
|
|
234
|
+
BODY = "body",
|
|
235
|
+
QUERY = "query",
|
|
236
|
+
HEADER = "header",
|
|
237
|
+
COOKIE = "cookie",
|
|
238
|
+
REQ = "req",
|
|
239
|
+
REPLY = "reply",
|
|
240
|
+
UPLOAD_FILE = "upload_file",
|
|
241
|
+
CUSTOM = "custom",
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
Utilisé en interne par les décorateurs pour stocker les métadonnées.
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
[← controllable.md](controllable.md) | [↑ decorators/](README.md) | [injection.md →](injection.md)
|