Skip to content

Commit

Permalink
Merge pull request #102 from Bitcoin-com/stage
Browse files Browse the repository at this point in the history
More update from Panda Cash
  • Loading branch information
cgcardona authored Nov 7, 2018
2 parents de867a0 + 289ae9c commit fe117d8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 63 deletions.
27 changes: 8 additions & 19 deletions dist/routes/v2/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var express = require("express");
var router = express.Router();
var requestUtils = require("./services/requestUtils");
var bitbox = require("./services/bitbox");
var axios_1 = require("axios");
var router = express.Router();
var BitboxHTTP = bitbox.getInstance();
var RateLimit = require("express-rate-limit");
var BitboxHTTP = axios_1.default.create({
baseURL: process.env.RPC_BASEURL
});
var username = process.env.RPC_USERNAME;
var password = process.env.RPC_PASSWORD;
var requestConfig = {
method: "post",
auth: {
username: username,
password: password
},
data: {
jsonrpc: "1.0"
}
};
var config = {
blockRateLimit1: undefined,
blockRateLimit2: undefined,
Expand Down Expand Up @@ -105,11 +93,12 @@ router.get("/detailsByHash/:hash", config.blockRateLimit2, function (req, res, n
});
}); });
router.get("/detailsByHeight/:height", config.blockRateLimit2, function (req, res, next) { return __awaiter(_this, void 0, void 0, function () {
var requestConfig;
var _this = this;
return __generator(this, function (_a) {
requestConfig.data.id = "getblockhash";
requestConfig.data.method = "getblockhash";
requestConfig.data.params = [parseInt(req.params.height)];
requestConfig = requestUtils.getRequestConfig("getblockhash", [
parseInt(req.params.height)
]);
BitboxHTTP(requestConfig)
.then(function (response) { return __awaiter(_this, void 0, void 0, function () {
var rsp, parsed, error_2;
Expand Down
7 changes: 7 additions & 0 deletions dist/routes/v2/services/bitbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios");
var BitboxHTTP = axios_1.default.create({
baseURL: process.env.RPC_BASEURL
});
exports.getInstance = function () { return BitboxHTTP; };
19 changes: 19 additions & 0 deletions dist/routes/v2/services/requestUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var username = process.env.RPC_USERNAME;
var password = process.env.RPC_PASSWORD;
exports.getRequestConfig = function (method, params) {
return {
method: "post",
auth: {
username: username,
password: password
},
data: {
jsonrpc: "1.0",
id: method,
method: method,
params: params
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rest.bitcoin.com",
"version": "1.10.4",
"version": "1.10.5",
"description": "REST API for Bitcoin.com's Cloud",
"author": "Gabriel Cardona @ Bitcoin.com",
"license": "MIT",
Expand Down
57 changes: 15 additions & 42 deletions src/routes/v2/block.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
"use strict"

import * as express from "express"
const router = express.Router()
import * as requestUtils from "./services/requestUtils"
import * as bitbox from "./services/bitbox"

import axios from "axios"
import { IRequestConfig } from "./interfaces/IRequestConfig"
const RateLimit = require("express-rate-limit")

const BitboxHTTP = axios.create({
baseURL: process.env.RPC_BASEURL
})
const username = process.env.RPC_USERNAME
const password = process.env.RPC_PASSWORD
const router: express.Router = express.Router()
const BitboxHTTP = bitbox.getInstance()

const requestConfig: IRequestConfig = {
method: "post",
auth: {
username: username,
password: password
},
data: {
jsonrpc: "1.0"
}
}
const RateLimit = require("express-rate-limit")

interface IRLConfig {
[blockRateLimit1: string]: any
Expand Down Expand Up @@ -54,26 +42,14 @@ while (i < 4) {
i++
}

router.get(
"/",
config.blockRateLimit1,
async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
res.json({ status: "block" })
}
)
router.get("/", config.blockRateLimit1, async (req, res, next) => {
res.json({ status: "block" })
})

router.get(
"/detailsByHash/:hash",
config.blockRateLimit2,
async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
async (req, res, next) => {
try {
const response = await axios.get(
`${process.env.BITCOINCOM_BASEURL}block/${req.params.hash}`
Expand All @@ -90,14 +66,11 @@ router.get(
router.get(
"/detailsByHeight/:height",
config.blockRateLimit2,
async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
requestConfig.data.id = "getblockhash"
requestConfig.data.method = "getblockhash"
requestConfig.data.params = [parseInt(req.params.height)]
async (req, res, next) => {
const requestConfig = requestUtils.getRequestConfig("getblockhash", [
parseInt(req.params.height)
])

BitboxHTTP(requestConfig)
.then(async response => {
try {
Expand Down
7 changes: 7 additions & 0 deletions src/routes/v2/services/bitbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import axios from "axios"

const BitboxHTTP = axios.create({
baseURL: process.env.RPC_BASEURL
})

export const getInstance = () => BitboxHTTP
25 changes: 25 additions & 0 deletions src/routes/v2/services/requestUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IRequestConfig } from "../interfaces/IRequestConfig"

const username = process.env.RPC_USERNAME
const password = process.env.RPC_PASSWORD

type RPCMethod = "getblockhash"

export const getRequestConfig = (
method: RPCMethod,
params: (string | number)[]
): IRequestConfig => {
return {
method: "post",
auth: {
username,
password
},
data: {
jsonrpc: "1.0",
id: method,
method,
params
}
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"module": "commonjs",
"target": "es5",
"allowJs": true,
"lib": ["es2015"],
"lib": ["es2015", "es2015.symbol", "dom"],
"typeRoots": ["node_modules/@types/"]
},
"include": ["src/**/*"],
Expand Down

0 comments on commit fe117d8

Please sign in to comment.