Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/andreijiroh-dev/api-servers. Monorepo for different Cloudflare Workers and Deno Deploy hosted APIs
Something went wrong. Try again.
14 kB · 477 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478import { DiscordInviteLink, GoLink, Prisma, PrismaClient } from "@prisma/client";import { PrismaD1 } from "@prisma/adapter-d1";import { EnvBindings } from "../types";import { Str } from "chanfana";import { getOffset, PAGE_SIZE } from "./utils";
/** * Get a list of golinks in batches of 10 from database. * @param db Should point to `c.env.golinks` * @param page Paging chores * @param isActive Filter by disabled links * @returns */export async function getGoLinks(db: EnvBindings["golinks"], page: number, isActive?: boolean, type?: "golinks" | "wikilinks" | null) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter }); // Assuming you have a Prisma client instance
const skip = getOffset(page); const take = PAGE_SIZE;
try { if (type == "wikilinks") { const result = await prisma.wikiLinks.findMany({ where: { is_active: isActive !== undefined ? isActive : undefined, // Filter by isActive if provided }, orderBy: { id: "desc", // Sort by newer IDs }, skip, take, }); return result; } else { const result = await prisma.goLink.findMany({ where: { is_active: isActive !== undefined ? isActive : undefined, // Filter by isActive if provided }, orderBy: { id: "desc", // Sort by newer IDs }, skip, take, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
/** * * @param db Always point to `c.env.golinks` * @param slug URL slug to query into * @param slug Either `golinks` or `wikilinks`, used for handling DB queries. * @returns */export async function getLink( db: EnvBindings["golinks"], slug: string, type?: "golinks" | "wikilinks" | null,): Promise<GoLink> | null { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { if (type == "wikilinks") { const result = prisma.wikiLinks.findUnique({ where: { slug: slug, }, }); return result; } else { const result = prisma.goLink.findUnique({ where: { slug: slug, }, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function addGoLink( db: EnvBindings["golinks"], slug: string, targetUrl: string, type?: "golinks" | "wikilinks" | null,): Promise<GoLink> | null { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { if (type == "wikilinks") { const result = prisma.wikiLinks.create({ data: { slug, targetUrl, }, }); return result; } else { const result = prisma.goLink.create({ data: { slug, targetUrl, }, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function updateGoLink( db: EnvBindings["golinks"], slug: string, targetUrl: string, type?: "golinks" | "wikilinks" | null, newSlug?: string,): Promise<GoLink> | null { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { if (type == "wikilinks") { const result = await prisma.wikiLinks.update({ where: { slug, }, data: { slug: newSlug !== undefined ? newSlug : slug, targetUrl, }, }); return result; } else { const result = await prisma.goLink.update({ where: { slug, }, data: { slug: newSlug !== undefined ? newSlug : slug, targetUrl, }, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function deprecateGoLink(db: EnvBindings["golinks"], slug: string, reason: string, type: "golinks" | "wikilinks" | null) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { if (type == "wikilinks") { const result = prisma.wikiLinks.update({ where: { slug, }, data: { is_active: false, deactivation_reason: reason, }, }); return result; } else { const result = prisma.goLink.update({ where: { slug, }, data: { is_active: false, deactivation_reason: reason, }, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function undeprecateGoLink(db: EnvBindings["golinks"], slug: string, type: "golinks" | "wikilinks" | null) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { if (type == "wikilinks") { const result = prisma.wikiLinks.update({ where: { slug, }, data: { is_active: true, deactivation_reason: null, }, }); return result; } else { const result = prisma.goLink.update({ where: { slug, }, data: { is_active: true, deactivation_reason: null, }, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function deleteGoLink(db: EnvBindings["golinks"], slug: string, type: "golinks" | "wikilinks" | null) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { if (type == "wikilinks") { const result = prisma.wikiLinks.delete({ where: { slug }, }); return result; } else { const result = prisma.goLink.delete({ where: { slug }, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
/** * Get a list of Discord invite codes in batches of 10 from database. * @param db Always point to `c.env.golinks` * @param page * @param isActive * @param isNsfw * @returns */export async function getDiscordInvites(db: EnvBindings["golinks"], page: number, isActive?: boolean, isNsfw?: boolean) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter }); // Assuming you have a Prisma client instance
const skip = getOffset(page); const take = PAGE_SIZE;
const result = await prisma.discordInviteLink.findMany({ where: { is_active: isActive !== undefined ? isActive : undefined, // Filter by isActive if provided nsfw: isNsfw !== undefined ? isNsfw : false, // Hide NSFW servers for safety by default. }, orderBy: { id: "desc", // Sort by newer IDs }, skip, take, }); return result;}
export async function getDiscordInvite(db: EnvBindings["golinks"], slug: string): Promise<DiscordInviteLink> | null { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { const result = await prisma.discordInviteLink.findUnique({ where: { slug, }, }); return result; } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function addDiscordInvite( db: EnvBindings["golinks"], slug: string, inviteCode: string, name: string, description: string, is_active?: boolean, nsfw?: boolean,) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter }); try { const result = prisma.discordInviteLink.create({ data: { slug, inviteCode, name, description, is_active: is_active !== undefined ? is_active : true, nsfw: nsfw !== undefined ? nsfw : false, }, }); return result; } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function updateDiscordLink( db: EnvBindings["golinks"], slug: string, inviteCode: string, name?: string, description?: string,) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { const existingData = await prisma.discordInviteLink.findUnique({ where: { slug }, });
if (existingData) { const result = await prisma.discordInviteLink.update({ where: { slug, }, data: { inviteCode, name: name !== undefined ? name : existingData.name, description: description !== undefined ? description : existingData.description, }, }); return result; } } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}
export async function deleteDiscordLink(db: EnvBindings["golinks"], slug: string) { const adapter = new PrismaD1(db); const prisma = new PrismaClient({ adapter });
try { const result = prisma.discordInviteLink.delete({ where: { slug }, }); return result; } catch (error) { if (error instanceof Prisma.PrismaClientKnownRequestError) { console.error(`[prisma-client] known client error: ${error.code} - ${error.message}`);
if (error.code === "P2002") { return Promise.reject(new Error("A Discord invite code with that slug already exists.")); }
return Promise.reject(new Error("A error occurred while querying the database.")); } else { console.error(`[prisma-client]- Unexpected error`, error); return Promise.reject(new Error("An unexpected error occurred.")); } }}