diff --git a/packages/function/package.json b/packages/function/package.json index 72a47a82..36524481 100644 --- a/packages/function/package.json +++ b/packages/function/package.json @@ -1,6 +1,7 @@ { "name": "@nestri/function", "type": "module", + "version":"0.0.1", "private": true, "devDependencies": { "@cloudflare/workers-types": "catalog:", diff --git a/packages/function/src/api/index.ts b/packages/function/src/api/index.ts index f53b3bd2..a6e4bb18 100644 --- a/packages/function/src/api/index.ts +++ b/packages/function/src/api/index.ts @@ -1,9 +1,11 @@ import { Hono } from "hono"; import { auth } from "./utils/auth"; import { logger } from "hono/logger"; +import { openAPIRouteHandler } from "hono-openapi"; import { Log } from "@nestri/core/utils/log"; import { HTTPException } from "hono/http-exception"; import { VisibleError, ErrorCodes } from "@nestri/core/error"; +import packageJson from "../../package.json" assert { type: "json" }; const log = Log.create({ service: "api" }); @@ -16,33 +18,72 @@ app }) .use(auth); -app.onError((error, c) => { - // Handle our custom VisibleError - if (error instanceof VisibleError) { - return c.json(error.toResponse(), error.statusCode()); - } +export const routes = app + .get("/", async (c) => { + return c.text("ok"); + }) + .onError((error, c) => { + // Handle our custom VisibleError + if (error instanceof VisibleError) { + return c.json(error.toResponse(), error.statusCode()); + } - // Handle HTTP exceptions - if (error instanceof HTTPException) { - console.error("http error:", error); + // Handle HTTP exceptions + if (error instanceof HTTPException) { + console.error("http error:", error); + return c.json( + { + type: "validation", + code: ErrorCodes.Validation.INVALID_PARAMETER, + message: "Invalid request", + }, + 400, + ); + } + + // Handle any other errors as internal server errors + log.error("Unhandled API error:", { error }); return c.json( { - type: "validation", - code: ErrorCodes.Validation.INVALID_PARAMETER, - message: "Invalid request", + type: "internal", + code: ErrorCodes.Server.INTERNAL_ERROR, + message: "Internal server error", }, - 400, + 500, ); - } + }); - // Handle any other errors as internal server errors - log.error("Unhandled API error:", { error }); - return c.json( - { - type: "internal", - code: ErrorCodes.Server.INTERNAL_ERROR, - message: "Internal server error", +app.get( + "/doc", + openAPIRouteHandler(routes, { + documentation: { + info: { + title: "Nestri API", + termsOfService: "https://nestri.io/terms", + description: + "The Nestri API gives you the power deploy and stream games/apps in the cloud. Run on our GPUs or bring your own.", + version: packageJson.version ?? "1.0.0", + }, + components: { + securitySchemes: { + Bearer: { + type: "http", + scheme: "bearer", + bearerFormat: "JWT", + }, + MemberID: { + type: "apiKey", + description: "The member ID to use for this query", + in: "header", + name: "x-nestri-member", + }, + }, + }, + security: [{ Bearer: [], MemberID: [] }], + servers: [ + { description: "Production", url: "https://api.nestri.io" }, + { description: "Sandbox", url: "https://api.dev.nestri.io" }, + ], }, - 500, - ); -}); + }), +);