export default async function fetchHelper(event: any, url: string, method: string = 'GET', token: string, body: any) { const config = useRuntimeConfig() //const logshipSession = getCookie(event, 'logship_session') const logshipToken = getCookie(event, 'logship_it') //await useStorage().getItem('logship_token_'+logshipSession) let options: any = {} if(token || logshipToken) { options = { ...options, headers: { 'Content-Type': 'application/json', Accept: 'application/json', Authorization: 'Bearer '+(token || logshipToken) } } } if(body) { options = { ...options, body: body } } const fullUrl = config.api.url+'/'+url // DEBUG: Log iDempiere API request console.log('╔════════════════════════════════════════════════════════') console.log('║ iDEMPIERE API REQUEST') console.log('╠════════════════════════════════════════════════════════') console.log('║ Timestamp:', new Date().toISOString()) console.log('║ Method:', method) console.log('║ URL:', fullUrl) console.log('║ Headers:', JSON.stringify({ ...options.headers, Authorization: options.headers?.Authorization ? 'Bearer [REDACTED]' : undefined }, null, 2)) if(body) { console.log('║ Body:', JSON.stringify(body, null, 2)) } console.log('╚════════════════════════════════════════════════════════') const startTime = Date.now() try { const response = await $fetch(fullUrl, { method: method, ...options }) const duration = Date.now() - startTime // DEBUG: Log iDempiere API response console.log('╔════════════════════════════════════════════════════════') console.log('║ iDEMPIERE API RESPONSE') console.log('╠════════════════════════════════════════════════════════') console.log('║ Timestamp:', new Date().toISOString()) console.log('║ Method:', method) console.log('║ URL:', fullUrl) console.log('║ Duration:', duration, 'ms') console.log('║ Status: SUCCESS') console.log('║ Response:', JSON.stringify(response, null, 2)) console.log('╚════════════════════════════════════════════════════════') return response } catch (error: any) { const duration = Date.now() - startTime // DEBUG: Log iDempiere API error console.log('╔════════════════════════════════════════════════════════') console.log('║ iDEMPIERE API ERROR') console.log('╠════════════════════════════════════════════════════════') console.log('║ Timestamp:', new Date().toISOString()) console.log('║ Method:', method) console.log('║ URL:', fullUrl) console.log('║ Duration:', duration, 'ms') console.log('║ Status: ERROR') console.log('║ Error:', JSON.stringify(error, null, 2)) console.log('╚════════════════════════════════════════════════════════') throw error } }