From 514a2d20034f4f775f84d2f8873ad8f6c040b8c1 Mon Sep 17 00:00:00 2001 From: Lars Hampe Date: Wed, 23 Oct 2024 22:05:02 +0200 Subject: [PATCH] feat(api): add changelog public route --- apps/api/src/changelog/public/byId.ts | 54 ++++++++++++++++++++++++++ apps/api/src/changelog/public/index.ts | 27 +++++++++++++ apps/api/src/index.ts | 3 ++ 3 files changed, 84 insertions(+) create mode 100644 apps/api/src/changelog/public/byId.ts create mode 100644 apps/api/src/changelog/public/index.ts diff --git a/apps/api/src/changelog/public/byId.ts b/apps/api/src/changelog/public/byId.ts new file mode 100644 index 0000000..6a4241c --- /dev/null +++ b/apps/api/src/changelog/public/byId.ts @@ -0,0 +1,54 @@ +import { changelog, db } from '@boring.tools/database' +import { ChangelogByIdParams, ChangelogOutput } from '@boring.tools/schema' +import { createRoute } from '@hono/zod-openapi' +import { and, eq } from 'drizzle-orm' +import { HTTPException } from 'hono/http-exception' + +export const route = createRoute({ + method: 'get', + path: '/:id', + request: { + params: ChangelogByIdParams, + }, + responses: { + 200: { + content: { + 'application/json': { + schema: ChangelogOutput, + }, + }, + description: 'Return changelog by id', + }, + 400: { + description: 'Bad Request', + }, + 500: { + description: 'Internal Server Error', + }, + }, +}) + +export const func = async ({ id }: { id: string }) => { + const result = await db.query.changelog.findFirst({ + where: and(eq(changelog.id, id)), + with: { + versions: { + orderBy: (changelog_version, { desc }) => [ + desc(changelog_version.createdAt), + ], + }, + }, + }) + + if (!result) { + throw new HTTPException(404, { message: 'Not found' }) + } + + const { userId, createdAt, ...rest } = result + return rest +} + +export default { + route, + func, +} diff --git a/apps/api/src/changelog/public/index.ts b/apps/api/src/changelog/public/index.ts new file mode 100644 index 0000000..fc04ac9 --- /dev/null +++ b/apps/api/src/changelog/public/index.ts @@ -0,0 +1,27 @@ +import { OpenAPIHono } from '@hono/zod-openapi' +import type { Variables } from '../..' +import { type ContextModule, captureSentry } from '../../utils/sentry' +import ById from './byId' + +const app = new OpenAPIHono<{ Variables: Variables }>() + +const module: ContextModule = { + name: 'changelog', + sub_module: 'public', +} + +app.openapi(ById.route, async (c) => { + try { + const id = c.req.param('id') + const result = await ById.func({ id }) + return c.json(result, 200) + } catch (error) { + return captureSentry({ + c, + error, + module, + }) + } +}) + +export default app diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index f1217b7..657e417 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -5,6 +5,7 @@ import { apiReference } from '@scalar/hono-api-reference' import { cors } from 'hono/cors' import changelog from './changelog' +import changelogPublic from './changelog/public' import version from './changelog/version' import user from './user' @@ -32,6 +33,8 @@ app.route('/v1/user', user) app.route('/v1/changelog', changelog) app.route('/v1/changelog/version', version) +app.route('/v1/changelog/public', changelogPublic) + app.doc('/openapi.json', { openapi: '3.0.0', info: {