eth-graph-query 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -3
- package/lib/index.d.ts +7 -1
- package/lib/index.js +22 -4
- package/lib/normal-query.d.ts +14 -0
- package/lib/normal-query.js +45 -0
- package/lib/query-builder.d.ts +6 -0
- package/lib/query-builder.js +58 -0
- package/lib/type.d.ts +19 -0
- package/lib/type.js +2 -0
- package/package.json +50 -5
package/README.md
CHANGED
|
@@ -1,7 +1,32 @@
|
|
|
1
|
-
<
|
|
2
|
-
|
|
3
|
-
</
|
|
1
|
+
<h1>
|
|
2
|
+
ETH GRAPH QUERY
|
|
3
|
+
</h1>
|
|
4
|
+
|
|
5
|
+
---
|
|
4
6
|
|
|
5
7
|
### Installation
|
|
6
8
|
|
|
9
|
+
```shell
|
|
10
|
+
npm install eth-graph-query
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
7
16
|
### Usage
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
### For develop
|
|
21
|
+
|
|
22
|
+
- To test package, you can run below command
|
|
23
|
+
|
|
24
|
+
```shell
|
|
25
|
+
npm run test
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- Before running above command, you must create `.env` file
|
|
29
|
+
|
|
30
|
+
```shell
|
|
31
|
+
cp .env.example .env
|
|
32
|
+
```
|
package/lib/index.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import NormalQuery from './normal-query';
|
|
3
|
+
import { GraphParams } from './type';
|
|
4
|
+
export default class EthGraphQuery extends NormalQuery {
|
|
5
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
6
|
+
query(collection: string, params: GraphParams): Promise<any>;
|
|
7
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
12
|
+
const normal_query_1 = require("./normal-query");
|
|
13
|
+
const query_builder_1 = require("./query-builder");
|
|
14
|
+
class EthGraphQuery extends normal_query_1.default {
|
|
15
|
+
constructor(rootUrl, config) {
|
|
16
|
+
super(rootUrl, config);
|
|
17
|
+
}
|
|
18
|
+
query(collection, params) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
const sQuery = query_builder_1.default.buildQuery(collection, params);
|
|
21
|
+
return yield this.post('', { query: query_builder_1.default.makeFullQuery(sQuery) });
|
|
22
|
+
});
|
|
23
|
+
}
|
|
6
24
|
}
|
|
7
|
-
exports.
|
|
25
|
+
exports.default = EthGraphQuery;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export declare const defaultHeader: {
|
|
3
|
+
Accept: string;
|
|
4
|
+
'Content-Type': string;
|
|
5
|
+
};
|
|
6
|
+
export default class NormalQuery {
|
|
7
|
+
root: string;
|
|
8
|
+
config: AxiosRequestConfig;
|
|
9
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
10
|
+
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
11
|
+
post<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
12
|
+
put<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
13
|
+
del<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.defaultHeader = void 0;
|
|
13
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
14
|
+
const axios_1 = require("axios");
|
|
15
|
+
exports.defaultHeader = { Accept: 'application/json', 'Content-Type': 'application/json' };
|
|
16
|
+
function responseBody(res) {
|
|
17
|
+
return res.data;
|
|
18
|
+
}
|
|
19
|
+
class NormalQuery {
|
|
20
|
+
constructor(rootUrl, config) {
|
|
21
|
+
this.root = rootUrl;
|
|
22
|
+
this.config = config ? Object.assign(Object.assign({}, config), { headers: exports.defaultHeader }) : { headers: exports.defaultHeader };
|
|
23
|
+
}
|
|
24
|
+
get(url, config) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
return yield axios_1.default.get(`${this.root}${url}`, Object.assign(Object.assign({}, config), this.config)).then(responseBody);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
post(url, data, config) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
return yield axios_1.default.post(`${this.root}${url}`, data, Object.assign(Object.assign({}, config), this.config)).then(responseBody);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
put(url, data, config) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
return yield axios_1.default.put(`${this.root}${url}`, data, Object.assign(Object.assign({}, config), this.config)).then(responseBody);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
del(url, config) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
return yield axios_1.default.delete(`${this.root}${url}`, Object.assign(Object.assign({}, config), this.config)).then(responseBody);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.default = NormalQuery;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class QueryBuilder {
|
|
4
|
+
static buildJsonQuery(query) {
|
|
5
|
+
const whereList = [];
|
|
6
|
+
for (const key in query) {
|
|
7
|
+
if (query[key] != undefined) {
|
|
8
|
+
if (typeof query[key] == 'object')
|
|
9
|
+
whereList.push(`${key}: {${this.buildJsonQuery(query[key])}}`);
|
|
10
|
+
else
|
|
11
|
+
whereList.push(`${key}: ${query[key]}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return whereList.join(', ');
|
|
15
|
+
}
|
|
16
|
+
static buildQuery(collection, params) {
|
|
17
|
+
const filters = [];
|
|
18
|
+
if (params.id != undefined)
|
|
19
|
+
filters.push(`id: ${params.id}`);
|
|
20
|
+
if (params.orderBy)
|
|
21
|
+
filters.push(`orderBy: ${params.orderBy}`);
|
|
22
|
+
if (params.orderDirection)
|
|
23
|
+
filters.push(`orderDirection: ${params.orderDirection}`);
|
|
24
|
+
if (params.first != undefined) {
|
|
25
|
+
if (params.first < 0)
|
|
26
|
+
params.first = 0;
|
|
27
|
+
else if (params.first > 1000)
|
|
28
|
+
params.first = 1000;
|
|
29
|
+
filters.push(`first: ${params.first}`);
|
|
30
|
+
}
|
|
31
|
+
if (params.skip != undefined) {
|
|
32
|
+
if (params.skip < 0)
|
|
33
|
+
params.skip = 0;
|
|
34
|
+
else if (params.skip > 5000)
|
|
35
|
+
params.skip = 5000;
|
|
36
|
+
filters.push(`skip: ${params.skip}`);
|
|
37
|
+
}
|
|
38
|
+
if (params.where) {
|
|
39
|
+
const sWhere = this.buildJsonQuery(params.where);
|
|
40
|
+
if (sWhere.length > 0)
|
|
41
|
+
filters.push(`where: {${sWhere}}`);
|
|
42
|
+
}
|
|
43
|
+
if (params.block) {
|
|
44
|
+
const sBlock = this.buildJsonQuery(params.block);
|
|
45
|
+
if (sBlock.length > 0)
|
|
46
|
+
filters.push(`block: {${sBlock}}`);
|
|
47
|
+
}
|
|
48
|
+
const filterString = filters.join(', ');
|
|
49
|
+
if (filterString.length > 0)
|
|
50
|
+
return `${collection}(${filterString}) {${params.elements.join(' ')}}`;
|
|
51
|
+
else
|
|
52
|
+
return `${collection} {${params.elements.join(' ')}}`;
|
|
53
|
+
}
|
|
54
|
+
static makeFullQuery(query) {
|
|
55
|
+
return `query MyQuery {${query}}`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.default = QueryBuilder;
|
package/lib/type.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type QueryJson = {
|
|
2
|
+
[key: string]: QueryJson | string | number | boolean | undefined;
|
|
3
|
+
};
|
|
4
|
+
export type BlockQuery = {
|
|
5
|
+
hash?: string;
|
|
6
|
+
number?: number;
|
|
7
|
+
number_gte?: number;
|
|
8
|
+
};
|
|
9
|
+
export interface GraphParams {
|
|
10
|
+
elements: Array<string>;
|
|
11
|
+
where?: QueryJson;
|
|
12
|
+
id?: string;
|
|
13
|
+
first?: number;
|
|
14
|
+
orderBy?: string;
|
|
15
|
+
orderDirection?: 'asc' | 'desc';
|
|
16
|
+
skip?: number;
|
|
17
|
+
subgraphError?: 'allow' | 'deny';
|
|
18
|
+
block?: BlockQuery;
|
|
19
|
+
}
|
package/lib/type.js
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eth-graph-query",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "simple package for create query command to
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "simple package for create query command to the GraphQL in ethereum",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -9,14 +9,19 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc",
|
|
12
|
-
"prepare": "npm run build"
|
|
12
|
+
"prepare": "npm run build && husky install",
|
|
13
|
+
"tape": "tape -r ts-node/register",
|
|
14
|
+
"test:node": "npm run tape -- test/*.spec.ts",
|
|
15
|
+
"test": "npm run test:node",
|
|
16
|
+
"eslint": "eslint . --ext .ts"
|
|
13
17
|
},
|
|
14
18
|
"repository": {
|
|
15
19
|
"type": "git",
|
|
16
20
|
"url": "git+https://github.com/phamhongphuc1999/eth-graph-query.git"
|
|
17
21
|
},
|
|
18
22
|
"keywords": [
|
|
19
|
-
"graph"
|
|
23
|
+
"the graph",
|
|
24
|
+
"ethereum"
|
|
20
25
|
],
|
|
21
26
|
"author": "phamhongphuc1999",
|
|
22
27
|
"license": "ISC",
|
|
@@ -24,7 +29,47 @@
|
|
|
24
29
|
"url": "https://github.com/phamhongphuc1999/eth-graph-query/issues"
|
|
25
30
|
},
|
|
26
31
|
"homepage": "https://github.com/phamhongphuc1999/eth-graph-query#readme",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"axios": "^1.4.0"
|
|
34
|
+
},
|
|
27
35
|
"devDependencies": {
|
|
28
|
-
"
|
|
36
|
+
"@commitlint/cli": "^17.6.5",
|
|
37
|
+
"@commitlint/config-conventional": "^17.6.5",
|
|
38
|
+
"@types/tape": "^5.6.0",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "5.59.6",
|
|
40
|
+
"dotenv": "^16.1.4",
|
|
41
|
+
"eslint": "^8.40.0",
|
|
42
|
+
"eslint-config-prettier": "^8.8.0",
|
|
43
|
+
"eslint-import-resolver-alias": "^1.1.2",
|
|
44
|
+
"eslint-plugin-import": "^2.26.0",
|
|
45
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
46
|
+
"husky": "^8.0.3",
|
|
47
|
+
"lint-staged": "^13.2.2",
|
|
48
|
+
"prettier": "^2.8.8",
|
|
49
|
+
"tape": "^5.6.3",
|
|
50
|
+
"ts-node": "10.9.1",
|
|
51
|
+
"typescript": "5.0.4"
|
|
52
|
+
},
|
|
53
|
+
"contributors": [
|
|
54
|
+
{
|
|
55
|
+
"name": "Phạm Hồng Phúc",
|
|
56
|
+
"email": "phamhongphuc12atm1@gmail.com",
|
|
57
|
+
"url": "https://github.com/phamhongphuc1999"
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=14",
|
|
62
|
+
"npm": ">=7"
|
|
63
|
+
},
|
|
64
|
+
"commitlint": {
|
|
65
|
+
"extends": [
|
|
66
|
+
"@commitlint/config-conventional"
|
|
67
|
+
]
|
|
68
|
+
},
|
|
69
|
+
"lint-staged": {
|
|
70
|
+
"*.{js,jsx,ts,tsx}": "eslint --config ./.eslintrc-staged.js --cache --fix",
|
|
71
|
+
"*.{json,yml,md}": [
|
|
72
|
+
"prettier --write"
|
|
73
|
+
]
|
|
29
74
|
}
|
|
30
75
|
}
|