-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
59 lines (52 loc) · 1.6 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import express, { Express, Request, Response } from "express";
import { router, publicProcedure, createContext } from "./trpc";
import * as trpcExpress from "@trpc/server/adapters/express";
import { db } from "./db";
import { z } from "zod";
import cors from "cors";
const app: Express = express();
app.use(
cors({
origin: "*", // Allow all origins
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
})
);
const port = 3000;
const appRouter = router({
userList: publicProcedure.query(async () => {
// Retrieve users from a datasource
const users = await db.user.findMany();
return users;
}),
userById: publicProcedure.input(z.string()).query(async (opts) => {
const { input } = opts;
// Retrieve the user with the given ID from a datasource
const user = await db.user.findById(input);
return user;
}),
userCreate: publicProcedure
.input(z.object({ name: z.string().min(5) }))
.mutation(async (opts) => {
const { input } = opts;
// Create a new user with the given input in a datasource
const user = await db.user.create(input);
return user;
}),
});
app.get("/", (req: Request, res: Response) => {
res.send("Hello World from Express + TypeScript + Node.js !");
});
app.use(
"/trpc",
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
})
);
app.listen(port, () => {
console.log(`Cyrus IMAP API Server listening on port ${port}`);
});
// Export type router type signature,
// NOT the router itself.
export type AppRouter = typeof appRouter;