Skip to content

Commit a2d2969

Browse files
author
Charles Thibault
committed
change dev pattern to forwarding class members
1 parent aae1613 commit a2d2969

23 files changed

+365
-179
lines changed

Diff for: dist/helpers/object.d.ts

-2
This file was deleted.

Diff for: dist/helpers/object.js

-13
This file was deleted.

Diff for: dist/index.d.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import Options, { OptionsProps } from './modules/Options';
2+
import Filters from './modules/Filters';
13
import Query from './modules/Query';
2-
import { Options } from './types';
34
export default class Algolib {
4-
protected options: Options;
5+
options: Options;
6+
filters: Filters;
57
query: Query;
6-
constructor(options?: Options);
8+
constructor(userOptions?: OptionsProps);
79
}

Diff for: dist/index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6-
var lodash_1 = require("lodash");
6+
var Options_1 = __importDefault(require("./modules/Options"));
7+
var Filters_1 = __importDefault(require("./modules/Filters"));
78
var Query_1 = __importDefault(require("./modules/Query"));
89
var Algolib = /** @class */ (function () {
9-
function Algolib(options) {
10-
this.options = (0, lodash_1.merge)({
11-
mode: 'MAINNET',
12-
indexerAPI: 'https://mainnet-idx.algonode.cloud',
13-
nodeAPI: 'https://mainnet-api.algonode.cloud',
14-
}, options);
15-
this.query = new Query_1.default(this.options);
10+
function Algolib(userOptions) {
11+
this.options = new Options_1.default(userOptions);
12+
this.filters = new Filters_1.default(this);
13+
this.query = new Query_1.default(this);
1614
}
1715
return Algolib;
1816
}());

Diff for: dist/modules/Base.d.ts

-5
This file was deleted.

Diff for: dist/modules/Base.js

-9
This file was deleted.

Diff for: dist/modules/Filters.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Algolib from '../index';
2+
import Options, { Cases } from './Options';
3+
export interface FiltersProps {
4+
[key: string]: string | number | boolean | undefined;
5+
}
6+
export default class Filters {
7+
protected options: Options;
8+
constructor(forwarded: Algolib);
9+
convertCase(obj: Object, toCase?: Cases): any;
10+
convertCaseOut: (obj: Object, toCase?: Cases) => any;
11+
convertCaseIn(obj: Object): any;
12+
stringifyValues(params: object): {
13+
[k: string]: string;
14+
};
15+
}

Diff for: dist/modules/Filters.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
var camelcase_keys_1 = __importDefault(require("camelcase-keys"));
7+
var snakecase_keys_1 = __importDefault(require("snakecase-keys"));
8+
var kebabcase_keys_1 = __importDefault(require("kebabcase-keys"));
9+
//
10+
// Filters class
11+
// ----------------------------------------------
12+
var Filters = /** @class */ (function () {
13+
function Filters(forwarded) {
14+
this.convertCaseOut = this.convertCase;
15+
this.options = forwarded.options;
16+
}
17+
//
18+
// Convert object key case
19+
// ----------------------------------------------
20+
Filters.prototype.convertCase = function (obj, toCase) {
21+
if (toCase === void 0) { toCase = this.options.convertCase; }
22+
if (toCase === 'none')
23+
return obj;
24+
if (toCase === 'camelcase')
25+
return (0, camelcase_keys_1.default)(obj, { deep: true });
26+
if (toCase === 'snakecase')
27+
return (0, snakecase_keys_1.default)(obj, { deep: true });
28+
if (toCase === 'kebabcase')
29+
return (0, kebabcase_keys_1.default)(obj, { deep: true });
30+
};
31+
Filters.prototype.convertCaseIn = function (obj) {
32+
if (this.options.convertCase === 'none')
33+
return obj;
34+
return (0, kebabcase_keys_1.default)(obj, { deep: true });
35+
};
36+
//
37+
// Convert object values to strings
38+
// ----------------------------------------------
39+
Filters.prototype.stringifyValues = function (params) {
40+
var obj = {};
41+
Object.entries(params)
42+
.forEach(function (_a) {
43+
var key = _a[0], value = _a[1];
44+
obj[key] = String(value);
45+
});
46+
return obj;
47+
};
48+
return Filters;
49+
}());
50+
exports.default = Filters;

Diff for: dist/modules/Options.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export declare type Modes = 'MAINNET' | 'TESTNET' | 'BETANET';
2+
export declare type Cases = 'kebabcase' | 'snakecase' | 'camelcase' | 'none';
3+
export interface OptionsProps {
4+
mode?: Modes;
5+
indexerAPI?: string;
6+
nodeAPI?: string;
7+
convertCase?: Cases;
8+
}
9+
export default class Options {
10+
mode: string;
11+
indexerAPI: string;
12+
nodeAPI: string;
13+
convertCase: string;
14+
constructor(userOptions?: OptionsProps);
15+
}

Diff for: dist/modules/Options.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
//
4+
// Options class
5+
// ----------------------------------------------
6+
var Options = /** @class */ (function () {
7+
function Options(userOptions) {
8+
this.mode = 'MAINNET';
9+
this.indexerAPI = 'https://mainnet-idx.algonode.cloud';
10+
this.nodeAPI = 'https://mainnet-api.algonode.cloud';
11+
this.convertCase = 'none';
12+
Object.assign(this, userOptions);
13+
}
14+
return Options;
15+
}());
16+
exports.default = Options;

Diff for: dist/modules/Query.d.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import BaseModule from './Base';
1+
import Algolib from '../index';
2+
import Filters from './Filters';
3+
import Options from './Options';
24
export interface QueryParams {
35
limit?: number;
46
[key: string]: string | number | boolean | undefined;
57
}
6-
export default class Query extends BaseModule {
7-
constructor(...args: [any]);
8-
private fetchData;
8+
export default class Query {
9+
protected options: Options;
10+
protected filters: Filters;
11+
constructor(forwarded: Algolib);
912
get(endpoint: string, params?: QueryParams): Promise<Object>;
13+
private fetchData;
1014
account(accountId: string, params?: QueryParams): Promise<Object>;
1115
accountTransactions(accountId: string, params?: QueryParams): Promise<Object>;
1216
application(appId: number, params?: QueryParams): Promise<Object>;

Diff for: dist/modules/Query.js

+34-54
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
"use strict";
2-
var __extends = (this && this.__extends) || (function () {
3-
var extendStatics = function (d, b) {
4-
extendStatics = Object.setPrototypeOf ||
5-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7-
return extendStatics(d, b);
8-
};
9-
return function (d, b) {
10-
if (typeof b !== "function" && b !== null)
11-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12-
extendStatics(d, b);
13-
function __() { this.constructor = d; }
14-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15-
};
16-
})();
172
var __assign = (this && this.__assign) || function () {
183
__assign = Object.assign || function(t) {
194
for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -75,49 +60,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
7560
};
7661
Object.defineProperty(exports, "__esModule", { value: true });
7762
var axios_1 = __importDefault(require("axios"));
78-
var camelcase_keys_1 = __importDefault(require("camelcase-keys"));
79-
var Base_1 = __importDefault(require("./Base"));
80-
var object_1 = require("../helpers/object");
8163
//
8264
// QUERY class
8365
// ----------------------------------------------
84-
var Query = /** @class */ (function (_super) {
85-
__extends(Query, _super);
86-
function Query() {
87-
var args = [];
88-
for (var _i = 0; _i < arguments.length; _i++) {
89-
args[_i] = arguments[_i];
90-
}
91-
return _super.apply(this, args) || this;
66+
var Query = /** @class */ (function () {
67+
function Query(forwarded) {
68+
this.options = forwarded.options;
69+
this.filters = forwarded.filters;
9270
}
9371
//
94-
// Fetch data
95-
// ----------------------------------------------
96-
Query.prototype.fetchData = function (endpoint, params) {
97-
if (params === void 0) { params = {}; }
98-
return __awaiter(this, void 0, void 0, function () {
99-
var stringParams, queryString, response, data, error_1;
100-
return __generator(this, function (_a) {
101-
switch (_a.label) {
102-
case 0:
103-
_a.trys.push([0, 2, , 3]);
104-
stringParams = (0, object_1.stringifyObjectValues)(params);
105-
queryString = new URLSearchParams(stringParams).toString();
106-
return [4 /*yield*/, axios_1.default.get("".concat(this.options.indexerAPI).concat(endpoint, "?").concat(queryString))];
107-
case 1:
108-
response = _a.sent();
109-
data = (0, camelcase_keys_1.default)(response.data, { deep: true });
110-
return [2 /*return*/, data];
111-
case 2:
112-
error_1 = _a.sent();
113-
console.dir(error_1);
114-
return [2 /*return*/, {}];
115-
case 3: return [2 /*return*/];
116-
}
117-
});
118-
});
119-
};
120-
//
12172
// Query wrapper
12273
// ----------------------------------------------
12374
Query.prototype.get = function (endpoint, params) {
@@ -156,6 +107,35 @@ var Query = /** @class */ (function (_super) {
156107
});
157108
};
158109
//
110+
// Fetch data
111+
// ----------------------------------------------
112+
Query.prototype.fetchData = function (endpoint, params) {
113+
if (params === void 0) { params = {}; }
114+
return __awaiter(this, void 0, void 0, function () {
115+
var filteredParams, queryString, response, data, error_1;
116+
return __generator(this, function (_a) {
117+
switch (_a.label) {
118+
case 0:
119+
_a.trys.push([0, 2, , 3]);
120+
filteredParams = void 0;
121+
filteredParams = this.filters.stringifyValues(params);
122+
filteredParams = this.filters.convertCaseIn(filteredParams);
123+
queryString = new URLSearchParams(filteredParams).toString();
124+
return [4 /*yield*/, axios_1.default.get("".concat(this.options.indexerAPI).concat(endpoint, "?").concat(queryString))];
125+
case 1:
126+
response = _a.sent();
127+
data = this.filters.convertCaseOut(response.data);
128+
return [2 /*return*/, data];
129+
case 2:
130+
error_1 = _a.sent();
131+
console.dir(error_1);
132+
return [2 /*return*/, {}];
133+
case 3: return [2 /*return*/];
134+
}
135+
});
136+
});
137+
};
138+
//
159139
// Quick methods
160140
// ----------------------------------------------
161141
// accounts
@@ -297,5 +277,5 @@ var Query = /** @class */ (function (_super) {
297277
});
298278
};
299279
return Query;
300-
}(Base_1.default));
280+
}());
301281
exports.default = Query;

Diff for: dist/types.d.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
export declare enum Mode {
2-
MAINNET = "MAINNET",
3-
TESTNET = "TESTNET",
4-
BETANET = "BETANET"
5-
}
6-
export interface Options {
7-
mode: Mode;
8-
indexerAPI?: string;
9-
nodeAPI?: string;
10-
}
111
export interface StringObj {
122
[k: string]: string;
133
}

Diff for: dist/types.js

-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.Mode = void 0;
4-
var Mode;
5-
(function (Mode) {
6-
Mode["MAINNET"] = "MAINNET";
7-
Mode["TESTNET"] = "TESTNET";
8-
Mode["BETANET"] = "BETANET";
9-
})(Mode = exports.Mode || (exports.Mode = {}));

0 commit comments

Comments
 (0)