|
| 1 | +import { Hono, HonoRequest } from "hono"; |
| 2 | +import { StatusCode } from "hono/utils/http-status"; |
| 3 | +import qs from "qs"; |
| 4 | +import { |
| 5 | + allEndpoints, |
| 6 | + AnyAPIDescription, |
| 7 | + AnyEndpoint, |
| 8 | + isStlError, |
| 9 | + NotFoundError, |
| 10 | +} from "stainless"; |
| 11 | +import { isValidRouteMatch, makeRouteMatcher } from "./routeMatcher"; |
| 12 | + |
| 13 | +export type HonoServerContext = { |
| 14 | + type: "hono"; |
| 15 | + args: [HonoRequest, Response]; |
| 16 | +}; |
| 17 | + |
| 18 | +declare module "stainless" { |
| 19 | + interface StlContext<EC extends AnyBaseEndpoint> { |
| 20 | + server: HonoServerContext; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +const methods = ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]; |
| 25 | + |
| 26 | +function makeApp(endpoints: AnyEndpoint[]) { |
| 27 | + const stl = endpoints[0]?.stl; |
| 28 | + if (!stl) { |
| 29 | + throw new Error(`endpoints[0].stl must be defined`); |
| 30 | + } |
| 31 | + |
| 32 | + const app = new Hono(); |
| 33 | + const routeMatcher = makeRouteMatcher(endpoints); |
| 34 | + |
| 35 | + return app.all("*", async (c) => { |
| 36 | + try { |
| 37 | + const match = routeMatcher.match(c.req.method, c.req.path); |
| 38 | + const { search } = new URL(c.req.url); |
| 39 | + |
| 40 | + if (!isValidRouteMatch(match)) { |
| 41 | + const enabledMethods = methods.filter((method) => |
| 42 | + isValidRouteMatch(routeMatcher.match(method, c.req.path)) |
| 43 | + ); |
| 44 | + if (enabledMethods.length) { |
| 45 | + return c.json( |
| 46 | + { |
| 47 | + message: `No handler for ${c.req.method}; only ${enabledMethods |
| 48 | + .map((x) => x.toUpperCase()) |
| 49 | + .join(", ")}.`, |
| 50 | + }, |
| 51 | + { status: 405 } |
| 52 | + ); |
| 53 | + } |
| 54 | + throw new NotFoundError(); |
| 55 | + } |
| 56 | + |
| 57 | + const [endpoint, path] = match[0][0]; |
| 58 | + const server: HonoServerContext = { |
| 59 | + type: "hono", |
| 60 | + args: [c.req, c.res], |
| 61 | + }; |
| 62 | + |
| 63 | + const context = stl.initContext({ |
| 64 | + endpoint, |
| 65 | + headers: c.req.header(), |
| 66 | + server, |
| 67 | + }); |
| 68 | + |
| 69 | + const params = stl.initParams({ |
| 70 | + path, |
| 71 | + query: search ? qs.parse(search.replace(/^\?/, "")) : {}, |
| 72 | + body: await c.req.json().catch(() => undefined), |
| 73 | + headers: c.req.header(), |
| 74 | + }); |
| 75 | + |
| 76 | + const result = await stl.execute(params, context); |
| 77 | + |
| 78 | + return c.json(result); |
| 79 | + } catch (error) { |
| 80 | + if (isStlError(error)) { |
| 81 | + return c.json(error.response, error.statusCode as StatusCode); |
| 82 | + } |
| 83 | + |
| 84 | + console.error( |
| 85 | + `ERROR in ${c.req.method} ${c.req.url}:`, |
| 86 | + error instanceof Error ? error.stack : error |
| 87 | + ); |
| 88 | + return c.json({ error, details: "Failed to handle the request." }, 500); |
| 89 | + } |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +export function apiRoute({ topLevel, resources }: AnyAPIDescription) { |
| 94 | + return makeApp( |
| 95 | + allEndpoints({ |
| 96 | + actions: topLevel?.actions, |
| 97 | + namespacedResources: resources, |
| 98 | + }) |
| 99 | + ); |
| 100 | +} |
0 commit comments