feat: V2 with TypeScript
This commit is contained in:
13
server/src/utils/app.ts
Normal file
13
server/src/utils/app.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import { ORIGIN } from '../constants/index'
|
||||
|
||||
// initialize app
|
||||
const app = express()
|
||||
|
||||
// middlewares
|
||||
app.use(cors({ origin: ORIGIN }))
|
||||
app.use(express.json()) // body parser
|
||||
app.use(express.urlencoded({ extended: false })) // url parser
|
||||
|
||||
export default app
|
||||
22
server/src/utils/crypt.ts
Normal file
22
server/src/utils/crypt.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import bcrypt from 'bcrypt'
|
||||
|
||||
class Crypt {
|
||||
instance: typeof bcrypt = bcrypt
|
||||
|
||||
constructor() {}
|
||||
|
||||
async hash(value: string) {
|
||||
const salt = await this.instance.genSalt(10)
|
||||
const hash = await this.instance.hash(value, salt)
|
||||
|
||||
return hash
|
||||
}
|
||||
|
||||
async validate(value: string, hash: string) {
|
||||
const isOk = await bcrypt.compare(value, hash)
|
||||
|
||||
return isOk
|
||||
}
|
||||
}
|
||||
|
||||
export default new Crypt()
|
||||
22
server/src/utils/joi.ts
Normal file
22
server/src/utils/joi.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import joi from 'joi'
|
||||
|
||||
class Joi {
|
||||
instance: typeof joi = joi
|
||||
|
||||
constructor() {}
|
||||
|
||||
async validate(schema: Record<string, any>, body: Record<string, any>) {
|
||||
try {
|
||||
await this.instance.object(schema).validateAsync(body)
|
||||
} catch (error: any) {
|
||||
console.log('❌ Joi validation error:', error.message)
|
||||
|
||||
return {
|
||||
statusCode: 400,
|
||||
message: error.message,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Joi()
|
||||
25
server/src/utils/jwt.ts
Normal file
25
server/src/utils/jwt.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import jsonwebtoken from 'jsonwebtoken'
|
||||
import { JWT_SECRET } from '../constants/index'
|
||||
|
||||
class JWT {
|
||||
instance: typeof jsonwebtoken = jsonwebtoken
|
||||
secret: string
|
||||
|
||||
constructor() {
|
||||
this.secret = JWT_SECRET
|
||||
}
|
||||
|
||||
signToken(payload: Record<string, any>, expiresIn: jsonwebtoken.SignOptions['expiresIn'] = '12h') {
|
||||
const token = this.instance.sign(payload, JWT_SECRET, { expiresIn })
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
verifyToken(token: string) {
|
||||
const auth = this.instance.verify(token, JWT_SECRET)
|
||||
|
||||
return auth
|
||||
}
|
||||
}
|
||||
|
||||
export default new JWT()
|
||||
37
server/src/utils/mongo.ts
Normal file
37
server/src/utils/mongo.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import mongoose from 'mongoose'
|
||||
import { MONGO_URI, MONGO_OPTIONS } from '../constants/index'
|
||||
|
||||
class Mongo {
|
||||
instance: typeof mongoose = mongoose
|
||||
mongoUri: string
|
||||
mongoOptions: mongoose.ConnectOptions
|
||||
isConnected: boolean
|
||||
|
||||
constructor() {
|
||||
this.mongoUri = MONGO_URI
|
||||
this.mongoOptions = MONGO_OPTIONS
|
||||
this.isConnected = false
|
||||
}
|
||||
|
||||
async connect() {
|
||||
if (this.isConnected) return
|
||||
|
||||
try {
|
||||
console.log('⏳ Connecting to MongoDB')
|
||||
|
||||
const db = await this.instance.connect(this.mongoUri, this.mongoOptions)
|
||||
const connection = db.connection
|
||||
|
||||
this.isConnected = connection.readyState === 1
|
||||
if (this.isConnected) console.log('✅ MongoDB connected')
|
||||
|
||||
connection.on('connected', () => console.log('✅ MongoDB connected')) // re-connected
|
||||
connection.on('disconnected', () => console.log('❌ MongoDB disconnected')) // disconnected
|
||||
connection.on('error', (error) => console.log('❌ MongoDB connection error', error)) // listen for errors during the session
|
||||
} catch (error: any) {
|
||||
console.log('❌ MongoDB connection error:', error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Mongo()
|
||||
Reference in New Issue
Block a user