eth-graph-query 0.1.3

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.
Files changed (42) hide show
  1. package/README.md +76 -0
  2. package/dist/cjs/core/index.d.ts +12 -0
  3. package/dist/cjs/core/index.js +21 -0
  4. package/dist/cjs/core/normal-query.d.ts +14 -0
  5. package/dist/cjs/core/normal-query.js +28 -0
  6. package/dist/cjs/core/query-builder.d.ts +10 -0
  7. package/dist/cjs/core/query-builder.js +74 -0
  8. package/dist/cjs/data-set.d.ts +5 -0
  9. package/dist/cjs/data-set.js +8 -0
  10. package/dist/cjs/index.d.ts +12 -0
  11. package/dist/cjs/index.js +21 -0
  12. package/dist/cjs/normal-query.d.ts +14 -0
  13. package/dist/cjs/normal-query.js +28 -0
  14. package/dist/cjs/package.json +3 -0
  15. package/dist/cjs/query-builder.d.ts +12 -0
  16. package/dist/cjs/query-builder.js +108 -0
  17. package/dist/cjs/type.d.ts +51 -0
  18. package/dist/cjs/type.js +24 -0
  19. package/dist/esm/core/index.d.ts +12 -0
  20. package/dist/esm/core/index.js +18 -0
  21. package/dist/esm/core/normal-query.d.ts +14 -0
  22. package/dist/esm/core/normal-query.js +24 -0
  23. package/dist/esm/core/query-builder.d.ts +10 -0
  24. package/dist/esm/core/query-builder.js +71 -0
  25. package/dist/esm/data-set.d.ts +5 -0
  26. package/dist/esm/data-set.js +5 -0
  27. package/dist/esm/index.d.ts +12 -0
  28. package/dist/esm/index.js +18 -0
  29. package/dist/esm/normal-query.d.ts +14 -0
  30. package/dist/esm/normal-query.js +24 -0
  31. package/dist/esm/package.json +3 -0
  32. package/dist/esm/query-builder.d.ts +12 -0
  33. package/dist/esm/query-builder.js +105 -0
  34. package/dist/esm/type.d.ts +51 -0
  35. package/dist/esm/type.js +21 -0
  36. package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
  37. package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
  38. package/package.json +89 -0
  39. package/src/index.ts +24 -0
  40. package/src/normal-query.ts +34 -0
  41. package/src/query-builder.ts +89 -0
  42. package/src/type.ts +86 -0
@@ -0,0 +1,34 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
3
+
4
+ export const defaultHeader = { Accept: 'application/json', 'Content-Type': 'application/json' };
5
+
6
+ function responseBody<T>(res: AxiosResponse<T>) {
7
+ return res.data;
8
+ }
9
+
10
+ export default class NormalQuery {
11
+ root: string;
12
+ config: AxiosRequestConfig;
13
+
14
+ constructor(rootUrl: string, config?: AxiosRequestConfig) {
15
+ this.root = rootUrl;
16
+ this.config = config ? { ...config, ...{ headers: defaultHeader } } : { headers: defaultHeader };
17
+ }
18
+
19
+ protected async get<T = any>(url: string, config?: AxiosRequestConfig) {
20
+ return await axios.get(`${this.root}${url}`, { ...config, ...this.config }).then<T>(responseBody);
21
+ }
22
+
23
+ protected async post<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig) {
24
+ return await axios.post(`${this.root}${url}`, data, { ...config, ...this.config }).then<T>(responseBody);
25
+ }
26
+
27
+ protected async put<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig) {
28
+ return await axios.put(`${this.root}${url}`, data, { ...config, ...this.config }).then<T>(responseBody);
29
+ }
30
+
31
+ protected async del<T = any>(url: string, config?: AxiosRequestConfig) {
32
+ return await axios.delete(`${this.root}${url}`, { ...config, ...this.config }).then<T>(responseBody);
33
+ }
34
+ }
@@ -0,0 +1,89 @@
1
+ import { ElementType, GraphParams, OptionKeys, OptionsKey, QueryJson, WhereOptions } from './type';
2
+
3
+ export default class QueryBuilder {
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ static isWhereOptions(data: any) {
6
+ const keys = Object.keys(data);
7
+ if (keys.length == 0) return true;
8
+ for (const key of keys) {
9
+ if (!OptionKeys.includes(key)) return false;
10
+ }
11
+ return true;
12
+ }
13
+
14
+ static buildJsonQuery(query: QueryJson): string {
15
+ const whereList = [];
16
+ for (const key in query) {
17
+ if (query[key] != undefined) {
18
+ if (Array.isArray(query[key])) {
19
+ const queryArray = query[key] as Array<string>;
20
+ whereList.push(`${key}: [${queryArray.map((item) => `"${item}"`).join(', ')}]`);
21
+ } else if (this.isWhereOptions(query[key])) {
22
+ const realJson: QueryJson = {};
23
+ const options = query[key] as WhereOptions;
24
+ for (const option in options) {
25
+ const value = options[option as OptionsKey];
26
+ if (value) realJson[`${key}_${option}`] = value;
27
+ }
28
+ whereList.push(`${this.buildJsonQuery(realJson as QueryJson)}`);
29
+ } else if (typeof query[key] == 'object')
30
+ whereList.push(`${key}: {${this.buildJsonQuery(query[key] as QueryJson)}}`);
31
+ else if (typeof query[key] == 'string') whereList.push(`${key}: "${query[key]}"`);
32
+ else whereList.push(`${key}: ${query[key]}`);
33
+ }
34
+ }
35
+ return whereList.join(', ');
36
+ }
37
+
38
+ static buildElements(elements: Array<ElementType>): Array<string> {
39
+ const elementList = [];
40
+ for (const element of elements) {
41
+ if (typeof element == 'string') elementList.push(element);
42
+ else {
43
+ const object = element as { collection: string; params: GraphParams };
44
+ elementList.push(this.buildQuery(object.collection, object.params));
45
+ }
46
+ }
47
+ return elementList;
48
+ }
49
+
50
+ static buildQuery(collection: string, params?: GraphParams) {
51
+ const filters: Array<string> = [];
52
+ if (params?.id != undefined) filters.push(`id: ${params.id}`);
53
+ if (params?.orderBy) filters.push(`orderBy: ${params.orderBy}`);
54
+ if (params?.orderDirection) filters.push(`orderDirection: ${params.orderDirection}`);
55
+ if (params?.first != undefined) {
56
+ if (params.first < 0) params.first = 0;
57
+ else if (params.first > 1000) params.first = 1000;
58
+ filters.push(`first: ${params.first}`);
59
+ }
60
+ if (params?.skip != undefined) {
61
+ if (params.skip < 0) params.skip = 0;
62
+ else if (params.skip > 5000) params.skip = 5000;
63
+ filters.push(`skip: ${params.skip}`);
64
+ }
65
+ if (params?.where) {
66
+ const sWhere = this.buildJsonQuery(params.where);
67
+ if (sWhere.length > 0) filters.push(`where: {${sWhere}}`);
68
+ }
69
+ if (params?.block) {
70
+ const sBlock = this.buildJsonQuery(params.block);
71
+ if (sBlock.length > 0) filters.push(`block: {${sBlock}}`);
72
+ }
73
+ const filterString = filters.join(', ');
74
+ let elements: Array<string> = ['id'];
75
+ if (params?.elements) if (params.elements.length > 0) elements = this.buildElements(params.elements);
76
+ if (filterString.length > 0) return `${collection}(${filterString}) {${elements.join(' ')}}`;
77
+ else return `${collection} {${elements.join(' ')}}`;
78
+ }
79
+
80
+ static mergeQuery(data: Array<{ collection: string; params?: GraphParams }>) {
81
+ const queries: Array<string> = [];
82
+ for (const item of data) queries.push(this.buildQuery(item.collection, item.params));
83
+ return queries.join(' ');
84
+ }
85
+
86
+ static makeFullQuery(query: string) {
87
+ return `query MyQuery {${query}}`;
88
+ }
89
+ }
package/src/type.ts ADDED
@@ -0,0 +1,86 @@
1
+ type BaseQueryType = Array<string> | string | number | boolean | undefined;
2
+
3
+ export const OptionKeys = [
4
+ 'contains',
5
+ 'contains_nocase',
6
+ 'ends_with',
7
+ 'end_with_nocase',
8
+ 'starts_with',
9
+ 'starts_with_nocase',
10
+ 'not_contains',
11
+ 'not_contains_nocase',
12
+ 'not_ends_with',
13
+ 'not_ends_with_nocase',
14
+ 'not_starts_with',
15
+ 'not_starts_with_nocase',
16
+ 'gt',
17
+ 'gte',
18
+ 'lt',
19
+ 'lts',
20
+ 'not',
21
+ 'in',
22
+ 'not_in',
23
+ ];
24
+ export type OptionsKey =
25
+ | 'contains'
26
+ | 'contains_nocase'
27
+ | 'ends_with'
28
+ | 'end_with_nocase'
29
+ | 'starts_with'
30
+ | 'starts_with_nocase'
31
+ | 'not_contains'
32
+ | 'not_contains_nocase'
33
+ | 'not_ends_with'
34
+ | 'not_ends_with_nocase'
35
+ | 'not_starts_with'
36
+ | 'not_starts_with_nocase'
37
+ | 'gt'
38
+ | 'gte'
39
+ | 'lt'
40
+ | 'lte'
41
+ | 'not'
42
+ | 'in'
43
+ | 'not_in';
44
+
45
+ export type TextWhereOptions = {
46
+ contains?: BaseQueryType;
47
+ contains_nocase?: BaseQueryType;
48
+ ends_with?: BaseQueryType;
49
+ end_with_nocase?: BaseQueryType;
50
+ starts_with?: BaseQueryType;
51
+ starts_with_nocase?: BaseQueryType;
52
+ not_contains?: BaseQueryType;
53
+ not_contains_nocase?: BaseQueryType;
54
+ not_ends_with?: BaseQueryType;
55
+ not_ends_with_nocase?: BaseQueryType;
56
+ not_starts_with?: BaseQueryType;
57
+ not_starts_with_nocase?: BaseQueryType;
58
+ };
59
+
60
+ export type CommonWhereOptions = {
61
+ gt?: BaseQueryType;
62
+ gte?: BaseQueryType;
63
+ lt?: BaseQueryType;
64
+ lte?: BaseQueryType;
65
+ not?: BaseQueryType;
66
+ in?: BaseQueryType;
67
+ not_in?: BaseQueryType;
68
+ };
69
+
70
+ export type WhereOptions = TextWhereOptions & CommonWhereOptions;
71
+
72
+ export type QueryJson = { [key: string]: QueryJson | WhereOptions | BaseQueryType };
73
+ export type BlockQuery = { hash?: string; number?: number; number_gte?: number };
74
+ export type ElementType = string | { collection: string; params?: GraphParams };
75
+
76
+ export interface GraphParams {
77
+ elements?: Array<ElementType>;
78
+ where?: QueryJson;
79
+ id?: string;
80
+ first?: number;
81
+ orderBy?: string;
82
+ orderDirection?: 'asc' | 'desc';
83
+ skip?: number;
84
+ subgraphError?: 'allow' | 'deny';
85
+ block?: BlockQuery;
86
+ }