diff --git a/apps/golinks-v2/src/api/discord.ts b/apps/golinks-v2/src/api/discord.ts index aa81cbb..0d69480 100644 --- a/apps/golinks-v2/src/api/discord.ts +++ b/apps/golinks-v2/src/api/discord.ts @@ -49,17 +49,17 @@ export class DiscordInviteLinkCreate extends OpenAPIRoute { }, }, }, - "401": { - description: "Missing or invalid admin token", - content: { - "application/json": { - schema: z.object({ - success: z.boolean().default(false), - error: z.string().default("Unauthorized") - }) - } - } - }, + "401": { + description: "Missing or invalid admin token", + content: { + "application/json": { + schema: z.object({ + success: z.boolean().default(false), + error: z.string().default("Unauthorized"), + }), + }, + }, + }, "409": { description: "Returns a error when the slug is already in use.", content: { diff --git a/apps/golinks-v2/src/api/golinks.ts b/apps/golinks-v2/src/api/golinks.ts index b88bef6..3455f88 100644 --- a/apps/golinks-v2/src/api/golinks.ts +++ b/apps/golinks-v2/src/api/golinks.ts @@ -66,7 +66,6 @@ export class GoLinkCreate extends OpenAPIRoute { }; async handle(c: Context) { - const data = await this.getValidatedData(); const linkToCreate = data.body; console.log(`[golinks-api] received body for link creation ${JSON.stringify(linkToCreate)}`); diff --git a/apps/golinks-v2/src/index.ts b/apps/golinks-v2/src/index.ts index 46f791b..5171f84 100644 --- a/apps/golinks-v2/src/index.ts +++ b/apps/golinks-v2/src/index.ts @@ -4,7 +4,7 @@ import { Context, Hono } from "hono"; import { cors } from "hono/cors"; import { EnvBindings, Env } from "./types"; import { getDiscordInvite, getLink } from "lib/db"; -import { adminApiKey, contact, getWorkersDashboardUrl, homepage, servers } from "lib/constants"; +import { adminApiKey, contact, getWorkersDashboardUrl, homepage, servers, errorMessages } from "lib/constants"; import { DiscordInviteLinkCreate, DiscordInviteLinkList } from "api/discord"; import { adminApiKeyAuth } from "lib/auth"; @@ -19,12 +19,12 @@ app.use( return origin; } }, - allowHeaders: ["X-Golinks-Admin-Key", "E-Tag"], + allowHeaders: ["X-Golinks-Admin-Key", "E-Tag", "Content-Type", "Authorization"], allowMethods: ["GET", "POST", "PATCH", "DELETE", "HEAD"], credentials: true, }), ); -app.use("/api/*", adminApiKeyAuth) +app.use("/api/*", adminApiKeyAuth); // Setup OpenAPI registry const openapi = fromHono(app, { @@ -98,7 +98,7 @@ app.get("/discord/:inviteCode", async (c) => { return c.redirect(`https://discord.gg/${result.inviteCode}`); } catch (error) { - console.error(`[prisma-client]`, error) + console.error(`[prisma-client]`, error); return c.newResponse(errorMessages.discordServerNotFound, 404); } }); diff --git a/apps/golinks-v2/src/lib/auth.ts b/apps/golinks-v2/src/lib/auth.ts index c1babc8..1ccab7a 100644 --- a/apps/golinks-v2/src/lib/auth.ts +++ b/apps/golinks-v2/src/lib/auth.ts @@ -1,19 +1,22 @@ import { Context, Next } from "hono"; export async function adminApiKeyAuth(c: Context, next: Next) { - if (c.req.method == "GET" || c.req.method == "HEAD") { - return await next() - } + if (c.req.method == "GET" || c.req.method == "HEAD") { + return await next(); + } - const adminApiKey = c.env.ADMIN_KEY; - const apiKeyHeader = c.req.header("X-Golinks-Admin-Key") - console.debug(`[auth] ${adminApiKey}:${apiKeyHeader}`) + const adminApiKey = c.env.ADMIN_KEY; + const apiKeyHeader = c.req.header("X-Golinks-Admin-Key"); + console.debug(`[auth] ${adminApiKey}:${apiKeyHeader}`); - if (!apiKeyHeader || apiKeyHeader !== adminApiKey) { - return c.json({ - success: false, - error: "Unauthorized" - }, 401) - } - return await next() + if (!apiKeyHeader || apiKeyHeader !== adminApiKey) { + return c.json( + { + success: false, + error: "Unauthorized", + }, + 401, + ); + } + return await next(); } diff --git a/apps/golinks-v2/src/lib/constants.ts b/apps/golinks-v2/src/lib/constants.ts index f23e900..9aaabf2 100644 --- a/apps/golinks-v2/src/lib/constants.ts +++ b/apps/golinks-v2/src/lib/constants.ts @@ -42,7 +42,6 @@ export const servers = [ }, ]; - export const errorMessages = { discordServerNotFound: `\ Either that server is not on our records or something went wrong on our side. diff --git a/apps/golinks-v2/src/lib/db.ts b/apps/golinks-v2/src/lib/db.ts index 6061b38..6993b9b 100644 --- a/apps/golinks-v2/src/lib/db.ts +++ b/apps/golinks-v2/src/lib/db.ts @@ -38,29 +38,27 @@ export async function getGoLinks(db: EnvBindings["golinks"], page: number, const take = PAGE_SIZE; try { - 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.")) - } - } + 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.")); + } + } } /** @@ -152,31 +150,29 @@ export async function addDiscordInvite( 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) { + 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.") - ) - } + 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.")); } - } + } } diff --git a/apps/golinks-v2/src/types.ts b/apps/golinks-v2/src/types.ts index 32f9aed..2078cc0 100644 --- a/apps/golinks-v2/src/types.ts +++ b/apps/golinks-v2/src/types.ts @@ -33,11 +33,11 @@ export const DiscordInvites = z.object({ export interface Env { DEPLOY_ENV: "production" | "staging" | "development"; golinks: D1Database; - ADMIN_KEY: string; + ADMIN_KEY: string; } export type EnvBindings = { golinks: D1Database; DEPLOY_ENV: "production" | "staging" | "development"; - ADMIN_KEY: string + ADMIN_KEY: string; };