@sigiljs-community/openapi-plugin
Version:
Plugin for SigilJS framework, that automatically derives an OpenAPI JSON spec from your routes and serves it at a configurable endpoint
43 lines (42 loc) • 1.44 kB
JavaScript
import { OpenApiTransformer as p } from "@sigiljs/openapi-transformer";
import { SigilPlugin as r } from "@sigiljs/sigil";
import { RawResponse as h } from "@sigiljs/sigil/responses";
import { nonNullable as o, sanitizePath as l } from "@sigiljs/sigil/utils";
class d extends r {
static name = "OpenApiPlugin";
openApiDefinition = {};
#i;
#t;
constructor() {
super();
const { info: s, transform: e, path: t } = this.$pluginConfig;
t !== !1 && (this.#t = o((t ?? "/swagger.json").split("/")).join("/"), !this.#t.startsWith("/") && !this.#t.startsWith("./") && (this.#t = "/" + this.#t)), this.#i = new p({
...s,
responseTemplate: this.$responseTemplate,
transform: e
});
}
onInitialize() {
this.#t && this.sigil.route("/").get(this.#t, () => new h(this.openApiDefinition));
}
onUpdateCallback() {
if (!this.#i) return;
const s = [];
this.$routes.forEach(([e, t]) => {
const a = o(t.exportRequests.map((n) => {
let i = (e + n.path).replace(/\/{2,}/g, "/");
return i.endsWith("/") && (i = i.slice(0, -1)), this.#t && i === this.#t ? null : {
method: n.method,
path: i,
schema: n.flatSchema,
meta: n.metadata ?? {},
tags: t.routeOptions?.tags ?? o([l(e, " ") || null])
};
}));
s.push(...a);
}), this.openApiDefinition = this.#i.transform(s);
}
}
export {
d as default
};