@teknologi-umum/pesto
Version:
JavaScript SDK for Pesto
2 lines (1 loc) • 3.51 kB
JavaScript
var PestoClient=function(e,t){"use strict";function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=s(t);function n(e,t,s,o){return new(s||(s=Promise))((function(n,r){function i(e){try{c(o.next(e))}catch(e){r(e)}}function a(e){try{c(o.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(i,a)}c((o=o.apply(e,t||[])).next())}))}class r extends Error{constructor(e){super(e),this.message=e}}class i extends Error{constructor(){super("Empty token")}}class a extends Error{constructor(){super("missing token")}}class c extends Error{constructor(){super("unauthorized for unknown reason")}}class u extends Error{constructor(e){super(e),this.message=e}}class l extends Error{constructor(){super("Token not registered")}}class d extends Error{constructor(){super("Token revoked")}}class h extends Error{constructor(){super("Monthly limit exceeded")}}class p extends Error{constructor(){super("Server rate limited")}}class m extends Error{constructor(e,t){super(`Provided language-version combination${void 0!==e&&void 0!==t?` of ${e} and ${t}`:""} does not exists on Pesto's API`)}}class w{constructor(e){var t;if(void 0===e.token||null===e.token||""===e.token)throw new i;this.token=e.token,this.baseURL=null!==(t=e.baseURL)&&void 0!==t?t:new URL("https://pesto.teknologiumum.com")}static fromToken(e){return new w({token:e})}ping(e){return n(this,void 0,void 0,(function*(){const t=yield o.default(new URL("/api/ping",this.baseURL).toString(),{signal:e,headers:{"X-Pesto-Token":this.token,Accept:"application/json"},method:"GET"});if(200!==t.status)throw yield this.processError(t);return{message:(yield t.json()).message}}))}listRuntimes(e){return n(this,void 0,void 0,(function*(){const t=yield o.default(new URL("/api/list-runtimes",this.baseURL).toString(),{signal:e,headers:{"X-Pesto-Token":this.token,Accept:"application/json"},method:"GET"});if(200!==t.status)throw yield this.processError(t);return{runtime:(yield t.json()).runtime}}))}execute(e,t){return n(this,void 0,void 0,(function*(){const s=yield o.default(new URL("/api/execute",this.baseURL).toString(),{signal:t,headers:{"X-Pesto-Token":this.token,Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(e),method:"POST"});200!==s.status&&(yield this.processError(s));const n=yield s.json();return{language:n.language,version:n.version,runtime:n.runtime,compile:n.compile}}))}processError(e){return n(this,void 0,void 0,(function*(){switch(e.status){case 404:throw new Error("api path not found");case 500:{const t=yield e.json();throw new u(t.message)}case 401:{const t=yield e.json();if("Token must be supplied"===t.message)throw new a;if("Token not registered"===t.message)throw new l;if("Token has been revoked"===t.message)throw new d;throw new c}case 429:{const t=yield e.json();if("Monthly limit exceeded"===(null==t?void 0:t.message))throw new h;throw new p}case 400:{const t=yield e.json();if("Runtime not found"===(null==t?void 0:t.message))throw new m;if(null==t?void 0:t.message.startsWith("Missing parameters"))throw new r(t.message);throw new Error(`${t.message} (this is probably a problem with the SDK, please submit an issue on our Github repository)`)}}const t=yield e.text();throw new Error(`Received ${e.status} with body ${t} (this is probably a problem with the SDK, please submit an issue on our Github repository)`)}))}}return e.PestoClient=w,Object.defineProperty(e,"__esModule",{value:!0}),e}({},fetch);