envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
160 lines (148 loc) • 4.04 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Env from "../../Env.res.mjs";
import * as Rest from "../../vendored/Rest.res.mjs";
import * as Utils from "../../Utils.res.mjs";
import * as React from "react";
import * as Logging from "../../Logging.res.mjs";
import * as ChainMap from "../../ChainMap.res.mjs";
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
let bodySchema = S$RescriptSchema.object(s => ({
envioVersion: s.f("envioVersion", S$RescriptSchema.string),
envioApiToken: s.f("envioApiToken", S$RescriptSchema.option(S$RescriptSchema.string)),
ecosystem: s.f("ecosystem", S$RescriptSchema.$$enum([
"evm",
"fuel",
"svm"
])),
hyperSyncNetworks: s.f("hyperSyncNetworks", S$RescriptSchema.array(S$RescriptSchema.int)),
rpcNetworks: s.f("rpcNetworks", S$RescriptSchema.array(S$RescriptSchema.int))
}));
function makeBody(envioVersion, envioApiToken, config) {
let hyperSyncNetworks = [];
let rpcNetworks = [];
ChainMap.values(config.chainMap).forEach(param => {
let sourceConfig = param.sourceConfig;
let usesHyperSync;
switch (sourceConfig.TAG) {
case "EvmSourceConfig" :
usesHyperSync = sourceConfig.hypersync !== undefined;
break;
case "FuelSourceConfig" :
usesHyperSync = true;
break;
case "SvmSourceConfig" :
usesHyperSync = false;
break;
case "CustomSources" :
usesHyperSync = sourceConfig._0.some(s => s.poweredByHyperSync);
break;
}
(
usesHyperSync ? hyperSyncNetworks : rpcNetworks
).push(param.id);
});
return {
envioVersion: envioVersion,
envioApiToken: envioApiToken,
ecosystem: config.ecosystem.name,
hyperSyncNetworks: hyperSyncNetworks,
rpcNetworks: rpcNetworks
};
}
function toTheme(color) {
switch (color) {
case "primary" :
return "#9860E5";
case "secondary" :
return "#FFBB2F";
case "info" :
return "#6CBFEE";
case "danger" :
return "#FF8269";
case "success" :
return "#3B8C3D";
case "white" :
return "white";
case "gray" :
return "gray";
}
}
let messageSchema = S$RescriptSchema.object(s => ({
color: s.f("color", S$RescriptSchema.$$enum([
"primary",
"secondary",
"info",
"danger",
"success",
"white",
"gray"
])),
content: s.f("content", S$RescriptSchema.string)
}));
let client = Rest.client(Env.envioAppUrl + "/api", undefined);
function route() {
return {
method: "POST",
path: "/hyperindex/init",
input: s => s.body(bodySchema),
responses: [s => s.field("messages", S$RescriptSchema.array(messageSchema))]
};
}
async function getMessages(config) {
let envioVersion = Utils.EnvioPackage.value.version;
let body = makeBody(envioVersion, Env.envioApiToken, config);
let messages;
try {
messages = await Rest.fetch(route, body, client);
} catch (raw_exn) {
let exn = Primitive_exceptions.internalToException(raw_exn);
return {
TAG: "Error",
_0: exn
};
}
return {
TAG: "Ok",
_0: messages
};
}
let InitApi = {
bodySchema: bodySchema,
makeBody: makeBody,
toTheme: toTheme,
messageSchema: messageSchema,
client: client,
route: route,
getMessages: getMessages
};
function useMessages(config) {
let match = React.useState(() => "Loading");
let setRequest = match[1];
React.useEffect(() => {
getMessages(config).then(res => {
if (res.TAG === "Ok") {
let data = res._0;
return setRequest(param => ({
TAG: "Data",
_0: data
}));
}
let e = res._0;
Logging.error({
msg: "Failed to load messages from envio server",
err: Utils.prettifyExn(e)
});
setRequest(param => ({
TAG: "Err",
_0: e
}));
});
}, []);
return match[0];
}
export {
InitApi,
useMessages,
}
/* bodySchema Not a pure module */