workery
Version:
Modern web framework for building APIs on Cloudflare Workers.
2 lines (1 loc) • 3.92 kB
JavaScript
"use strict";var e=require("@asteasolutions/zod-to-openapi"),t=require("cookie"),s=require("./parameters.cjs"),r=require("./responses.cjs"),i=require("./routing.cjs"),n=require("./renderers.cjs"),a=require("./helpers.cjs");function o(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var c=o(t);class h extends i.Router{rootPath;title;description;version;tagsInfo;servers;contact;license;termsOfService;securitySchemes;security;openapiUrl;swaggerUrl;redocUrl;middleware;exceptionHandler;_rootPathRegex;_openapi;constructor(e){super(e),this.rootPath=e.rootPath??"/",this._rootPathRegex=new RegExp("^"+this.rootPath),this.title=e.title??"Workery API",this.description=e.description??"",this.version=e.version??"0.1.0",this.tagsInfo=e.tagsInfo??[],this.servers=[{url:this.rootPath},...e.servers??[]],this.contact=e.contact,this.license=e.license,this.termsOfService=e.termsOfService,this.securitySchemes=e.securitySchemes??{},this.security=e.security??Object.keys(this.securitySchemes).map((e=>({[e]:[]}))),this.openapiUrl=null===e.openapiUrl?null:e.openapiUrl??"/openapi.json",this.swaggerUrl=null===e.swaggerUrl?null:e.swaggerUrl??"/docs",this.redocUrl=null===e.redocUrl?null:e.redocUrl??"/redoc",this.middleware=e.middleware??[],this.routeMatcher.set(null,{middleware:this.middleware}),this.exceptionHandler=e.exceptionHandler??a.baseExceptionHandler,this.openapiUrl&&(this.get(this.openapiUrl,{includeInSchema:!1,responseClass:r.JSONResponse,parameters:{},handle:()=>this.openapi()}),this.swaggerUrl&&this.get(this.swaggerUrl,{includeInSchema:!1,responseClass:r.HTMLResponse,parameters:{},handle:()=>n.renderSwagger(i.fixPathSlashes(this.rootPath+this.openapiUrl),{title:this.title})}),this.redocUrl&&this.get(this.redocUrl,{includeInSchema:!1,responseClass:r.HTMLResponse,parameters:{},handle:()=>n.renderRedoc(i.fixPathSlashes(this.rootPath+this.openapiUrl),{title:this.title})}))}include(e,t){for(const s of t.routeMatcher){const r=new i.Route({...s,path:e+s.path,security:s.security??this.security});this.routeMatcher.push(r),this.routeMatcher.set(e,{middleware:t.middleware})}}openapi(){if(this._openapi)return this._openapi;const t=new e.OpenAPIRegistry;for(const e of this.routeMatcher)e.includeInSchema&&t.registerPath(e.openapi());const s=new e.OpenApiGeneratorV31(t.definitions);return this._openapi=s.generateDocument({openapi:"3.1.0",info:{title:this.title,version:this.version,description:this.description,contact:this.contact,license:this.license,termsOfService:this.termsOfService},servers:this.servers,tags:this.tagsInfo}),this._openapi.components=this._openapi.components||{},this._openapi.components.securitySchemes=this.securitySchemes,this._openapi}async handle(e){const{req:t}=e;try{const n=new URL(t.url),[o,h,l]=this.routeMatcher.match(t.method,n.pathname.replace(this._rootPathRegex,"")),p=c.parse(t.headers.get("Cookie")??""),u=i.searchParamsToQueries(n.searchParams),d={};let m;m=void 0===o?async()=>new r.JSONResponse({detail:"Not Found"},{status:404}):null===o?async()=>new r.JSONResponse({detail:"Method Not Allowed"},{status:405,headers:h}):async()=>{const[t,i]=a.createResolveLater();try{const n=await s.parseArgs(o.parameters,{baseArgs:e,later:i,rawParameters:{params:h,queries:u,cookies:p}});let a;return n.success?(a=await o.handle({...e,...n.args}),a instanceof Response||(a=new o.responseClass(a,{status:o.statusCode}))):a=new r.JSONResponse({detail:n.errors},{status:422}),t(a),a}catch(e){if(e instanceof Response)return t(e),e;throw e}},d[l.length-1]=m;for(let t=l.length-1;t>=0;t--)m=async()=>await l[t].handle(e,d[t]),d[t-1]=m;return await m()}catch(t){return t instanceof Response?t:await this.exceptionHandler(e,t)}}fetch=(e,t,s)=>this.handle({req:e,env:t,ctx:s})}exports.App=h;