Skip to content

Commit

Permalink
Merge pull request #6 from quiknode-labs/sc-63518-marketplace-starter…
Browse files Browse the repository at this point in the history
…-js-missing-transformer

[sc-63518] add Marketplace starter JS missing transformer
  • Loading branch information
jcotillo authored May 2, 2023
2 parents 6dad2ef + ddba624 commit 891db2e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
72 changes: 72 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "Luc Castera <[email protected]> (https://github.com/luccastera)",
"license": "MIT",
"dependencies": {
"axios": "^1.4.0",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
Expand Down
9 changes: 8 additions & 1 deletion src/routes/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Router } from "express";

const router = Router();

import models, { sequelize } from "../models";
import models from "../models";
import RPCRequestTranslator from "../translator";

router.post("/", async (request, response) => {
console.log(request.body);
Expand Down Expand Up @@ -53,13 +54,19 @@ router.post("/", async (request, response) => {
} else {
// If you need to make a request to the QuickNode endpoint here,
// then you can use endpoint.get('http_url') to get the URL of the endpoint.
const newTranslator = new RPCRequestTranslator();
const translatedRequestResponse = await newTranslator.translateRequest(
rpcMethod,
rpcParams
);

response.status(200).json({
id: 1,
result: {
message: "Welcome to the JSON-RPC API.",
method: rpcMethod,
params: rpcParams,
result: translatedRequestResponse,
},
jsonrpc: "2.0",
});
Expand Down
31 changes: 31 additions & 0 deletions src/translator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axios from "axios"; // Import the Axios library

class RPCRequestTranslator {
constructor() {
this.API_BASE_URL = "http://example.com/";
this.METHOD_PATHS = { qn_test: "" };
}

async translateRequest(rpcMethod, rpcParams) {
console.log(" matched method to: " + this.METHOD_PATHS[rpcMethod]);
const translatedRequest = {
method: "get",
url: `${this.API_BASE_URL}${this.METHOD_PATHS[rpcMethod]}`,
body: rpcParams,
headers: {
"Content-Type": "application/json",
"X-Custom-Header": "some-value",
},
};

try {
// Use the Axios library to perform the translated request
const response = await axios(translatedRequest);
return response.data;
} catch (error) {
throw new Error(`Error performing request: ${error.message}`);
}
}
}

export default RPCRequestTranslator;

0 comments on commit 891db2e

Please sign in to comment.