helper.js 960 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. let host = "http://localhost/api-wash";
  2. let f = async (url, method="get", token=false, data=[]) => {
  3. let options = {
  4. method: method.toUpperCase(),
  5. headers: {
  6. "Content-type": "application/json"
  7. }
  8. }
  9. if (token) {
  10. options.headers["Authorization"] = `Bearer ${token}`
  11. }
  12. if (["post", "patch", "put"].includes(method)) {
  13. options.body = JSON.stringify(data)
  14. }
  15. return await fetch(`${host}/${url}`, options).then(res=>res.json());
  16. }
  17. const dEvent = (event, detail, el) => {
  18. if (!el) {
  19. document.dispatchEvent(new CustomEvent(
  20. event, {detail:detail}
  21. ))
  22. } else {
  23. el.dispatchEvent(new CustomEvent(
  24. event, {detail:detail}
  25. ))
  26. }
  27. }
  28. const bindEvents = (name, type, callback ) => {
  29. console.log("BIND EVENT - " + name)
  30. document.addEventListener(type,(e) => {
  31. callback(e);
  32. })
  33. }
  34. export {f, dEvent, bindEvents}