@x82-softworks/aws-api
Version:
An OpenAPI compatible api system intended for use with AWS Lambda and API Gateway
2 lines • 11.1 kB
JavaScript
var e,t={n:e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return t.d(r,{a:r}),r},d:(e,r)=>{for(var o in r)t.o(r,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:r[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},r={};t.d(r,{xn:()=>v,tF:()=>m,$h:()=>f,IX:()=>n,d9:()=>p,Hu:()=>l,ox:()=>y,Cv:()=>X,tf:()=>h,xK:()=>E,RF:()=>d,Ol:()=>W,vu:()=>D,yf:()=>b,qU:()=>a,JR:()=>i,Dc:()=>s,Wu:()=>o,$l:()=>u,Qj:()=>w,jd:()=>c,Ue:()=>ee,ZP:()=>Q,Wz:()=>re,oy:()=>te}),function(e){e.GET="get",e.POST="post",e.PUT="put",e.DELETE="delete",e.PATCH="patch",e.OPTIONS="options"}(e||(e={}));const o="number",n="boolean",s="integer",a="int32",i="int64",c="string",d="float",h="double",u="object",p="byte",f="binary",l="date",y="dateTime",m="array",b="header",w="password",v="apikey",E="email",g=/({[^}]*})/g;class C{constructor(e){const t=[];let r;for(;r=g.exec(e);)r=r[0].slice(1,-1),t.push(r);this.keys=t,e=e.replace(/({[^}]*})/g,"([a-zA-Z0-9-\\._~ %]+)"),this.regex=new RegExp(e)}match(e){let t,r=null;if(t=this.regex.exec(e)){r={};for(let e=0;e<this.keys.length;e++)r[this.keys[e]]=t[e+1]}return r}}var x=function(e,t,r,o){return new(r||(r=Promise))((function(n,s){function a(e){try{c(o.next(e))}catch(e){s(e)}}function i(e){try{c(o.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,i)}c((o=o.apply(e,t||[])).next())}))};const D="GenericError",R=e=>(e.body&&"string"!=typeof e.body&&(e.body=JSON.stringify(e.body)),e),q=e=>"object"==typeof e&&"string"==typeof(null==e?void 0:e.$ref),O=(e,t)=>{switch(e.type){case u:"string"==typeof t&&(t=JSON.parse(t));break;case s:case o:t=Number(t);break;case n:t=Boolean(t)}return t},P=function(e){return x(this,void 0,void 0,(function*(){return R(Object.assign(Object.assign({},e),{statusCode:404}))}))},k=e=>new C(e),T=require("serialize-error"),A=require("@x82-softworks/clone-merge");var S=t.n(A);const j=require("cookie");var B=t.n(j);const N=require("ajv");var I=t.n(N);const M=require("is-integer");var H=t.n(M);const L=new(I())({useDefaults:!0,coerceTypes:!0});L.addFormat(a,{validate:function(e){return H()(e)&&e<=2147483647&&e>=-2147483647},type:o}),L.addFormat(i,{validate:function(e){return H()(e)},type:o});const U=(e,t)=>{let r,o=L.compile(e);if(!o(t))throw r=new Error("Validation error"),r.additional=o.errors,r};var F=function(e,t,r,o){return new(r||(r=Promise))((function(n,s){function a(e){try{c(o.next(e))}catch(e){s(e)}}function i(e){try{c(o.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,i)}c((o=o.apply(e,t||[])).next())}))};const $=require("@x82-softworks/aws-api-multipart"),_=require("query-string");var z=t.n(_),G=function(e,t,r,o){return new(r||(r=Promise))((function(n,s){function a(e){try{c(o.next(e))}catch(e){s(e)}}function i(e){try{c(o.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,i)}c((o=o.apply(e,t||[])).next())}))};function J(e,t,r){return G(this,void 0,void 0,(function*(){let o,n,s,a=null===e.body||void 0===e.body;if(t){if(q(t)&&(t=r.getRef(t)),t.required&&a)throw new Error("Request body must be supplied");if(!a&&(s=e.headers["content-type"],s)){if(s=s.split(";")[0],!(o=t.content[s]))throw new Error(`Content Type "${s}" is not supported`);switch(n=o.schema,s){case"application/json":try{e.body=JSON.parse(e.body)}catch(e){throw new Error("Cannot parse request body")}break;case"application/x-www-form-urlencoded":e.body=z().parse(e.body);break;case"multipart/form-data":const t=yield(0,$.parse)(e),{val:r,multiparts:o}=t.files.reduce(((e,t)=>(e.val[t.fieldName]=t.data.toString("utf-8"),e.multiparts[t.fieldName]=t,e)),{val:{},multiparts:{}});e.body=r,e.multiparts=o}!function(e,t,r){if(!t)return;q(t)&&(t=r.getRef(t));const o=e.body=O(t,e.body);U(t,o)}(e,n,r)}}}))}var V=function(e,t,r,o){return new(r||(r=Promise))((function(n,s){function a(e){try{c(o.next(e))}catch(e){s(e)}}function i(e){try{c(o.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,i)}c((o=o.apply(e,t||[])).next())}))};const W="application/x-www-form-urlencoded",X={"Access-Control-Allow-Methods":"GET,OPTIONS,HEAD","Access-Control-Allow-headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"},K=/\{([^}]*)\}/;function Y(e,t){return{$ref:"#/components/"+e+"/"+t}}class Z{constructor(e,t){this.devMode=!!t,this.rootDef=S()({openapi:"3.0.0",info:{version:"0.0.0",description:"Default API",termsOfService:"http://swagger.io/terms/",title:"",contact:{email:"no-reply@example.com"},license:{name:"Proprietary"}},host:"localhost",basePath:"/",paths:{}},e),this.corsConfig=function(e,t){const r=e.headers.Origin?e.headers.Origin:"*";t.headers=S()(t.headers,{"Access-Control-Allow-Origin":r},X)},this.paths=[],this.defSchema("genericError",{type:"object",properties:{code:{description:"Error code",type:c},message:{description:"Human readable message",type:c}},required:["code","message"]}),this.defResponse("genericError",{description:"An unexpected error occurred",content:{"application/json":{schema:this.refSchema("genericError")}}}),this._suppressValidationErrors=!1}suppressValidationErrors(e){this._suppressValidationErrors=e}getRoot(){return S()(!0,this.rootDef)}defRoot(e){this.rootDef=S()(!0,this.rootDef,e)}cors(e){this.corsConfig=e}defComponent(e,t,r){if(this.rootDef.components||(this.rootDef.components={}),this.rootDef.components[e]||(this.rootDef.components[e]={}),this.rootDef.components[e][t])throw new Error('Component of type : "'+e+'" and name : "'+t+'" is already defined');return this.rootDef.components[e][t]=r,this}defSecurity(e,t){return this.defComponent("securitySchemes",e,t)}defResponse(e,t){return this.defComponent("responses",e,t)}ref(e,t){return Y(e,t)}defSchema(e,t){return t.title||(t.title=e),this.defComponent("schemas",e,t)}getHost(){return this.rootDef.host}getRef(e){if(!q(e))throw new Error("Not a valid reference object");return e.$ref.split("/").slice(1).reduce((function(e,t){return e[t]}),this.rootDef)}refResponse(e){return Y("responses",e)}refSchema(e){return Y("schemas",e)}refRequestBody(e){return Y("requestBodies",e)}refSecurity(e){return Y("securitySchemes",e)}refExample(e){return Y("examples",e)}refParameter(e){return Y("parameters",e)}refHeader(e){return Y("headers",e)}refCallback(e){return Y("callbacks",e)}refLink(e){return Y("links",e)}path(e,t,r,o){o||(o=r,r=null),(r=S()(!0,{summary:"",description:"",tags:[],parameters:[],requestBody:null,responses:{200:{description:"Success"},default:this.refResponse("genericError")}},r)).requestBody||delete r.requestBody;const n=t.toLowerCase();if(r.handler=o,this.rootDef.paths||(this.rootDef.paths={}),this.rootDef.paths[e]||(this.rootDef.paths[e]={}),this.rootDef.paths[e][n])throw new Error("Redefinition of path : "+e+" method : "+n);return this.rootDef.paths[e][n]=r,K.test(e)&&this.paths.push({regex:k(e),original:e}),this}get(t,r,o){return this.path(t,e.GET,r,o)}post(t,r,o){return this.path(t,e.POST,r,o)}put(t,r,o){return this.path(t,e.PUT,r,o)}patch(t,r,o){return this.path(t,e.PATCH,r,o)}delete(t,r,o){return this.path(t,e.DELETE,r,o)}defParameter(e,t){return this.defComponent("parameters",e,t)}defExample(e,t){return this.defComponent("examples",e,t)}defRequestBody(e,t){return this.defComponent("requestBodies",e,t)}defHeader(e,t){return this.defComponent("headers",e,t)}defLink(e,t){return this.defComponent("links",e,t)}defCallback(e,t){return this.defComponent("callbacks",e,t)}lambda(){const e=this;return function(t){return V(this,void 0,void 0,(function*(){if(t.headers&&t.headers.Host){let r="";t.requestContext&&0===t.requestContext.path.indexOf("/"+t.requestContext.stage)&&(r="/"+t.requestContext.stage),e.defRoot({host:"https://"+t.headers.Host+r})}return yield e.dispatch(t)}))}}dispatch(t){var r;return V(this,void 0,void 0,(function*(){const o=Object.assign({path:"/",rawPath:"/",httpMethod:e.GET,headers:{},rawHeaders:{},queryStringParameters:{},params:{},stageVariables:{},requestContext:{},pathParams:{},body:null,isBase64Encoded:!1,rawBody:null},t),n=this;if(o.rawPath=o.path,o.httpMethod=o.httpMethod.toLowerCase(),o.headers=Object.keys(o.headers||{}).reduce(((e,t)=>(e[t.toLowerCase()]=o.headers[t],e)),{}),o.rawBody=o.body,o.rawHeaders=o.headers,(null===(r=o.headers)||void 0===r?void 0:r.cookie)&&(o.headers.cookie=B().parse(o.headers.cookie)),"/"!==o.path[0]&&(o.path="/"+o.path),!o.httpMethod||!o.path)throw new Error("Event is not wellformed");const s={body:void 0,headers:{},statusCode:200};if(0!==o.path.indexOf(n.rootDef.basePath))return P(s);n.corsConfig(o,s),o.path=o.path.slice(n.rootDef.basePath.length),"/"!==o.path[0]&&(o.path="/"+o.path);let a,i=n.rootDef.paths[o.path],c=null;if(!i)for(let e=0,t=n.paths.length;e<t;e++)if(c=n.paths[e].regex.match(o.path)){i=n.rootDef.paths[n.paths[e].original];break}return o.pathParams=c,i&&(a=i[o.httpMethod])?J(o,a.requestBody,n).catch((e=>{throw s.statusCode=400,e})).then((()=>V(this,void 0,void 0,(function*(){return yield((e,t,r)=>F(void 0,void 0,void 0,(function*(){(t.parameters||[]).forEach((t=>{let o;switch(t.in){case"query":o=(e.queryStringParameters||{})[t.name];break;case"header":o=e.headers[t.name];break;case"path":o=decodeURIComponent(e.pathParams[t.name]);break;case"cookie":o=e.headers.cookie[t.name];break;default:throw new Error("Unknown param type : "+t.in)}if(void 0===o){if(t.required)throw new Error(`Required parameter "${t.name}" not found`)}else if(t.schema){let e=t.schema;q(e)&&(e=r.getRef(e)),o=O(e,o),U(e,o)}e.params[t.name]=o}))})))(o,a,n),new Promise(((e,t)=>{try{e(a.handler(o,s))}catch(e){t(e)}})).catch((e=>{s.body||(n.devMode?s.body=e:(console.error(e),s.body=new Error("An internal server error has occurred")),s.statusCode=s.body.statusCode?s.body.statusCode:500)}))})))).catch((e=>{s.body=e})).then((()=>{var e,t;return s.body instanceof Error&&(n.devMode?s.body=(0,T.serializeError)(s.body):s.body={code:(null===(e=s.body)||void 0===e?void 0:e.code)||D,message:s.body.message,additional:n._suppressValidationErrors?null:null===(t=s.body)||void 0===t?void 0:t.additional},s.statusCode||(s.statusCode=s.body.statusCode||500)),"object"!=typeof s.body||s.headers["content-type"]||(s.headers["content-type"]="application/json"),R(s)})):"options"===o.httpMethod?s:P(s)}))}}const Q=Z,ee=function(e,t){return new Z(e,t)},te=e=>({required:!0,content:{"application/json":{schema:e}}}),re=e=>({"application/json":{schema:e}});var oe=r.xn,ne=r.tF,se=r.$h,ae=r.IX,ie=r.d9,ce=r.Hu,de=r.ox,he=r.Cv,ue=r.tf,pe=r.xK,fe=r.RF,le=r.Ol,ye=r.vu,me=r.yf,be=r.qU,we=r.JR,ve=r.Dc,Ee=r.Wu,ge=r.$l,Ce=r.Qj,xe=r.jd,De=r.Ue,Re=r.ZP,qe=r.Wz,Oe=r.oy;export{oe as APIKEY,ne as ARRAY,se as BINARY,ae as BOOLEAN,ie as BYTE,ce as DATE,de as DATETIME,he as DEFAULT_CORS,ue as DOUBLE,pe as EMAIL,fe as FLOAT,le as FORM_ENCODED,ye as GENERIC_ERROR,me as HEADER,be as INT32,we as INT64,ve as INTEGER,Ee as NUMBER,ge as OBJECT,Ce as PASSWORD,xe as STRING,De as create,Re as default,qe as jsonResponseContent,Oe as requiredJsonRequest};
//# sourceMappingURL=esm.min.js.map