This commit is contained in:
2026-06-24 14:10:53 +02:00
commit fdb3768d63
122 changed files with 13239 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
export const getExtensionFromFilename = (filename: string): string => {
return filename.slice((filename.lastIndexOf(".") - 1 >>> 0) + 2)
}
+8
View File
@@ -0,0 +1,8 @@
export * from './isValidEmail'
export * from './sendEmail'
export * from './sleep'
export * from './validateLogin'
export * from './validateRegister'
export * from './toPostgresTime'
export * from './saveAttributes'
export * from './getExtensionFromFilename'
+2
View File
@@ -0,0 +1,2 @@
export const isValidEmail = (email: string): boolean =>
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)
+9
View File
@@ -0,0 +1,9 @@
export const saveAttributes = <E extends {}, I extends {}>(org: E & any, attrs: I) => {
const keys = Object.keys(attrs)
for (const key of keys) {
const value = attrs[key as keyof I]
if (typeof value != undefined && value != null) {
org[key as keyof I] = value
}
}
}
+24
View File
@@ -0,0 +1,24 @@
import nodemailer from 'nodemailer'
export async function sendEmail(to: string, html: string) {
// const transporter = nodemailer.createTransport({
// host: process.env.SMTP_HOST,
// port: process.env.SMTP_PORT,
// secure: (new Boolean(process.env.SMTP_SECURE).valueOf()),
// auth: {
// user: process.env.SMTP_USER,
// pass: process.env.SMTP_PASS
// }
// } as any)
// const info = await transporter.sendMail({
// from: 'comrootz@gmail.com',
// to: to,
// subject: 'Change password',
// html
// })
// console.log('Message sent: %s', info.messageId)
// console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info))
console.log('Message sent')
}
+1
View File
@@ -0,0 +1 @@
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
+4
View File
@@ -0,0 +1,4 @@
export const toPostgresTime = (cursor: string) => {
const date = (new Date(parseInt(cursor)))
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString()
}
+59
View File
@@ -0,0 +1,59 @@
import { isValidEmail } from '.'
import { FieldError } from '../graphql-types'
interface LoginValidation {
input: 'Email' | 'Username'
errors: FieldError[] | null
}
export const validateLogin = (emailOrUsername: string, password: string): LoginValidation => {
const result: LoginValidation = {
input: 'Email',
errors: []
}
if (emailOrUsername.includes('@')) { // email provided
if (!isValidEmail(emailOrUsername)) {
result.errors!.push({
field: 'emailOrUsername',
message: 'Invalid email.'
})
}
}
else { // username provided
result.input = 'Username'
if (emailOrUsername.length <= 2) {
result.errors!.push({
field: 'emailOrUsername',
message: 'Username must be longer than 2 characters.'
})
}
if (emailOrUsername.length > 32) {
result.errors!.push({
field: 'emailOrUsername',
message: 'Username must not be longer than 32 characters.'
})
}
if (!/^[A-Za-z0-9_-]*$/.test(emailOrUsername)) {
result.errors!.push({
field: 'emailOrUsername',
message: 'Username must contain only letters, numbers, underscores and dashes.'
})
}
}
if (password.length < 8) {
result.errors!.push({
field: 'password',
message: 'Password must be at least 8 characters.'
})
}
if (password.length > 128) {
result.errors!.push({
field: 'password',
message: 'Password must not be longer than 128 characters.'
})
}
return {
input: result.input,
errors: result.errors!.length > 0 ? result.errors : null
}
}
+43
View File
@@ -0,0 +1,43 @@
import { isValidEmail } from '.'
import { FieldError } from '../graphql-types'
export const validateRegister = (email: string, username: string, password: string): FieldError[] | null => {
const errors: FieldError[] = []
if (!isValidEmail(email)) {
errors.push({
field: 'email',
message: 'Invalid email.'
})
}
if (username.length <= 2) {
errors.push({
field: 'username',
message: 'Username must be longer than 2 characters.'
})
}
if (username.length > 32) {
errors.push({
field: 'username',
message: 'Username must not be longer than 32 characters.'
})
}
if (!/^[A-Za-z0-9_-]*$/.test(username)) {
errors.push({
field: 'username',
message: 'Username must contain only letters, numbers, underscores and dashes.'
})
}
if (password.length < 8) {
errors.push({
field: 'password',
message: 'Password must be at least 8 characters.'
})
}
if (password.length > 128) {
errors.push({
field: 'password',
message: 'Password must not be longer than 128 characters.'
})
}
return errors.length > 0 ? errors : null
}