betterstackmanager 0.2.0 → 0.2.1

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.
@@ -0,0 +1,75 @@
1
+ # Monitor Groups
2
+
3
+ Class: `client.monitorGroups`
4
+
5
+ Methods: `create`, `edit`, `get`, `listMonitors`, `remove`.
6
+
7
+ ## `create`
8
+
9
+ `create(monitorGroup)`
10
+
11
+ Create a new monitor group.
12
+
13
+ | Value | Type | Required | About |
14
+ |-|-|-|-|
15
+ | `monitorGroup.team_name` | String | true | The team name that will own this group. |
16
+ | `monitorGroup.paused` | Boolean | false | Pause monitoring for all monitors in this group. |
17
+ | `monitorGroup.name` | String | false | The name of this group. |
18
+ | `monitorGroup.sort_index` | Number | false | How to sort this group. |
19
+
20
+ ## `edit`
21
+
22
+ `edit(id, monitorGroup)`
23
+
24
+ Edit an existing monitor group.
25
+
26
+ | Value | Type | Required | About |
27
+ |-|-|-|-|
28
+ | `id` | Number | true | The monitor group ID. |
29
+ | `monitorGroup.team_name` | String | true | The team name that will own this group. |
30
+ | `monitorGroup.paused` | Boolean | false | Pause monitoring for all monitors in this group. |
31
+ | `monitorGroup.name` | String | false | The name of this group. |
32
+ | `monitorGroup.sort_index` | Number | false | How to sort this group. |
33
+
34
+ ## `get`
35
+
36
+ `get(monitorGroupId)`
37
+
38
+ Get a single monitor group.
39
+
40
+ | Value | Type | Required | About |
41
+ |-|-|-|-|
42
+ | `monitorGroupId` | Number | true | Specify the ID of the monitor group. |
43
+
44
+ This can return:
45
+ - If successful: HTTP 200 and the Monitor Group object containing the data.
46
+
47
+ ## `listMonitors`
48
+
49
+ `listMonitors(monitorGroupId)`
50
+
51
+ Show monitors that belong to a group.
52
+
53
+ | Value | Type | Required | About |
54
+ |-|-|-|-|
55
+ | `monitorGroupId` | Number | true | Specify the ID of the monitor group. |
56
+ | `perPage` | Number | false | Amount of monitors per page. Default value is 50. |
57
+ | `page` | Number | false | Results page number. Default value is 1. |
58
+
59
+ > This method supports **pagination**!
60
+
61
+ This can return:
62
+ - If successful: HTTP 200 and a list of Monitors.
63
+
64
+ ## `remove`
65
+
66
+ `remove(monitorGroupId)`
67
+
68
+ Delete an existing monitor group.
69
+
70
+ | Value | Type | Required | About |
71
+ |-|-|-|-|
72
+ | `monitorGroupId` | Number | true | Specify the ID of the monitor group. |
73
+
74
+ This can return:
75
+ - If successful: HTTP 204 and an empty body.
@@ -1,5 +1,5 @@
1
1
  const BSM = require('betterstackmanager');
2
- const config = require('./config.json');
2
+ const config = require('../config.json');
3
3
 
4
4
  const client = new BSM.Client(config.token);
5
5
 
@@ -1,5 +1,5 @@
1
1
  const BSM = require('betterstackmanager');
2
- const config = require('./config.json');
2
+ const config = require('../config.json');
3
3
 
4
4
  const client = new BSM.Client(config.token);
5
5
 
@@ -1,5 +1,5 @@
1
1
  const BSM = require('betterstackmanager');
2
- const config = require('./config.json');
2
+ const config = require('../config.json');
3
3
 
4
4
  const client = new BSM.Client(config.token);
5
5
 
@@ -1,5 +1,5 @@
1
1
  const BSM = require('betterstackmanager');
2
- const config = require('./config.json');
2
+ const config = require('../config.json');
3
3
 
4
4
  const client = new BSM.Client(config.token);
5
5
 
@@ -1,5 +1,5 @@
1
1
  const BSM = require('betterstackmanager');
2
- const config = require('./config.json');
2
+ const config = require('../config.json');
3
3
 
4
4
  const client = new BSM.Client(config.token);
5
5
 
@@ -10,25 +10,9 @@ You'll need to have, at least,
10
10
  - your **global API** key,
11
11
  - a mail address.
12
12
 
13
- ## Following examples
13
+ ## Examples
14
14
 
15
- All examples show how to get your BSM client up and running.
16
-
17
- ## acknowledgeIncident.js
18
-
19
- This short example acknowledges an incident with the given ID.
20
-
21
- ## createIncident.js
22
-
23
- This short example creates an incident with the given name, summary and explanation.
24
-
25
- ## createIncidentComment.js
26
-
27
- This example appends a new comment to an existing incident.
28
-
29
- ## resolveIncident.js
30
-
31
- This example resolves an existing incident with the given ID.
15
+ You'll find examples in the subfolders. They're grouped by the topic they cover.
32
16
 
33
17
  ## Links
34
18
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "betterstackmanager",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Interact with Better Stack in Node.JS",
5
5
  "license": "GPL-3.0-only",
6
6
  "author": "Cyanic76",
@@ -12,6 +12,6 @@
12
12
  "lib": "src"
13
13
  },
14
14
  "dependencies": {
15
- "axios": "^1.13.2"
15
+ "axios": "^1.13.4"
16
16
  }
17
17
  }
@@ -1,8 +1,9 @@
1
1
  const axios = require('axios');
2
2
 
3
- const Incidents = require('../etc/Incident');
4
- const IncidentComments = require('../etc/IncidentComment');
5
- const Monitors = require('../etc/Monitor');
3
+ const Incidents = require('../etc/Incident'),
4
+ IncidentComments = require('../etc/IncidentComment'),
5
+ Monitors = require('../etc/Monitor'),
6
+ MonitorGroup = require('../etc/MonitorGroup');
6
7
 
7
8
  const { handleError } = require('../errors/Error');
8
9
 
@@ -19,12 +20,14 @@ class Client {
19
20
  }
20
21
  });
21
22
 
22
- // Get the right classes as found in the folder.
23
+ // Get the right classes as found in the /src/etc folder.
23
24
  this.incidents = new Incidents(this);
24
25
  this.incidentComments = new IncidentComments(this);
25
26
  this.monitors = new Monitors(this);
27
+ this.monitorGroups = new MonitorGroup(this);
26
28
  }
27
29
 
30
+ // Handle HTTP requests to the upstream API.
28
31
  async delete (endpoint) {
29
32
  try {
30
33
  const response = await this.client.delete(endpoint);
@@ -2,8 +2,7 @@ const util = require('../client/utils');
2
2
 
3
3
  class Incidents {
4
4
  /**
5
- * @param {string} token
6
- * @param {string} baseURL
5
+ * @param {Client} client
7
6
  */
8
7
  constructor(client) {
9
8
  this.client = client;
@@ -21,7 +20,7 @@ class Incidents {
21
20
  if(!incidentData.name) throw new Error("Incident creation: name is a required incident option.");
22
21
  if(!incidentData.summary) throw new Error("Incident creation: summary is a required incident option.");
23
22
  if(!incidentData.description) throw new Error("Incident creation: description is a required incident option.");
24
- if(!incidentData.email || typeof(incidentData.email) != 'boolean') throw new Error("Incident creation: email is a required boolean incident option.");
23
+ if(incidentData.email && typeof(incidentData.email) != 'boolean') throw new Error("Incident creation: email is a required boolean incident option.");
25
24
 
26
25
  // Modify a copy of the provided object.
27
26
  const data = { ...incidentData };
@@ -43,8 +42,8 @@ class Incidents {
43
42
  */
44
43
 
45
44
  async acknowledge(incidentId) {
46
- if(!incidentId) throw new Error("Incident acknowledgement: incidentId is a required incident option.");
47
- if(isNaN(incidentId)) throw new Error("Incident acknowledgement: incidentId is not a Number.");
45
+ if(!incidentId || incidentId && isNaN(incidentId)) throw new Error("Incident acknowledgement: incidentId must be a Number.");
46
+ if(incidentId < 1) throw new Error("Incident acknowledgement: incidentId must be greater than 0.");
48
47
 
49
48
  try {
50
49
  return await this.client.post(`/v3/incidents/${incidentId}/acknowledge`);
@@ -60,8 +59,8 @@ class Incidents {
60
59
  */
61
60
 
62
61
  async get(incidentId) {
63
- if(!incidentId) throw new Error("Incident fetching: incidentId is a required incident option.");
64
- if(isNaN(incidentId)) throw new Error("Incident acknowledgement: incidentId is not a Number.");
62
+ if(!incidentId || incidentId && isNaN(incidentId)) throw new Error("Incident: incidentId must be a Number.");
63
+ if(incidentId < 1) throw new Error("Incident: incidentId must be greater than 0.");
65
64
 
66
65
  try {
67
66
  return await this.client.get(`/v3/incidents/${incidentId}`);
@@ -123,8 +122,7 @@ class Incidents {
123
122
  */
124
123
 
125
124
  async resolve(incidentId) {
126
- if(!incidentId) throw new Error("Incident resolution: incidentId is a required incident option.");
127
- if(isNaN(incidentId)) throw new Error("Incident acknowledgement: incidentId is not a Number.");
125
+ if(!incidentId || incidentId && isNaN(incidentId) || incidentId && parseInt(incidentId) < 1) throw new Error("Incident resolution: incidentId must be a positive Number.");
128
126
 
129
127
  try {
130
128
  return await this.client.post(`/v3/incidents/${incidentId}/resolve`);
@@ -143,10 +141,10 @@ class Incidents {
143
141
  async escalate(incidentId, escalationData){
144
142
 
145
143
  // Before trying anything, check some things right away.
146
- if(!incidentId || !escalationData) throw new Error("Incident resolution: incidentId and escalationData are required incident options.");
147
- if(isNaN(incidentId)) throw new Error("Incident acknowledgement: incidentId is not a Number.");
144
+ if(!escalationData) throw new Error("Incident escalation: escalationData is required.");
145
+ if(!incidentId || incidentId && isNaN(incidentId) || incidentId && parseInt(incidentId) < 1) throw new Error("Incident escalation: incidentId must be a positive Number.");
148
146
  const allowedTypes = ["User", "Team", "Schedule", "Policy", "Organization"];
149
- if(!allowedTypes.includes(escalationData.type)) throw new Error("Incident resolution: Escalation Type is invalid.");
147
+ if(!allowedTypes.includes(escalationData.type)) throw new Error("Incident escalation: Escalation Type is invalid.");
150
148
 
151
149
  switch(escalationData.type){
152
150
  case 'User':
@@ -178,8 +176,7 @@ class Incidents {
178
176
  */
179
177
  async remove(incidentId){
180
178
 
181
- if(!incidentId) throw new Error("Incident deletion: incidentId is a required incident option.");
182
- if(isNaN(incidentId)) throw new Error("Incident acknowledgement: incidentId is not a Number.");
179
+ if(!incidentId || incidentId && isNaN(incidentId) || incidentId && parseInt(incidentId) < 1) throw new Error("Incident deletion: incidentId must be a positive Number.");
183
180
 
184
181
  try {
185
182
  return await this.client.delete(`/v3/incidents/${incidentId}`);
@@ -1,7 +1,6 @@
1
1
  class IncidentComments {
2
2
  /**
3
- * @param {string} token
4
- * @param {string} baseURL
3
+ * @param {Client} client
5
4
  */
6
5
  constructor(client) {
7
6
  this.client = client;
@@ -54,8 +53,8 @@ class IncidentComments {
54
53
  */
55
54
  async getAll(incidentId) {
56
55
 
57
- if(!incidentId) throw new Error("Incident comment fetch: incidentId is a required options.");
58
- if(isNaN(incidentId) || incidentId < 1) throw new Error("Incident comment fetch: incidentId must be a Number greater than 0.");
56
+ if(!incidentId || incidentId && isNaN(incidentId)) throw new Error("Incident comment fetch: incidentId is a required option.");
57
+ if(incidentId < 1) throw new Error("Incident comment fetch: incidentId must be a positive Number.");
59
58
 
60
59
  try {
61
60
  return await this.client.get(`/v2/incidents/${incidentId}/comments`);
@@ -68,7 +67,7 @@ class IncidentComments {
68
67
  async remove(incidentId, incidentCommentId) {
69
68
 
70
69
  if(!incidentId || !incidentCommentId || incidentId && isNaN(incidentId) || incidentCommentId && isNaN(incidentCommentId)) throw new Error("Incident comment removal: incidentId and incidentCommentId must be Numbers.");
71
- if(incidentId < 1 || incidentCommentId < 1) throw new Error("Incident comment removal: incidentId and incidentCommentId must be greater than 0.");
70
+ if(incidentId < 1 || incidentCommentId < 1) throw new Error("Incident comment removal: incidentId and incidentCommentId must be a positive Number.");
72
71
 
73
72
  try {
74
73
  return await this.client.delete(`/v2/incidents/${incidentId}/comments/${incidentCommentId}`);
@@ -87,9 +86,9 @@ class IncidentComments {
87
86
  */
88
87
  async update(incidentId, incidentCommentId, incidentCommentText) {
89
88
 
90
- if(!incidentId || !incidentCommentId || !incidentCommentText ) throw new Error("Incident comment update: incidentId, incidentCommentId, incidentCommentText are required options.");
91
- if(isNaN(incidentId) || isNaN(incidentCommentId)) throw new Error("Incident comment update: incidentId and incidentCommentId must both be Numbers.");
92
- if(incidentId < 1 || incidentCommentId < 1) throw new Error("Incident comment update: incidentId and incidentCommentId must be greater than 0.");
89
+ if(!incidentId || !incidentCommentId || !incidentCommentText) throw new Error("Incident comment update: incidentId, incidentCommentId, incidentCommentText are required options.");
90
+ if(isNaN(incidentId) || isNaN(incidentCommentId)) throw new Error("Incident comment update: incidentId and incidentCommentId must be Numbers.");
91
+ if(incidentId < 1 || incidentCommentId < 1) throw new Error("Incident comment update: incidentId and incidentCommentId must be a positive Number.");
93
92
 
94
93
  try {
95
94
  return await this.client.patch(`/v2/incidents/${incidentId}/comments/${incidentCommentId}`, {
@@ -2,8 +2,7 @@ const util = require('../client/utils');
2
2
 
3
3
  class Monitor {
4
4
  /**
5
- * @param {string} token
6
- * @param {string} baseURL
5
+ * @param {Client} client
7
6
  */
8
7
  constructor(client) {
9
8
  this.client = client;
@@ -0,0 +1,122 @@
1
+ class MonitorGroup {
2
+ /**
3
+ * @param {Client} client
4
+ */
5
+ constructor(client) {
6
+ this.client = client;
7
+ };
8
+
9
+ /**
10
+ * Create a new monitor group.
11
+ * @param {Object} monitorGroup
12
+ * @returns
13
+ */
14
+ async create(monitorGroup) {
15
+
16
+ if(!monitorGroup) throw new Error("Montior Group: Must provide a Monitor Group object.");
17
+ if(!monitorGroup.team_name) throw new Error("Monitor Group: Team Name must be provided.");
18
+ if(monitorGroup.paused && typeof(monitorGroup.paused) != "Boolean") throw new Error("Monitor Group: paused must be either true or false.");
19
+ if(monitorGroup.sort_index && isNaN(monitorGroup.sort_index) || monitorGroup.sort_index && parseInt(monitorGroup.sort_index) < 1) throw new Error("Monitor Group: sort_index must be a positive number.");
20
+
21
+ try {
22
+ return await this.client.post(`/v2/monitor-groups/`, {
23
+ monitorGroup
24
+ });
25
+ } catch(error) {
26
+ throw error;
27
+ }
28
+
29
+ }
30
+
31
+ /**
32
+ * Edit an existing monitor group.
33
+ * @param {Number} id
34
+ * @param {Object} monitorGroup
35
+ * @returns
36
+ */
37
+ async edit(id, monitorGroup) {
38
+
39
+ if(!id || !monitorGroup) throw new Error("Montior Group: Must provide an ID and a Monitor Group object.");
40
+ if(parseInt(id) < 1 || isNaN(id)) throw new Error("Monitor Group: The ID must be a positive Number.");
41
+ if(monitorGroup.paused && typeof(monitorGroup.paused) != "Boolean") throw new Error("Monitor Group: paused must be either true or false.");
42
+ if(monitorGroup.sort_index && isNaN(monitorGroup.sort_index) || monitorGroup.sort_index && parseInt(monitorGroup.sort_index) < 1) throw new Error("Monitor Group: sort_index must be a positive number.");
43
+
44
+ try {
45
+ return await this.client.patch(`/v2/monitor-groups/`, {
46
+ monitorGroup
47
+ });
48
+ } catch(error) {
49
+ throw error;
50
+ }
51
+
52
+ }
53
+
54
+ /**
55
+ * Get an existing monitor group.
56
+ * @param {Number} monitorGroupId
57
+ * @returns
58
+ */
59
+ async get(monitorGroupId) {
60
+
61
+ if(!monitorGroupId || monitorGroupId && isNaN(monitorGroupId) || monitorGroupId && monitorGroupId < 1) throw new Error("Montior Group Deletion: ID must be a positive number.");
62
+
63
+ try {
64
+ return await this.client.get(`/v2/monitor-groups/${monitorGroupId}`);
65
+ } catch(error) {
66
+ throw error;
67
+ }
68
+
69
+ }
70
+
71
+ /**
72
+ * List monitors that belong to this group.
73
+ * @param {Number} monitorGroupId
74
+ * @param {Number} perPage
75
+ * @param {Number} page
76
+ * @returns
77
+ */
78
+ async listMonitors(monitorGroupId, perPage, page) {
79
+ if(!monitorGroupId || monitorGroupId && isNaN(monitorGroupId) || monitorGroupId && monitorGroupId < 1) throw new Error("Monitor Group ID must be a positive number.");
80
+
81
+ if(page && isNaN(page) || page && !isNaN(page) && page < 1) throw new Error("pageId must be a number greater than or equal to 0.");
82
+ if(perPage && isNaN(perPage) || perPage && perPage > 250 || perPage && perPage < 1) throw new Error("perPage must be a number between 1 and 250.");
83
+
84
+ const endpoint = `/v2/monitor-groups/${monitorGroupId}/monitors`;
85
+
86
+ // Build query from passed parameters
87
+ let parameters = {
88
+ "perPage": perPage ? perPage : 50,
89
+ "page": page ? page : 1
90
+ }
91
+ for (let key in parameters) {
92
+ let value = parameters[key];
93
+ if(value != null) endpoint += `${key}=${value}&`;
94
+ };
95
+
96
+ try {
97
+ return await this.client.get(endpoint);
98
+ } catch(error) {
99
+ throw error;
100
+ }
101
+
102
+ }
103
+
104
+ /**
105
+ * Delete an existing monitor group.
106
+ * @param {Number} monitorGroupId
107
+ * @returns
108
+ */
109
+ async remove(monitorGroupId) {
110
+
111
+ if(!monitorGroupId || monitorGroupId && isNaN(monitorGroupId) || monitorGroupId && monitorGroupId < 1) throw new Error("Montior Group Deletion: ID must be a positive number.");
112
+
113
+ try {
114
+ return await this.client.delete(`/v2/monitor-groups/${monitorGroupId}`);
115
+ } catch(error) {
116
+ throw error;
117
+ }
118
+
119
+ }
120
+ }
121
+
122
+ module.exports = { MonitorGroup };
package/src/index.js CHANGED
@@ -1,7 +1,8 @@
1
- const { Client } = require('./client');
2
- const { Incidents } = require('./etc/Incident');
3
- const { IncidentComments } = require('./etc/IncidentComment');
4
- const { Monitor } = require('./etc/Monitor');
1
+ const { Client } = require('./client'),
2
+ { Incidents } = require('./etc/Incident'),
3
+ { IncidentComments } = require('./etc/IncidentComment'),
4
+ { Monitor } = require('./etc/Monitor'),
5
+ { MonitorGroup } = require('./etc/MonitorGroup');
5
6
 
6
7
  module.exports = {
7
8
  Client,
@@ -9,5 +10,6 @@ module.exports = {
9
10
  Incidents,
10
11
  IncidentComments,
11
12
 
12
- Monitor
13
+ Monitor,
14
+ MonitorGroup
13
15
  };
package/src/readme.md ADDED
@@ -0,0 +1,21 @@
1
+ # BSM - Better Stack Manager
2
+
3
+ Manage your Better Stack incidents & more using the API with this library.
4
+
5
+ You're in the **source code**.
6
+
7
+ ## Directories
8
+
9
+ - `client/` - The main client code and other utilities.
10
+ - `errors/` - The error handler.
11
+ - `etc/` - Other classes as defined in the documentation.
12
+
13
+ ## Links
14
+
15
+ - [Documentation](https://codeberg.org/Cyanic76/BSM.js/src/branch/main/docs)
16
+ - [Source Code](https://codeberg.org/Cyanic76/BSM.js/)
17
+ - [Discord server](https://cyanic.me/discord)
18
+
19
+ Get the package on
20
+ [Codeberg](https://codeberg.org/Cyanic76/-/packages/npm/betterstackmanager/) or on
21
+ [NPM](https://www.npmjs.com/package/betterstackmanager).