@mountainpass/server-agnostic-functions-aws
Version:
Provides an interface for writing server-agnostic (serverless) functions.
2 lines (1 loc) • 2.92 kB
JavaScript
var e=require("@mountainpass/server-agnostic-functions-core");function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},t.apply(this,arguments)}function r(e,t){return r=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},r(e,t)}var n=function(e,t){return void 0===t&&(t=!1),null==e?{}:Object.fromEntries(Object.entries(e).filter(function(e){return e.length>1&&void 0!==e[1]}).map(function(e){var r=e[0],n=e[1];return[t?r.toLowerCase():r,"string"==typeof n?n:""]}))},o=function(e,t){return void 0===t&&(t=!1),null==e?{}:Object.fromEntries(Object.entries(e).filter(function(e){return e.length>1&&void 0!==e[1]}).map(function(e){var r=e[0],n=e[1];return[t?r.toLowerCase():r,"string"==typeof n?[n]:n]}))};exports.AwsWrapper=/*#__PURE__*/function(s){var a,i;function u(){return s.apply(this,arguments)||this}i=s,(a=u).prototype=Object.create(i.prototype),a.prototype.constructor=a,r(a,i);var c=u.prototype;return c.mapRequest=function(r,s){try{return void 0===s&&(s=new e.HttpRequest),Promise.resolve(function(r,s,a){if(void 0===a&&(a=new e.HttpRequest),void 0===r.version||"1.0"===r.version){var i=r;a.method=i.httpMethod.toLowerCase(),a.path=i.path,a.headers=t({},o(i.headers,!0),o(i.multiValueHeaders,!0)),a.query=t({},o(i.queryStringParameters),o(i.multiValueQueryStringParameters)),a.params=t({},n(i.pathParameters)),a.body=null===i.body?"":i.body,a.underlying={event:i,context:s}}else{if("2.0"!==r.version)throw new Error("Unknown API Gateway version: "+r.version);var u=r;u.requestContext.http.method.toLowerCase(),a.method=u.requestContext.http.method.toLowerCase(),a.path=u.requestContext.http.path,a.headers=t({},o(u.headers,!0)),a.query=t({},(void 0===p&&(p=!1),null==(c=u.queryStringParameters)?{}:Object.fromEntries(Object.entries(c).filter(function(e){return e.length>1&&void 0!==e[1]}).map(function(e){var t=e[0],r=e[1];return[p?t.toLowerCase():t,"string"==typeof r?r.split(","):[]]})))),a.params=t({},n(u.pathParameters)),a.body=u.body?u.body:"",a.underlying={event:u,context:s}}var c,p;return a}(r.event,r.context,s))}catch(e){return Promise.reject(e)}},c.mapResponse=function(e,t){try{return Promise.resolve({statusCode:(r=e).statusCode,body:r.body,multiValueHeaders:r.headers})}catch(e){return Promise.reject(e)}var r},c.wrap=function(t){var r=this;return function(n,o){try{return Promise.resolve(function(s,a){try{var i=Promise.resolve(r.mapRequest({event:n,context:o})).then(function(n){var o=new e.HttpResponse;return Promise.resolve(t.handle(n,o)).then(function(){return r.mapResponse(o)})})}catch(e){return a(e)}return i&&i.then?i.then(void 0,a):i}(0,function(e){return console.log(e),{statusCode:500,body:JSON.stringify({message:e.message})}}))}catch(e){return Promise.reject(e)}}},u}(e.AgnosticRouterWrapperInterface);