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,217 @@
|
|
|
1
|
+
# OpenAPI Plugin
|
|
2
|
+
|
|
3
|
+
**Fichier :** `source/framework/plugins/openapi.plugin.ts`
|
|
4
|
+
**Import :** `import { openApiPlugin } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← body-parser.md](body-parser.md) | [↑ plugins/](README.md) | [security.md →](security.md)
|
|
7
|
+
|
|
8
|
+
Génère une spécification OpenAPI 3.0 à partir des métadonnées collectées par les décorateurs OpenAPI (`@ApiOperation`, `@ApiParam`, `@ApiResponse`, etc.).
|
|
9
|
+
|
|
10
|
+
```typescript
|
|
11
|
+
openApiPlugin(options?: OpenApiOptions): PluginInterface
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Options
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
interface OpenApiOptions {
|
|
18
|
+
route?: string // Route pour l'UI (ex: "/docs") — nécessite `ui` function
|
|
19
|
+
ui?: (config, context) => string // Fonction retournant le HTML de l'UI
|
|
20
|
+
endpoint?: string | false // Endpoint JSON (ex: "/docs/json") — false pour désactiver
|
|
21
|
+
title?: string // Titre de l'API (défaut: "API")
|
|
22
|
+
version?: string // Version (défaut: "1.0.0")
|
|
23
|
+
description?: string // Description
|
|
24
|
+
termsOfService?: string // URL des CGU
|
|
25
|
+
contact?: { // Contact
|
|
26
|
+
name?: string
|
|
27
|
+
url?: string
|
|
28
|
+
email?: string
|
|
29
|
+
}
|
|
30
|
+
license?: { // Licence
|
|
31
|
+
name?: string
|
|
32
|
+
url?: string
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Utilisation de base
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { openApiPlugin } from "raiton/framework"
|
|
41
|
+
|
|
42
|
+
app.register(openApiPlugin({
|
|
43
|
+
title: "Mon API REST",
|
|
44
|
+
version: "1.0.0",
|
|
45
|
+
description: "API de gestion des utilisateurs et produits",
|
|
46
|
+
endpoint: "/docs/json",
|
|
47
|
+
contact: {
|
|
48
|
+
name: "Support API",
|
|
49
|
+
email: "api@example.com",
|
|
50
|
+
},
|
|
51
|
+
license: {
|
|
52
|
+
name: "MIT",
|
|
53
|
+
url: "https://opensource.org/licenses/MIT",
|
|
54
|
+
},
|
|
55
|
+
}))
|
|
56
|
+
|
|
57
|
+
// GET /docs/json → retourne la spec OpenAPI 3.0 complète
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Avec interface Swagger UI
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
app.register(openApiPlugin({
|
|
64
|
+
title: "Mon API",
|
|
65
|
+
endpoint: "/docs/json",
|
|
66
|
+
route: "/docs",
|
|
67
|
+
ui: (config) => `<!DOCTYPE html>
|
|
68
|
+
<html>
|
|
69
|
+
<head>
|
|
70
|
+
<title>${config.title}</title>
|
|
71
|
+
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" />
|
|
72
|
+
</head>
|
|
73
|
+
<body>
|
|
74
|
+
<div id="swagger-ui"></div>
|
|
75
|
+
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
|
|
76
|
+
<script>
|
|
77
|
+
window.onload = () => {
|
|
78
|
+
SwaggerUIBundle({
|
|
79
|
+
url: '${config.endpoint}',
|
|
80
|
+
domId: '#swagger-ui',
|
|
81
|
+
presets: [
|
|
82
|
+
SwaggerUIBundle.presets.apis,
|
|
83
|
+
SwaggerUIBundle.SwaggerUIStandalonePreset,
|
|
84
|
+
],
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
</script>
|
|
88
|
+
</body>
|
|
89
|
+
</html>`,
|
|
90
|
+
}))
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Ce qui est généré automatiquement
|
|
94
|
+
|
|
95
|
+
La spec OpenAPI est générée en parcourant toutes les routes enregistrées. Pour chaque route, elle collecte :
|
|
96
|
+
|
|
97
|
+
### Depuis les décorateurs
|
|
98
|
+
|
|
99
|
+
- **Paramètres de chemin** `@ApiParam` + `@Param`
|
|
100
|
+
- **Paramètres query** `@ApiQuery` + `@Query`
|
|
101
|
+
- **Corps de la requête** `@ApiBody` + `@Body`
|
|
102
|
+
- **Réponses** `@ApiResponse`, `@ApiOkResponse`, etc.
|
|
103
|
+
- **Tags** `@ApiTags`
|
|
104
|
+
- **Sécurité** `@ApiSecurity`
|
|
105
|
+
- **Opération** `@ApiOperation`
|
|
106
|
+
|
|
107
|
+
### Depuis TypeScript (fallback)
|
|
108
|
+
|
|
109
|
+
Si aucun décorateur OpenAPI n'est défini, le système tente d'inférer :
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
// Type de retour (design:returntype) → réponse 200 par défaut
|
|
113
|
+
// Paramètres via design:paramtypes et design:paramtypes
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Exemple complet avec décorateurs
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import {
|
|
120
|
+
Controllable, Get, Post, Param, Body, Query,
|
|
121
|
+
ApiTags, ApiOperation, ApiParam, ApiQuery, ApiBody,
|
|
122
|
+
ApiOkResponse, ApiCreatedResponse, ApiNotFoundResponse,
|
|
123
|
+
openApiPlugin,
|
|
124
|
+
} from "raiton/framework"
|
|
125
|
+
|
|
126
|
+
@ApiTags("Produits")
|
|
127
|
+
@Controllable("/products")
|
|
128
|
+
export class ProductController {
|
|
129
|
+
@Get("/")
|
|
130
|
+
@ApiOperation({ summary: "Liste des produits", description: "Retourne une liste paginée" })
|
|
131
|
+
@ApiQuery("page", { description: "N° de page", type: Number, required: false })
|
|
132
|
+
@ApiOkResponse("Liste des produits", Array, true)
|
|
133
|
+
list(@Query("page") page = "1") {
|
|
134
|
+
return []
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@Get("/:id")
|
|
138
|
+
@ApiOperation({ summary: "Détail d'un produit" })
|
|
139
|
+
@ApiParam("id", { description: "ID produit", type: Number })
|
|
140
|
+
@ApiOkResponse("Produit trouvé", Object)
|
|
141
|
+
@ApiNotFoundResponse("Produit introuvable")
|
|
142
|
+
get(@Param("id") id: string) {
|
|
143
|
+
return { id, name: "Produit" }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
@Post("/")
|
|
147
|
+
@ApiOperation({ summary: "Créer un produit" })
|
|
148
|
+
@ApiBody({ description: "Données du produit", type: Object })
|
|
149
|
+
@ApiCreatedResponse("Produit créé", Object)
|
|
150
|
+
@ApiBadRequestResponse("Données invalides")
|
|
151
|
+
create(@Body() data: any) {
|
|
152
|
+
return { id: 1, ...data }
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Dans le bootstrapper :
|
|
157
|
+
app.register(openApiPlugin({
|
|
158
|
+
title: "Catalogue API",
|
|
159
|
+
version: "1.0.0",
|
|
160
|
+
endpoint: "/api/docs",
|
|
161
|
+
}))
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Exemple de spec générée
|
|
165
|
+
|
|
166
|
+
```json
|
|
167
|
+
{
|
|
168
|
+
"openapi": "3.0.0",
|
|
169
|
+
"info": {
|
|
170
|
+
"title": "Catalogue API",
|
|
171
|
+
"version": "1.0.0"
|
|
172
|
+
},
|
|
173
|
+
"servers": [{ "url": "http://localhost:3000" }],
|
|
174
|
+
"paths": {
|
|
175
|
+
"/products": {
|
|
176
|
+
"get": {
|
|
177
|
+
"tags": ["Produits"],
|
|
178
|
+
"summary": "Liste des produits",
|
|
179
|
+
"parameters": [
|
|
180
|
+
{ "name": "page", "in": "query", "schema": { "type": "number" } }
|
|
181
|
+
],
|
|
182
|
+
"responses": {
|
|
183
|
+
"200": { "description": "Liste des produits", "content": { "application/json": { "schema": { "type": "array", "items": { "type": "string" } } } } }
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
"post": {
|
|
187
|
+
"tags": ["Produits"],
|
|
188
|
+
"summary": "Créer un produit",
|
|
189
|
+
"requestBody": {
|
|
190
|
+
"required": true,
|
|
191
|
+
"content": { "application/json": { "schema": { "type": "object" } } }
|
|
192
|
+
},
|
|
193
|
+
"responses": {
|
|
194
|
+
"201": { "description": "Produit créé", "content": { "application/json": { "schema": { "type": "object" } } } },
|
|
195
|
+
"400": { "description": "Données invalides" }
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
"/products/{id}": {
|
|
200
|
+
"get": {
|
|
201
|
+
"tags": ["Produits"],
|
|
202
|
+
"parameters": [
|
|
203
|
+
{ "name": "id", "in": "path", "required": true, "schema": { "type": "number" } }
|
|
204
|
+
],
|
|
205
|
+
"responses": {
|
|
206
|
+
"200": { "description": "Produit trouvé", "content": { "application/json": { "schema": { "type": "object" } } } },
|
|
207
|
+
"404": { "description": "Produit introuvable" }
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
[← body-parser.md](body-parser.md) | [↑ plugins/](README.md) | [security.md →](security.md)
|
|
217
|
+
```
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# Plugins de Sécurité
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/plugins/security/`
|
|
4
|
+
**Import :** `import { Security } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← openapi.md](openapi.md) | [↑ plugins/](README.md)
|
|
7
|
+
|
|
8
|
+
La classe `Security` regroupe 5 middlewares de sécurité.
|
|
9
|
+
|
|
10
|
+
```typescript
|
|
11
|
+
class Security {
|
|
12
|
+
static headers // secureHeaders (activé automatiquement)
|
|
13
|
+
static cors // secureCors(options?)
|
|
14
|
+
static rateLimit // secureRateLimit(options?)
|
|
15
|
+
static bodyLimit // secureBodyLimit(options?)
|
|
16
|
+
static methodGuard // secureMethodGuard(options?)
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## `Security.headers` (automatique)
|
|
21
|
+
|
|
22
|
+
Ajoute des en-têtes de sécurité HTTP à chaque réponse. Activé automatiquement par `Application.initialize()`.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
// En-têtes ajoutés :
|
|
26
|
+
X-Content-Type-Options: nosniff
|
|
27
|
+
X-Frame-Options: DENY
|
|
28
|
+
X-XSS-Protection: 1; mode=block
|
|
29
|
+
Strict-Transport-Security: max-age=31536000; includeSubDomains
|
|
30
|
+
Content-Security-Policy: default-src 'self'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## `Security.cors(options?)`
|
|
34
|
+
|
|
35
|
+
Middleware CORS configurable.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
app.use(Security.cors({
|
|
39
|
+
origin: "*", // ou "https://example.com" ou ["https://a.com", "https://b.com"]
|
|
40
|
+
methods: ["GET", "POST", "PUT", "DELETE"], // Méthodes autorisées
|
|
41
|
+
headers: ["Content-Type", "Authorization"], // En-têtes autorisés
|
|
42
|
+
credentials: true, // Support des cookies cross-origin
|
|
43
|
+
maxAge: 86400, // Cache preflight (secondes)
|
|
44
|
+
exposeHeaders: ["X-Request-Id"], // En-têtes exposés au client
|
|
45
|
+
}))
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Options `origin`
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
// Toutes les origines (*)
|
|
52
|
+
origin: "*"
|
|
53
|
+
|
|
54
|
+
// Origine spécifique
|
|
55
|
+
origin: "https://monapp.com"
|
|
56
|
+
|
|
57
|
+
// Multiple origines
|
|
58
|
+
origin: ["https://app1.com", "https://app2.com"]
|
|
59
|
+
|
|
60
|
+
// Booléen — reflet de l'origine demandée
|
|
61
|
+
origin: true
|
|
62
|
+
|
|
63
|
+
// Fonction de validation personnalisée
|
|
64
|
+
origin: async (origin) => {
|
|
65
|
+
return origin.endsWith(".example.com")
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Comportement preflight (OPTIONS)
|
|
70
|
+
|
|
71
|
+
Si la requête est une `OPTIONS` (preflight CORS) :
|
|
72
|
+
|
|
73
|
+
1. Ajoute `Access-Control-Allow-Methods`
|
|
74
|
+
2. Ajoute `Access-Control-Allow-Headers`
|
|
75
|
+
3. Si `maxAge` défini, ajoute `Access-Control-Max-Age`
|
|
76
|
+
4. Répond avec `204 No Content` (sans passer au handler suivant)
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// Exemple de réponse OPTIONS :
|
|
80
|
+
HTTP/1.1 204 No Content
|
|
81
|
+
Access-Control-Allow-Origin: *
|
|
82
|
+
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH
|
|
83
|
+
Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## `Security.bodyLimit(options?)`
|
|
87
|
+
|
|
88
|
+
Limite la taille du corps des requêtes.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
app.use(Security.bodyLimit({
|
|
92
|
+
maxSize: 1024 * 1024, // 1 MB
|
|
93
|
+
}))
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Utilise `parseBytes` de `source/core/bytes.util.ts` pour accepter des chaînes :
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
// parseBytes("1MB") → 1048576
|
|
100
|
+
// parseBytes("500KB") → 512000
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## `Security.rateLimit(options?)`
|
|
104
|
+
|
|
105
|
+
Middleware de rate limiting.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
app.use(Security.rateLimit({
|
|
109
|
+
windowMs: 60 * 1000, // Fenêtre : 1 minute
|
|
110
|
+
maxRequests: 100, // 100 requêtes max par fenêtre
|
|
111
|
+
}))
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## `Security.methodGuard(options?)`
|
|
115
|
+
|
|
116
|
+
Restreint les méthodes HTTP autorisées.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
app.use(Security.methodGuard({
|
|
120
|
+
methods: ["GET", "POST", "PUT", "DELETE"], // Méthodes autorisées
|
|
121
|
+
}))
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Exemple : tout activer
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { Security } from "raiton/framework"
|
|
128
|
+
|
|
129
|
+
// Dans le constructeur de l'Application ou après
|
|
130
|
+
app
|
|
131
|
+
.use(Security.cors({
|
|
132
|
+
origin: "https://frontend.example.com",
|
|
133
|
+
credentials: true,
|
|
134
|
+
}))
|
|
135
|
+
.use(Security.bodyLimit({ maxSize: "5MB" }))
|
|
136
|
+
.use(Security.rateLimit({
|
|
137
|
+
windowMs: 60_000,
|
|
138
|
+
maxRequests: 60,
|
|
139
|
+
}))
|
|
140
|
+
.use(Security.methodGuard({
|
|
141
|
+
methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
|
|
142
|
+
}))
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Ordre d'enregistrement recommandé
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
1. Security.cors → CORS preflight
|
|
149
|
+
2. Security.methodGuard → Méthodes autorisées
|
|
150
|
+
3. Security.bodyLimit → Taille max
|
|
151
|
+
4. Security.rateLimit → Rate limiting
|
|
152
|
+
5. Security.headers → En-têtes de sécurité (automatique)
|
|
153
|
+
6. bodyParserPlugin → Parsing body (automatique)
|
|
154
|
+
7. Middleware personnalisés
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
[← openapi.md](openapi.md) | [↑ plugins/](README.md)
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# Réponses et Exceptions
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/responses/`, `source/framework/exceptions/`
|
|
4
|
+
**Import :** `import { HttpResponse, HttpErrorResponse, ThrowableResponse, RaitonResponses, HttpException, Throwable, throwError, throwException, throwWarning, throwCritical } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← runtime.md](runtime.md) | [↑ framework/](README.md) | [encryption.md →](encryption.md)
|
|
7
|
+
|
|
8
|
+
## Réponses
|
|
9
|
+
|
|
10
|
+
### `HttpResponse`
|
|
11
|
+
|
|
12
|
+
Réponse HTTP standard avec code statut et en-têtes.
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { HttpResponse } from "raiton/framework"
|
|
16
|
+
|
|
17
|
+
new HttpResponse(200, { data: "ok" })
|
|
18
|
+
// Corps : { data: "ok" }, Statut : 200
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### `HttpErrorResponse`
|
|
22
|
+
|
|
23
|
+
Réponse d'erreur avec gestion de pile d'erreurs.
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { HttpErrorResponse } from "raiton/framework"
|
|
27
|
+
|
|
28
|
+
const error = new HttpErrorResponse("Erreur de validation", {
|
|
29
|
+
statusCode: 422,
|
|
30
|
+
details: [{ field: "email", message: "Email invalide" }],
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### `ThrowableResponse`
|
|
35
|
+
|
|
36
|
+
Réponse d'exception. Utilisée en interne par le handler pour détecter les réponses d'erreur.
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { ThrowableResponse } from "raiton/framework"
|
|
40
|
+
|
|
41
|
+
const response = new ThrowableResponse("Resource introuvable", {
|
|
42
|
+
statusCode: 404,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// Dans Application.handle(), si le handler retourne un ThrowableResponse :
|
|
46
|
+
if (responses instanceof ThrowableResponse) {
|
|
47
|
+
context.reply.status(responses.statusCode ?? 500)
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### `RaitonResponses`
|
|
52
|
+
|
|
53
|
+
Builder de réponses.
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { RaitonResponses } from "raiton/framework"
|
|
57
|
+
|
|
58
|
+
// Réponse de succès
|
|
59
|
+
RaitonResponses.success(data, "Opération réussie", 200)
|
|
60
|
+
|
|
61
|
+
// Réponse d'échec
|
|
62
|
+
RaitonResponses.fail("Erreur", 400)
|
|
63
|
+
|
|
64
|
+
// Réponse paginée
|
|
65
|
+
RaitonResponses.paginated(items, total, page, limit)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Exceptions
|
|
69
|
+
|
|
70
|
+
### `HttpException`
|
|
71
|
+
|
|
72
|
+
Exception avec code HTTP. Attrapée par `Application.handle()` pour générer une réponse appropriée.
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { HttpException } from "raiton/framework"
|
|
76
|
+
|
|
77
|
+
// Création
|
|
78
|
+
throw new HttpException(404, "Utilisateur non trouvé")
|
|
79
|
+
|
|
80
|
+
// Dans le handler, l'exception est automatiquement attrapée
|
|
81
|
+
// et une réponse 404 est envoyée :
|
|
82
|
+
// { statusCode: 404, error: true, message: "Utilisateur non trouvé", data: null }
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
@Controllable("/users")
|
|
87
|
+
export class UserController {
|
|
88
|
+
@Get("/:id")
|
|
89
|
+
get(@Param("id") id: string) {
|
|
90
|
+
const user = users.find(u => u.id === Number(id))
|
|
91
|
+
if (!user) {
|
|
92
|
+
throw new HttpException(404, "Utilisateur introuvable")
|
|
93
|
+
}
|
|
94
|
+
return user
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### `Throwable`
|
|
100
|
+
|
|
101
|
+
Exception de base avec helpers statiques.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { Throwable, throwError, throwException, throwWarning, throwCritical } from "raiton/framework"
|
|
105
|
+
|
|
106
|
+
// Lance une erreur simple
|
|
107
|
+
throwError("Quelque chose a mal tourné")
|
|
108
|
+
|
|
109
|
+
// Lance une exception
|
|
110
|
+
throwException("Erreur métier", 422)
|
|
111
|
+
|
|
112
|
+
// Lance un avertissement
|
|
113
|
+
throwWarning("Attention", { detail: "valeur" })
|
|
114
|
+
|
|
115
|
+
// Lance une erreur critique
|
|
116
|
+
throwCritical("Erreur fatale", { code: "FATAL_001" })
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Propagation dans le pipeline
|
|
120
|
+
|
|
121
|
+
Les exceptions sont attrapées à deux niveaux :
|
|
122
|
+
|
|
123
|
+
1. **Dans le handler de route** — `Application.handle()` → `try/catch` → `reply.send()`
|
|
124
|
+
2. **Dans le middleware** — `this.root.middleware.run()` → `try/catch` → `ctx.reply.send()`
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// Application.handle() extrait :
|
|
128
|
+
try {
|
|
129
|
+
await this.root.middleware.run(ctx, handler)
|
|
130
|
+
} catch (err) {
|
|
131
|
+
if (err instanceof HttpException || err instanceof ThrowableResponse) {
|
|
132
|
+
ctx.reply.status(err.statusCode ?? 500)
|
|
133
|
+
return ctx.reply.send(err.render())
|
|
134
|
+
}
|
|
135
|
+
ctx.reply.send({
|
|
136
|
+
statusCode: 500,
|
|
137
|
+
error: true,
|
|
138
|
+
message: err.message ?? err,
|
|
139
|
+
data: null,
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Renvoyer une réponse depuis un contrôleur
|
|
145
|
+
|
|
146
|
+
Le handler de route peut retourner :
|
|
147
|
+
- Un **objet** → automatiquement sérialisé en JSON
|
|
148
|
+
- Un **ThrowableResponse** → code statut personnalisé
|
|
149
|
+
- Une **instance de classe** → envoyée telle quelle
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
@Controllable("/example")
|
|
153
|
+
export class ExampleController {
|
|
154
|
+
@Get("/object")
|
|
155
|
+
object() {
|
|
156
|
+
return { message: "Ceci sera du JSON" }
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@Get("/error")
|
|
160
|
+
error() {
|
|
161
|
+
return new ThrowableResponse("Erreur personnalisée", { statusCode: 400 })
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@Get("/exception")
|
|
165
|
+
exception() {
|
|
166
|
+
throw new HttpException(403, "Accès refusé")
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
@Get("/custom")
|
|
170
|
+
custom(@Reply() reply: any) {
|
|
171
|
+
reply.type("text/html")
|
|
172
|
+
reply.status(200)
|
|
173
|
+
reply.send("<h1>Hello</h1>")
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
[← runtime.md](runtime.md) | [↑ framework/](README.md) | [encryption.md →](encryption.md)
|
|
180
|
+
```
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Runtime
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/runtime/`
|
|
4
|
+
**Import :** `import { Runtime, RuntimeType } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← framework/](README.md) | [↑ framework/](README.md) | [responses.md →](responses.md)
|
|
7
|
+
|
|
8
|
+
## `Runtime`
|
|
9
|
+
|
|
10
|
+
Fabrique d'adaptateurs runtime. Choisit automatiquement l'implémentation de serveur HTTP selon l'environnement.
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
class Runtime {
|
|
14
|
+
constructor(public readonly type: RuntimeType)
|
|
15
|
+
|
|
16
|
+
get isNode(): boolean
|
|
17
|
+
get isBun(): boolean
|
|
18
|
+
|
|
19
|
+
adapter(): RuntimeAdapterInterface
|
|
20
|
+
createServer(handler: RuntimeHandlerCallable): RuntimeServerInterface
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Création
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
// Détection automatique (dans RaitonThread.setup) :
|
|
28
|
+
const defaultRuntime = typeof Bun !== 'undefined' ? RuntimeType.Bun : RuntimeType.Node
|
|
29
|
+
|
|
30
|
+
// Manuel :
|
|
31
|
+
const runtime = new Runtime(RuntimeType.Bun)
|
|
32
|
+
const runtime = new Runtime(RuntimeType.Node)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Création du serveur
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
const runtime = new Runtime(RuntimeType.Bun)
|
|
39
|
+
const server = runtime.createServer(async (req, reply) => {
|
|
40
|
+
reply.send({ message: "Hello" })
|
|
41
|
+
})
|
|
42
|
+
await server.listen(3000, "0.0.0.0")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Adaptateurs disponibles
|
|
46
|
+
|
|
47
|
+
### `bunRuntime` (Bun.serve)
|
|
48
|
+
|
|
49
|
+
**Fichier :** `source/framework/runtime/bun/server.ts`
|
|
50
|
+
|
|
51
|
+
Utilise `Bun.serve()` de manière native pour des performances maximales.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
// Utilisation interne :
|
|
55
|
+
const server = bunRuntime.createServer(handler)
|
|
56
|
+
await server.listen(port, hostname)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- **TCP** : `Bun.listen({ ... })`
|
|
60
|
+
- **Requêtes** : Native `Request` / `Response`
|
|
61
|
+
- **Recommandé** pour la production
|
|
62
|
+
|
|
63
|
+
### `nodeRuntime` (Node.js HTTP)
|
|
64
|
+
|
|
65
|
+
**Fichiers :** `source/framework/runtime/node/`
|
|
66
|
+
|
|
67
|
+
Utilise `node:http.createServer()` avec des wrappers pour standardiser l'interface.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
// Fichiers :
|
|
71
|
+
node/
|
|
72
|
+
├── server.ts → nodeRuntime.createServer(handler) → listen(port, hostname)
|
|
73
|
+
├── request.ts → NodeRequest (wrapper IncomingMessage)
|
|
74
|
+
└── reply.ts → NodeReply (wrapper ServerResponse)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
- `NodeRequest` : wrapper autour de `IncomingMessage` avec interface `RequestInterface`
|
|
78
|
+
- `NodeReply` : wrapper autour de `ServerResponse` avec interface `ReplyInterface`
|
|
79
|
+
|
|
80
|
+
### `denoRuntime` (stub)
|
|
81
|
+
|
|
82
|
+
**Fichier :** `source/framework/runtime/deno/server.ts`
|
|
83
|
+
|
|
84
|
+
Non implémenté — stub uniquement.
|
|
85
|
+
|
|
86
|
+
## Interface `RuntimeAdapterInterface`
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
interface RuntimeAdapterInterface {
|
|
90
|
+
createServer(handler: RuntimeHandlerCallable): RuntimeServerInterface
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface RuntimeServerInterface {
|
|
94
|
+
listen(port: number, hostname?: string): Promise<void>
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Exemple d'utilisation
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { Runtime, RuntimeType } from "raiton/framework"
|
|
102
|
+
import type { ThreadInterface } from "raiton/types"
|
|
103
|
+
import { Application } from "raiton/core"
|
|
104
|
+
import "reflect-metadata"
|
|
105
|
+
|
|
106
|
+
export default async function (thread: ThreadInterface) {
|
|
107
|
+
const app = new Application({ port: 3000 })
|
|
108
|
+
|
|
109
|
+
// Runtime est configuré automatiquement par thread.setup()
|
|
110
|
+
// Mais on peut spécifier manuellement :
|
|
111
|
+
return await thread
|
|
112
|
+
.setup({ application: app, runtime: RuntimeType.Bun })
|
|
113
|
+
.run()
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Détection automatique
|
|
118
|
+
|
|
119
|
+
Dans `RaitonThread.setup()` :
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
setup({ application, runtime }) {
|
|
123
|
+
const defaultRuntime = typeof Bun !== 'undefined'
|
|
124
|
+
? RuntimeType.Bun
|
|
125
|
+
: RuntimeType.Node
|
|
126
|
+
|
|
127
|
+
this.runtime = new Runtime(runtime || defaultRuntime)
|
|
128
|
+
this.application = application
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Si aucun runtime n'est spécifié, Raiton détecte automatiquement :
|
|
133
|
+
- **Bun** si `globalThis.Bun` est défini
|
|
134
|
+
- **Node.js** sinon
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
[← framework/](README.md) | [↑ framework/](README.md) | [responses.md →](responses.md)
|