import { MessageBuilder } from "../shared/message-side"; const messageBuilder = new MessageBuilder(); async function fetchDirectory(ctx) { try { const res = await fetch({ url: "https://directory.spaceapi.io", method: "GET", }); const resBody = typeof res.body === "string" ? JSON.parse(res.body) : res.body; ctx.response({ data: { success: true, result: resBody }, }); } catch (error) { ctx.response({ data: { success: false, result: {} }, }); } } async function fetchStatus(url, ctx) { try { const res = await fetch({ url: url, method: "GET", }); const resBody = typeof res.body === "string" ? JSON.parse(res.body) : res.body; ctx.response({ data: { success: true, result: resBody }, }); } catch (error) { ctx.response({ data: { success: false, result: {} }, }); } } AppSideService({ onInit() { messageBuilder.listen(() => {}); messageBuilder.on("request", (ctx) => { const jsonRpc = messageBuilder.buf2Json(ctx.request.payload); if (jsonRpc.method === "GET_DIRECTORY") { return fetchDirectory(ctx); } else if (jsonRpc.method === "GET_STATUS") { return fetchStatus(jsonRpc.url, ctx); } }); }, onRun() {}, onDestroy() {}, });