@restorecommerce/gql-bot 0.2.2 → 0.2.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.
- package/CHANGELOG.md +11 -0
- package/package.json +2 -2
- package/src/client.ts +6 -2
- package/src/job_processor.ts +3 -32
- package/src/job_processor_gql.ts +2 -2
- package/src/utils.ts +29 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [0.2.3](https://github.com/restorecommerce/libs/compare/@restorecommerce/gql-bot@0.2.2...@restorecommerce/gql-bot@0.2.3) (2022-06-20)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **gql-bot:** correctly handle null objects ([e7e44ef](https://github.com/restorecommerce/libs/commit/e7e44ef95585d423ddee7d41b6cc196ad1668284))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [0.2.2](https://github.com/restorecommerce/libs/compare/@restorecommerce/gql-bot@0.2.1...@restorecommerce/gql-bot@0.2.2) (2022-06-20)
|
|
7
18
|
|
|
8
19
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@restorecommerce/gql-bot",
|
|
3
3
|
"description": "GraphQL Client Automated Task Processor",
|
|
4
4
|
"main": "lib/index",
|
|
5
|
-
"version": "0.2.
|
|
5
|
+
"version": "0.2.3",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/restorecommerce/libs.git"
|
|
@@ -71,5 +71,5 @@
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "6a7808de256668f2681bf1ef62c2090a2a0d1d22"
|
|
75
75
|
}
|
package/src/client.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
|
|
|
7
7
|
import fetch from 'node-fetch'; // required for apollo-link-http
|
|
8
8
|
import { createHttpLink, HttpLink } from 'apollo-link-http';
|
|
9
9
|
import * as https from 'https';
|
|
10
|
+
import { processResponse } from './utils';
|
|
10
11
|
|
|
11
12
|
const _checkVariableMutation = (mutation: string): Boolean => {
|
|
12
13
|
const mutationName = mutation.slice(mutation.indexOf(' '),
|
|
@@ -38,7 +39,7 @@ const _createQueryVariables = (inputVarName: string, queryVarKey: string, varVal
|
|
|
38
39
|
};
|
|
39
40
|
|
|
40
41
|
const checkError = (data: any): any => {
|
|
41
|
-
if (typeof data === 'object') {
|
|
42
|
+
if (typeof data === 'object' && data !== null) {
|
|
42
43
|
if (Array.isArray(data)) {
|
|
43
44
|
const result = data.map(value => {
|
|
44
45
|
const inner = checkError(value);
|
|
@@ -139,7 +140,7 @@ export class Client {
|
|
|
139
140
|
return url.resolve(this.entryBaseUrl, extendURL);
|
|
140
141
|
}
|
|
141
142
|
|
|
142
|
-
async post(source: any, job?: any, verbose = false, ignoreSelfSigned = false): Promise<any> {
|
|
143
|
+
async post(source: any, job?: any, verbose = false, ignoreErrors = false, ignoreSelfSigned = false): Promise<any> {
|
|
143
144
|
const normalUrl = this._normalizeUrl();
|
|
144
145
|
|
|
145
146
|
let mutation;
|
|
@@ -209,6 +210,9 @@ export class Client {
|
|
|
209
210
|
variables,
|
|
210
211
|
response
|
|
211
212
|
}));
|
|
213
|
+
} else if (ignoreErrors) {
|
|
214
|
+
const processed = processResponse(response);
|
|
215
|
+
console.error(JSON.stringify(processed));
|
|
212
216
|
}
|
|
213
217
|
throw new Error(JSON.stringify(error));
|
|
214
218
|
}
|
package/src/job_processor.ts
CHANGED
|
@@ -5,36 +5,7 @@ import * as through2 from 'through2';
|
|
|
5
5
|
import * as readdirp from 'readdirp';
|
|
6
6
|
import * as path from 'path';
|
|
7
7
|
import { EventEmitter } from 'events';
|
|
8
|
-
import { stringToChalk } from './utils';
|
|
9
|
-
|
|
10
|
-
const unwrap = (data: any): any => {
|
|
11
|
-
let result = data;
|
|
12
|
-
while (typeof result === 'object' && Object.keys(result).length == 1) {
|
|
13
|
-
result = result[Object.keys(result)[0]];
|
|
14
|
-
}
|
|
15
|
-
return result;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const removeType = (data: any): any => {
|
|
19
|
-
if (typeof data === 'object') {
|
|
20
|
-
if (Array.isArray(data)) {
|
|
21
|
-
data = data.map(removeType);
|
|
22
|
-
} else {
|
|
23
|
-
delete data['__typename'];
|
|
24
|
-
Object.keys(data).forEach(k => data[k] = removeType(data[k]));
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return data;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const processResponse = (body: any | any[]): any => {
|
|
31
|
-
const result = [];
|
|
32
|
-
for (const response of Array.isArray(body) ? body : [body]) {
|
|
33
|
-
const clean = unwrap(removeType(response));
|
|
34
|
-
result.push(clean);
|
|
35
|
-
}
|
|
36
|
-
return result;
|
|
37
|
-
};
|
|
8
|
+
import { stringToChalk, processResponse } from './utils';
|
|
38
9
|
|
|
39
10
|
export class ReadArrayStream extends Readable {
|
|
40
11
|
array: any[];
|
|
@@ -112,9 +83,9 @@ export class JobProcessor {
|
|
|
112
83
|
|
|
113
84
|
if (verbose) {
|
|
114
85
|
const processed = processResponse(body);
|
|
115
|
-
console.log(`[${logColor(task.name)}] Completed
|
|
86
|
+
console.log(`[${logColor(task.name)}] Completed`, JSON.stringify(processed));
|
|
116
87
|
} else {
|
|
117
|
-
console.log(`[${logColor(task.name)}] Completed
|
|
88
|
+
console.log(`[${logColor(task.name)}] Completed`);
|
|
118
89
|
}
|
|
119
90
|
|
|
120
91
|
task.inputTask.processing--;
|
package/src/job_processor_gql.ts
CHANGED
|
@@ -85,7 +85,7 @@ export class GraphQLProcessor {
|
|
|
85
85
|
// 'yamlStream' readable stream.
|
|
86
86
|
yamlStream.on('pause', async () => {
|
|
87
87
|
try {
|
|
88
|
-
resultArr.push(await this.client.post(docArr, task, verbose, ignoreSelfSigned));
|
|
88
|
+
resultArr.push(await this.client.post(docArr, task, verbose, ignoreErrors, ignoreSelfSigned));
|
|
89
89
|
yamlStream.resume();
|
|
90
90
|
} catch (e) {
|
|
91
91
|
!ignoreErrors && reject(e);
|
|
@@ -119,7 +119,7 @@ export class GraphQLProcessor {
|
|
|
119
119
|
batchCounter++;
|
|
120
120
|
console.log(`[${logColor(task.name)}] Processing batch: ${batchCounter}${batchText}`);
|
|
121
121
|
try {
|
|
122
|
-
resultArr.push(await this.client.post(docArr, task, verbose, ignoreSelfSigned));
|
|
122
|
+
resultArr.push(await this.client.post(docArr, task, verbose, ignoreErrors, ignoreSelfSigned));
|
|
123
123
|
} catch (e) {
|
|
124
124
|
!ignoreErrors && reject(e);
|
|
125
125
|
}
|
package/src/utils.ts
CHANGED
|
@@ -8,3 +8,32 @@ const colorHash = new ColorHash({
|
|
|
8
8
|
export const stringToChalk = (str) => {
|
|
9
9
|
return chalk.hex(colorHash.hex(str));
|
|
10
10
|
};
|
|
11
|
+
|
|
12
|
+
const unwrap = (data: any): any => {
|
|
13
|
+
let result = data;
|
|
14
|
+
while (typeof result === 'object' && Object.keys(result).length == 1) {
|
|
15
|
+
result = result[Object.keys(result)[0]];
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const removeType = (data: any): any => {
|
|
21
|
+
if (typeof data === 'object' && data !== null) {
|
|
22
|
+
if (Array.isArray(data)) {
|
|
23
|
+
data = data.map(removeType);
|
|
24
|
+
} else {
|
|
25
|
+
delete data['__typename'];
|
|
26
|
+
Object.keys(data).forEach(k => data[k] = removeType(data[k]));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return data;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const processResponse = (body: any | any[]): any => {
|
|
33
|
+
const result = [];
|
|
34
|
+
for (const response of Array.isArray(body) ? body : [body]) {
|
|
35
|
+
const clean = unwrap(removeType(response));
|
|
36
|
+
result.push(clean);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
};
|