You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
import { createNIP98Auth } from './auth.js'; |
|
|
|
/** |
|
* Make authenticated API request |
|
*/ |
|
export async function apiRequest(server, endpoint, method = 'GET', body = null, options = {}) { |
|
const url = `${server.replace(/\/$/, '')}/api${endpoint}`; |
|
const authHeader = createNIP98Auth(url, method, body); |
|
|
|
const headers = { |
|
'Authorization': authHeader, |
|
'Content-Type': 'application/json' |
|
}; |
|
|
|
const fetchOptions = { |
|
method, |
|
headers, |
|
...options |
|
}; |
|
|
|
if (body && method !== 'GET') { |
|
fetchOptions.body = typeof body === 'string' ? body : JSON.stringify(body); |
|
} |
|
|
|
const response = await fetch(url, fetchOptions); |
|
const text = await response.text(); |
|
|
|
let data; |
|
try { |
|
data = JSON.parse(text); |
|
} catch { |
|
data = text; |
|
} |
|
|
|
if (!response.ok) { |
|
// Sanitize error message to prevent key leaks |
|
const { sanitizeErrorMessage } = await import('./error-sanitizer.js'); |
|
const errorData = typeof data === 'object' ? JSON.stringify(data, null, 2) : String(data); |
|
const sanitizedData = sanitizeErrorMessage(errorData); |
|
throw new Error(`API request failed: ${response.status} ${response.statusText}\n${sanitizedData}`); |
|
} |
|
|
|
return data; |
|
}
|
|
|