Skip to content

Commit 6d6e3fa

Browse files
committed
add prettier
1 parent 886628c commit 6d6e3fa

21 files changed

+97
-128
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yarn.lock

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 100,
5+
"singleAttributePerLine": true
6+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"prestart": "tsup src/config --onSuccess \"node dist/database.js\"",
88
"start": "node dist/server.js",
99
"dev": "tsup src/server.ts --watch --onSuccess \"node dist/server.js\"",
10-
"build": "tsup"
10+
"build": "tsup",
11+
"prettier:fix": "npx prettier --write ."
1112
},
1213
"keywords": [],
1314
"author": "",

src/app.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import express from "express";
2-
import { env } from "config/env";
3-
import errorMiddleware from "middleware/error.middleware";
4-
import { type Controller } from "interfaces/controller.interface";
5-
import cookieParser from "cookie-parser";
1+
import express from 'express';
2+
import { env } from 'config/env';
3+
import errorMiddleware from 'middleware/error.middleware';
4+
import { type Controller } from 'interfaces/controller.interface';
5+
import cookieParser from 'cookie-parser';
66

77
class App {
88
private app: express.Application;
@@ -23,7 +23,7 @@ class App {
2323

2424
private initializeControllers(controllers: Controller[]) {
2525
controllers.forEach((controller) => {
26-
this.app.use("/api/v1", controller.router);
26+
this.app.use('/api/v1', controller.router);
2727
});
2828
}
2929

src/auth/auth.controller.ts

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,34 @@
1-
import { Request, Response, NextFunction } from "express";
2-
import validationMiddleware from "middleware/validation.middleware";
3-
import { loginSchema, registerSchema } from "./auth.validation";
4-
import { AuthService } from "./auth.service";
5-
import { Controller } from "interfaces/controller.interface";
6-
import authMiddleware from "middleware/auth.middleware";
1+
import { Request, Response, NextFunction } from 'express';
2+
import validationMiddleware from 'middleware/validation.middleware';
3+
import { loginSchema, registerSchema } from './auth.validation';
4+
import { AuthService } from './auth.service';
5+
import { Controller } from 'interfaces/controller.interface';
6+
import authMiddleware from 'middleware/auth.middleware';
77

88
export class AuthController extends Controller {
99
private authService = new AuthService();
1010

1111
constructor() {
12-
super("/auth");
12+
super('/auth');
1313
this.initializeRoutes();
1414
}
1515

1616
protected initializeRoutes() {
17-
this.router.post(
18-
`${this.path}/register`,
19-
validationMiddleware(registerSchema),
20-
this.register
21-
);
22-
this.router.post(
23-
`${this.path}/login`,
24-
validationMiddleware(loginSchema),
25-
this.login
26-
);
17+
this.router.post(`${this.path}/register`, validationMiddleware(registerSchema), this.register);
18+
this.router.post(`${this.path}/login`, validationMiddleware(loginSchema), this.login);
2719
this.router.get(`${this.path}/me`, authMiddleware, this.isLoggedIn);
2820
this.router.delete(`${this.path}/logout`, authMiddleware, this.logout);
2921
}
3022

31-
private register = async (
32-
request: Request,
33-
response: Response,
34-
next: NextFunction
35-
) => {
23+
private register = async (request: Request, response: Response, next: NextFunction) => {
3624
try {
3725
const createdUser = await this.authService.registerUser(request.body);
38-
if (!createdUser) return next("No user created");
26+
if (!createdUser) return next('No user created');
3927

4028
response.cookie(
41-
"Authentication",
29+
'Authentication',
4230
this.authService.createToken(createdUser.id),
43-
this.authService.createCookieOptions()
31+
this.authService.createCookieOptions(),
4432
);
4533

4634
response.status(201).json(createdUser);
@@ -49,19 +37,15 @@ export class AuthController extends Controller {
4937
}
5038
};
5139

52-
private login = async (
53-
request: Request,
54-
response: Response,
55-
next: NextFunction
56-
) => {
40+
private login = async (request: Request, response: Response, next: NextFunction) => {
5741
try {
5842
const user = await this.authService.login(request.body);
59-
if (!user) return next("No user found");
43+
if (!user) return next('No user found');
6044

6145
response.cookie(
62-
"Authentication",
46+
'Authentication',
6347
this.authService.createToken(user.id),
64-
this.authService.createCookieOptions()
48+
this.authService.createCookieOptions(),
6549
);
6650

6751
response.json(user);
@@ -70,11 +54,7 @@ export class AuthController extends Controller {
7054
}
7155
};
7256

73-
private isLoggedIn = async (
74-
request: Request,
75-
response: Response,
76-
next: NextFunction
77-
) => {
57+
private isLoggedIn = async (request: Request, response: Response, next: NextFunction) => {
7858
try {
7959
const loggedUser = await this.authService.isLoggedIn(request.userId);
8060
if (!loggedUser) return next();
@@ -87,7 +67,7 @@ export class AuthController extends Controller {
8767

8868
private logout = (_: Request, response: Response) => {
8969
response
90-
.setHeader("Set-Cookie", ["Authentication=; Max-Age=0; Path=/; HttpOnly"])
70+
.setHeader('Set-Cookie', ['Authentication=; Max-Age=0; Path=/; HttpOnly'])
9171
.status(204)
9272
.end();
9373
};

src/auth/auth.repository.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
import pool from "config/database";
2-
import { RegisterPayload } from "./auth.validation";
3-
import { User } from "users/users.model";
1+
import pool from 'config/database';
2+
import { RegisterPayload } from './auth.validation';
3+
import { User } from 'users/users.model';
44

55
export class AuthRepository {
66
public async createUser(payload: RegisterPayload) {
77
const result = await pool.query<User>(
8-
"INSERT INTO users (username, email, password) VALUES ($1, $2, $3) RETURNING *",
9-
[payload.username, payload.email, payload.password]
8+
'INSERT INTO users (username, email, password) VALUES ($1, $2, $3) RETURNING *',
9+
[payload.username, payload.email, payload.password],
1010
);
1111
return result.rows[0];
1212
}
1313

1414
public async findUserById(id: number) {
15-
const result = await pool.query<User>("SELECT * FROM users WHERE id = $1", [
16-
id,
17-
]);
15+
const result = await pool.query<User>('SELECT * FROM users WHERE id = $1', [id]);
1816
return result.rows[0];
1917
}
2018

2119
public async findUserByEmail(email: string) {
22-
const result = await pool.query<User>(
23-
"SELECT * FROM users WHERE email = $1",
24-
[email]
25-
);
20+
const result = await pool.query<User>('SELECT * FROM users WHERE email = $1', [email]);
2621
return result.rows[0];
2722
}
2823
}

src/auth/auth.service.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import jwt from "jsonwebtoken";
2-
import bcrypt from "bcrypt";
3-
import { LoginPayload, RegisterPayload } from "./auth.validation";
4-
import { AuthRepository } from "./auth.repository";
5-
import { env } from "config/env";
6-
import { BadRequestError } from "errors/bad-request.error";
7-
import { CookieOptions } from "express";
8-
import { User } from "users/users.model";
1+
import jwt from 'jsonwebtoken';
2+
import bcrypt from 'bcrypt';
3+
import { LoginPayload, RegisterPayload } from './auth.validation';
4+
import { AuthRepository } from './auth.repository';
5+
import { env } from 'config/env';
6+
import { BadRequestError } from 'errors/bad-request.error';
7+
import { CookieOptions } from 'express';
8+
import { User } from 'users/users.model';
99

1010
export class AuthService {
1111
private authRepository = new AuthRepository();
@@ -14,15 +14,15 @@ export class AuthService {
1414
// Check if email already exists
1515
const foundUser = await this.authRepository.findUserByEmail(payload.email);
1616
if (foundUser) {
17-
throw new BadRequestError("User with that email already exists");
17+
throw new BadRequestError('User with that email already exists');
1818
}
1919

2020
const user = await this.authRepository.createUser({
2121
...payload,
2222
password: await bcrypt.hash(payload.password, 10), // Hash password
2323
});
2424
if (!user) {
25-
throw new BadRequestError("Failed to create user");
25+
throw new BadRequestError('Failed to create user');
2626
}
2727

2828
return this.removePassword(user);
@@ -31,15 +31,12 @@ export class AuthService {
3131
public async login(payload: LoginPayload) {
3232
const user = await this.authRepository.findUserByEmail(payload.email);
3333
if (!user) {
34-
throw new BadRequestError("Invalid email or password");
34+
throw new BadRequestError('Invalid email or password');
3535
}
3636

37-
const isPasswordCorrect = await bcrypt.compare(
38-
payload.password,
39-
user.password
40-
);
37+
const isPasswordCorrect = await bcrypt.compare(payload.password, user.password);
4138
if (!isPasswordCorrect) {
42-
throw new BadRequestError("Invalid email or password");
39+
throw new BadRequestError('Invalid email or password');
4340
}
4441

4542
return this.removePassword(user);
@@ -58,8 +55,8 @@ export class AuthService {
5855
return {
5956
maxAge: 5 * 60 * 60 * 1000, // 5 hours
6057
httpOnly: true,
61-
sameSite: "lax",
62-
secure: process.env.NODE_ENV === "production",
58+
sameSite: 'lax',
59+
secure: process.env.NODE_ENV === 'production',
6360
};
6461
}
6562

src/auth/auth.validation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { z } from "zod";
1+
import { z } from 'zod';
22

33
export const registerSchema = z.object({
44
body: z.object({
@@ -8,7 +8,7 @@ export const registerSchema = z.object({
88
}),
99
});
1010

11-
export type RegisterPayload = z.infer<typeof registerSchema>["body"];
11+
export type RegisterPayload = z.infer<typeof registerSchema>['body'];
1212

1313
export const loginSchema = z.object({
1414
body: z.object({
@@ -17,4 +17,4 @@ export const loginSchema = z.object({
1717
}),
1818
});
1919

20-
export type LoginPayload = z.infer<typeof loginSchema>["body"];
20+
export type LoginPayload = z.infer<typeof loginSchema>['body'];

src/config/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Pool } from "pg";
2-
import { env } from "./env";
1+
import { Pool } from 'pg';
2+
import { env } from './env';
33

44
const pool = new Pool({
55
host: env.POSTGRES_HOST,

src/config/env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { config } from "dotenv";
2-
import { cleanEnv, port, str } from "envalid";
1+
import { config } from 'dotenv';
2+
import { cleanEnv, port, str } from 'envalid';
33

44
config();
55

0 commit comments

Comments
 (0)