gigachat-node
Version:
The unoffical JavaScript/TypesSript library for the GigaChat API
1 lines • 3.56 kB
JavaScript
;var __awaiter=this&&this.__awaiter||function(t,e,a,r){return new(a||(a=Promise))((function(o,n){function i(t){try{h(r.next(t))}catch(t){n(t)}}function s(t){try{h(r.throw(t))}catch(t){n(t)}}function h(t){var e;t.done?o(t.value):(e=t.value,e instanceof a?e:new a((function(t){t(e)}))).then(i,s)}h((r=r.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.HTTPClient=void 0;const https_1=require("https"),stream_1=require("stream"),FormDataBuilder_1=require("./FormDataBuilder"),FormDataFile_1=require("./FormDataFile"),GigaChatError_1=require("./GigaChatError");class HTTPClient{constructor(t,e,a){this.baseUrl=t,this.authorization=e,this.isIgnoreTSL=a}setAuthorization(t){return __awaiter(this,void 0,void 0,(function*(){this.authorization=t}))}makeRequest(t,e,a=0,r=!1){return __awaiter(this,void 0,void 0,(function*(){return new Promise(((o,n)=>{const i=(0,https_1.request)(t,(i=>{if(i.statusCode&&i.statusCode>=300&&i.statusCode<400&&i.headers.location){if(a<5)return void n(new Error("Too many redirects"));const r=new URL(i.headers.location,`https://${t.hostname}${t.path}`),s=Object.assign({},t);return s.hostname=r.hostname,s.path=r.pathname+r.search,s.port=r.port||443,307===i.statusCode||308===i.statusCode?s.method=t.method:(s.method="GET",e=""),"GET"===s.method&&(delete s.headers["Content-Length"],delete s.headers["Content-Type"]),void this.makeRequest(s,e,a+1).then(o).catch(n)}if(i.statusCode&&(i.statusCode<200||i.statusCode>=300))n(new Error(`HTTP Error: ${i.statusCode} ${i.statusMessage}`));else{if(t.headers&&"application/jpg"===t.headers.Accept){const t=[];return i.on("data",(e=>t.push(e))),void i.on("end",(()=>{const e=Buffer.concat(t);o(e)}))}if(r){const t=new stream_1.Readable({read(){}});i.on("data",(e=>{const a=e.toString("utf-8");a.startsWith("data: [DONE]")?t.push(null):a.startsWith("data: ")&&t.push(e)})),o(t)}else{let t="";i.on("data",(e=>{t+=e})),i.on("end",(()=>{try{const e=JSON.parse(t);o(e)}catch(e){o(t)}}))}}}));i.on("error",n),"POST"===t.method&&i.write(e),i.end()}))}))}get(t,e=!1){return __awaiter(this,void 0,void 0,(function*(){const a=new URL(`${this.baseUrl}${t}`),r={hostname:a.hostname,path:a.pathname+a.search,port:a.port||443,method:"GET",headers:{Authorization:`Bearer ${this.authorization}`,Accept:e?"application/jpg":"application/json"},agent:new https_1.Agent({rejectUnauthorized:!this.isIgnoreTSL})};return yield this.makeRequest(r,"")}))}post(t,e,a=!1){return __awaiter(this,void 0,void 0,(function*(){const r=new URL(`${this.baseUrl}${t}`),o=JSON.stringify(e),n={hostname:r.hostname,path:r.pathname+r.search,port:r.port||443,method:"POST",headers:{Authorization:`Bearer ${this.authorization}`,"Content-Type":"application/json","Content-Length":Buffer.byteLength(o)},agent:new https_1.Agent({rejectUnauthorized:!this.isIgnoreTSL})};return yield this.makeRequest(n,o,0,a)}))}postFiles(t,e){return __awaiter(this,void 0,void 0,(function*(){const a=new FormDataFile_1.FormDataFile(t),r=new FormDataBuilder_1.FormDataBuilder;r.appendField("purpose",e),r.appendFile("file",a);const o=new URL(`${this.baseUrl}/files`),n={hostname:o.hostname,path:o.pathname+o.search,port:o.port||443,method:"POST",headers:Object.assign({Authorization:`Bearer ${this.authorization}`,Accept:"application/json"},r.getHeaders()),agent:new https_1.Agent({rejectUnauthorized:!this.isIgnoreTSL})};try{return yield this.makeRequest(n,r.getBody())}catch(t){throw new GigaChatError_1.GigaChatError(`File upload failed: ${t}`,"FILE_UPLOAD_ERROR")}}))}}exports.HTTPClient=HTTPClient;