feat(shared): reworked shared library

Shared library is now access using `@shared/module`
This commit is contained in:
maix0 2025-06-16 23:16:34 +02:00 committed by Maieul BOYER
parent 1bc33ab912
commit 37403d304a
56 changed files with 366 additions and 970 deletions

2
src/@shared/dist/auth.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
import { FastifyInstance } from 'fastify';
export default function (_fastify: FastifyInstance, _options: any): Promise<void>;

3
src/@shared/dist/auth.js vendored Normal file
View file

@ -0,0 +1,3 @@
export default async function (_fastify, _options) {
console.log("inside the plugin !");
}

23
src/@shared/dist/database/a.d.ts vendored Normal file
View file

@ -0,0 +1,23 @@
import { Result } from 'typescript-result';
import { UUIDv7 } from '@shared/uuid';
export declare class DBUserExists extends Error {
readonly type = "db-user-exists";
}
export declare class Database {
private db;
private st;
constructor(db_path: string);
destroy(): void;
private prepare;
createUser(user: string): Result<UUIDv7, DBUserExists>;
}
declare module 'fastify' {
interface FastifyInstance {
db: Database;
}
}
export type DatabaseOption = {
path: string;
};
export declare const uDatabase: import("fastify").FastifyPluginCallback<DatabaseOption, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
export default uDatabase;

43
src/@shared/dist/database/a.js vendored Normal file
View file

@ -0,0 +1,43 @@
import fp from 'fastify-plugin';
import sqlite from 'better-sqlite3';
import { Result } from 'typescript-result';
import initSql from "./init.sql.js";
import { newUUIDv7 } from '@shared/uuid';
export class DBUserExists extends Error {
type = 'db-user-exists';
}
export class Database {
db;
st = new Map();
constructor(db_path) {
this.db = sqlite(db_path, {});
this.db.pragma('journal_mode = WAL');
this.db.transaction(() => this.db.exec(initSql))();
}
destroy() {
this.st.clear();
this.db?.close();
}
prepare(query) {
let st = this.st.get(query);
if (st !== undefined)
return st;
st = this.db.prepare(query);
this.st.set(query, st);
return st;
}
createUser(user) {
const st = this.prepare('INSERT INTO users VALUES (?, ?) RETURNING id');
try {
st.get(newUUIDv7(), user);
}
catch (e) {
console.log(e);
console.log(typeof e);
}
return Result.ok(newUUIDv7());
}
}
export const uDatabase = fp(async function (_fastify, _options) {
});
export default uDatabase;

1
src/@shared/dist/database/index.d.ts vendored Normal file
View file

@ -0,0 +1 @@
export {};

1
src/@shared/dist/database/index.js vendored Normal file
View file

@ -0,0 +1 @@
export {};

View file

@ -0,0 +1,2 @@
declare const _default: "-- this file will make sure that the database is always up to date with the correct schema\n-- when editing this file, make sure to always include stuff like `IF NOT EXISTS` such as to not throw error\n-- NEVER DROP ANYTHING IN THIS FILE\nCREATE TABLE IF NOT EXISTS users (\n id STRING UNIQUE PRIMARY KEY, -- UUIDv7 as a string\n name STRING UNIQUE, -- name of the user\n token STRING UNIQUE, -- the token of the user (aka the cookie)\n);\n\n";
export default _default;

1
src/@shared/dist/database/init.sql.js vendored Normal file
View file

@ -0,0 +1 @@
export default "-- this file will make sure that the database is always up to date with the correct schema\n-- when editing this file, make sure to always include stuff like `IF NOT EXISTS` such as to not throw error\n-- NEVER DROP ANYTHING IN THIS FILE\nCREATE TABLE IF NOT EXISTS users (\n id STRING UNIQUE PRIMARY KEY, -- UUIDv7 as a string\n name STRING UNIQUE, -- name of the user\n token STRING UNIQUE, -- the token of the user (aka the cookie)\n);\n\n";

10
src/@shared/dist/uuid/index.d.ts vendored Normal file
View file

@ -0,0 +1,10 @@
import { Result } from "typescript-result";
export declare class InvalidUUID extends Error {
readonly type = "invalid-uuid";
}
export type UUIDv7 = string & {
readonly __brand: unique symbol;
};
export declare function isUUIDv7(value: string): value is UUIDv7;
export declare function toUUIDv7(value: string): Result<UUIDv7, InvalidUUID>;
export declare function newUUIDv7(): UUIDv7;

18
src/@shared/dist/uuid/index.js vendored Normal file
View file

@ -0,0 +1,18 @@
import { Result } from "typescript-result";
import { uuidv7 } from "uuidv7";
export class InvalidUUID extends Error {
type = 'invalid-uuid';
}
;
const uuidv7Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
export function isUUIDv7(value) {
return uuidv7Regex.test(value);
}
export function toUUIDv7(value) {
if (!isUUIDv7(value))
return Result.error(new InvalidUUID());
return Result.ok(value.toLowerCase());
}
export function newUUIDv7() {
return uuidv7();
}