import { Client, Users } from 'node-appwrite'; import { XMLParser } from 'fast-xml-parser'; // This Appwrite function will be executed every time your function is triggered export default async ({ req, res, log, error }: any) => { // You can use the Appwrite SDK to interact with other services // For this example, we're using the Users service const client = new Client() .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT ?? '') .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID ?? '') .setKey(req.headers['x-appwrite-key'] ?? ''); const users = new Users(client); try { const response = await fetch('https://www.aemet.es/xml/municipios_h/localidad_h_31019.xml'); if (!response.ok) { throw new Error(`Error! status: ${response.status}`); } const decoder = new TextDecoder('iso-8859-1'); // It should be iso-8859-15 but Bun 1.1 does not support it let xml = decoder.decode(await response.arrayBuffer()) const parser = new XMLParser(); let json = parser.parse(xml); log(json) return res.json({sucess: true, data: json}) } catch(err: any) { error("Error fetching forecast: " + err.message); return res.json({success: false, error: err.message}) } };