70 lines
No EOL
2 KiB
JavaScript
70 lines
No EOL
2 KiB
JavaScript
export default defineEventHandler(async (event) => {
|
|
const profile = getRouterParam(event, 'profile') // 'jennie' or 'henry'
|
|
|
|
// Get API token from environment variables
|
|
const apiToken = profile === 'jennie'
|
|
? process.env.WISE_API_KEY_JENNIE
|
|
: process.env.WISE_API_KEY_HENRY
|
|
|
|
if (!apiToken) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: `Wise API token not configured for ${profile}`
|
|
})
|
|
}
|
|
|
|
try {
|
|
// First, get the profile ID
|
|
const profileResponse = await fetch('https://api.wise.com/v1/profiles', {
|
|
headers: {
|
|
'Authorization': `Bearer ${apiToken}`,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
|
|
if (!profileResponse.ok) {
|
|
throw new Error(`Profile fetch failed: ${profileResponse.statusText}`)
|
|
}
|
|
|
|
const profiles = await profileResponse.json()
|
|
const personalProfile = profiles.find(p => p.type === 'personal')
|
|
|
|
if (!personalProfile) {
|
|
throw new Error('Personal profile not found')
|
|
}
|
|
|
|
// Get balances for the profile
|
|
const balancesResponse = await fetch(`https://api.wise.com/v4/profiles/${personalProfile.id}/balances?types=STANDARD`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${apiToken}`,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
|
|
if (!balancesResponse.ok) {
|
|
throw new Error(`Balances fetch failed: ${balancesResponse.statusText}`)
|
|
}
|
|
|
|
const balances = await balancesResponse.json()
|
|
|
|
// Filter and format the balances - include all currencies
|
|
const formattedBalances = balances
|
|
.filter(balance => balance.amount) // Only filter out balances without amount data
|
|
.map(balance => ({
|
|
currency: balance.amount.currency,
|
|
value: {
|
|
value: balance.amount.value
|
|
}
|
|
}))
|
|
|
|
return formattedBalances
|
|
|
|
} catch (error) {
|
|
console.error(`Wise API error for ${profile}:`, error)
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: `Failed to fetch Wise balances: ${error.message}`
|
|
})
|
|
}
|
|
}) |