Skip to content

Instantly share code, notes, and snippets.

@QuentinRoy
Created January 13, 2023 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuentinRoy/0f129f6e0e087b7188db0441af854e91 to your computer and use it in GitHub Desktop.
Save QuentinRoy/0f129f6e0e087b7188db0441af854e91 to your computer and use it in GitHub Desktop.
{
"name": "zodios-test",
"version": "1.0.0",
"license": "ISC",
"packageManager": "^pnpm@7.24.3",
"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"
}
}
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);
{
"$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