Skip to content

Commit

Permalink
refactor: change way to use z.infer from zod
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhiz committed May 2, 2023
1 parent 525ae5e commit 031f690
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/@types/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from 'zod';

import exampleSchema from '../schemas/example.schema';

export type exampleProps = z.infer<typeof exampleSchema.example>;
1 change: 1 addition & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './example';
4 changes: 2 additions & 2 deletions src/controllers/example.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import chalk from 'chalk';

import { Request, Response, NextFunction } from 'express';
// import { z } from "zod";
// import { exampleProps } from '../@types';

const create = async (req: Request, res: Response, next: NextFunction) => {
// const { name, password } = res.locals.sanitizedRequest as z.infer<typeof exampleSchema>;
// const { email, password } = res.locals.sanitizedRequest as exampleProps;
console.log(chalk.cyan('POST /example'));

try {
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type JWTPayload = {
userId: number;
};

const tokenMiddleware = async (req: Request, res: Response, next: NextFunction) => {
const authMiddleware = async (req: Request, res: Response, next: NextFunction) => {
const { authorization } = req.headers;
const token = authorization?.split(' ')[1]?.trim();

Expand All @@ -25,4 +25,4 @@ const tokenMiddleware = async (req: Request, res: Response, next: NextFunction)
next();
};

export default tokenMiddleware;
export default authMiddleware;
13 changes: 6 additions & 7 deletions src/routes/exemple.route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Router } from 'express';
// import { Router } from 'express';
// import authMiddleware from '../middlewares/auth.middleware';
// import schemaMiddleware from "../middlewares/schema.middleware";

const exampleRouter = Router();

// exampleRouter.post("/", schemaMiddleware());

// exempleRouter.get("/", schemaMiddleware());
// const exampleRouter = Router();
// exampleRouter.all('/*', authMiddleware);
// exampleRouter.post('/', schemaMiddleware());

// outras rotas aqui

export default exampleRouter;
// export default exampleRouter;
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { z } from 'zod';

const exampleSchema = z.object({
const example = z.object({
email: z.string().email(),
password: z.string().min(3).max(20)
});

export default {
exampleSchema
example
};
5 changes: 2 additions & 3 deletions src/services/example.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// import erros from '../erros/index';
// import { z } from 'zod';
// import erros from '../erros';

// const create = async ({}: z.infer<typeof exampleSchema>) => {};
// const create = async ({}) => {};

// export default {
// create
Expand Down

0 comments on commit 031f690

Please sign in to comment.