1. 1 : /**
  2. 2 : * Class embodying Load functionality.
  3. 3 : * @memberof Spyral
  4. 4 : * @class
  5. 5 : * @hideconstructor
  6. 6 : */
  7. 7 : class Load {
  8. 8 : static baseUrl;
  9. 9 :
  10. 10 : /**
  11. 11 : * Set the base URL for use with the Load class
  12. 12 : * @param {string} baseUrl
  13. 13 : * @static
  14. 14 : */
  15. 15 : static setBaseUrl(baseUrl) {
  16. 16 : this.baseUrl = baseUrl;
  17. 17 : }
  18. 18 :
  19. 19 : /**
  20. 20 : * Make a call to trombone
  21. 21 : * @param {Object} config
  22. 22 : * @param {Object} params
  23. 23 : * @returns {JSON}
  24. 24 : * @static
  25. 25 : */
  26. 26 : static trombone(config = {}, params) {
  27. 27 : let url = new URL(config.trombone ? config.trombone : this.baseUrl + 'trombone', window.location.origin);
  28. 28 : delete config.trombone;
  29. 29 :
  30. 30 : let all = { ...config, ...params };
  31. 31 : for (let key in all) {
  32. 32 : if (all[key] === undefined) { delete all[key]; }
  33. 33 : }
  34. 34 :
  35. 35 : let method = all.method;
  36. 36 : if (method === undefined) {
  37. 37 : method = 'GET';
  38. 38 : } else {
  39. 39 : delete all.method;
  40. 40 : }
  41. 41 :
  42. 42 : let opt = {};
  43. 43 : if (method === 'GET' || method === 'POST') {
  44. 44 : if (method === 'POST' || JSON.stringify(all).length > 1000) {
  45. 45 : opt = {
  46. 46 : method: 'POST'
  47. 47 : };
  48. 48 : if ('body' in all) {
  49. 49 : // TODO assume FormData or set this header to ensure UTF-8?
  50. 50 : // opt.headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' };
  51. 51 : opt.body = all['body'];
  52. 52 : } else {
  53. 53 : // don't set header as it messes up boundaries
  54. 54 : const formData = new FormData();
  55. 55 : for (let key in all) {
  56. 56 : if (all[key] instanceof Array) {
  57. 57 : all[key].forEach((val) => {
  58. 58 : formData.append(key, val);
  59. 59 : });
  60. 60 : } else {
  61. 61 : formData.set(key, all[key]);
  62. 62 : }
  63. 63 : }
  64. 64 : opt.body = formData;
  65. 65 : }
  66. 66 : } else {
  67. 67 : for (let key in all) {
  68. 68 : if (all[key] instanceof Array) {
  69. 69 : all[key].forEach((val) => {
  70. 70 : url.searchParams.append(key, val);
  71. 71 : });
  72. 72 : } else {
  73. 73 : url.searchParams.set(key, all[key]);
  74. 74 : }
  75. 75 : }
  76. 76 : }
  77. 77 : } else {
  78. 78 : throw Error('Load.trombone: unsupported method:', method);
  79. 79 : }
  80. 80 :
  81. 81 : return fetch(url.toString(), opt).then(response => {
  82. 82 : if (response.ok) {
  83. 83 : return response.json();
  84. 84 : }
  85. 85 : else {
  86. 86 : return response.text().then(text => {
  87. 87 : if (window.console) { console.error(text); }
  88. 88 : throw Error(text);
  89. 89 : });
  90. 90 : }
  91. 91 : });
  92. 92 : }
  93. 93 :
  94. 94 : /**
  95. 95 : * Fetch content from a URL, often resolving cross-domain data constraints
  96. 96 : * @param {string} urlToFetch
  97. 97 : * @param {Object} config
  98. 98 : * @returns {Response}
  99. 99 : * @static
  100. 100 : */
  101. 101 : static load(urlToFetch, config) {
  102. 102 : let url = new URL(config && config.trombone ? config.trombone : this.baseUrl + 'trombone');
  103. 103 : url.searchParams.set('fetchData', urlToFetch);
  104. 104 : return fetch(url.toString()).then(response => {
  105. 105 : if (response.ok) {
  106. 106 : return response;
  107. 107 : }
  108. 108 : else {
  109. 109 : return response.text().then(text => {
  110. 110 : if (window.console) { console.error(text); }
  111. 111 : throw Error(text);
  112. 112 : });
  113. 113 : }
  114. 114 : }).catch(err => { throw err; });
  115. 115 : }
  116. 116 :
  117. 117 : /**
  118. 118 : * Fetch HTML content from a URL
  119. 119 : * @param {string} url
  120. 120 : * @returns {Document}
  121. 121 : * @static
  122. 122 : */
  123. 123 : static html(url) {
  124. 124 : return this.text(url).then(text => new DOMParser().parseFromString(text, 'text/html'));
  125. 125 : }
  126. 126 :
  127. 127 : /**
  128. 128 : * Fetch XML content from a URL
  129. 129 : * @param {string} url
  130. 130 : * @returns {XMLDocument}
  131. 131 : * @static
  132. 132 : */
  133. 133 : static xml(url) {
  134. 134 : return this.text(url).then(text => new DOMParser().parseFromString(text, 'text/xml'));
  135. 135 : }
  136. 136 :
  137. 137 : /**
  138. 138 : * Fetch JSON content from a URL
  139. 139 : * @param {string} url
  140. 140 : * @returns {JSON}
  141. 141 : * @static
  142. 142 : */
  143. 143 : static json(url) {
  144. 144 : return this.load(url).then(response => response.json());
  145. 145 : }
  146. 146 :
  147. 147 : /**
  148. 148 : * Fetch text content from a URL
  149. 149 : * @param {string} url
  150. 150 : * @returns {string}
  151. 151 : * @static
  152. 152 : */
  153. 153 : static text(url) {
  154. 154 : return this.load(url).then(response => response.text());
  155. 155 : }
  156. 156 : }
  157. 157 :
  158. 158 : export default Load;