Skip to content

Commit

Permalink
fix dumb issue
Browse files Browse the repository at this point in the history
  • Loading branch information
NeutronicMC committed Jun 20, 2020
1 parent 274549d commit 47bd06e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./lib/HiveApi");
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "hive-tools-wrapper",
"version": "0.1.0",
"version": "0.1.1",
"description": "Advanced Hive Bedrock API wrapper with caching.",
"main": "/src/HiveApi.ts",
"main": "index.js",
"files": [
"lib/**/*"
],
Expand Down
22 changes: 11 additions & 11 deletions src/api/ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Axios, {AxiosResponse} from "axios";

export interface params {
export interface Params {
[key: string]: any;
}

interface cacheData {
interface CacheData {
[key: string]: {
timestamp: number,
data: AxiosResponse
Expand All @@ -22,7 +22,7 @@ export default class ApiClient {
return await Axios.get(this.buildUrl(path, pathParams));
}

public buildUrl(path: string, pathParams: params) {
public buildUrl(path: string, pathParams: Params) {
if (!path.match(/^\//)) {
path = '/' + path;
}
Expand All @@ -34,7 +34,7 @@ export default class ApiClient {
}

public static paramToString(param: any): string {
if (param == undefined) {
if (param === undefined) {
return '';
}
if (param instanceof Date) {
Expand All @@ -44,7 +44,7 @@ export default class ApiClient {
return param.toString();
}

public static buildPath(path: string, pathParams: params): string {
public static buildPath(path: string, pathParams: Params): string {
return path.replace(/\{([\w-]+)\}/g, (fullMatch, key) => {
let value;
if (pathParams.hasOwnProperty(key)) {
Expand All @@ -57,21 +57,21 @@ export default class ApiClient {
});
}

cacheData: cacheData = {};
public async getData(path: string, params: params, cache: boolean) {
let key = ApiClient.buildPath(path, params);
let now = new Date().getTime();
cacheData: CacheData = {};
public async getData(path: string, pathParams: Params, cache: boolean) {
const key = ApiClient.buildPath(path, pathParams);
const now = new Date().getTime();
if (this.cacheData.hasOwnProperty(key) && cache) {
if (this.cacheData[key].timestamp + (this.cacheTimeout * 1000) < now) {
this.cacheData[key] = {
timestamp: now,
data: await this.callApi(path, params)
data: await this.callApi(path, pathParams)
}
}
} else {
this.cacheData[key] = {
timestamp: now,
data: await this.callApi(path, params)
data: await this.callApi(path, pathParams)
}
}
return this.cacheData[key].data;
Expand Down

0 comments on commit 47bd06e

Please sign in to comment.