@honeybadger-io/js 6.7.1 → 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.
@@ -3,8 +3,8 @@
3
3
  var require$$0$2 = require('os');
4
4
  var require$$1$1 = require('domain');
5
5
  var require$$1 = require('fs');
6
- var require$$2 = require('util');
7
- var require$$3 = require('cosmiconfig');
6
+ var require$$2 = require('path');
7
+ var require$$3 = require('util');
8
8
  var require$$0$3 = require('url');
9
9
  var require$$3$1 = require('http');
10
10
  var require$$4 = require('https');
@@ -1105,7 +1105,7 @@ var Client = /** @class */ (function () {
1105
1105
  this.__notifier = {
1106
1106
  name: '@honeybadger-io/core',
1107
1107
  url: 'https://github.com/honeybadger-io/honeybadger-js/tree/master/packages/core',
1108
- version: '6.7.1'
1108
+ version: '6.8.0'
1109
1109
  };
1110
1110
  this.config = __assign$1(__assign$1({}, defaults_1.CONFIG), opts);
1111
1111
  this.__initStore();
@@ -1551,8 +1551,8 @@ Object.defineProperty(util, "__esModule", { value: true });
1551
1551
  util.readConfigFromFileSystem = util.getSourceFile = util.getStats = util.fatallyLogAndExit = void 0;
1552
1552
  var os_1 = __importDefault$3(require$$0__default["default"]);
1553
1553
  var fs_1 = __importDefault$3(require$$1__default["default"]);
1554
- var util_1$3 = require$$2__default["default"];
1555
- var cosmiconfig_1 = require$$3__default["default"];
1554
+ var path_1 = __importDefault$3(require$$2__default["default"]);
1555
+ var util_1$3 = require$$3__default["default"];
1556
1556
  var readFile = (0, util_1$3.promisify)(fs_1.default.readFile);
1557
1557
  function fatallyLogAndExit(err) {
1558
1558
  console.error('[Honeybadger] Exiting process due to uncaught exception');
@@ -1639,21 +1639,18 @@ function getSourceFile(path) {
1639
1639
  }
1640
1640
  util.getSourceFile = getSourceFile;
1641
1641
  function readConfigFromFileSystem() {
1642
- return readConfigForModule('honeybadger');
1643
- }
1644
- util.readConfigFromFileSystem = readConfigFromFileSystem;
1645
- function readConfigForModule(moduleName) {
1646
- var _a;
1647
- try {
1648
- var fileExplorer = (0, cosmiconfig_1.cosmiconfigSync)(moduleName);
1649
- var result = fileExplorer.search();
1650
- return (_a = result === null || result === void 0 ? void 0 : result.config) !== null && _a !== void 0 ? _a : null;
1651
- }
1652
- catch (e) {
1653
- console.debug("[Honeybadger] Could not read config for module ".concat(moduleName, ": ").concat(e.message));
1654
- return null;
1642
+ var options = ['honeybadger.config.js', 'honeybadger.config.ts'];
1643
+ var config = null;
1644
+ while (config === null && options.length > 0) {
1645
+ var configPath = options.shift();
1646
+ try {
1647
+ config = require(path_1.default.join(process.cwd(), configPath));
1648
+ }
1649
+ catch (_e) { /* empty */ }
1655
1650
  }
1651
+ return config;
1656
1652
  }
1653
+ util.readConfigFromFileSystem = readConfigFromFileSystem;
1657
1654
 
1658
1655
  var uncaught_exception_plugin = {};
1659
1656
 
@@ -2234,7 +2231,6 @@ var CheckIn = /** @class */ (function () {
2234
2231
  this.gracePeriod = props.gracePeriod;
2235
2232
  this.cronSchedule = props.cronSchedule;
2236
2233
  this.cronTimezone = props.cronTimezone;
2237
- this.projectId = props.projectId;
2238
2234
  this.deleted = false;
2239
2235
  }
2240
2236
  CheckIn.prototype.isDeleted = function () {
@@ -2244,39 +2240,36 @@ var CheckIn = /** @class */ (function () {
2244
2240
  this.deleted = true;
2245
2241
  };
2246
2242
  CheckIn.prototype.validate = function () {
2247
- if (!this.projectId) {
2248
- throw new Error('projectId is required for each check-in');
2249
- }
2250
- if (!this.name) {
2251
- throw new Error('name is required for each check-in');
2243
+ if (!this.slug) {
2244
+ throw new Error('slug is required for each check-in');
2252
2245
  }
2253
2246
  if (!this.scheduleType) {
2254
2247
  throw new Error('scheduleType is required for each check-in');
2255
2248
  }
2256
2249
  if (!['simple', 'cron'].includes(this.scheduleType)) {
2257
- throw new Error("".concat(this.name, " [scheduleType] must be \"simple\" or \"cron\""));
2250
+ throw new Error("".concat(this.slug, " [scheduleType] must be \"simple\" or \"cron\""));
2258
2251
  }
2259
2252
  if (this.scheduleType === 'simple' && !this.reportPeriod) {
2260
- throw new Error("".concat(this.name, " [reportPeriod] is required for simple check-ins"));
2253
+ throw new Error("".concat(this.slug, " [reportPeriod] is required for simple check-ins"));
2261
2254
  }
2262
2255
  if (this.scheduleType === 'cron' && !this.cronSchedule) {
2263
- throw new Error("".concat(this.name, " [cronSchedule] is required for cron check-ins"));
2256
+ throw new Error("".concat(this.slug, " [cronSchedule] is required for cron check-ins"));
2264
2257
  }
2265
2258
  };
2266
2259
  CheckIn.prototype.asRequestPayload = function () {
2267
- var _a, _b, _c;
2260
+ var _a, _b;
2268
2261
  var payload = {
2269
2262
  name: this.name,
2270
2263
  schedule_type: this.scheduleType,
2271
- slug: (_a = this.slug) !== null && _a !== void 0 ? _a : '',
2272
- grace_period: (_b = this.gracePeriod) !== null && _b !== void 0 ? _b : '' // default is empty string
2264
+ slug: this.slug,
2265
+ grace_period: (_a = this.gracePeriod) !== null && _a !== void 0 ? _a : '' // default is empty string
2273
2266
  };
2274
2267
  if (this.scheduleType === 'simple') {
2275
2268
  payload.report_period = this.reportPeriod;
2276
2269
  }
2277
2270
  else {
2278
2271
  payload.cron_schedule = this.cronSchedule;
2279
- payload.cron_timezone = (_c = this.cronTimezone) !== null && _c !== void 0 ? _c : ''; // default is empty string
2272
+ payload.cron_timezone = (_b = this.cronTimezone) !== null && _b !== void 0 ? _b : ''; // default is empty string
2280
2273
  }
2281
2274
  return payload;
2282
2275
  };
@@ -2284,21 +2277,25 @@ var CheckIn = /** @class */ (function () {
2284
2277
  * Compares two check-ins, usually the one from the API and the one from the config file.
2285
2278
  * If the one in the config file does not match the check-in from the API,
2286
2279
  * then we issue an update request.
2280
+ *
2281
+ * `name`, `gracePeriod` and `cronTimezone` are optional fields that are automatically
2282
+ * set to a value from the server if one is not provided,
2283
+ * so we ignore their values if they are not set locally.
2287
2284
  */
2288
2285
  CheckIn.prototype.isInSync = function (other) {
2289
- var _a, _b, _c, _d, _e, _f;
2290
- return this.name === other.name
2291
- && this.projectId === other.projectId
2286
+ var ignoreNameCheck = this.name === undefined;
2287
+ var ignoreGracePeriodCheck = this.gracePeriod === undefined;
2288
+ var ignoreCronTimezoneCheck = this.cronTimezone === undefined;
2289
+ return this.slug === other.slug
2292
2290
  && this.scheduleType === other.scheduleType
2293
2291
  && this.reportPeriod === other.reportPeriod
2294
2292
  && this.cronSchedule === other.cronSchedule
2295
- && ((_a = this.slug) !== null && _a !== void 0 ? _a : '') === ((_b = other.slug) !== null && _b !== void 0 ? _b : '')
2296
- && ((_c = this.gracePeriod) !== null && _c !== void 0 ? _c : '') === ((_d = other.gracePeriod) !== null && _d !== void 0 ? _d : '')
2297
- && ((_e = this.cronTimezone) !== null && _e !== void 0 ? _e : '') === ((_f = other.cronTimezone) !== null && _f !== void 0 ? _f : '');
2293
+ && (ignoreNameCheck || this.name === other.name)
2294
+ && (ignoreGracePeriodCheck || this.gracePeriod === other.gracePeriod)
2295
+ && (ignoreCronTimezoneCheck || this.cronTimezone === other.cronTimezone);
2298
2296
  };
2299
- CheckIn.fromResponsePayload = function (projectId, payload) {
2297
+ CheckIn.fromResponsePayload = function (payload) {
2300
2298
  return new CheckIn({
2301
- projectId: projectId,
2302
2299
  id: payload.id,
2303
2300
  name: payload.name,
2304
2301
  slug: payload.slug,
@@ -2358,17 +2355,40 @@ var CheckInsClient = /** @class */ (function () {
2358
2355
  this.transport = transport;
2359
2356
  this.config = config;
2360
2357
  this.logger = config.logger;
2361
- this.cache = {};
2362
2358
  }
2359
+ CheckInsClient.prototype.getProjectId = function (projectApiKey) {
2360
+ var _a;
2361
+ return __awaiter(this, void 0, void 0, function () {
2362
+ var response, data;
2363
+ return __generator(this, function (_b) {
2364
+ switch (_b.label) {
2365
+ case 0:
2366
+ if (!this.config.personalAuthToken || this.config.personalAuthToken === '') {
2367
+ throw new Error('personalAuthToken is required');
2368
+ }
2369
+ return [4 /*yield*/, this.transport.send({
2370
+ method: 'GET',
2371
+ headers: this.getHeaders(),
2372
+ endpoint: "".concat(this.BASE_URL, "/v2/project_keys/").concat(projectApiKey),
2373
+ logger: this.logger,
2374
+ })];
2375
+ case 1:
2376
+ response = _b.sent();
2377
+ if (response.statusCode !== 200) {
2378
+ throw new Error("Failed to fetch project[".concat(projectApiKey, "]: ").concat(this.getErrorMessage(response.body)));
2379
+ }
2380
+ data = JSON.parse(response.body);
2381
+ return [2 /*return*/, (_a = data === null || data === void 0 ? void 0 : data.project) === null || _a === void 0 ? void 0 : _a.id];
2382
+ }
2383
+ });
2384
+ });
2385
+ };
2363
2386
  CheckInsClient.prototype.listForProject = function (projectId) {
2364
2387
  return __awaiter(this, void 0, void 0, function () {
2365
- var response, data, checkIns;
2388
+ var response, data;
2366
2389
  return __generator(this, function (_a) {
2367
2390
  switch (_a.label) {
2368
2391
  case 0:
2369
- if (this.cache[projectId]) {
2370
- return [2 /*return*/, this.cache[projectId]];
2371
- }
2372
2392
  if (!this.config.personalAuthToken || this.config.personalAuthToken === '') {
2373
2393
  throw new Error('personalAuthToken is required');
2374
2394
  }
@@ -2384,16 +2404,14 @@ var CheckInsClient = /** @class */ (function () {
2384
2404
  throw new Error("Failed to fetch checkins for project[".concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
2385
2405
  }
2386
2406
  data = JSON.parse(response.body);
2387
- checkIns = data.results.map(function (checkin) { return check_in_1.CheckIn.fromResponsePayload(projectId, checkin); });
2388
- this.cache[projectId] = checkIns;
2389
- return [2 /*return*/, checkIns];
2407
+ return [2 /*return*/, data.results.map(function (checkin) { return check_in_1.CheckIn.fromResponsePayload(checkin); })];
2390
2408
  }
2391
2409
  });
2392
2410
  });
2393
2411
  };
2394
2412
  CheckInsClient.prototype.get = function (projectId, checkInId) {
2395
2413
  return __awaiter(this, void 0, void 0, function () {
2396
- var response, data, checkIn;
2414
+ var response, data;
2397
2415
  return __generator(this, function (_a) {
2398
2416
  switch (_a.label) {
2399
2417
  case 0:
@@ -2412,16 +2430,14 @@ var CheckInsClient = /** @class */ (function () {
2412
2430
  throw new Error("Failed to fetch check-in[".concat(checkInId, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
2413
2431
  }
2414
2432
  data = JSON.parse(response.body);
2415
- checkIn = check_in_1.CheckIn.fromResponsePayload(projectId, data);
2416
- checkIn.projectId = projectId;
2417
- return [2 /*return*/, checkIn];
2433
+ return [2 /*return*/, check_in_1.CheckIn.fromResponsePayload(data)];
2418
2434
  }
2419
2435
  });
2420
2436
  });
2421
2437
  };
2422
- CheckInsClient.prototype.create = function (checkIn) {
2438
+ CheckInsClient.prototype.create = function (projectId, checkIn) {
2423
2439
  return __awaiter(this, void 0, void 0, function () {
2424
- var response, data, result;
2440
+ var response, data;
2425
2441
  return __generator(this, function (_a) {
2426
2442
  switch (_a.label) {
2427
2443
  case 0:
@@ -2431,23 +2447,21 @@ var CheckInsClient = /** @class */ (function () {
2431
2447
  return [4 /*yield*/, this.transport.send({
2432
2448
  method: 'POST',
2433
2449
  headers: this.getHeaders(),
2434
- endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(checkIn.projectId, "/check_ins"),
2450
+ endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(projectId, "/check_ins"),
2435
2451
  logger: this.logger,
2436
2452
  }, { check_in: checkIn.asRequestPayload() })];
2437
2453
  case 1:
2438
2454
  response = _a.sent();
2439
2455
  if (response.statusCode !== 201) {
2440
- throw new Error("Failed to create check-in[".concat(checkIn.name, "] for project[").concat(checkIn.projectId, "]: ").concat(this.getErrorMessage(response.body)));
2456
+ throw new Error("Failed to create check-in[".concat(checkIn.slug, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
2441
2457
  }
2442
2458
  data = JSON.parse(response.body);
2443
- result = check_in_1.CheckIn.fromResponsePayload(checkIn.projectId, data);
2444
- result.projectId = checkIn.projectId;
2445
- return [2 /*return*/, result];
2459
+ return [2 /*return*/, check_in_1.CheckIn.fromResponsePayload(data)];
2446
2460
  }
2447
2461
  });
2448
2462
  });
2449
2463
  };
2450
- CheckInsClient.prototype.update = function (checkIn) {
2464
+ CheckInsClient.prototype.update = function (projectId, checkIn) {
2451
2465
  return __awaiter(this, void 0, void 0, function () {
2452
2466
  var response;
2453
2467
  return __generator(this, function (_a) {
@@ -2459,20 +2473,20 @@ var CheckInsClient = /** @class */ (function () {
2459
2473
  return [4 /*yield*/, this.transport.send({
2460
2474
  method: 'PUT',
2461
2475
  headers: this.getHeaders(),
2462
- endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(checkIn.projectId, "/check_ins/").concat(checkIn.id),
2476
+ endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(projectId, "/check_ins/").concat(checkIn.id),
2463
2477
  logger: this.logger,
2464
2478
  }, { check_in: checkIn.asRequestPayload() })];
2465
2479
  case 1:
2466
2480
  response = _a.sent();
2467
2481
  if (response.statusCode !== 204) {
2468
- throw new Error("Failed to update checkin[".concat(checkIn.name, "] for project[").concat(checkIn.projectId, "]: ").concat(this.getErrorMessage(response.body)));
2482
+ throw new Error("Failed to update checkin[".concat(checkIn.slug, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
2469
2483
  }
2470
2484
  return [2 /*return*/, checkIn];
2471
2485
  }
2472
2486
  });
2473
2487
  });
2474
2488
  };
2475
- CheckInsClient.prototype.remove = function (checkIn) {
2489
+ CheckInsClient.prototype.remove = function (projectId, checkIn) {
2476
2490
  return __awaiter(this, void 0, void 0, function () {
2477
2491
  var response;
2478
2492
  return __generator(this, function (_a) {
@@ -2484,13 +2498,13 @@ var CheckInsClient = /** @class */ (function () {
2484
2498
  return [4 /*yield*/, this.transport.send({
2485
2499
  method: 'DELETE',
2486
2500
  headers: this.getHeaders(),
2487
- endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(checkIn.projectId, "/check_ins/").concat(checkIn.id),
2501
+ endpoint: "".concat(this.BASE_URL, "/v2/projects/").concat(projectId, "/check_ins/").concat(checkIn.id),
2488
2502
  logger: this.logger,
2489
2503
  })];
2490
2504
  case 1:
2491
2505
  response = _a.sent();
2492
2506
  if (response.statusCode !== 204) {
2493
- throw new Error("Failed to remove checkin[".concat(checkIn.name, "] for project[").concat(checkIn.projectId, "]: ").concat(this.getErrorMessage(response.body)));
2507
+ throw new Error("Failed to remove checkin[".concat(checkIn.slug, "] for project[").concat(projectId, "]: ").concat(this.getErrorMessage(response.body)));
2494
2508
  }
2495
2509
  return [2 /*return*/];
2496
2510
  }
@@ -2657,66 +2671,72 @@ client.CheckInsClient = CheckInsClient;
2657
2671
  Honeybadger.prototype.showUserFeedbackForm = function () {
2658
2672
  throw new Error('Honeybadger.showUserFeedbackForm() is not supported on the server-side');
2659
2673
  };
2660
- Honeybadger.prototype.checkIn = function (idOrName) {
2674
+ Honeybadger.prototype.checkIn = function (idOrSlug) {
2675
+ return __awaiter(this, void 0, void 0, function () {
2676
+ return __generator(this, function (_a) {
2677
+ if (this.isCheckInSlug(idOrSlug)) {
2678
+ return [2 /*return*/, this.checkInWithSlug(idOrSlug)];
2679
+ }
2680
+ return [2 /*return*/, this.checkInWithId(idOrSlug)];
2681
+ });
2682
+ });
2683
+ };
2684
+ Honeybadger.prototype.checkInWithSlug = function (slug) {
2661
2685
  return __awaiter(this, void 0, void 0, function () {
2662
- var id, err_1;
2686
+ var err_1;
2663
2687
  return __generator(this, function (_a) {
2664
2688
  switch (_a.label) {
2665
2689
  case 0:
2666
- _a.trys.push([0, 3, , 4]);
2667
- return [4 /*yield*/, this.getCheckInId(idOrName)];
2668
- case 1:
2669
- id = _a.sent();
2690
+ _a.trys.push([0, 2, , 3]);
2670
2691
  return [4 /*yield*/, this.__transport
2671
2692
  .send({
2672
2693
  method: 'GET',
2673
- endpoint: endpoint(this.config.endpoint, "v1/check_in/".concat(id)),
2694
+ endpoint: endpoint(this.config.endpoint, "v1/check_in/".concat(this.config.apiKey, "/").concat(slug)),
2674
2695
  logger: this.logger,
2675
2696
  })];
2676
- case 2:
2697
+ case 1:
2677
2698
  _a.sent();
2678
- this.logger.info('CheckIn sent');
2679
- return [3 /*break*/, 4];
2680
- case 3:
2699
+ this.logger.info("Check-In with slug[".concat(slug, "] sent"));
2700
+ return [3 /*break*/, 3];
2701
+ case 2:
2681
2702
  err_1 = _a.sent();
2682
- this.logger.error("CheckIn[".concat(idOrName, "] failed: an unknown error occurred."), "message=".concat(err_1.message));
2683
- return [3 /*break*/, 4];
2684
- case 4: return [2 /*return*/];
2703
+ this.logger.error("CheckIn with slug[".concat(slug, "] failed: an unknown error occurred."), "message=".concat(err_1.message));
2704
+ return [3 /*break*/, 3];
2705
+ case 3: return [2 /*return*/];
2685
2706
  }
2686
2707
  });
2687
2708
  });
2688
2709
  };
2689
- Honeybadger.prototype.getCheckInId = function (idOrName) {
2710
+ Honeybadger.prototype.checkInWithId = function (id) {
2690
2711
  return __awaiter(this, void 0, void 0, function () {
2691
- var localCheckIn, projectCheckIns, remoteCheckIn;
2712
+ var err_2;
2692
2713
  return __generator(this, function (_a) {
2693
2714
  switch (_a.label) {
2694
2715
  case 0:
2695
- if (!this.config.checkins || this.config.checkins.length === 0) {
2696
- return [2 /*return*/, idOrName];
2697
- }
2698
- localCheckIn = this.config.checkins.find(function (c) { return c.name === idOrName; });
2699
- if (!localCheckIn) {
2700
- return [2 /*return*/, idOrName];
2701
- }
2702
- if (localCheckIn.id) {
2703
- return [2 /*return*/, localCheckIn.id];
2704
- }
2705
- return [4 /*yield*/, this.__checkInsClient.listForProject(localCheckIn.projectId)];
2716
+ _a.trys.push([0, 2, , 3]);
2717
+ return [4 /*yield*/, this.__transport
2718
+ .send({
2719
+ method: 'GET',
2720
+ endpoint: endpoint(this.config.endpoint, "v1/check_in/".concat(id)),
2721
+ logger: this.logger,
2722
+ })];
2706
2723
  case 1:
2707
- projectCheckIns = _a.sent();
2708
- remoteCheckIn = projectCheckIns.find(function (c) { return c.name === localCheckIn.name; });
2709
- if (!remoteCheckIn) {
2710
- this.logger.debug("Checkin[".concat(idOrName, "] was not found on HB. This should not happen. Was the sync command executed?"));
2711
- return [2 /*return*/, idOrName];
2712
- }
2713
- // store the id in-memory, so subsequent check-ins won't have to call the API
2714
- localCheckIn.id = remoteCheckIn.id;
2715
- return [2 /*return*/, localCheckIn.id];
2724
+ _a.sent();
2725
+ this.logger.info("Check-In with id[".concat(id, "] sent"));
2726
+ return [3 /*break*/, 3];
2727
+ case 2:
2728
+ err_2 = _a.sent();
2729
+ this.logger.error("Check-In with id[".concat(id, "] failed: an unknown error occurred."), "message=".concat(err_2.message));
2730
+ return [3 /*break*/, 3];
2731
+ case 3: return [2 /*return*/];
2716
2732
  }
2717
2733
  });
2718
2734
  });
2719
2735
  };
2736
+ Honeybadger.prototype.isCheckInSlug = function (idOrSlug) {
2737
+ var _a, _b;
2738
+ return (_b = (_a = this.config.checkins) === null || _a === void 0 ? void 0 : _a.some(function (c) { return c.slug === idOrSlug; })) !== null && _b !== void 0 ? _b : false;
2739
+ };
2720
2740
  // This method is intended for web frameworks.
2721
2741
  // It allows us to track context for individual requests without leaking to other requests
2722
2742
  // by doing two things:
@@ -2749,7 +2769,7 @@ client.CheckInsClient = CheckInsClient;
2749
2769
  var NOTIFIER = {
2750
2770
  name: '@honeybadger-io/js',
2751
2771
  url: 'https://github.com/honeybadger-io/honeybadger-js/tree/master/packages/js',
2752
- version: '6.7.1'
2772
+ version: '6.8.0'
2753
2773
  };
2754
2774
  var userAgent = function () {
2755
2775
  return "Honeybadger JS Server Client ".concat(NOTIFIER.version, ", ").concat(os_1.default.version(), "; ").concat(os_1.default.platform());