@theholocron/holocron-plugin-checkly 5.0.0-alpha.212 → 5.0.0-alpha.213

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.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthError, Monitoring, MonitoringCheck, MonitoringCheckInput, ResolveTokenInput, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
1
+ import { AuthError, Monitoring, MonitoringAlertChannelInput, MonitoringAlertChannelResult, MonitoringCheck, MonitoringCheckInput, ResolveTokenInput, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
2
2
  import { ApiCheck, ApiCheckInput, ApiCheckRequest, Assertion, AssertionComparison, AssertionSource, ChecklyClient, ChecklyClientOptions, HttpMethod, ListChecksParams, createChecklyClient } from "@theholocron/checkly-client";
3
3
  //#region src/auth.d.ts
4
4
  declare const resolveToken: (input?: ResolveTokenInput) => string;
@@ -20,6 +20,15 @@ declare class ChecklyMonitoring implements Monitoring {
20
20
  check: MonitoringCheck;
21
21
  alreadyExists: boolean;
22
22
  }>;
23
+ /**
24
+ * A Discord alert channel: a `WEBHOOK` Checkly formats for Discord. Found by name, so one made by hand is adopted and
25
+ * kept up to date, not duplicated. Every check on the account is subscribed, since a check made through the API is
26
+ * never subscribed on its own; subscriptions already there (including ones to checks outside Holocron) are kept.
27
+ * An existing channel keeps its own message template and send settings.
28
+ */
29
+ ensureAlertChannel(input: MonitoringAlertChannelInput): Promise<MonitoringAlertChannelResult>;
30
+ /** The id of every check on the account, read a page at a time. */
31
+ private allCheckIds;
23
32
  /** Checkly's `search` param is a case-insensitive partial match — narrow to the exact name ourselves. */
24
33
  private findByExactName;
25
34
  }
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AuthError, createResolveToken, createRestClient } from "@theholocron/cli";
2
- import { createChecklyClient } from "@theholocron/checkly-client";
2
+ import { DISCORD_TEMPLATE, createChecklyClient } from "@theholocron/checkly-client";
3
3
  import { errorMessage } from "@theholocron/misc-utils";
4
4
  //#region src/auth.ts
5
5
  const resolveToken = createResolveToken({
@@ -83,6 +83,101 @@ var ChecklyMonitoring = class {
83
83
  alreadyExists: false
84
84
  };
85
85
  }
86
+ /**
87
+ * A Discord alert channel: a `WEBHOOK` Checkly formats for Discord. Found by name, so one made by hand is adopted and
88
+ * kept up to date, not duplicated. Every check on the account is subscribed, since a check made through the API is
89
+ * never subscribed on its own; subscriptions already there (including ones to checks outside Holocron) are kept.
90
+ * An existing channel keeps its own message template and send settings.
91
+ */
92
+ async ensureAlertChannel(input) {
93
+ const client = this.client();
94
+ const [channels, checkIds] = await Promise.all([client.alertChannels.list(), this.allCheckIds()]);
95
+ const existing = channels.find((c) => c.type === "WEBHOOK" && c.config.name === input.name);
96
+ if (!existing) {
97
+ if (!input.url) throw new Error(`cannot create alert channel "${input.name}": its webhook already exists and Discord shows a webhook URL only once, so delete that webhook and run setup again`);
98
+ await client.alertChannels.create({
99
+ type: "WEBHOOK",
100
+ config: {
101
+ name: input.name,
102
+ url: input.url,
103
+ method: "POST",
104
+ headers: [],
105
+ queryParameters: [],
106
+ template: DISCORD_TEMPLATE,
107
+ webhookType: "WEBHOOK_DISCORD"
108
+ },
109
+ sendFailure: true,
110
+ sendRecovery: true,
111
+ sendDegraded: true,
112
+ autoSubscribe: false,
113
+ subscriptions: checkIds.map((checkId) => ({
114
+ checkId,
115
+ activated: true
116
+ }))
117
+ });
118
+ return {
119
+ created: true,
120
+ updated: [],
121
+ subscribed: checkIds.length
122
+ };
123
+ }
124
+ const known = new Set(existing.subscriptions.flatMap((s) => s.checkId ? [s.checkId] : []));
125
+ const active = new Set(existing.subscriptions.flatMap((s) => s.checkId && s.activated ? [s.checkId] : []));
126
+ const missing = checkIds.filter((id) => !known.has(id));
127
+ const urlChanged = input.url !== void 0 && existing.config.url !== input.url;
128
+ const formatChanged = existing.config.webhookType !== "WEBHOOK_DISCORD";
129
+ const updated = [
130
+ ...urlChanged ? ["url"] : [],
131
+ ...formatChanged ? ["format"] : [],
132
+ ...missing.length > 0 ? ["subscriptions"] : []
133
+ ];
134
+ const total = active.size + missing.length;
135
+ if (updated.length === 0) return {
136
+ created: false,
137
+ updated,
138
+ subscribed: total
139
+ };
140
+ const replacement = {
141
+ type: "WEBHOOK",
142
+ config: {
143
+ ...existing.config,
144
+ ...urlChanged ? { url: input.url } : {},
145
+ ...formatChanged ? {
146
+ webhookType: "WEBHOOK_DISCORD",
147
+ template: DISCORD_TEMPLATE
148
+ } : {}
149
+ },
150
+ sendRecovery: existing.sendRecovery,
151
+ sendFailure: existing.sendFailure,
152
+ sendDegraded: existing.sendDegraded,
153
+ sslExpiry: existing.sslExpiry,
154
+ sslExpiryThreshold: existing.sslExpiryThreshold,
155
+ autoSubscribe: existing.autoSubscribe,
156
+ subscriptions: [...existing.subscriptions, ...missing.map((checkId) => ({
157
+ checkId,
158
+ activated: true
159
+ }))]
160
+ };
161
+ await client.alertChannels.update(existing.id, replacement);
162
+ return {
163
+ created: false,
164
+ updated,
165
+ subscribed: total
166
+ };
167
+ }
168
+ /** The id of every check on the account, read a page at a time. */
169
+ async allCheckIds() {
170
+ const PAGE = 100;
171
+ const ids = [];
172
+ for (let page = 1;; page++) {
173
+ const batch = await this.client().checks.list({
174
+ limit: PAGE,
175
+ page
176
+ });
177
+ ids.push(...batch.map((c) => c.id));
178
+ if (batch.length < PAGE) return ids;
179
+ }
180
+ }
86
181
  /** Checkly's `search` param is a case-insensitive partial match — narrow to the exact name ourselves. */
87
182
  async findByExactName(name) {
88
183
  return (await this.client().checks.list({ search: name })).find((c) => c.name === name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-checkly",
3
- "version": "5.0.0-alpha.212",
3
+ "version": "5.0.0-alpha.213",
4
4
  "description": "Holocron plugin for Checkly. Implements the monitoring capability against Checkly's REST API, plus exports verifyToken + AUTH_HINT for `holocron auth`.",
5
5
  "keywords": [
6
6
  "checkly",
@@ -30,7 +30,7 @@
30
30
  "dist"
31
31
  ],
32
32
  "devDependencies": {
33
- "@theholocron/checkly-client": "^1.43.1",
33
+ "@theholocron/checkly-client": "^1.52.0",
34
34
  "@theholocron/http-client": "^1.43.1",
35
35
  "@theholocron/misc-utils": "^1.16.0",
36
36
  "@theholocron/tsconfig": "^8.8.2",
@@ -42,12 +42,12 @@
42
42
  "tsx": "4.23.15",
43
43
  "typescript": "^5.9.3",
44
44
  "vitest": "^4.1.11",
45
- "@theholocron/cli": "5.0.0-alpha.212"
45
+ "@theholocron/cli": "5.0.0-alpha.213"
46
46
  },
47
47
  "peerDependencies": {
48
- "@theholocron/checkly-client": "^1.43.1",
48
+ "@theholocron/checkly-client": "^1.52.0",
49
49
  "@theholocron/misc-utils": "^1.16.0",
50
- "@theholocron/cli": "5.0.0-alpha.212"
50
+ "@theholocron/cli": "5.0.0-alpha.213"
51
51
  },
52
52
  "engines": {
53
53
  "node": ">=22"