"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,a){void 0===a&&(a=r);var s=Object.getOwnPropertyDescriptor(t,r);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,a,s)}:function(e,t,r,a){void 0===a&&(a=r),e[a]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.HTTPSnippet=void 0;const url=__importStar(require("url")),utils_1=require("../../utils"),constants_1=require("../../../constants"),targets_1=__importDefault(require("./targets")),reducer_1=__importDefault(require("./helpers/reducer")),utils_2=require("../../../utils");class HTTPSnippet{constructor(e){Object.defineProperty(this,"request",{enumerable:!0,configurable:!0,writable:!0,value:void 0});const t=Object.assign({},e);this.request=this.prepare(Object.assign(Object.assign({},t),{httpVersion:t.httpVersion||"HTTP/1.1",queryString:t.queryString||[],headers:t.headers||[],cookies:t.cookies||[],postData:t.postData?Object.assign(Object.assign({},t.postData),{jsonObj:void 0,paramsObj:void 0}):void 0,bodySize:0,headersSize:0,queryObj:{},headersObj:{},cookiesObj:{},allHeaders:{},uriObj:{},fullUrl:"",pathParameters:t.pathParameters||{},serverVariables:t.serverVariables||{}}))}prepare(e){var t;if(e.queryString&&e.queryString.length&&(e.queryObj=e.queryString.reduce(reducer_1.default,{})),e.headers&&e.headers.length){const t=/^HTTP\/2/;e.headersObj=e.headers.reduce((function(r,a){let s=a.name;return e.httpVersion.match(t)&&(s=s.toLowerCase()),r[s]=a.value,r}),{})}e.cookies&&e.cookies.length&&(e.cookiesObj=e.cookies.reduceRight((function(e,t){return e[t.name]=t.value,e}),{}));const r=e.cookies.map((function(e){return encodeURIComponent(e.name)+"="+encodeURIComponent(e.value)}));if(r.length&&(e.allHeaders.cookie=r.join("; ")),e.postData)switch((0,utils_1.normalizeMimeType)(e.postData.mimeType)){case"multipart/mixed":case"multipart/related":case constants_1.MediaTypes.MULTIPART:case"multipart/alternative":break;case constants_1.MediaTypes.URL_ENCODED:(null===(t=e.postData)||void 0===t?void 0:t.params)?(e.postData.paramsObj=e.postData.params.reduce(reducer_1.default,{}),e.postData.text=utils_2.queryString.stringify(e.postData.paramsObj)):e.postData.text="";break;case"text/json":case"text/x-json":case constants_1.MediaTypes.JSON:case"application/x-json":if(e.postData.text)try{e.postData.jsonObj=JSON.parse(e.postData.text)}catch(t){e.postData.mimeType="text/plain"}}return e.allHeaders=Object.assign(e.allHeaders,e.headersObj),e.uriObj=url.parse(e.url,!0,!0),e.queryObj=Object.assign(e.queryObj,e.uriObj.query),e.uriObj.query=null,e.uriObj.search=null,e.uriObj.path=e.uriObj.pathname=(0,utils_1.unescapeServerVariables)(e.uriObj.pathname),e.url=(0,utils_1.unescapeServerVariables)(url.format(e.uriObj)),e.uriObj.query=e.queryObj,e.uriObj.search=utils_2.queryString.stringify(e.queryObj),e.uriObj.search&&(e.uriObj.path=e.uriObj.pathname+"?"+e.uriObj.search),e.fullUrl=(0,utils_1.unescapeServerVariables)(url.format(e.uriObj)),e}convert(e,t,r){!r&&t&&(r=t);const a=this._matchTarget(e,t);if(a){const s=a(this.request,r,{target:e,client:t});return 1===s.length?s[0]:s}throw new Error("Cannot match target")}_matchTarget(e,t){return!!Object.prototype.hasOwnProperty.call(targets_1.default,e)&&("string"==typeof t&&"function"==typeof targets_1.default[e][t]?targets_1.default[e][t]:targets_1.default[e][targets_1.default[e].info.default])}}exports.HTTPSnippet=HTTPSnippet;
//# sourceMappingURL=index.js.map