Skip to content

Commit 9a27fbf

Browse files
committed
chore(emitter-express): Update readme with examples
1 parent 8aaea6d commit 9a27fbf

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

.changeset/tame-cups-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@typespec-tools/emitter-express": patch
3+
---
4+
5+
Update readme with examples

packages/emitter-express/README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ TypeSpec library for emitting a helper function to build type-safe Express route
1313

1414
- [x] Emit a function that can build type-safe Express routes
1515
- [ ] Add support for typed responses based on status codes
16-
- [ ] Add support for header types
1716
- [ ] Add support for authorization
17+
- [ ] Add support for header types
1818
- [ ] Add support for Zod schema validation of inputs: Path, Query, Body.
1919
- [ ] Add support for Zod schema validation of output body
2020

@@ -53,7 +53,54 @@ options:
5353
5454
### Examples
5555
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+
```
57104

58105
### Emitter options
59106

0 commit comments

Comments
 (0)