Adicionando um middleware de tratamento de erro no server.ts
.
yarn add express-async-errors
src/server.ts
:
import "reflect-metadata";
import "express-async-errors";
import express, { Request, Response, NextFunction, response } from "express";
import "./database";
import { router } from "./routes";
const app = express();
app.use(express.json());
app.use(router);
app.use((err: Error, request: Request, response: Response, next: NextFunction) => {
if (err instanceof Error) {
return response.status(400).json({
error: err.message
});
}
});
app.listen(3000, () => console.log("Server is running"));
yarn typeorm migration:create -n CreateTags
src/database/migrations/*CreateTags.ts
:
import { MigrationInterface, QueryRunner, Table } from "typeorm";
export class CreateTags1624528928967 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "tags",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true
},
{
name: "name",
type: "varchar",
},
{
name: "created_at",
type: "timestamp",
default: "now()"
},
{
name: "updated_at",
type: "timestamp",
default: "now()"
}
]
})
)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable("tags");
}
}
yarn typeorm migration:run
src/entities/Tag.ts
import { Column, CreateDateColumn, Entity, PrimaryColumn, UpdateDateColumn } from "typeorm";
import { v4 as uuid } from "uuid";
@Entity("tags")
class Tag {
@PrimaryColumn()
readonly id: string;
@Column()
name: string;
@CreateDateColumn()
created_at: Date;
@UpdateDateColumn()
updated_at: Date;
constructor() {
if (!this.id) {
this.id = uuid();
}
}
}
export { Tag };
src/repositories/TagsRepositories.ts
import { EntityRepository, Repository } from "typeorm";
import { Tag } from "../entities/Tag";
@EntityRepository(Tag)
class TagsRepositories extends Repository<Tag> { }
export { TagsRepositories };
src/services/CreateTagService.ts
:
import { getCustomRepository } from "typeorm";
import { TagsRepositories } from "../repositories/TagRepositories";
class CreateTagService {
async execute(name: string) {
const tagsRepositories = getCustomRepository(TagsRepositories);
if (!name) {
throw new Error("Incorrect name!");
}
const tagAlreadyExists = await tagsRepositories.findOne({
name
});
if (tagAlreadyExists) {
throw new Error("Tag already exists!");
}
const tag = tagsRepositories.create({
name
});
await tagsRepositories.save(tag);
return tag;
}
}
export { CreateTagService };
src/controllers/CreateTagController.ts
:
import { Request, response, Response } from "express";
import { CreateTagService } from "../services/CreateTagService";
class CreateTagController {
async handle(request: Request, response: Response) {
const { name } = request.body;
const createTagService = new CreateTagService();
const tag = await createTagService.execute(name);
return response.json(tag);
}
}
export { CreateTagController };
mkdir -p src/middlewares
src/middlewares/ensureAdmin.ts
:
import { Request, Response, NextFunction } from "express";
export function ensureAdmin(
request: Request,
response: Response,
next: NextFunction
) {
// TODO: remove this hardcoded test
const admin = true
if (admin) {
return next();
}
return response.status(401).json({
error: "Unauthorized"
});
}
Ajuste em src/routes
50:34 - após criação do middleware ensureAdmin
yarn dev
# testar adicionar uma tag via insomnia
Testar no insomnia:
admin = false
admin = true