Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit ebf0790

Browse files
bump version to 1.0.4, add getDomainUrl function for dynamic domain handling, and update server routes to utilize the new function
1 parent 0ab10cf commit ebf0790

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

dist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moodle-openapi-server",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Moddle OpenAPI server",
55
"private": true,
66
"type": "module",

dist/src/get-domain-url.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* get the domain url from the request
3+
*/
4+
export function getDomainUrl(request, options) {
5+
const host = request.headers.get("X-Forwarded-Host") ??
6+
request.headers.get("Host") ??
7+
options?.defaultHost;
8+
if (!host) {
9+
throw new Error("Host is required");
10+
}
11+
const protocol = options?.protocol ?? (host.includes("localhost") ? "http" : "https");
12+
return `${protocol}://${host}`;
13+
}

dist/src/server.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HTTPException } from "hono/http-exception";
66
import { basicAuth } from "hono/basic-auth";
77
import { parseUsers } from "./parse-users.js";
88
import { html } from "hono/html";
9+
import { getDomainUrl } from "./get-domain-url.js";
910
const baseURL = process.env.MOODLE_BASE_URL;
1011
if (!baseURL) {
1112
throw new Error("MOODLE_BASE_URL is not set");
@@ -40,6 +41,13 @@ const moodleAuth = async (c, next) => {
4041
const app = new Hono();
4142
app.use(cors());
4243
app.use(logger());
44+
app.use(async (c, next) => {
45+
const domainUrl = getDomainUrl(c.req.raw, {
46+
defaultHost: "localhost:3000",
47+
});
48+
c.set("domainUrl", domainUrl);
49+
await next();
50+
});
4351
// Apply middleware to all /api routes
4452
app.use("/api/*", moodleAuth);
4553
app.post("/api/usecases/:function", async (c) => {
@@ -127,22 +135,33 @@ app.get("/meta", async (c) => {
127135
// swagger 2.0 docs
128136
app.get("/docs/swagger/:id", (c) => {
129137
const id = c.req.param("id");
138+
const domainUrl = c.get("domainUrl");
130139
const swaggerDocs = swagger2.filter(Boolean);
131140
const doc = swaggerDocs[Number(id)];
132141
if (!doc) {
133142
return c.json({ error: true, message: "Invalid id" }, { status: 400 });
134143
}
144+
// we need to update the basePath in the swagger doc
145+
doc.host = domainUrl;
135146
return c.json(doc, { status: 200 });
136147
});
137148
app.get("/docs/openapi_3_1/:id{.+\\.json}", (c) => {
138149
const id = c.req.param("id");
150+
const domainUrl = c.get("domainUrl");
139151
// remove the .json from the id
140152
const idWithoutJson = id.replace(".json", "");
141153
const openapi31Docs = [openapi31, usecaseOpenapi].filter(Boolean);
142154
const doc = openapi31Docs[Number(idWithoutJson)];
143155
if (!doc) {
144156
return c.json({ error: true, message: "Invalid id" }, { status: 400 });
145157
}
158+
// we need to update the servers in the openapi doc
159+
doc.servers = [
160+
{
161+
url: `${domainUrl}/api`,
162+
description: "Moodle webservice API",
163+
},
164+
];
146165
return c.json(doc, { status: 200 });
147166
});
148167
app.get("/docs/openapi_3_1/:id", (c) => {

0 commit comments

Comments
 (0)