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