Skip to content

Commit

Permalink
Replace uuid by nanoid
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Nov 14, 2024
1 parent 655b626 commit 31b0eaa
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 45 deletions.
40 changes: 19 additions & 21 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@types/node": "^22.9.0",
"@types/nodemailer": "^6.4.16",
"@types/supertest": "^6.0.2",
"@types/uuid": "^10.0.0",
"@types/validator": "^13.12.2",
"axios": "^1.7.7",
"babel-jest": "^29.7.0",
Expand All @@ -49,13 +48,13 @@
"jose": "^5.9.6",
"mongoose": "^8.8.0",
"multer": "^1.4.4",
"nanoid": "^5.0.8",
"nocache": "^4.0.0",
"nodemailer": "^6.9.16",
"rimraf": "^6.0.1",
"stripe": "^17.3.1",
"supertest": "^7.0.0",
"typescript": "^5.4.5",
"uuid": "^11.0.2",
"validator": "^13.12.0",
"winston": "^3.16.0"
},
Expand Down
4 changes: 2 additions & 2 deletions api/src/common/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import mongoose from 'mongoose'
import validator from 'validator'
import Stripe from 'stripe'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import axios from 'axios'
import * as wexcommerceTypes from ':wexcommerce-types'

Expand Down Expand Up @@ -155,7 +155,7 @@ export const isValidEmail = (email?: string) => !!email && validator.isEmail(ema
*
* @returns {string}
*/
export const generateToken = () => `${uuid()}-${Date.now()}`
export const generateToken = () => `${nanoid()}-${Date.now()}`

/**
* The IETF language tag of the locale Checkout is displayed in.
Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/categoryController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose'
import path from 'node:path'
import fs from 'node:fs/promises'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import escapeStringRegexp from 'escape-string-regexp'
import { Request, Response } from 'express'
import * as wexcommerceTypes from ':wexcommerce-types'
Expand Down Expand Up @@ -529,7 +529,7 @@ export const createImage = async (req: Request, res: Response) => {
throw new Error('[category.createImage] req.file not found')
}

const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${uuid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${nanoid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filepath = path.join(env.CDN_TEMP_CATEGORIES, filename)

await fs.writeFile(filepath, req.file.buffer)
Expand Down
8 changes: 4 additions & 4 deletions api/src/controllers/productController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose, { Expression } from 'mongoose'
import fs from 'fs/promises'
import path from 'path'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import { Request, Response } from 'express'
import escapeStringRegexp from 'escape-string-regexp'
import * as wexcommerceTypes from ':wexcommerce-types'
Expand Down Expand Up @@ -31,7 +31,7 @@ export const uploadImage = async (req: Request, res: Response) => {
throw new Error('[product.uploadImage] req.file not found')
}

const filename = `${uuid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filename = `${nanoid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filepath = path.join(env.CDN_TEMP_PRODUCTS, filename)

await fs.writeFile(filepath, req.file.buffer)
Expand Down Expand Up @@ -156,7 +156,7 @@ export const create = async (req: Request, res: Response) => {
const __image = path.join(env.CDN_TEMP_PRODUCTS, image)

if (await helper.exists(__image)) {
const filename = `${product._id}_${uuid()}_${Date.now()}_${i}${path.extname(image)}`
const filename = `${product._id}_${nanoid()}_${Date.now()}_${i}${path.extname(image)}`
const newPath = path.join(env.CDN_PRODUCTS, filename)

await fs.rename(__image, newPath)
Expand Down Expand Up @@ -263,7 +263,7 @@ export const update = async (req: Request, res: Response) => {
const _image = path.join(env.CDN_TEMP_PRODUCTS, imageFile)

if (await helper.exists(_image)) {
const filename = `${product._id}_${uuid()}_${Date.now()}_${i}${path.extname(imageFile)}`
const filename = `${product._id}_${nanoid()}_${Date.now()}_${i}${path.extname(imageFile)}`
const newPath = path.join(env.CDN_PRODUCTS, filename)

await fs.rename(_image, newPath)
Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'node:path'
import fs from 'node:fs/promises'
import escapeStringRegexp from 'escape-string-regexp'
import axios from 'axios'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import * as wexcommerceTypes from ':wexcommerce-types'
import * as logger from '../common/logger'
import i18n from '../lang/i18n'
Expand Down Expand Up @@ -1141,7 +1141,7 @@ export const createAvatar = async (req: Request, res: Response) => {
throw new Error('[user.createAvatar] req.file not found')
}

const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${uuid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${nanoid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filepath = path.join(env.CDN_TEMP_USERS, filename)

await fs.writeFile(filepath, req.file.buffer)
Expand Down
6 changes: 3 additions & 3 deletions api/tests/testHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest'
import cookieParser from 'cookie-parser'
import bcrypt from 'bcrypt'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as wexcommerceTypes from ':wexcommerce-types'
import app from '../src/app'
Expand All @@ -16,7 +16,7 @@ import * as settingController from '../src/controllers/settingController'

export const getName = (prefix: string) => {
expect(prefix.length).toBeGreaterThan(1)
return `${prefix}.${uuid()}`
return `${prefix}.${nanoid()}`.toLowerCase()
}

export const getRandomString = () => getName(Date.now().toString())
Expand Down Expand Up @@ -109,7 +109,7 @@ export const signinAsAdmin = () => signin(wexcommerceTypes.AppType.Backend, ADMI

export const signinAsUser = () => signin(wexcommerceTypes.AppType.Frontend, USER_EMAIL)

export const GetRandomEmail = () => `random.${uuid()}.${Date.now()}@test.wexcommerce.ma`
export const GetRandomEmail = () => `random.${nanoid()}.${Date.now()}@test.wexcommerce.ma`

export const GetRandromObjectId = () => new mongoose.Types.ObjectId()

Expand Down
18 changes: 9 additions & 9 deletions api/tests/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import request from 'supertest'
import url from 'url'
import path from 'path'
import fs from 'node:fs/promises'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as wexcommerceTypes from ':wexcommerce-types'
import app from '../src/app'
Expand Down Expand Up @@ -50,7 +50,7 @@ afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()

await Token.deleteMany({ user: { $in: [ADMIN_ID] } })
await Token.deleteMany({ user: { $in: [ADMIN_ID, USER1_ID] } })

await databaseHelper.close()
}
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('POST /api/sign-up', () => {

const email = testHelper.GetRandomEmail()
payload.email = email
payload.avatar = `${uuid()}.jpg`
payload.avatar = `${nanoid()}.jpg`
res = await request(app)
.post('/api/sign-up')
.send(payload)
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('GET /api/check-token/:type/:userId/:email/:token', () => {
expect(res.statusCode).toBe(204)

res = await request(app)
.get(`/api/check-token/${wexcommerceTypes.AppType.Frontend}/${USER1_ID}/${USER1_EMAIL}/${uuid()}`)
.get(`/api/check-token/${wexcommerceTypes.AppType.Frontend}/${USER1_ID}/${USER1_EMAIL}/${nanoid()}`)
expect(res.statusCode).toBe(204)

res = await request(app)
Expand Down Expand Up @@ -238,11 +238,11 @@ describe('GET /api/confirm-email/:email/:token', () => {
expect(res.statusCode).toBe(204)

res = await request(app)
.get(`/api/confirm-email/${USER1_EMAIL}/${uuid()}`)
.get(`/api/confirm-email/${USER1_EMAIL}/${nanoid()}`)
expect(res.statusCode).toBe(400)

res = await request(app)
.get(`/api/confirm-email/unknown/${uuid()}`)
.get(`/api/confirm-email/unknown/${nanoid()}`)
expect(res.statusCode).toBe(400)
})
})
Expand Down Expand Up @@ -499,7 +499,7 @@ describe('POST /api/validate-access-token', () => {

res = await request(app)
.post('/api/validate-access-token')
.set(env.X_ACCESS_TOKEN, uuid())
.set(env.X_ACCESS_TOKEN, nanoid())

expect(res.statusCode).toBe(401)

Expand Down Expand Up @@ -646,7 +646,7 @@ describe('POST /api/update-avatar/:userId', () => {
expect(user?.avatar).toBeDefined()
expect(user?.avatar).not.toBeNull()

user!.avatar = `${uuid()}.jpg`
user!.avatar = `${nanoid()}.jpg`
await user?.save()
res = await request(app)
.post(`/api/update-avatar/${USER1_ID}`)
Expand Down Expand Up @@ -706,7 +706,7 @@ describe('POST /api/delete-avatar/:userId', () => {
expect(user).not.toBeNull()
expect(user?.avatar).toBeUndefined()

user!.avatar = `${uuid()}.jpg`
user!.avatar = `${nanoid()}.jpg`
await user?.save()
res = await request(app)
.post(`/api/delete-avatar/${USER1_ID}`)
Expand Down

0 comments on commit 31b0eaa

Please sign in to comment.