UNPKG

@modulae.systems/rpc-server

Version:
102 lines (101 loc) 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initRpcWithApiCallback = initRpcWithApiCallback; const express = require("express"); const dotenv = require("dotenv"); const cors = require("cors"); dotenv.config(); const app = express(); app.use(express.json()); app.use(cors()); let publicCallbackFunc; let protectedCallbackFunc; let authCallbackFunc; app.post("/rpc/public", async (req, res) => { const content = req.body; const headers = req.headers; try { if (publicCallbackFunc) { let response = await publicCallbackFunc(content.method, content.data); res.json(response); } else { res.status(404).json({ error: { code: 404, description: "Endpoint is not configured", }, data: null, }); } } catch (e) { const errResponse = { error: { code: 500, description: "Something went wrong", debug: e, }, data: null, }; res.status(500).json(errResponse); } }); app.post("/rpc/protected", async (req, res) => { const content = req.body; const headers = req.headers; try { if (protectedCallbackFunc && authCallbackFunc) { if (headers.authorization) { const token = headers.authorization.split(" ")[1]; // remove bearer or Bearer if (authCallbackFunc(content.method, token)) { let response = await protectedCallbackFunc(content.method, content.data); res.json(response); return; } } res.status(401).json({ error: { code: 401, description: "Not Authorised", }, data: null, }); } else { res.status(404).json({ error: { code: 404, description: "Endpoint is not configured", }, data: null, }); } } catch (e) { const errResponse = { error: { code: 500, description: "Something went wrong", debug: e, }, data: null, }; res.status(500).json(errResponse); } }); /** * This function initiates the RPC API, saving the publicCallback, protectedCallback (if defined) and authorizationCallback (if defined). All method from this RPC will go through these 3 methods. * * @param publicCallback A callback where you get the body and the header. Define your logic on what to do when a method is called. * @param b The second number. */ function initRpcWithApiCallback(publicCallback, protectedCallback, authorizationCallback) { publicCallbackFunc = publicCallback; protectedCallbackFunc = protectedCallback; authCallbackFunc = authorizationCallback; const port = process.env.RPC_PORT; app.listen(port, () => { console.log(`[server]: Server is running at http://localhost:${port}`); }); }