@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.
Files changed (52) hide show
  1. package/README.md +55 -1
  2. package/dist/index.cjs +754 -0
  3. package/dist/index.d.cts +498 -0
  4. package/dist/index.d.mts +498 -0
  5. package/dist/index.mjs +700 -0
  6. package/package.json +17 -11
  7. package/dist/eslint.config.d.ts +0 -2
  8. package/dist/eslint.config.js +0 -38
  9. package/dist/exclusions.d.ts +0 -5
  10. package/dist/exclusions.js +0 -6
  11. package/dist/index.d.ts +0 -1
  12. package/dist/index.js +0 -1
  13. package/dist/lib/weakmap.d.ts +0 -15
  14. package/dist/lib/weakmap.js +0 -77
  15. package/dist/src/__test__/first.d.ts +0 -6
  16. package/dist/src/__test__/first.js +0 -20
  17. package/dist/src/__test__/second.d.ts +0 -6
  18. package/dist/src/__test__/second.js +0 -20
  19. package/dist/src/annotation/controller.d.ts +0 -21
  20. package/dist/src/annotation/controller.js +0 -78
  21. package/dist/src/annotation/index.d.ts +0 -4
  22. package/dist/src/annotation/index.js +0 -4
  23. package/dist/src/annotation/injectable.d.ts +0 -25
  24. package/dist/src/annotation/injectable.js +0 -72
  25. package/dist/src/annotation/middleware.d.ts +0 -9
  26. package/dist/src/annotation/middleware.js +0 -11
  27. package/dist/src/annotation/route.d.ts +0 -47
  28. package/dist/src/annotation/route.js +0 -77
  29. package/dist/src/enum/http.d.ts +0 -68
  30. package/dist/src/enum/http.js +0 -71
  31. package/dist/src/enum/index.d.ts +0 -1
  32. package/dist/src/enum/index.js +0 -1
  33. package/dist/src/helper/error.d.ts +0 -10
  34. package/dist/src/helper/error.js +0 -19
  35. package/dist/src/helper/index.d.ts +0 -4
  36. package/dist/src/helper/index.js +0 -4
  37. package/dist/src/helper/redirect.d.ts +0 -14
  38. package/dist/src/helper/redirect.js +0 -19
  39. package/dist/src/helper/response.d.ts +0 -68
  40. package/dist/src/helper/response.js +0 -90
  41. package/dist/src/helper/sapling.d.ts +0 -101
  42. package/dist/src/helper/sapling.js +0 -153
  43. package/dist/src/html/404.d.ts +0 -4
  44. package/dist/src/html/404.js +0 -14
  45. package/dist/src/html/index.d.ts +0 -1
  46. package/dist/src/html/index.js +0 -1
  47. package/dist/src/index.d.ts +0 -5
  48. package/dist/src/index.js +0 -5
  49. package/dist/src/types.d.ts +0 -21
  50. package/dist/src/types.js +0 -11
  51. package/dist/vite.config.d.ts +0 -2
  52. 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