* Execute a SQL query against the /api/sql endpoint. * * @param {string} sql - SQL query string * @returns {Promise } - The parsed JSON response
(sql)
| 16 | * @returns {Promise<Object>} - The parsed JSON response |
| 17 | */ |
| 18 | async function query(sql) { |
| 19 | const response = await fetch('/api/sql', { |
| 20 | method: 'POST', |
| 21 | body: JSON.stringify({ query: sql }), |
| 22 | headers: { 'Content-Type': 'application/json' }, |
| 23 | }); |
| 24 | if (!response.ok) { |
| 25 | const text = await response.text(); |
| 26 | throw `request failed: ${response.status} ${response.statusText}: ${text}`; |
| 27 | } |
| 28 | const data = await response.json(); |
| 29 | for (const result of data.results) { |
| 30 | if (result.error) { |
| 31 | throw `SQL error: ${result.error.message}`; |
| 32 | } |
| 33 | } |
| 34 | return data; |
| 35 | } |
no test coverage detected