import { IDEMPIERE_FETCH_TIMEOUT_MS, noteIdempiereError, clearIdempiereError } from '../utils/fetchHelper' export default defineEventHandler(async (event) => { const config = useRuntimeConfig() //const logshipSession = getCookie(event, 'logship_session') const logshipToken = getCookie(event, 'logship_it') //await useStorage().getItem('logship_token_'+logshipSession) event.context.fetch = async (url: string, method: string = 'GET', token: string, body: any) => { 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 } } /* // "if you want to see the request body on this endpoint" return body */ /* // "if you want to used the javascript fetch api" const res: any = await fetch(config.api.urlV1+'/'+url, { method: method, headers: options['headers'], body: JSON.stringify(options['body']) }); return await res.json(); */ /* // "try catch for 500 error not 401 unauthorized problem" console.log(options) if(options?.body?.C_OrderLine) { console.log(options.body.C_OrderLine) } if(options?.body?.C_InvoiceLine) { console.log(options.body.C_InvoiceLine) } try { return await $fetch(config.api.urlV1+'/'+url, { method: method, ...options }) } catch(error: any) { console.log(error) } */ // Debug logging only when explicitly enabled — never log options/headers // (they contain the Authorization Bearer token). if (process.env.IDEMPIERE_DEBUG === 'true') { console.log(`[fetch] ${method} ${url}`) console.log(JSON.stringify(options?.body || {})) } try { const res = await $fetch(config.api.url+'/'+url, { method: method, // No ofetch auto-retry against iDempiere (see fetchHelper.ts). retry: 0, timeout: IDEMPIERE_FETCH_TIMEOUT_MS, ...options }) clearIdempiereError(event) return res } catch (error: any) { noteIdempiereError(event, error, url) throw error } } })