-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
2 lines (2 loc) · 4.68 KB
/
Copy pathbundle.js
File metadata and controls
2 lines (2 loc) · 4.68 KB
1
2
!function(){"use strict";var t,e;!function(t){t.GET="GET",t.POST="POST",t.PUT="PUT",t.PATCH="PATCH",t.DELETE="DELETE"}(t||(t={})),function(t){t.IDLE="idle",t.LOADING="loading",t.SUCCEEDED="succeeded",t.FAILED="failed"}(e||(e={}));function s(t,e,s,n){return new(s||(s=Promise))((function(a,o){function d(t){try{l(n.next(t))}catch(t){o(t)}}function i(t){try{l(n.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?a(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(d,i)}l((n=n.apply(t,e||[])).next())}))}const n=new class{constructor(){this.observers=[],this.state={status:e.IDLE,error:null}}addObserver(t){this.observers.push(t)}removeObserver(t){this.observers=this.observers.filter((e=>e!==t))}notifyObservers(){this.observers.forEach((t=>t.update(this.state)))}setState(t){this.state=Object.assign(Object.assign({},this.state),t),this.notifyObservers()}getState(){return this.state}makeRequest(t,n,a){return s(this,void 0,void 0,(function*(){this.setState({status:e.LOADING,error:null});const s={method:t,headers:{"Content-Type":"application/json"},body:a?JSON.stringify(a):void 0};try{const t=yield fetch(n,s);if(!t.ok)throw new Error(`HTTP error! status: ${t.status}`);const a=yield t.json();return this.setState({status:e.SUCCEEDED,data:a}),a}catch(t){throw this.setState({status:e.FAILED,error:t instanceof Error?t.message:"An unknown error occurred."}),console.error("Request failed:",t),t}}))}};var a,o,d,i,l,r,c,u;const m=new class{constructor(t=document.body,e=document.createElement("div")){this.parentElement=t,this.dataElement=e,this.dataElement.id="data",this.elements={},this.parentElement.appendChild(this.dataElement)}set parent(t){this.parentElement=t}set data(t){this.dataElement=t}createElement(t){const e=document.createElement("div");switch(e.id=t,e.style.display="none",t){case"idle":e.innerText="Idle...";break;case"loading":e.innerText="Loading...";break;case"succeeded":e.innerText="Request succeeded!";break;case"failed":e.innerText="Request failed!"}return this.parentElement.appendChild(e),e}update(t){this.elements[t.status]||(this.elements[t.status]=this.createElement(t.status));for(let t in this.elements)this.elements[t].style.display="none";this.elements[t.status].style.display="block","succeeded"===t.status?this.dataElement.innerText=JSON.stringify(t.data,null,2):this.dataElement.innerText=""}};m.parent=document.getElementById("status"),m.data=document.getElementById("data");const h=new class{constructor(t,e){this.changeHandler=t,this.dataElementId=e,this.data=null,this.displayElement=null,this.displayElement=document.getElementById(this.dataElementId)}update(t){this.data=t,this.displayData()}displayData(){var t;this.displayElement&&(null===(t=this.data)||void 0===t?void 0:t.data)&&(this.displayElement.innerText=JSON.stringify(this.data.data)),this.changeHandler(this.data)}}((t=>{m.update(t)}),"data");function E(t){const s={status:e.LOADING,error:null,data:null};h.update(s),t.then(p).catch(v).finally((()=>{console.log("Request complete!")}))}function p(t){const s={status:e.SUCCEEDED,error:null,data:t};h.update(s)}function v(t){const s={status:e.FAILED,error:t.message,data:null};h.update(s)}null===(a=document.getElementById("getPosts"))||void 0===a||a.addEventListener("click",(()=>{E(n.makeRequest(t.GET,"https://jsonplaceholder.typicode.com/posts"))})),null===(o=document.getElementById("getPostComments"))||void 0===o||o.addEventListener("click",(()=>{var e;E((e=1,n.makeRequest(t.GET,`https://jsonplaceholder.typicode.com/posts/${e}/comments`)))})),null===(d=document.getElementById("getCommentsByPostId"))||void 0===d||d.addEventListener("click",(()=>{var e;E((e=1,n.makeRequest(t.GET,`https://jsonplaceholder.typicode.com/comments?postId=${e}`)))})),null===(i=document.getElementById("createPost"))||void 0===i||i.addEventListener("click",(()=>{var e;E((e={title:"foo",body:"bar",userId:1},s(void 0,void 0,void 0,(function*(){return n.makeRequest(t.POST,"https://jsonplaceholder.typicode.com/posts",e)}))))})),null===(l=document.getElementById("patchPost"))||void 0===l||l.addEventListener("click",(()=>{var e,s;E((e=1,s={title:"foo"},n.makeRequest(t.PUT,`https://jsonplaceholder.typicode.com/posts/${e}`,s)))})),null===(r=document.getElementById("deletePost"))||void 0===r||r.addEventListener("click",(()=>{var e;E((e=1,n.makeRequest(t.DELETE,`https://jsonplaceholder.typicode.com/posts/${e}`)))})),null===(c=document.getElementById("getTodos"))||void 0===c||c.addEventListener("click",(()=>{E(n.makeRequest(t.GET,"https://jsonplaceholder.typicode.com/todos"))})),null===(u=document.getElementById("createTodo"))||void 0===u||u.addEventListener("click",(()=>{var e;E((e={title:"foo",completed:!1},n.makeRequest(t.POST,"https://jsonplaceholder.typicode.com/todos",e)))}))}();
//# sourceMappingURL=bundle.js.map