lofig.js 792 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. window.lofig = {
  2. folder: '/config/default.json',
  3. config: null,
  4. fetchConfig: async () => {
  5. if (!lofig.config) {
  6. const response = await fetch(lofig.folder);
  7. if (!response.ok) {
  8. const message = `An error has occured: ${response.status}`;
  9. throw new Error(message);
  10. }
  11. lofig.config = response.json();
  12. }
  13. return lofig.config;
  14. },
  15. get: async (query, cb) => {
  16. let json = await lofig.fetchConfig();
  17. query.split('.')
  18. .forEach(property => {
  19. json = json[property];
  20. });
  21. if (cb) return cb(json);
  22. return json;
  23. },
  24. has: async (query, cb) => {
  25. let json = await lofig.fetchConfig();
  26. query.split('.')
  27. .forEach(property => {
  28. json = json[property];
  29. });
  30. if (cb) return cb(!!json);
  31. return !!json;
  32. }
  33. };