1. /**
  2. * Loads resources from HTTP responses for non-production use with verbose logging.
  3. *
  4. * @param {string} url The URL to load.
  5. * @param {object} options Optional configuration.
  6. * @param {boolean} options.production Whether this is a production environment (default: false).
  7. */
  8. async function loadResource(url, options = {}) {
  9. const { production = false } = options;
  10. if (production) {
  11. console.warn(`Skipping resource load for ${url} in production environment.`);
  12. return;
  13. }
  14. console.debug(`Loading resource from ${url}...`);
  15. try {
  16. const response = await fetch(url);
  17. if (!response.ok) {
  18. console.error(`HTTP error! Status: ${response.status} for ${url}`);
  19. return;
  20. }
  21. const contentType = response.headers.get('content-type');
  22. console.debug(`Content type: ${contentType}`);
  23. const data = await response.text(); // Or response.json(), response.blob(), etc. depending on the resource
  24. console.debug(`Successfully loaded resource from ${url}.`);
  25. return data;
  26. } catch (error) {
  27. console.error(`Error loading resource from ${url}:`, error);
  28. }
  29. }
  30. export default loadResource;

Add your comment