@tahminator/sapling 1.5.27 → 1.5.28-beta.03ae0bff
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/README.md +55 -1
- package/dist/index.cjs +754 -0
- package/dist/index.d.cts +498 -0
- package/dist/index.d.mts +498 -0
- package/dist/index.mjs +700 -0
- package/package.json +17 -11
- package/dist/eslint.config.d.ts +0 -2
- package/dist/eslint.config.js +0 -38
- package/dist/exclusions.d.ts +0 -5
- package/dist/exclusions.js +0 -6
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/lib/weakmap.d.ts +0 -15
- package/dist/lib/weakmap.js +0 -77
- package/dist/src/__test__/first.d.ts +0 -6
- package/dist/src/__test__/first.js +0 -20
- package/dist/src/__test__/second.d.ts +0 -6
- package/dist/src/__test__/second.js +0 -20
- package/dist/src/annotation/controller.d.ts +0 -21
- package/dist/src/annotation/controller.js +0 -78
- package/dist/src/annotation/index.d.ts +0 -4
- package/dist/src/annotation/index.js +0 -4
- package/dist/src/annotation/injectable.d.ts +0 -25
- package/dist/src/annotation/injectable.js +0 -72
- package/dist/src/annotation/middleware.d.ts +0 -9
- package/dist/src/annotation/middleware.js +0 -11
- package/dist/src/annotation/route.d.ts +0 -47
- package/dist/src/annotation/route.js +0 -77
- package/dist/src/enum/http.d.ts +0 -68
- package/dist/src/enum/http.js +0 -71
- package/dist/src/enum/index.d.ts +0 -1
- package/dist/src/enum/index.js +0 -1
- package/dist/src/helper/error.d.ts +0 -10
- package/dist/src/helper/error.js +0 -19
- package/dist/src/helper/index.d.ts +0 -4
- package/dist/src/helper/index.js +0 -4
- package/dist/src/helper/redirect.d.ts +0 -14
- package/dist/src/helper/redirect.js +0 -19
- package/dist/src/helper/response.d.ts +0 -68
- package/dist/src/helper/response.js +0 -90
- package/dist/src/helper/sapling.d.ts +0 -101
- package/dist/src/helper/sapling.js +0 -153
- package/dist/src/html/404.d.ts +0 -4
- package/dist/src/html/404.js +0 -14
- package/dist/src/html/index.d.ts +0 -1
- package/dist/src/html/index.js +0 -1
- package/dist/src/index.d.ts +0 -5
- package/dist/src/index.js +0 -5
- package/dist/src/types.d.ts +0 -21
- package/dist/src/types.js +0 -11
- package/dist/vite.config.d.ts +0 -2
- package/dist/vite.config.js +0 -18
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ A lightweight Express.js dependency injection & route abstraction library.
|
|
|
19
19
|
* [Responses](#responses)
|
|
20
20
|
* [Error Handling](#error-handling)
|
|
21
21
|
* [Middleware](#middleware)
|
|
22
|
+
* [Request Validation](#request-validation)
|
|
22
23
|
* [Redirects](#redirects)
|
|
23
24
|
* [Dependency Injection](#dependency-injection)
|
|
24
25
|
* [Custom Serialization](#custom-serialization)
|
|
@@ -142,8 +143,11 @@ Sapling supports the usual suspects:
|
|
|
142
143
|
- `@DELETE(path?)`
|
|
143
144
|
- `@PATCH(path?)`
|
|
144
145
|
- `@Middleware(path?)` - for middleware
|
|
146
|
+
- `@RequestBody(schema)` - validate & parse the request body
|
|
147
|
+
- `@RequestParam(schema)` - validate & parse route params
|
|
148
|
+
- `@RequestQuery(schema)` - validate & parse the query string
|
|
145
149
|
|
|
146
|
-
Path defaults to `"/"` if you don't pass one.
|
|
150
|
+
Path defaults to `"/"` if you don't pass one. The request schema decorators accept any [Standard Schema](https://github.com/standard-schema/standard-schema) compatible validator (e.g. Zod, Valibot, ArkType).
|
|
147
151
|
|
|
148
152
|
### Responses
|
|
149
153
|
|
|
@@ -234,6 +238,56 @@ app.use(Sapling.resolve(CookieParserMiddleware));
|
|
|
234
238
|
app.use(cookieParser());
|
|
235
239
|
```
|
|
236
240
|
|
|
241
|
+
### Request Validation
|
|
242
|
+
|
|
243
|
+
Validate and transform request bodies, route params, and query strings at the controller level using `@RequestBody`, `@RequestParam`, and `@RequestQuery`. These decorators accept any [Standard Schema](https://github.com/standard-schema/standard-schema) compatible validator (Zod, Valibot, ArkType, etc.).
|
|
244
|
+
|
|
245
|
+
If validation fails, a `ParserError` is thrown, which Express handles as a `400 Bad Request` by default:
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import { z } from "zod";
|
|
249
|
+
|
|
250
|
+
const CreateUserSchema = z.object({ name: z.string(), age: z.number() });
|
|
251
|
+
const UserParamsSchema = z.object({ id: z.string() });
|
|
252
|
+
const ListUsersQuerySchema = z.object({ page: z.coerce.number() });
|
|
253
|
+
|
|
254
|
+
@Controller({ prefix: "/users" })
|
|
255
|
+
class UserController {
|
|
256
|
+
@RequestBody(CreateUserSchema)
|
|
257
|
+
@POST()
|
|
258
|
+
createUser(request: Request): ResponseEntity<User> {
|
|
259
|
+
// request.body has been fully validated and rewritten. you can safely assert the type!
|
|
260
|
+
const requestBody = request.body as unknown as z.infer<CreateUserSchema>;
|
|
261
|
+
|
|
262
|
+
const user = this.database.user.create(requestBody.name, requestBody.age)
|
|
263
|
+
|
|
264
|
+
return ResponseEntity.ok().body(user);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
@RequestParam(UserParamsSchema)
|
|
268
|
+
@GET("/:id")
|
|
269
|
+
getUser(request: Request): ResponseEntity<z.infer<typeof UserParamsSchema>> {
|
|
270
|
+
// request.params has been fully validated and rewritten. you can safely assert the type!
|
|
271
|
+
const params = request.params as unknown as z.infer<typeof UserParamsSchema>;
|
|
272
|
+
|
|
273
|
+
const user = this.database.user.findById(params.id);
|
|
274
|
+
|
|
275
|
+
return ResponseEntity.ok().body(user);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
@RequestQuery(ListUsersQuerySchema)
|
|
279
|
+
@GET()
|
|
280
|
+
listUsers(request: Request): ResponseEntity<z.infer<typeof ListUsersQuerySchema>> {
|
|
281
|
+
// request.query has been fully validated and rewritten. you can safely assert the type!
|
|
282
|
+
const query = request.query as unknown as z.infer<typeof ListUsersQuerySchema>;
|
|
283
|
+
|
|
284
|
+
const users = this.database.user.findAll({ page: query.page });
|
|
285
|
+
|
|
286
|
+
return ResponseEntity.ok().body(users);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
237
291
|
### Redirects
|
|
238
292
|
|
|
239
293
|
```typescript
|