@@ -13,8 +13,8 @@ TypeSpec library for emitting a helper function to build type-safe Express route
13
13
14
14
- [x] Emit a function that can build type-safe Express routes
15
15
- [ ] Add support for typed responses based on status codes
16
- - [ ] Add support for header types
17
16
- [ ] Add support for authorization
17
+ - [ ] Add support for header types
18
18
- [ ] Add support for Zod schema validation of inputs: Path, Query, Body.
19
19
- [ ] Add support for Zod schema validation of output body
20
20
@@ -53,7 +53,54 @@ options:
53
53
54
54
### Examples
55
55
56
- TODO
56
+ Given the following Typespec:
57
+ ` ` ` typespec
58
+ import "@typespec/http";
59
+
60
+ enum petType {...}
61
+ model Pet {...}
62
+
63
+ @route("/pets")
64
+ namespace Pets {
65
+ @get
66
+ op listPets(@query type? : petType): {
67
+ @body pets: Pet[];
68
+ };
69
+ }
70
+ ```
71
+
72
+ This emitter will allow you to implement the following:
73
+ ``` typescript
74
+ import { createTypedRouter } from ' tsp-output/@typespec-tools/emitter-express/output";
75
+
76
+ const app = express ();
77
+ const router = express .Router ();
78
+
79
+ typedRouter = createTypedRouter (router );
80
+
81
+ typedRouter .Pets .listPets ((req , res ) => {
82
+ // req.query.type is typed as petType enum
83
+ // the response has type: { pets: Pet[] }
84
+ res .json ({ pets: [... ] });
85
+ });
86
+
87
+ app .use (typedRouter .router );
88
+ ```
89
+
90
+ Alternatively, you can implement a single namespace:
91
+ ``` typescript
92
+ import { createPetsRouter } from ' tsp-output/@typespec-tools/emitter-express/output";
93
+
94
+ export const router = express .Router ();
95
+
96
+ typedPetsRouter = createPetsRouter (router );
97
+
98
+ typedPetsRouter .listPets ((req , res ) => {
99
+ // req.query.type is typed as petType enum
100
+ // the response has type: { pets: Pet[] }
101
+ res .json ({ pets: [... ] });
102
+ });
103
+ ```
57
104
58
105
### Emitter options
59
106
0 commit comments