@driveflux/reporter 6.0.0 → 6.0.2

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.
@@ -1,352 +1,129 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
8
- }
9
- if (info.done) {
10
- resolve(value);
11
- } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _async_to_generator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- function _class_call_check(instance, Constructor) {
31
- if (!(instance instanceof Constructor)) {
32
- throw new TypeError("Cannot call a class as a function");
33
- }
34
- }
35
- function _defineProperties(target, props) {
36
- for(var i = 0; i < props.length; i++){
37
- var descriptor = props[i];
38
- descriptor.enumerable = descriptor.enumerable || false;
39
- descriptor.configurable = true;
40
- if ("value" in descriptor) descriptor.writable = true;
41
- Object.defineProperty(target, descriptor.key, descriptor);
42
- }
43
- }
44
- function _create_class(Constructor, protoProps, staticProps) {
45
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
46
- if (staticProps) _defineProperties(Constructor, staticProps);
47
- return Constructor;
48
- }
49
- function _define_property(obj, key, value) {
50
- if (key in obj) {
51
- Object.defineProperty(obj, key, {
52
- value: value,
53
- enumerable: true,
54
- configurable: true,
55
- writable: true
56
- });
57
- } else {
58
- obj[key] = value;
59
- }
60
- return obj;
61
- }
62
- function _ts_generator(thisArg, body) {
63
- var f, y, t, _ = {
64
- label: 0,
65
- sent: function() {
66
- if (t[0] & 1) throw t[1];
67
- return t[1];
68
- },
69
- trys: [],
70
- ops: []
71
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
72
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
73
- return this;
74
- }), g;
75
- function verb(n) {
76
- return function(v) {
77
- return step([
78
- n,
79
- v
80
- ]);
81
- };
82
- }
83
- function step(op) {
84
- if (f) throw new TypeError("Generator is already executing.");
85
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
86
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
87
- if (y = 0, t) op = [
88
- op[0] & 2,
89
- t.value
90
- ];
91
- switch(op[0]){
92
- case 0:
93
- case 1:
94
- t = op;
95
- break;
96
- case 4:
97
- _.label++;
98
- return {
99
- value: op[1],
100
- done: false
101
- };
102
- case 5:
103
- _.label++;
104
- y = op[1];
105
- op = [
106
- 0
107
- ];
108
- continue;
109
- case 7:
110
- op = _.ops.pop();
111
- _.trys.pop();
112
- continue;
113
- default:
114
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
115
- _ = 0;
116
- continue;
117
- }
118
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
119
- _.label = op[1];
120
- break;
121
- }
122
- if (op[0] === 6 && _.label < t[1]) {
123
- _.label = t[1];
124
- t = op;
125
- break;
126
- }
127
- if (t && _.label < t[2]) {
128
- _.label = t[2];
129
- _.ops.push(op);
130
- break;
131
- }
132
- if (t[2]) _.ops.pop();
133
- _.trys.pop();
134
- continue;
135
- }
136
- op = body.call(thisArg, _);
137
- } catch (e) {
138
- op = [
139
- 6,
140
- e
141
- ];
142
- y = 0;
143
- } finally{
144
- f = t = 0;
145
- }
146
- if (op[0] & 5) throw op[1];
147
- return {
148
- value: op[0] ? op[1] : void 0,
149
- done: true
150
- };
151
- }
152
- }
153
1
  import { enhancedFetch } from '@driveflux/fetch';
154
- import { ProblemSeverity, hasSeverityGte } from '@driveflux/problem';
2
+ import { ProblemSeverity, hasSeverityGte, } from '@driveflux/problem';
155
3
  import { isEmpty, omit } from '@driveflux/utils';
156
4
  import { flatten } from 'flat';
157
5
  import { config } from '../config.js';
158
- var lines = function(object) {
6
+ const lines = (object) => {
159
7
  return {
160
8
  type: 'section',
161
- fields: Object.keys(object).map(function(key) {
162
- return {
163
- type: 'mrkdwn',
164
- body: "*".concat(key, ":*\n").concat(JSON.stringify(object[key]))
165
- };
166
- })
9
+ fields: Object.keys(object).map((key) => ({
10
+ type: 'mrkdwn',
11
+ body: `*${key}:*\n${JSON.stringify(object[key])}`,
12
+ })),
167
13
  };
168
14
  };
169
- export var SlackDriver = /*#__PURE__*/ function() {
170
- "use strict";
171
- function SlackDriver(slackConfig) {
172
- _class_call_check(this, SlackDriver);
173
- _define_property(this, "accessToken", void 0);
174
- _define_property(this, "payload", {});
175
- _define_property(this, "channel", void 0);
15
+ export class SlackDriver {
16
+ accessToken;
17
+ payload = {};
18
+ channel;
19
+ constructor(slackConfig) {
176
20
  this.accessToken = slackConfig.token;
177
21
  this.channel = slackConfig.errorChannelId;
178
22
  }
179
- _create_class(SlackDriver, [
180
- {
181
- key: "reportError",
182
- value: function reportError(err, metadata) {
183
- return _async_to_generator(function() {
184
- var blocks;
185
- return _ts_generator(this, function(_state) {
186
- switch(_state.label){
187
- case 0:
188
- // We won't check for severity for Error objects as they are most likely always critical
189
- blocks = [
190
- {
191
- type: 'header',
192
- text: {
193
- type: 'mrkdwn',
194
- text: ':fire: Unexpected error happened',
195
- emoji: true
196
- }
197
- },
198
- {
199
- type: 'section',
200
- text: {
201
- type: 'mrkdwn',
202
- text: err.message
203
- }
204
- }
205
- ];
206
- this.standardBody(blocks, metadata);
207
- return [
208
- 4,
209
- this.slack(blocks)
210
- ];
211
- case 1:
212
- _state.sent();
213
- return [
214
- 2
215
- ];
216
- }
217
- });
218
- }).call(this);
219
- }
220
- },
221
- {
222
- key: "reportProblem",
223
- value: function reportProblem(problem) {
224
- return _async_to_generator(function() {
225
- var _config_slack, blocks;
226
- return _ts_generator(this, function(_state) {
227
- switch(_state.label){
228
- case 0:
229
- if (!hasSeverityGte(problem, ((_config_slack = config.slack) === null || _config_slack === void 0 ? void 0 : _config_slack.minimumSeverity) || ProblemSeverity.CRITICAL)) {
230
- return [
231
- 2
232
- ];
233
- }
234
- blocks = [
235
- {
236
- type: 'header',
237
- text: {
238
- type: 'plain_text',
239
- text: 'Problem',
240
- emoji: true
241
- }
242
- },
243
- {
244
- type: 'section',
245
- text: {
246
- type: 'mrkdwn',
247
- text: "*Code:*: ".concat(problem.code, "\n*Message:* ").concat(problem.message)
248
- }
249
- }
250
- ];
251
- this.standardBody(blocks, problem.metadata);
252
- return [
253
- 4,
254
- this.slack(blocks)
255
- ];
256
- case 1:
257
- _state.sent();
258
- return [
259
- 2
260
- ];
261
- }
262
- });
263
- }).call(this);
264
- }
265
- },
266
- {
267
- key: "setReporterPayload",
268
- value: function setReporterPayload(payload) {
269
- this.payload = payload;
270
- }
271
- },
272
- {
273
- key: "slack",
274
- value: function slack(blocks) {
275
- return _async_to_generator(function() {
276
- return _ts_generator(this, function(_state) {
277
- switch(_state.label){
278
- case 0:
279
- return [
280
- 4,
281
- enhancedFetch('https://slack.com/api/chat.postMessage', {
282
- method: 'POST',
283
- headers: {
284
- Authorization: "Bearer ".concat(this.accessToken)
285
- },
286
- body: JSON.stringify({
287
- channel: this.channel,
288
- blocks: blocks
289
- })
290
- })
291
- ];
292
- case 1:
293
- return [
294
- 2,
295
- _state.sent()
296
- ];
297
- }
298
- });
299
- }).call(this);
300
- }
301
- },
302
- {
303
- key: "standardBody",
304
- value: function standardBody(blocks, metadata) {
305
- if (metadata === null || metadata === void 0 ? void 0 : metadata.request) {
306
- var _metadata_request, _metadata_request1;
307
- blocks.push({
308
- type: 'section',
309
- text: {
310
- type: 'plain_text',
311
- text: 'Request:'
312
- }
313
- });
314
- blocks.push({
315
- type: 'section',
316
- fields: [
317
- {
318
- type: 'mrkdwn',
319
- body: "*URL:*\n".concat((_metadata_request = metadata.request) === null || _metadata_request === void 0 ? void 0 : _metadata_request.url)
320
- },
321
- {
322
- type: 'mrkdwn',
323
- body: "*Method:*\n".concat((_metadata_request1 = metadata.request) === null || _metadata_request1 === void 0 ? void 0 : _metadata_request1.method)
324
- }
325
- ]
326
- });
327
- }
328
- if (metadata) {
329
- blocks.push({
330
- type: 'section',
331
- text: {
332
- type: 'plain_text',
333
- text: 'Other Information:'
334
- }
335
- });
336
- blocks.push(lines(flatten(omit(metadata, 'request'))));
337
- }
338
- if (!isEmpty(this.payload)) {
339
- blocks.push({
340
- type: 'section',
341
- text: {
342
- type: 'plain_text',
343
- text: 'Other Information:'
344
- }
345
- });
346
- blocks.push(lines(flatten(this.payload)));
347
- }
348
- }
23
+ async reportError(err, metadata) {
24
+ // We won't check for severity for Error objects as they are most likely always critical
25
+ const blocks = [
26
+ {
27
+ type: 'header',
28
+ text: {
29
+ type: 'mrkdwn',
30
+ text: ':fire: Unexpected error happened',
31
+ emoji: true,
32
+ },
33
+ },
34
+ {
35
+ type: 'section',
36
+ text: {
37
+ type: 'mrkdwn',
38
+ text: err.message,
39
+ },
40
+ },
41
+ ];
42
+ this.standardBody(blocks, metadata);
43
+ await this.slack(blocks);
44
+ }
45
+ async reportProblem(problem) {
46
+ if (!hasSeverityGte(problem, config.slack?.minimumSeverity || ProblemSeverity.CRITICAL)) {
47
+ return;
48
+ }
49
+ const blocks = [
50
+ {
51
+ type: 'header',
52
+ text: {
53
+ type: 'plain_text',
54
+ text: 'Problem',
55
+ emoji: true,
56
+ },
57
+ },
58
+ {
59
+ type: 'section',
60
+ text: {
61
+ type: 'mrkdwn',
62
+ text: `*Code:*: ${problem.code}\n*Message:* ${problem.message}`,
63
+ },
64
+ },
65
+ ];
66
+ this.standardBody(blocks, problem.metadata);
67
+ await this.slack(blocks);
68
+ }
69
+ setReporterPayload(payload) {
70
+ this.payload = payload;
71
+ }
72
+ async slack(blocks) {
73
+ return await enhancedFetch('https://slack.com/api/chat.postMessage', {
74
+ method: 'POST',
75
+ headers: {
76
+ Authorization: `Bearer ${this.accessToken}`,
77
+ },
78
+ body: JSON.stringify({
79
+ channel: this.channel,
80
+ blocks,
81
+ }),
82
+ });
83
+ }
84
+ standardBody(blocks, metadata) {
85
+ if (metadata?.request) {
86
+ blocks.push({
87
+ type: 'section',
88
+ text: {
89
+ type: 'plain_text',
90
+ text: 'Request:',
91
+ },
92
+ });
93
+ blocks.push({
94
+ type: 'section',
95
+ fields: [
96
+ {
97
+ type: 'mrkdwn',
98
+ body: `*URL:*\n${metadata.request?.url}`,
99
+ },
100
+ {
101
+ type: 'mrkdwn',
102
+ body: `*Method:*\n${metadata.request?.method}`,
103
+ },
104
+ ],
105
+ });
106
+ }
107
+ if (metadata) {
108
+ blocks.push({
109
+ type: 'section',
110
+ text: {
111
+ type: 'plain_text',
112
+ text: 'Other Information:',
113
+ },
114
+ });
115
+ blocks.push(lines(flatten(omit(metadata, 'request'))));
349
116
  }
350
- ]);
351
- return SlackDriver;
352
- }();
117
+ if (!isEmpty(this.payload)) {
118
+ blocks.push({
119
+ type: 'section',
120
+ text: {
121
+ type: 'plain_text',
122
+ text: 'Other Information:',
123
+ },
124
+ });
125
+ blocks.push(lines(flatten(this.payload)));
126
+ }
127
+ }
128
+ }
129
+ //# sourceMappingURL=slack.js.map