fix 4
This commit is contained in:
@@ -16,6 +16,15 @@ function adminHeaders() {
|
||||
}
|
||||
}
|
||||
|
||||
function formatRequestBody(body, method) {
|
||||
if (!body || method === 'GET' || method === 'DELETE') return body
|
||||
if (body.data !== undefined) return body
|
||||
const { documentId, ...fields } = body
|
||||
const payload = { data: fields }
|
||||
if (documentId) payload.documentId = documentId
|
||||
return payload
|
||||
}
|
||||
|
||||
async function adminFetch(path, { method = 'GET', body, queries = [] } = {}) {
|
||||
if (!config.appwrite.apiKey) {
|
||||
const error = new Error('APPWRITE_API_KEY fehlt in .env')
|
||||
@@ -28,10 +37,12 @@ async function adminFetch(path, { method = 'GET', body, queries = [] } = {}) {
|
||||
url.searchParams.append('queries[]', q)
|
||||
}
|
||||
|
||||
const requestBody = formatRequestBody(body, method)
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method,
|
||||
headers: adminHeaders(),
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
body: requestBody ? JSON.stringify(requestBody) : undefined,
|
||||
})
|
||||
|
||||
const text = await response.text()
|
||||
@@ -48,6 +59,23 @@ async function adminFetch(path, { method = 'GET', body, queries = [] } = {}) {
|
||||
const error = new Error(data?.message || `Appwrite ${response.status}`)
|
||||
error.status = response.status >= 500 ? 500 : response.status
|
||||
error.code = data?.code
|
||||
error.type = data?.type
|
||||
if (response.status === 401 && data?.type === 'user_unauthorized') {
|
||||
// #region agent log
|
||||
fetch('http://127.0.0.1:7281/ingest/30e8e71c-b377-4e72-84f9-593826c6d234', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'X-Debug-Session-Id': '80bbfc' },
|
||||
body: JSON.stringify({
|
||||
sessionId: '80bbfc',
|
||||
location: 'appwriteAdmin.js:adminFetch',
|
||||
message: 'API key unauthorized',
|
||||
data: { path, status: response.status, type: data?.type, code: data?.code },
|
||||
hypothesisId: 'H9',
|
||||
timestamp: Date.now(),
|
||||
}),
|
||||
}).catch(() => {})
|
||||
// #endregion
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
@@ -142,6 +170,24 @@ export async function upsertWebsiteProjectByRepo(repoFullName, data) {
|
||||
})
|
||||
}
|
||||
|
||||
export async function verifyDatabaseAccess() {
|
||||
if (!config.appwrite.apiKey) {
|
||||
return { ok: false, reason: 'APPWRITE_API_KEY fehlt' }
|
||||
}
|
||||
try {
|
||||
await listDocuments(config.collections.customers, [Query.limit(1)])
|
||||
return { ok: true }
|
||||
} catch (err) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: err.message,
|
||||
code: err.code,
|
||||
type: err.type,
|
||||
status: err.status,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createAdminClient() {
|
||||
return { usesNativeFetch: true, databaseId: WOMS_DATABASE_ID }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user