update cuz presentation
This commit is contained in:
parent
7a9e451739
commit
4e67cc4e19
29 changed files with 1065 additions and 88 deletions
|
@ -5,7 +5,7 @@ import { ResultSetHeader } from "mysql2/promise";
|
|||
|
||||
import { data, database } from "./database";
|
||||
import { isString } from "./isString";
|
||||
import Snowflake from "./snowflake";
|
||||
import Snowflake from "~/utils/snowflake";
|
||||
import { client } from "~/utils/types/database";
|
||||
|
||||
type queryType = {
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
import mysql from "mysql2/promise";
|
||||
import mysql, { Connection } from "mysql2/promise";
|
||||
|
||||
export const database = await mysql.createConnection({
|
||||
const connectionOptions: mysql.ConnectionOptions = {
|
||||
host: process.env.DB_HOST,
|
||||
port: Number(process.env.DB_PORT),
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_SCHEMA,
|
||||
});
|
||||
decimalNumbers: true,
|
||||
supportBigNumbers: true,
|
||||
};
|
||||
|
||||
export const database =
|
||||
await mysql.createConnection(connectionOptions) as Connection & { new: () => Promise<Connection> };
|
||||
database.new = () => { return mysql.createConnection(connectionOptions); };
|
||||
|
||||
export type data<T> = [T[], mysql.FieldPacket[]];
|
||||
|
|
25
server/utils/getRequestingUser.ts
Normal file
25
server/utils/getRequestingUser.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* global getCookie, createError */
|
||||
import { H3Event } from "h3";
|
||||
|
||||
import { database, data } from "./database";
|
||||
import { user } from "~/utils/types/database";
|
||||
|
||||
export default async function getRequestingUser(e: H3Event) {
|
||||
const cookie = getCookie(e, "token");
|
||||
const [[user]] = await database.query(
|
||||
["SELECT",
|
||||
"CONVERT(`users`.`id`, CHAR) as `id`,",
|
||||
"`users`.`username`,",
|
||||
"`users`.`email`,",
|
||||
"`users`.`display_name`",
|
||||
"FROM",
|
||||
"`sessions`",
|
||||
"LEFT JOIN `users` ON `sessions`.`user` = `users`.`id`",
|
||||
"WHERE `sessions`.`id` = ?",
|
||||
].join(" "),
|
||||
[cookie],
|
||||
) as data<user>;
|
||||
|
||||
if (!user) throw createError("User not found");
|
||||
return user;
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
export default class Snowflake {
|
||||
static current_increment = 0n;
|
||||
|
||||
public static increment() {
|
||||
this.current_increment = BigInt.asUintN(12, this.current_increment + 1n);
|
||||
return this.current_increment;
|
||||
}
|
||||
|
||||
state = 0n;
|
||||
public set_timestamp(value: number | bigint) {
|
||||
value = BigInt.asUintN(64 - 22, BigInt(value));
|
||||
const state = BigInt.asUintN(22, this.state);
|
||||
this.state = state + (value << 22n);
|
||||
}
|
||||
|
||||
public set_machineid(value: number | bigint) {
|
||||
value = BigInt.asUintN(12 - 17, BigInt(value));
|
||||
const state = BigInt.asUintN(17, this.state) + (this.state >> 22n) << 22n;
|
||||
this.state = state + (value << 12n);
|
||||
}
|
||||
|
||||
public set_processid(value: number | bigint) {
|
||||
value = BigInt.asUintN(17 - 12, BigInt(value));
|
||||
const state = BigInt.asUintN(12, this.state) + (this.state >> 17n) << 17n;
|
||||
this.state = state + (value << 12n);
|
||||
}
|
||||
|
||||
public set_increment(value: number | bigint) {
|
||||
value = BigInt.asUintN(12 - 0, BigInt(value));
|
||||
const state = (this.state >> 12n) << 12n;
|
||||
this.state = state + (value << 0n);
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.set_timestamp(Date.now());
|
||||
this.set_processid(1);
|
||||
this.set_increment(Snowflake.increment());
|
||||
}
|
||||
|
||||
public toString() {
|
||||
return this.state.toString();
|
||||
}
|
||||
}
|
31
server/utils/validation.ts
Normal file
31
server/utils/validation.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* global createError */
|
||||
|
||||
export function createValidationError(errors: Map<string, string>) {
|
||||
let message = "Invalid parameters: ";
|
||||
for (const i in errors)
|
||||
message += i + ", ";
|
||||
message = message.slice(0, -2);
|
||||
return createError({
|
||||
statusCode: 400,
|
||||
message,
|
||||
data: {
|
||||
errors: Object.fromEntries(errors),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function handleRecursedValidationError(e: unknown, errors: Map<string, string>, element: string) {
|
||||
if (typeof e !== "object") throw e;
|
||||
if (!e) throw e;
|
||||
if (!(e as any).data || !(e as any).message) throw e;
|
||||
const upstreamErrors = (e as any).data.errors as any;
|
||||
const upstreamMessage = (e as any).message;
|
||||
if (upstreamErrors) {
|
||||
for (const j in upstreamErrors)
|
||||
errors.set(`${element}.${j}`, String(upstreamErrors[j]));
|
||||
} else if (upstreamMessage) {
|
||||
errors.set(`${element}`, String(upstreamMessage));
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue