"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.info=void 0;const stringify_object_1=__importDefault(require("stringify-object")),code_builder_1=require("../../helpers/code-builder"),code_helpers_1=require("../../helpers/code-helpers"),utils_1=require("../../../../utils"),__1=require("../.."),constants_1=require("../../helpers/constants"),constants_2=require("../../../../../constants"),handler=function(e,t,{target:s,client:a}){var n,r,i,o,d,l;const c=Object.assign({indent:"  ",credentials:null},t),p=new code_builder_1.CodeBuilder({indentation:c.indent,variablesPrefix:c.variablesPrefix,capitalize:!0,lang:constants_1.Lang.JAVASCRIPT}),u={method:e.method.toUpperCase()};Object.keys(e.headersObj).length&&(u.headers=e.headersObj);let h=!1;if(c.withComments&&addComments(p),null===(n=e.securityOAuth2ExtraCalls)||void 0===n?void 0:n.length){const t=new __1.HTTPSnippet(null===(r=e.securityOAuth2ExtraCalls)||void 0===r?void 0:r[0]).convert(s,a,Object.assign(Object.assign({},c),{withImports:!1,withComments:!1,variablesPrefix:"oAuth2"}));p.push(t),p.blank(),u.headers=u.headers||{},u.headers.Authorization="'Bearer ' + oAuth2Data.access_token",h=!0}null!==c.credentials&&(u.credentials=c.credentials);const _=Object.getOwnPropertyNames(e.queryObj).length;if(_&&p.push(`const ${p.var("query")} = new URLSearchParams(${(0,stringify_object_1.default)(e.queryObj,{indent:c.indent,inlineCharacterLimit:25})}).toString();`).blank(),e.postData)switch((0,utils_1.normalizeMimeType)(e.postData.mimeType)){case constants_2.MediaTypes.URL_ENCODED:u.body=e.postData.paramsObj?`new URLSearchParams(${p.var("formData")}).toString()`:e.postData.text,e.postData.paramsObj&&p.push(`const ${p.var("formData")} = ${(0,code_helpers_1.addIndentation)((0,stringify_object_1.default)(e.postData.paramsObj,{indent:c.indent,inlineCharacterLimit:25}),{level:0,firstLine:!1})};`).blank();break;case constants_2.MediaTypes.JSON:e.postData.jsonObj&&(u.body=`JSON.stringify(${(0,code_helpers_1.addIndentation)((0,stringify_object_1.default)(e.postData.jsonObj,{indent:c.indent,inlineCharacterLimit:25}),{level:1,firstLine:!1})})`);break;case constants_2.MediaTypes.MULTIPART:p.push(`const ${p.var("form")} = new FormData();`),(null===(i=null==u?void 0:u.headers)||void 0===i?void 0:i["Content-Type"])&&(null===(o=u.headers)||void 0===o||delete o["Content-Type"]),e.postData.params.forEach((function(e){p.push(`${p.var("form")}.append(%s, %s);`,JSON.stringify(e.name),JSON.stringify(e.value||e.fileName||""))})),u.body=p.var("form"),p.blank();break;default:e.postData.text&&(u.body=`\`${(0,code_helpers_1.addIndentation)(e.postData.text,{level:2,indent:c.indent,firstLine:!1}).trim()}\``)}const m=u.headers&&u.headers.Accept&&(0,utils_1.normalizeMimeType)(u.headers.Accept)===constants_2.MediaTypes.JSON||(0,utils_1.normalizeMimeType)(null===(d=e.postData)||void 0===d?void 0:d.mimeType)===constants_2.MediaTypes.JSON;if(e.basicAuth){const{username:t,password:s}=e.basicAuth;u.headers=u.headers||{},u.headers.Authorization=`'Basic ' + btoa('${t}:${s}')`,h=!0}(0,code_helpers_1.printUrlVariablesDeclarations)(e,p);const f=(0,code_helpers_1.buildUrlExpression)(e,p);return p.push(`const ${p.var("resp")} = await fetch(`).push(1,"`"+f+(_?"?${"+p.var("query")+"}":"")+"`,").push((0,code_helpers_1.addIndentation)((0,stringify_object_1.default)(u,{indent:c.indent,inlineCharacterLimit:25,transform:(0,code_helpers_1.getPreserveTransformer)({body:!0,authorizationHeader:h})}),{level:1,indent:c.indent})).push(");").blank(),(null===(l=e.allResponseCodes)||void 0===l?void 0:l.includes("204"))?(p.push("if (resp.status === 204) {").push(1,"console.log('success');").push("} else {"),p.push(1,`const ${p.var("data")} = await ${p.var("resp")}.${m?"json()":"text()"};`).push(1,`console.log(${p.var("data")});`).push("}")):p.push(`const ${p.var("data")} = await ${p.var("resp")}.${m?"json()":"text()"};`).push(`console.log(${p.var("data")});`),p.join()};function addComments(e){e.push("/**"),e.push(" * Requires ES8-compatible browser: https://caniuse.com/async-functions"),e.push(" */"),e.blank()}exports.info={key:"fetch",title:"fetch",link:"https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch",description:"Perform asynchronous HTTP requests with the Fetch API"},exports.default=handler;
//# sourceMappingURL=fetch.js.map