(url, options = {})
| 51 | }; |
| 52 | |
| 53 | const request = async (url, options = {}) => { |
| 54 | if ( |
| 55 | options.body && |
| 56 | !(options.body instanceof FormData) && |
| 57 | typeof options.body === 'object' |
| 58 | ) { |
| 59 | options.body = JSON.stringify(options.body); |
| 60 | options.headers = { |
| 61 | ...options.headers, |
| 62 | 'Content-Type': 'application/json', |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | if (options.auth !== false) { |
| 67 | options.headers = { |
| 68 | ...options.headers, |
| 69 | Authorization: `Bearer ${await API.getAuthToken()}`, |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | const response = await fetch(url, options); |
| 74 | |
| 75 | if (!response.ok) { |
| 76 | const error = new Error(`HTTP error! Status: ${response.status}`); |
| 77 | |
| 78 | let errorBody = await response.text(); |
| 79 | |
| 80 | try { |
| 81 | errorBody = JSON.parse(errorBody); |
| 82 | } catch (e) { |
| 83 | // If parsing fails, leave errorBody as the raw text |
| 84 | } |
| 85 | |
| 86 | error.status = response.status; |
| 87 | error.response = response; |
| 88 | error.body = errorBody; |
| 89 | |
| 90 | throw error; |
| 91 | } |
| 92 | |
| 93 | try { |
| 94 | const retval = await response.json(); |
| 95 | return retval; |
| 96 | } catch (e) { |
| 97 | return ''; |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | export default class API { |
| 102 | static lastQueryParams = new URLSearchParams(); |
no test coverage detected