@honeybadger-io/js 6.7.2 → 6.8.0

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.
@@ -193,7 +193,6 @@ var CheckIn = /** @class */ (function () {
193
193
  this.gracePeriod = props.gracePeriod;
194
194
  this.cronSchedule = props.cronSchedule;
195
195
  this.cronTimezone = props.cronTimezone;
196
- this.projectId = props.projectId;
197
196
  this.deleted = false;
198
197
  }
199
198
  CheckIn.prototype.isDeleted = function () {
@@ -203,39 +202,36 @@ var CheckIn = /** @class */ (function () {
203
202
  this.deleted = true;
204
203
  };
205
204
  CheckIn.prototype.validate = function () {
206
- if (!this.projectId) {
207
- throw new Error('projectId is required for each check-in');
208
- }
209
- if (!this.name) {
210
- throw new Error('name is required for each check-in');
205
+ if (!this.slug) {
206
+ throw new Error('slug is required for each check-in');
211
207
  }
212
208
  if (!this.scheduleType) {
213
209
  throw new Error('scheduleType is required for each check-in');
214
210
  }
215
211
  if (!['simple', 'cron'].includes(this.scheduleType)) {
216
- throw new Error("".concat(this.name, " [scheduleType] must be \"simple\" or \"cron\""));
212
+ throw new Error("".concat(this.slug, " [scheduleType] must be \"simple\" or \"cron\""));
217
213
  }
218
214
  if (this.scheduleType === 'simple' && !this.reportPeriod) {
219
- throw new Error("".concat(this.name, " [reportPeriod] is required for simple check-ins"));
215
+ throw new Error("".concat(this.slug, " [reportPeriod] is required for simple check-ins"));
220
216
  }
221
217
  if (this.scheduleType === 'cron' && !this.cronSchedule) {
222
- throw new Error("".concat(this.name, " [cronSchedule] is required for cron check-ins"));
218
+ throw new Error("".concat(this.slug, " [cronSchedule] is required for cron check-ins"));
223
219
  }
224
220
  };
225
221
  CheckIn.prototype.asRequestPayload = function () {
226
- var _a, _b, _c;
222
+ var _a, _b;
227
223
  var payload = {
228
224
  name: this.name,
229
225
  schedule_type: this.scheduleType,
230
- slug: (_a = this.slug) !== null && _a !== void 0 ? _a : '',
231
- grace_period: (_b = this.gracePeriod) !== null && _b !== void 0 ? _b : '' // default is empty string
226
+ slug: this.slug,
227
+ grace_period: (_a = this.gracePeriod) !== null && _a !== void 0 ? _a : '' // default is empty string
232
228
  };
233
229
  if (this.scheduleType === 'simple') {
234
230
  payload.report_period = this.reportPeriod;
235
231
  }
236
232
  else {
237
233
  payload.cron_schedule = this.cronSchedule;
238
- payload.cron_timezone = (_c = this.cronTimezone) !== null && _c !== void 0 ? _c : ''; // default is empty string
234
+ payload.cron_timezone = (_b = this.cronTimezone) !== null && _b !== void 0 ? _b : ''; // default is empty string
239
235
  }
240
236
  return payload;
241
237
  };
@@ -243,21 +239,25 @@ var CheckIn = /** @class */ (function () {
243
239
  * Compares two check-ins, usually the one from the API and the one from the config file.
244
240
  * If the one in the config file does not match the check-in from the API,
245
241
  * then we issue an update request.
242
+ *
243
+ * `name`, `gracePeriod` and `cronTimezone` are optional fields that are automatically
244
+ * set to a value from the server if one is not provided,
245
+ * so we ignore their values if they are not set locally.
246
246
  */
247
247
  CheckIn.prototype.isInSync = function (other) {
248
- var _a, _b, _c, _d, _e, _f;
249
- return this.name === other.name
250
- && this.projectId === other.projectId
248
+ var ignoreNameCheck = this.name === undefined;
249
+ var ignoreGracePeriodCheck = this.gracePeriod === undefined;
250
+ var ignoreCronTimezoneCheck = this.cronTimezone === undefined;
251
+ return this.slug === other.slug
251
252
  && this.scheduleType === other.scheduleType
252
253
  && this.reportPeriod === other.reportPeriod
253
254
  && this.cronSchedule === other.cronSchedule
254
- && ((_a = this.slug) !== null && _a !== void 0 ? _a : '') === ((_b = other.slug) !== null && _b !== void 0 ? _b : '')
255
- && ((_c = this.gracePeriod) !== null && _c !== void 0 ? _c : '') === ((_d = other.gracePeriod) !== null && _d !== void 0 ? _d : '')
256
- && ((_e = this.cronTimezone) !== null && _e !== void 0 ? _e : '') === ((_f = other.cronTimezone) !== null && _f !== void 0 ? _f : '');
255
+ && (ignoreNameCheck || this.name === other.name)
256
+ && (ignoreGracePeriodCheck || this.gracePeriod === other.gracePeriod)
257
+ && (ignoreCronTimezoneCheck || this.cronTimezone === other.cronTimezone);
257
258
  };
258
- CheckIn.fromResponsePayload = function (projectId, payload) {
259
+ CheckIn.fromResponsePayload = function (payload) {
259
260
  return new CheckIn({
260
- projectId: projectId,
261
261
  id: payload.id,
262
262
  name: payload.name,
263
263
  slug: payload.slug,
@@ -317,17 +317,40 @@ var CheckInsClient = /** @class */ (function () {
317
317
  this.transport = transport;
318
318
  this.config = config;
319
319
  this.logger = config.logger;
320
- this.cache = {};
321
320
  }
321
+ CheckInsClient.prototype.getProjectId = function (projectApiKey) {
322
+ var _a;
323
+ return __awaiter$2(this, void 0, void 0, function () {
324
+ var response, data;
325
+ return __generator$2(this, function (_b) {
326
+ switch (_b.label) {
327
+ case 0:
328
+ if (!this.config.personalAuthToken || this.config.personalAuthToken === '') {
329
+ throw new Error('personalAuthToken is required');
330
+ }
331
+ return [4 /*yield*/, this.transport.send({
332
+ method: 'GET',
333
+ headers: this.getHeaders(),
334
+ endpoint: "".concat(this.BASE_URL, "/v2/project_keys/").concat(projectApiKey),
335
+ logger: this.logger,
336
+ })];
337
+ case 1:
338
+ response = _b.sent();
339
+ if (response.statusCode !== 200) {
340
+ throw new Error("Failed to fetch project[".concat(projectApiKey, "]: ").concat(this.getErrorMessage(response.body)));
341
+ }
342
+ data = JSON.parse(response.body);
343
+ return [2 /*return*/, (_a = data === null || data === void 0 ? void 0 : data.project) === null || _a === void 0 ? void 0 : _a.id];
344
+ }
345
+ });
346
+ });
347
+ };
322
348
  CheckInsClient.prototype.listForProject = function (projectId) {
323
349
  return __awaiter$2(this, void 0, void 0, function () {
324
- var response, data, checkIns;
350
+ var response, data;
325
351
  return __generator$2(this, function (_a) {
326
352
  switch (_a.label) {
327
353
  case 0:
328
- if (this.cache[projectId]) {
329
- return [2 /*return*/, this.cache[projectId]];
330
- }
331
354
  if (!this.config.personalAuthToken || this.config.personalAuthToken === '') {
332
355
  throw new Error('personalAuthToken is required');
333
356
  }
@@ -343,16 +366,14 @@ var CheckInsClient = /** @class */ (function () {
343
366
  throw new Error("Failed to fetch checkins for project[".concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
344
367
  }
345
368
  data = JSON.parse(response.body);
346
- checkIns = data.results.map(function (checkin) { return check_in_1$1.CheckIn.fromResponsePayload(projectId, checkin); });
347
- this.cache[projectId] = checkIns;
348
- return [2 /*return*/, checkIns];
369
+ return [2 /*return*/, data.results.map(function (checkin) { return check_in_1$1.CheckIn.fromResponsePayload(checkin); })];
349
370
  }
350
371
  });
351
372
  });
352
373
  };
353
374
  CheckInsClient.prototype.get = function (projectId, checkInId) {
354
375
  return __awaiter$2(this, void 0, void 0, function () {
355
- var response, data, checkIn;
376
+ var response, data;
356
377
  return __generator$2(this, function (_a) {
357
378
  switch (_a.label) {
358
379
  case 0:
@@ -371,16 +392,14 @@ var CheckInsClient = /** @class */ (function () {
371
392
  throw new Error("Failed to fetch check-in[".concat(checkInId, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
372
393
  }
373
394
  data = JSON.parse(response.body);
374
- checkIn = check_in_1$1.CheckIn.fromResponsePayload(projectId, data);
375
- checkIn.projectId = projectId;
376
- return [2 /*return*/, checkIn];
395
+ return [2 /*return*/, check_in_1$1.CheckIn.fromResponsePayload(data)];
377
396
  }
378
397
  });
379
398
  });
380
399
  };
381
- CheckInsClient.prototype.create = function (checkIn) {
400
+ CheckInsClient.prototype.create = function (projectId, checkIn) {
382
401
  return __awaiter$2(this, void 0, void 0, function () {
383
- var response, data, result;
402
+ var response, data;
384
403
  return __generator$2(this, function (_a) {
385
404
  switch (_a.label) {
386
405
  case 0:
@@ -390,23 +409,21 @@ var CheckInsClient = /** @class */ (function () {
390
409
  return [4 /*yield*/, this.transport.send({
391
410
  method: 'POST',
392
411
  headers: this.getHeaders(),
393
- endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(checkIn.projectId, "/check_ins"),
412
+ endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(projectId, "/check_ins"),
394
413
  logger: this.logger,
395
414
  }, { check_in: checkIn.asRequestPayload() })];
396
415
  case 1:
397
416
  response = _a.sent();
398
417
  if (response.statusCode !== 201) {
399
- throw new Error("Failed to create check-in[".concat(checkIn.name, "] for project[").concat(checkIn.projectId, "]: ").concat(this.getErrorMessage(response.body)));
418
+ throw new Error("Failed to create check-in[".concat(checkIn.slug, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
400
419
  }
401
420
  data = JSON.parse(response.body);
402
- result = check_in_1$1.CheckIn.fromResponsePayload(checkIn.projectId, data);
403
- result.projectId = checkIn.projectId;
404
- return [2 /*return*/, result];
421
+ return [2 /*return*/, check_in_1$1.CheckIn.fromResponsePayload(data)];
405
422
  }
406
423
  });
407
424
  });
408
425
  };
409
- CheckInsClient.prototype.update = function (checkIn) {
426
+ CheckInsClient.prototype.update = function (projectId, checkIn) {
410
427
  return __awaiter$2(this, void 0, void 0, function () {
411
428
  var response;
412
429
  return __generator$2(this, function (_a) {
@@ -418,20 +435,20 @@ var CheckInsClient = /** @class */ (function () {
418
435
  return [4 /*yield*/, this.transport.send({
419
436
  method: 'PUT',
420
437
  headers: this.getHeaders(),
421
- endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(checkIn.projectId, "/check_ins/").concat(checkIn.id),
438
+ endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(projectId, "/check_ins/").concat(checkIn.id),
422
439
  logger: this.logger,
423
440
  }, { check_in: checkIn.asRequestPayload() })];
424
441
  case 1:
425
442
  response = _a.sent();
426
443
  if (response.statusCode !== 204) {
427
- throw new Error("Failed to update checkin[".concat(checkIn.name, "] for project[").concat(checkIn.projectId, "]: ").concat(this.getErrorMessage(response.body)));
444
+ throw new Error("Failed to update checkin[".concat(checkIn.slug, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
428
445
  }
429
446
  return [2 /*return*/, checkIn];
430
447
  }
431
448
  });
432
449
  });
433
450
  };
434
- CheckInsClient.prototype.remove = function (checkIn) {
451
+ CheckInsClient.prototype.remove = function (projectId, checkIn) {
435
452
  return __awaiter$2(this, void 0, void 0, function () {
436
453
  var response;
437
454
  return __generator$2(this, function (_a) {
@@ -443,13 +460,13 @@ var CheckInsClient = /** @class */ (function () {
443
460
  return [4 /*yield*/, this.transport.send({
444
461
  method: 'DELETE',
445
462
  headers: this.getHeaders(),
446
- endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(checkIn.projectId, "/check_ins/").concat(checkIn.id),
463
+ endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(projectId, "/check_ins/").concat(checkIn.id),
447
464
  logger: this.logger,
448
465
  })];
449
466
  case 1:
450
467
  response = _a.sent();
451
468
  if (response.statusCode !== 204) {
452
- throw new Error("Failed to remove checkin[".concat(checkIn.name, "] for project[").concat(checkIn.projectId, "]: ").concat(this.getErrorMessage(response.body)));
469
+ throw new Error("Failed to remove checkin[".concat(checkIn.slug, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
453
470
  }
454
471
  return [2 /*return*/];
455
472
  }
@@ -646,36 +663,48 @@ var transport_1 = transport;
646
663
  var check_in_1 = checkIn;
647
664
  var CheckInsManager$1 = /** @class */ (function () {
648
665
  function CheckInsManager(config, client) {
649
- var _a, _b, _c, _d;
666
+ var _a, _b, _c, _d, _e;
650
667
  this.config = {
651
668
  debug: (_a = config.debug) !== null && _a !== void 0 ? _a : false,
652
- personalAuthToken: (_b = config.personalAuthToken) !== null && _b !== void 0 ? _b : undefined,
653
- checkins: (_c = config.checkins) !== null && _c !== void 0 ? _c : [],
654
- logger: (_d = config.logger) !== null && _d !== void 0 ? _d : console,
669
+ apiKey: (_b = config.apiKey) !== null && _b !== void 0 ? _b : undefined,
670
+ personalAuthToken: (_c = config.personalAuthToken) !== null && _c !== void 0 ? _c : undefined,
671
+ checkins: (_d = config.checkins) !== null && _d !== void 0 ? _d : [],
672
+ logger: (_e = config.logger) !== null && _e !== void 0 ? _e : console,
655
673
  };
656
674
  var transport = new transport_1.ServerTransport();
657
675
  this.logger = core_1.Util.logger(this);
658
676
  this.client = client !== null && client !== void 0 ? client : new client_1.CheckInsClient({
677
+ apiKey: config.apiKey,
659
678
  personalAuthToken: config.personalAuthToken,
660
679
  logger: this.logger
661
680
  }, transport);
662
681
  }
663
682
  CheckInsManager.prototype.sync = function () {
664
683
  return __awaiter$1(this, void 0, void 0, function () {
665
- var localCheckIns, createdOrUpdated, removed;
684
+ var localCheckIns, projectId, remoteCheckIns, createdOrUpdated, removed;
666
685
  return __generator$1(this, function (_a) {
667
686
  switch (_a.label) {
668
687
  case 0:
688
+ // check if api key is set
689
+ if (!this.config.apiKey || this.config.apiKey === '') {
690
+ throw new Error('apiKey is required');
691
+ }
669
692
  // check if personal auth token is set
670
693
  if (!this.config.personalAuthToken || this.config.personalAuthToken === '') {
671
694
  throw new Error('personalAuthToken is required');
672
695
  }
673
696
  localCheckIns = this.getLocalCheckIns();
674
- return [4 /*yield*/, this.createOrUpdate(localCheckIns)];
697
+ return [4 /*yield*/, this.client.getProjectId(this.config.apiKey)];
675
698
  case 1:
676
- createdOrUpdated = _a.sent();
677
- return [4 /*yield*/, this.remove(localCheckIns)];
699
+ projectId = _a.sent();
700
+ return [4 /*yield*/, this.client.listForProject(projectId)];
678
701
  case 2:
702
+ remoteCheckIns = _a.sent();
703
+ return [4 /*yield*/, this.createOrUpdate(projectId, localCheckIns, remoteCheckIns)];
704
+ case 3:
705
+ createdOrUpdated = _a.sent();
706
+ return [4 /*yield*/, this.remove(projectId, localCheckIns, remoteCheckIns)];
707
+ case 4:
679
708
  removed = _a.sent();
680
709
  return [2 /*return*/, __spreadArray(__spreadArray([], createdOrUpdated, true), removed, true)];
681
710
  }
@@ -689,16 +718,15 @@ var CheckInsManager$1 = /** @class */ (function () {
689
718
  checkIn.validate();
690
719
  return checkIn;
691
720
  });
692
- // validate that we have unique check-in names
693
- // throw error if there are check-ins with the same name and project id
694
- var checkInNames = localCheckIns.map(function (checkIn) { return "".concat(checkIn.projectId, "_").concat(checkIn.name); });
695
- var uniqueCheckInNames = new Set(checkInNames);
696
- if (checkInNames.length !== uniqueCheckInNames.size) {
697
- throw new Error('check-in names must be unique per project');
721
+ // validate that we have unique check-in slugs
722
+ var checkInSlugs = localCheckIns.map(function (checkIn) { return checkIn.slug; });
723
+ var uniqueCheckInSlugs = new Set(checkInSlugs);
724
+ if (checkInSlugs.length !== uniqueCheckInSlugs.size) {
725
+ throw new Error('check-in slugs must be unique');
698
726
  }
699
727
  return localCheckIns;
700
728
  };
701
- CheckInsManager.prototype.createOrUpdate = function (localCheckIns) {
729
+ CheckInsManager.prototype.createOrUpdate = function (projectId, localCheckIns, remoteCheckIns) {
702
730
  return __awaiter$1(this, void 0, void 0, function () {
703
731
  var results, _loop_1, this_1, _i, localCheckIns_1, localCheckIn;
704
732
  return __generator$1(this, function (_a) {
@@ -706,34 +734,32 @@ var CheckInsManager$1 = /** @class */ (function () {
706
734
  case 0:
707
735
  results = [];
708
736
  _loop_1 = function (localCheckIn) {
709
- var projectCheckIns, remoteCheckIn, _b, _c, _d, _e;
737
+ var remoteCheckIn, _b, _c, _d, _e;
710
738
  return __generator$1(this, function (_f) {
711
739
  switch (_f.label) {
712
- case 0: return [4 /*yield*/, this_1.client.listForProject(localCheckIn.projectId)];
713
- case 1:
714
- projectCheckIns = _f.sent();
715
- remoteCheckIn = projectCheckIns.find(function (checkIn) {
716
- return checkIn.name === localCheckIn.name;
740
+ case 0:
741
+ remoteCheckIn = remoteCheckIns.find(function (checkIn) {
742
+ return checkIn.slug === localCheckIn.slug;
717
743
  });
718
- if (!!remoteCheckIn) return [3 /*break*/, 3];
744
+ if (!!remoteCheckIn) return [3 /*break*/, 2];
719
745
  _c = (_b = results).push;
720
- return [4 /*yield*/, this_1.client.create(localCheckIn)];
721
- case 2:
746
+ return [4 /*yield*/, this_1.client.create(projectId, localCheckIn)];
747
+ case 1:
722
748
  _c.apply(_b, [_f.sent()]);
723
- return [3 /*break*/, 6];
724
- case 3:
725
- if (!!localCheckIn.isInSync(remoteCheckIn)) return [3 /*break*/, 5];
749
+ return [3 /*break*/, 5];
750
+ case 2:
751
+ if (!!localCheckIn.isInSync(remoteCheckIn)) return [3 /*break*/, 4];
726
752
  localCheckIn.id = remoteCheckIn.id;
727
753
  _e = (_d = results).push;
728
- return [4 /*yield*/, this_1.client.update(localCheckIn)];
729
- case 4:
754
+ return [4 /*yield*/, this_1.client.update(projectId, localCheckIn)];
755
+ case 3:
730
756
  _e.apply(_d, [_f.sent()]);
731
- return [3 /*break*/, 6];
732
- case 5:
757
+ return [3 /*break*/, 5];
758
+ case 4:
733
759
  // no change - still need to add to results
734
760
  results.push(remoteCheckIn);
735
- _f.label = 6;
736
- case 6: return [2 /*return*/];
761
+ _f.label = 5;
762
+ case 5: return [2 /*return*/];
737
763
  }
738
764
  });
739
765
  };
@@ -755,37 +781,27 @@ var CheckInsManager$1 = /** @class */ (function () {
755
781
  });
756
782
  });
757
783
  };
758
- CheckInsManager.prototype.remove = function (localCheckIns) {
784
+ CheckInsManager.prototype.remove = function (projectId, localCheckIns, remoteCheckIns) {
759
785
  return __awaiter$1(this, void 0, void 0, function () {
760
- var projectIds, remoteCheckInsPerProject, allRemoteCheckIns, checkInsToRemove;
786
+ var checkInsToRemove;
761
787
  var _this = this;
762
788
  return __generator$1(this, function (_a) {
763
- switch (_a.label) {
764
- case 0:
765
- projectIds = Array.from(new Set(localCheckIns.map(function (checkIn) { return checkIn.projectId; })));
766
- return [4 /*yield*/, Promise.all(projectIds.map(function (projectId) {
767
- return _this.client.listForProject(projectId);
768
- }))];
769
- case 1:
770
- remoteCheckInsPerProject = _a.sent();
771
- allRemoteCheckIns = remoteCheckInsPerProject.flat();
772
- checkInsToRemove = allRemoteCheckIns.filter(function (remoteCheckIn) {
773
- return !localCheckIns.find(function (localCheckIn) {
774
- return localCheckIn.name === remoteCheckIn.name;
775
- });
789
+ checkInsToRemove = remoteCheckIns.filter(function (remoteCheckIn) {
790
+ return !localCheckIns.find(function (localCheckIn) {
791
+ return localCheckIn.slug === remoteCheckIn.slug;
792
+ });
793
+ });
794
+ return [2 /*return*/, Promise.all(checkInsToRemove.map(function (checkIn) { return __awaiter$1(_this, void 0, void 0, function () {
795
+ return __generator$1(this, function (_a) {
796
+ switch (_a.label) {
797
+ case 0: return [4 /*yield*/, this.client.remove(projectId, checkIn)];
798
+ case 1:
799
+ _a.sent();
800
+ checkIn.markAsDeleted();
801
+ return [2 /*return*/, checkIn];
802
+ }
776
803
  });
777
- return [2 /*return*/, Promise.all(checkInsToRemove.map(function (checkIn) { return __awaiter$1(_this, void 0, void 0, function () {
778
- return __generator$1(this, function (_a) {
779
- switch (_a.label) {
780
- case 0: return [4 /*yield*/, this.client.remove(checkIn)];
781
- case 1:
782
- _a.sent();
783
- checkIn.markAsDeleted();
784
- return [2 /*return*/, checkIn];
785
- }
786
- });
787
- }); }))];
788
- }
804
+ }); }))];
789
805
  });
790
806
  });
791
807
  };