Created
January 13, 2023 11:06
-
-
Save QuentinRoy/0f129f6e0e087b7188db0441af854e91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "zodios-test", | |
"version": "1.0.0", | |
"license": "ISC", | |
"packageManager": "^[email protected]", | |
"dependencies": { | |
"@types/express": "^4.17.15", | |
"@zodios/core": "^10.7.1", | |
"@zodios/express": "^10.4.4", | |
"axios": "^1.2.2", | |
"express": "^4.18.2", | |
"typescript": "^4.9.4", | |
"zod": "^3.20.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { makeApi } from "@zodios/core"; | |
import { zodiosContext } from "@zodios/express"; | |
import { z } from "zod"; | |
const ctx = zodiosContext( | |
z.object({ | |
user: z.object({ | |
id: z.number(), | |
name: z.string(), | |
isAdmin: z.boolean(), | |
}), | |
}), | |
); | |
const api = makeApi([ | |
{ | |
path: "/users/:id", | |
method: "get", | |
response: z.object({ | |
id: z.string(), | |
name: z.string(), | |
bidule: z.number(), | |
}), | |
}, | |
]); | |
const app = ctx.app(api); | |
// `app.get`, `req`, and `res` are of type `any` ! | |
app.get("/users/:id", (req, res) => { | |
if (req.user.isAdmin) { | |
return res.json({ | |
id: String(req.params.id), | |
name: "John Doe", | |
}); | |
} | |
return res.status(403).end(); | |
}); | |
app.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"$schema": "https://json.schemastore.org/tsconfig", | |
"compilerOptions": { | |
"esModuleInterop": true, | |
"moduleResolution": "Node" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment