Initial commit
This commit is contained in:
commit
c310cd7943
19 changed files with 3939 additions and 0 deletions
60
app-side/index.js
Normal file
60
app-side/index.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
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() {},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue