@sigiljs-community/swagger-ui-plugin
Version:
Plugin for SigilJS framework, that generates and serves Swagger UI at a configurable endpoint
43 lines (42 loc) • 1.33 kB
JavaScript
import { OpenApiPlugin as r } from "@sigiljs-community/openapi-plugin";
import { SigilPlugin as o } from "@sigiljs/sigil";
import p from "node:path";
import l from "./gen/generateHtml.mjs";
class m extends o {
static name = "SwaggerUiPlugin";
#t;
#i;
#s = "";
constructor() {
super(), this.#i = require("swagger-ui-dist"), this.#t = (this.$pluginConfig.path ?? "/docs/").trim(), this.#t.endsWith("/") || (this.#t += "/");
}
onInitialize() {
if (!this.#i) return;
const e = this.#i.absolutePath();
this.sigil.addMiddleware(async (i, t, n) => {
if (i.path.startsWith(this.#t.slice(0, -1))) {
if ([".js", ".css"].some((s) => i.path.endsWith(s))) {
const s = t.fileResponse(p.join(e, i.path.slice(this.#t.length)));
return n.headers && Object.entries(n.headers).forEach(([a, h]) => {
s.headers.set(a, h);
}), s;
}
return t.rawResponse(this.#s, { ...n.headers || {}, "content-type": "text/html" });
}
});
}
async onUpdateCallback() {
const e = this.sigil.plugin(r), { path: i, ...t } = this.$pluginConfig;
this.#s = await l({
...t,
base: this.#t,
swaggerInitOptions: {
...t.swaggerInitOptions,
swaggerDoc: e.openApiDefinition
}
});
}
}
export {
m as default
};