UNPKG

@mountainpass/server-agnostic-functions-aws

Version:

Provides an interface for writing server-agnostic (serverless) functions.

2 lines (1 loc) 1.9 kB
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)}var r=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=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.mapRequest=function(o,a,s){if(void 0===s&&(s=new e.HttpRequest),void 0===o.version||"1.0"===o.version){var i=o;s.method=i.httpMethod.toLowerCase(),s.path=i.path,s.headers=t({},n(i.headers,!0),n(i.multiValueHeaders,!0)),s.query=t({},n(i.queryStringParameters),n(i.multiValueQueryStringParameters)),s.params=t({},r(i.pathParameters)),s.body=null===i.body?"":i.body,s.underlying={event:i,context:a}}else{if("2.0"!==o.version)throw new Error("Unknown API Gateway version: "+o.version);var u=o;u.requestContext.http.method.toLowerCase(),s.method=u.requestContext.http.method.toLowerCase(),s.path=u.requestContext.http.path,s.headers=t({},n(u.headers,!0)),s.query=t({},(void 0===p&&(p=!1),null==(d=u.queryStringParameters)?{}:Object.fromEntries(Object.entries(d).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(","):[]]})))),s.params=t({},r(u.pathParameters)),s.body=u.body?u.body:"",s.underlying={event:u,context:a}}var d,p;return s},exports.mapResponse=function(e){return{statusCode:e.statusCode,body:e.body,multiValueHeaders:e.headers}};