UNPKG

@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

2 lines (1 loc) 1.22 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const r=require("@sigiljs/openapi-transformer"),l=require("@sigiljs/sigil"),p=require("@sigiljs/sigil/responses"),o=require("@sigiljs/sigil/utils");class h extends l.SigilPlugin{static name="OpenApiPlugin";openApiDefinition={};#t;#e;constructor(){super();const{info:i,transform:s,path:e}=this.$pluginConfig;e!==!1&&(this.#e=o.nonNullable((e??"/swagger.json").split("/")).join("/"),!this.#e.startsWith("/")&&!this.#e.startsWith("./")&&(this.#e="/"+this.#e)),this.#t=new r.OpenApiTransformer({...i,responseTemplate:this.$responseTemplate,transform:s})}onInitialize(){this.#e&&this.sigil.route("/").get(this.#e,()=>new p.RawResponse(this.openApiDefinition))}onUpdateCallback(){if(!this.#t)return;const i=[];this.$routes.forEach(([s,e])=>{const a=o.nonNullable(e.exportRequests.map(n=>{let t=(s+n.path).replace(/\/{2,}/g,"/");return t.endsWith("/")&&(t=t.slice(0,-1)),this.#e&&t===this.#e?null:{method:n.method,path:t,schema:n.flatSchema,meta:n.metadata??{},tags:e.routeOptions?.tags??o.nonNullable([o.sanitizePath(s," ")||null])}}));i.push(...a)}),this.openApiDefinition=this.#t.transform(i)}}exports.default=h;