diff --git a/apps/golinks-v2/src/api/golinks.ts b/apps/golinks-v2/src/api/golinks.ts index 6b388d7..2c0044e 100644 --- a/apps/golinks-v2/src/api/golinks.ts +++ b/apps/golinks-v2/src/api/golinks.ts @@ -1,13 +1,25 @@ -import { OpenAPIRoute, Num, Bool, Str } from "chanfana" +import { OpenAPIRoute, Num, Bool, Str, contentJson } from "chanfana" import { z } from "zod" import { GoLinks } from "types" import { addGoLink, generateSlug, getGoLinks } from "lib/db"; +import { adminApiKey } from "lib/constants"; +import { Context } from "hono"; export class GoLinkCreate extends OpenAPIRoute { schema = { - tags: ['golinks'], summary: 'Create a golink', + parameters: [ + { + in: 'header', + name: 'X-Golinks-Admin-Key', + schema: { + type: 'string', + }, + required: true, + description: 'Admin API key', + }, + ], request: { body: { content: { @@ -28,22 +40,65 @@ export class GoLinkCreate extends OpenAPIRoute { ], responses: { '200': { - description: 'Returns the generated golink', + description: 'Returns the generated golink information', + contentJson: { + schema: z.object({ + success: Bool(), + result: GoLinks + }) + } + }, + '400': { + description: 'Existing entry found', + content: { + 'application/json': { + schema: z.object({ + success: Bool({ default: false }).default(false), + error: Str({ default: 'Existing entry found (error code: PrismaClientKnownRequestError:P2002)' }), + }), + }, + }, }, }, }; - async handle(c) { + async handle(c: Context) { const data = await this.getValidatedData(); const linkToCreate = data.body; - const slug = data.body.slug !== null ? data.body.slug : generateSlug(12); + console.log(`[golinks-api] received body for link creation ${JSON.stringify(linkToCreate)}`); + const slug = linkToCreate.slug !== undefined ? linkToCreate.slug : generateSlug(12); - const result = await addGoLink(c.env.golinks, slug, linkToCreate.targetUrl, linkToCreate.is_active); + const result = addGoLink(c.env.golinks, slug, linkToCreate.targetUrl, linkToCreate.is_active) + .then((value) => { + return { + success: true, + result: value, + }; + }) + .catch((err) => { + if (err.code == 'P2002') { + return c.newResponse( + JSON.stringify({ + success: false, + error: `Existing entry found (error code: ${err.name}:${err.code})`, + }), + 400, + { 'Content-Type': 'application/json' }, + ); + } else { + return c.newResponse( + JSON.stringify({ + success: false, + error: 'Something went wrong on the backend. The devDebug object should provide hints for devs on where to fix.', + devDebug: err, + }), + 500, + { 'Content-Type': 'application/json' }, + ); + } + }); - return { - success: true, - data: linkToCreate, - }; + return result; } } @@ -91,7 +146,7 @@ export class GoLinkList extends OpenAPIRoute { return { success: true, - links - } + result: links, + }; } } diff --git a/apps/golinks-v2/worker-configuration.d.ts b/apps/golinks-v2/worker-configuration.d.ts index f3f482e..a9999f7 100644 --- a/apps/golinks-v2/worker-configuration.d.ts +++ b/apps/golinks-v2/worker-configuration.d.ts @@ -1,6 +1,7 @@ -// Generated by Wrangler on Tue Jul 16 2024 13:58:56 GMT+0800 (Philippine Standard Time) +// Generated by Wrangler on Wed Jul 17 2024 01:25:40 GMT+0800 (Philippine Standard Time) // by running `wrangler types` -interface Env { +export interface Env { + DEPLOY_ENV: 'production' | 'staging' | 'development'; golinks: D1Database; }