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