|
| 1 | +import { |
| 2 | + TYPED_HANDLERS_ERROR_STATUS_CODES_BY_KEY, |
| 3 | + TypedHandlersError, |
| 4 | +} from "@asyncstatus/typed-handlers"; |
| 5 | +import { typedHandlersHonoServer } from "@asyncstatus/typed-handlers/hono"; |
| 6 | +import { Hono } from "hono"; |
| 7 | +import { cors } from "hono/cors"; |
| 8 | +import type { ContentfulStatusCode } from "hono/utils/http-status"; |
| 9 | +import { createContext, type HonoEnv } from "./lib/env"; |
| 10 | +import { |
| 11 | + getChangelogBySlugHandler, |
| 12 | + listChangelogsByRepoHandler, |
| 13 | + listReposByOwnerHandler, |
| 14 | + startChangelogGenerationHandler, |
| 15 | +} from "./typed-handlers/changelog-handlers"; |
| 16 | + |
| 17 | +const app = new Hono<HonoEnv>() |
| 18 | + .use( |
| 19 | + "*", |
| 20 | + cors({ |
| 21 | + origin: (origin, c) => { |
| 22 | + if (c.env.NODE_ENV === "development") { |
| 23 | + return origin; |
| 24 | + } |
| 25 | + if (origin.endsWith("asyncstatus.com")) { |
| 26 | + return origin; |
| 27 | + } |
| 28 | + if (origin.endsWith("changelogs.ai")) { |
| 29 | + return origin; |
| 30 | + } |
| 31 | + if (origin.endsWith("chlgs.ai")) { |
| 32 | + return origin; |
| 33 | + } |
| 34 | + return "https://changelogs.ai"; |
| 35 | + }, |
| 36 | + allowHeaders: [ |
| 37 | + "Content-Type", |
| 38 | + "Authorization", |
| 39 | + "Content-Disposition", |
| 40 | + "Content-Length", |
| 41 | + "ETag", |
| 42 | + "Cache-Control", |
| 43 | + ], |
| 44 | + allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"], |
| 45 | + maxAge: 600, |
| 46 | + credentials: true, |
| 47 | + }), |
| 48 | + ) |
| 49 | + .use(async (c, next) => { |
| 50 | + const context = await createContext(c); |
| 51 | + c.set("db", context.db); |
| 52 | + c.set("resend", context.resend); |
| 53 | + c.set("anthropicClient", context.anthropicClient); |
| 54 | + c.set("openRouterProvider", context.openRouterProvider); |
| 55 | + c.set("voyageClient", context.voyageClient); |
| 56 | + c.set("workflow", context.workflow); |
| 57 | + c.set("slack", context.slack); |
| 58 | + c.set("discord", context.discord); |
| 59 | + c.set("betterAuthUrl", context.betterAuthUrl); |
| 60 | + c.set("github", context.github); |
| 61 | + c.set("gitlab", context.gitlab); |
| 62 | + c.set("linear", context.linear); |
| 63 | + return next(); |
| 64 | + }) |
| 65 | + .onError((err, c) => { |
| 66 | + console.error(err); |
| 67 | + if (err instanceof TypedHandlersError) { |
| 68 | + return c.json( |
| 69 | + { type: err.name, message: err.message, code: err.code, cause: err.cause }, |
| 70 | + TYPED_HANDLERS_ERROR_STATUS_CODES_BY_KEY[ |
| 71 | + err.code ?? "INTERNAL_SERVER_ERROR" |
| 72 | + ] as ContentfulStatusCode, |
| 73 | + ); |
| 74 | + } |
| 75 | + return c.json({ message: "Unknown error", code: "INTERNAL_SERVER_ERROR", cause: err }, 500); |
| 76 | + }); |
| 77 | + |
| 78 | +const typedHandlersApp = typedHandlersHonoServer( |
| 79 | + app, |
| 80 | + [ |
| 81 | + listChangelogsByRepoHandler, |
| 82 | + listReposByOwnerHandler, |
| 83 | + getChangelogBySlugHandler, |
| 84 | + startChangelogGenerationHandler, |
| 85 | + ], |
| 86 | + { |
| 87 | + getContext: (c) => ({ |
| 88 | + db: c.get("db"), |
| 89 | + resend: c.get("resend"), |
| 90 | + anthropicClient: c.get("anthropicClient"), |
| 91 | + openRouterProvider: c.get("openRouterProvider"), |
| 92 | + voyageClient: c.get("voyageClient"), |
| 93 | + discord: c.get("discord"), |
| 94 | + slack: c.get("slack"), |
| 95 | + changelogAppUrl: c.env.CHANGELOG_APP_URL, |
| 96 | + workflow: c.get("workflow"), |
| 97 | + betterAuthUrl: c.env.BETTER_AUTH_URL, |
| 98 | + github: c.get("github"), |
| 99 | + gitlab: c.get("gitlab"), |
| 100 | + linear: c.get("linear"), |
| 101 | + }), |
| 102 | + }, |
| 103 | +); |
| 104 | + |
| 105 | +export default { |
| 106 | + fetch: typedHandlersApp.fetch, |
| 107 | +}; |
| 108 | +export type App = typeof app; |
| 109 | + |
| 110 | +export { ChangelogGenerationJobWorkflow } from "./workflows/github/changelog-generation-job"; |
0 commit comments