schema.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import express from "express";
  2. import { express as logger } from "../logger.js";
  3. import PACKAGE from "../package.json" with { type: "json" };
  4. import { getCompiledSchema } from "../schema/index.js";
  5. const router = express.Router({
  6. caseSensitive: true,
  7. strict: true,
  8. mergeParams: true,
  9. });
  10. router
  11. .route("/")
  12. .options((_, res) => {
  13. res.sendStatus(204);
  14. })
  15. /**
  16. * GET /schema
  17. */
  18. .get(async (req, res) => {
  19. try {
  20. const swaggerJSON = await getCompiledSchema();
  21. let proto = req.protocol;
  22. if (typeof req.headers["x-forwarded-proto"] !== "undefined" && req.headers["x-forwarded-proto"]) {
  23. proto = req.headers["x-forwarded-proto"];
  24. }
  25. let origin = `${proto}://${req.hostname}`;
  26. if (typeof req.headers.origin !== "undefined" && req.headers.origin) {
  27. origin = req.headers.origin;
  28. }
  29. swaggerJSON.info.version = PACKAGE.version;
  30. swaggerJSON.servers[0].url = `${origin}/api`;
  31. res.status(200).send(swaggerJSON);
  32. } catch (err) {
  33. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  34. next(err);
  35. }
  36. });
  37. export default router;