@restorecommerce/gql-bot 0.2.0 → 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 CHANGED
@@ -3,6 +3,39 @@
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
+
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)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * **gql-bot:** resume if errors are skipped ([7f01a84](https://github.com/restorecommerce/libs/commit/7f01a84f19a81360b94d8dc9421cb572d0fb3cd5))
23
+
24
+
25
+
26
+
27
+
28
+ ## [0.2.1](https://github.com/restorecommerce/libs/compare/@restorecommerce/gql-bot@0.2.0...@restorecommerce/gql-bot@0.2.1) (2022-06-20)
29
+
30
+
31
+ ### Bug Fixes
32
+
33
+ * **gql-bot:** add option to ignore ssl errors ([b82d302](https://github.com/restorecommerce/libs/commit/b82d3020318ec5495ad1c6143cd5c80cb1657f80))
34
+
35
+
36
+
37
+
38
+
6
39
  # [0.2.0](https://github.com/restorecommerce/libs/compare/@restorecommerce/gql-bot@0.1.18...@restorecommerce/gql-bot@0.2.0) (2022-06-20)
7
40
 
8
41
 
package/lib/client.d.ts CHANGED
@@ -5,5 +5,5 @@ export declare class Client {
5
5
  constructor(opts: any);
6
6
  _buildURLs(): any;
7
7
  _normalizeUrl(source?: any): string;
8
- post(source: any, job?: any, verbose?: boolean): Promise<any>;
8
+ post(source: any, job?: any, verbose?: boolean, ignoreSelfSigned?: boolean): Promise<any>;
9
9
  }
package/lib/client.js CHANGED
@@ -18,6 +18,7 @@ const apollo_client_1 = require("apollo-client");
18
18
  const apollo_cache_inmemory_1 = require("apollo-cache-inmemory");
19
19
  const node_fetch_1 = require("node-fetch"); // required for apollo-link-http
20
20
  const apollo_link_http_1 = require("apollo-link-http");
21
+ const https = require("https");
21
22
  const _checkVariableMutation = (mutation) => {
22
23
  const mutationName = mutation.slice(mutation.indexOf(' '), mutation.indexOf('($'));
23
24
  if (mutationName.indexOf('$') > 0) {
@@ -132,7 +133,7 @@ class Client {
132
133
  }
133
134
  return url.resolve(this.entryBaseUrl, extendURL);
134
135
  }
135
- post(source, job, verbose = false) {
136
+ post(source, job, verbose = false, ignoreSelfSigned = false) {
136
137
  return __awaiter(this, void 0, void 0, function* () {
137
138
  const normalUrl = this._normalizeUrl();
138
139
  let mutation;
@@ -171,6 +172,11 @@ class Client {
171
172
  if (this.opts.headers) {
172
173
  apolloLinkOpts['headers'] = this.opts.headers;
173
174
  }
175
+ if (ignoreSelfSigned) {
176
+ apolloLinkOpts.fetchOptions = {
177
+ agent: new https.Agent({ rejectUnauthorized: false }),
178
+ };
179
+ }
174
180
  let apolloLink = apollo_link_http_1.createHttpLink(apolloLinkOpts);
175
181
  const apolloCache = new apollo_cache_inmemory_1.InMemoryCache();
176
182
  const apolloClient = new apollo_client_1.ApolloClient({
@@ -19,6 +19,6 @@ export declare class JobProcessor {
19
19
  processedTasks: number;
20
20
  taskStream: ps.PromiseStream<any>;
21
21
  constructor(jobInfo: any);
22
- start(tasks?: any, job?: Job, verbose?: boolean, ignoreErrors?: boolean): Promise<any>;
22
+ start(tasks?: any, job?: Job, verbose?: boolean, ignoreErrors?: boolean, ignoreSelfSigned?: boolean): Promise<any>;
23
23
  sync(task: any, job: Job): Promise<any>;
24
24
  }
@@ -99,13 +99,13 @@ class JobProcessor {
99
99
  processor: null
100
100
  });
101
101
  }
102
- start(tasks, job, verbose = false, ignoreErrors = false) {
102
+ start(tasks, job, verbose = false, ignoreErrors = false, ignoreSelfSigned = false) {
103
103
  return __awaiter(this, void 0, void 0, function* () {
104
104
  job = job || new Job();
105
105
  tasks = tasks || this.jobInfo.tasks;
106
106
  const concurrency = this.jobInfo.options.concurrency;
107
107
  this.taskStream = ps.map({ concurrent: concurrency }, (task) => {
108
- return this.jobInfo.options.processor.process(task, verbose, ignoreErrors).then((body) => {
108
+ return this.jobInfo.options.processor.process(task, verbose, ignoreErrors, ignoreSelfSigned).then((body) => {
109
109
  const logColor = utils_1.stringToChalk(task.name);
110
110
  if (verbose) {
111
111
  const processed = processResponse(body);
@@ -6,5 +6,5 @@ export declare class GraphQLProcessor {
6
6
  opts: any;
7
7
  client: Client;
8
8
  constructor(opts: any);
9
- process(task: any, verbose?: boolean, ignoreErrors?: boolean): Promise<any>;
9
+ process(task: any, verbose?: boolean, ignoreErrors?: boolean, ignoreSelfSigned?: boolean): Promise<any>;
10
10
  }
@@ -30,7 +30,7 @@ class GraphQLProcessor {
30
30
  this.opts = opts;
31
31
  this.client = new index_1.Client(opts);
32
32
  }
33
- process(task, verbose = false, ignoreErrors = false) {
33
+ process(task, verbose = false, ignoreErrors = false, ignoreSelfSigned = false) {
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
35
  let yamlStream = new yaml_document_stream_1.YamlStreamReadTransformer();
36
36
  let jobPath = task.path;
@@ -68,7 +68,7 @@ class GraphQLProcessor {
68
68
  counter = 0;
69
69
  let batchText = '';
70
70
  if (batchsize > 0) {
71
- const from = batchCounter * batchsize;
71
+ const from = batchCounter * batchsize + 1;
72
72
  const to = from + (docArr.length - 1);
73
73
  batchText = from == to ? ` (${from})` : ` (${from} - ${to})`;
74
74
  }
@@ -89,11 +89,12 @@ class GraphQLProcessor {
89
89
  // 'yamlStream' readable stream.
90
90
  yamlStream.on('pause', () => __awaiter(this, void 0, void 0, function* () {
91
91
  try {
92
- resultArr.push(yield this.client.post(docArr, task, verbose));
92
+ resultArr.push(yield this.client.post(docArr, task, verbose, ignoreSelfSigned));
93
93
  yamlStream.resume();
94
94
  }
95
95
  catch (e) {
96
96
  !ignoreErrors && reject(e);
97
+ ignoreErrors && yamlStream.resume();
97
98
  }
98
99
  }));
99
100
  let runOnResume;
@@ -113,14 +114,14 @@ class GraphQLProcessor {
113
114
  if (docArr && !_.isEmpty(docArr)) {
114
115
  let batchText = '';
115
116
  if (batchsize > 0) {
116
- const from = batchCounter * batchsize;
117
+ const from = batchCounter * batchsize + 1;
117
118
  const to = from + (docArr.length - 1);
118
119
  batchText = from == to ? ` (${from})` : ` (${from} - ${to})`;
119
120
  }
120
121
  batchCounter++;
121
122
  console.log(`[${logColor(task.name)}] Processing batch: ${batchCounter}${batchText}`);
122
123
  try {
123
- resultArr.push(yield this.client.post(docArr, task, verbose));
124
+ resultArr.push(yield this.client.post(docArr, task, verbose, ignoreSelfSigned));
124
125
  }
125
126
  catch (e) {
126
127
  !ignoreErrors && reject(e);
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.0",
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": "65fa379f6d4a215fb6c760431f9718c25b6f51ad"
74
+ "gitHead": "6a7808de256668f2681bf1ef62c2090a2a0d1d22"
75
75
  }
package/src/client.ts CHANGED
@@ -5,7 +5,9 @@ import gql from 'graphql-tag';
5
5
  import { ApolloClient } from 'apollo-client';
6
6
  import { InMemoryCache } from 'apollo-cache-inmemory';
7
7
  import fetch from 'node-fetch'; // required for apollo-link-http
8
- import { createHttpLink } from 'apollo-link-http';
8
+ import { createHttpLink, HttpLink } from 'apollo-link-http';
9
+ import * as https from 'https';
10
+ import { processResponse } from './utils';
9
11
 
10
12
  const _checkVariableMutation = (mutation: string): Boolean => {
11
13
  const mutationName = mutation.slice(mutation.indexOf(' '),
@@ -37,7 +39,7 @@ const _createQueryVariables = (inputVarName: string, queryVarKey: string, varVal
37
39
  };
38
40
 
39
41
  const checkError = (data: any): any => {
40
- if (typeof data === 'object') {
42
+ if (typeof data === 'object' && data !== null) {
41
43
  if (Array.isArray(data)) {
42
44
  const result = data.map(value => {
43
45
  const inner = checkError(value);
@@ -138,7 +140,7 @@ export class Client {
138
140
  return url.resolve(this.entryBaseUrl, extendURL);
139
141
  }
140
142
 
141
- async post(source: any, job?: any, verbose = false): Promise<any> {
143
+ async post(source: any, job?: any, verbose = false, ignoreErrors = false, ignoreSelfSigned = false): Promise<any> {
142
144
  const normalUrl = this._normalizeUrl();
143
145
 
144
146
  let mutation;
@@ -172,7 +174,7 @@ export class Client {
172
174
  mutation = _replaceInlineVars(mutation, { resource_list, apiKey });
173
175
  }
174
176
 
175
- const apolloLinkOpts = {
177
+ const apolloLinkOpts: HttpLink.Options = {
176
178
  uri: normalUrl,
177
179
  fetch
178
180
  };
@@ -181,6 +183,12 @@ export class Client {
181
183
  apolloLinkOpts['headers'] = this.opts.headers;
182
184
  }
183
185
 
186
+ if (ignoreSelfSigned) {
187
+ apolloLinkOpts.fetchOptions = {
188
+ agent: new https.Agent({rejectUnauthorized: false}),
189
+ };
190
+ }
191
+
184
192
  let apolloLink = createHttpLink(apolloLinkOpts);
185
193
 
186
194
  const apolloCache = new InMemoryCache();
@@ -202,6 +210,9 @@ export class Client {
202
210
  variables,
203
211
  response
204
212
  }));
213
+ } else if (ignoreErrors) {
214
+ const processed = processResponse(response);
215
+ console.error(JSON.stringify(processed));
205
216
  }
206
217
  throw new Error(JSON.stringify(error));
207
218
  }
@@ -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[];
@@ -101,20 +72,20 @@ export class JobProcessor {
101
72
  });
102
73
  }
103
74
 
104
- async start(tasks?: any, job?: Job, verbose = false, ignoreErrors = false): Promise<any> {
75
+ async start(tasks?: any, job?: Job, verbose = false, ignoreErrors = false, ignoreSelfSigned = false): Promise<any> {
105
76
  job = job || new Job();
106
77
  tasks = tasks || this.jobInfo.tasks;
107
78
 
108
79
  const concurrency = this.jobInfo.options.concurrency;
109
80
  this.taskStream = ps.map({concurrent: concurrency}, (task: any) => {
110
- return this.jobInfo.options.processor.process(task, verbose, ignoreErrors).then((body) => {
81
+ return this.jobInfo.options.processor.process(task, verbose, ignoreErrors, ignoreSelfSigned).then((body) => {
111
82
  const logColor = stringToChalk(task.name);
112
83
 
113
84
  if (verbose) {
114
85
  const processed = processResponse(body);
115
- console.log(`[${logColor(task.name)}] Completed successfully`, JSON.stringify(processed));
86
+ console.log(`[${logColor(task.name)}] Completed`, JSON.stringify(processed));
116
87
  } else {
117
- console.log(`[${logColor(task.name)}] Completed successfully`);
88
+ console.log(`[${logColor(task.name)}] Completed`);
118
89
  }
119
90
 
120
91
  task.inputTask.processing--;
@@ -23,7 +23,7 @@ export class GraphQLProcessor {
23
23
  this.client = new Client(opts);
24
24
  }
25
25
 
26
- async process(task: any, verbose = false, ignoreErrors = false): Promise<any> {
26
+ async process(task: any, verbose = false, ignoreErrors = false, ignoreSelfSigned = false): Promise<any> {
27
27
  let yamlStream = new YamlStreamReadTransformer();
28
28
  let jobPath = task.path;
29
29
  let data = false;
@@ -64,7 +64,7 @@ export class GraphQLProcessor {
64
64
  counter = 0;
65
65
  let batchText = '';
66
66
  if (batchsize > 0) {
67
- const from = batchCounter * batchsize;
67
+ const from = batchCounter * batchsize + 1;
68
68
  const to = from + (docArr.length - 1);
69
69
  batchText = from == to ? ` (${from})` : ` (${from} - ${to})`;
70
70
  }
@@ -85,10 +85,11 @@ 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));
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);
92
+ ignoreErrors && yamlStream.resume();
92
93
  }
93
94
  });
94
95
 
@@ -111,14 +112,14 @@ export class GraphQLProcessor {
111
112
  if (docArr && !_.isEmpty(docArr)) {
112
113
  let batchText = '';
113
114
  if (batchsize > 0) {
114
- const from = batchCounter * batchsize;
115
+ const from = batchCounter * batchsize + 1;
115
116
  const to = from + (docArr.length - 1);
116
117
  batchText = from == to ? ` (${from})` : ` (${from} - ${to})`;
117
118
  }
118
119
  batchCounter++;
119
120
  console.log(`[${logColor(task.name)}] Processing batch: ${batchCounter}${batchText}`);
120
121
  try {
121
- resultArr.push(await this.client.post(docArr, task, verbose));
122
+ resultArr.push(await this.client.post(docArr, task, verbose, ignoreErrors, ignoreSelfSigned));
122
123
  } catch (e) {
123
124
  !ignoreErrors && reject(e);
124
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
+ };