matterbridge-webhooks 0.0.2 → 0.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.
package/CHANGELOG.md CHANGED
@@ -8,6 +8,22 @@ If you like this project and find it useful, please consider giving it a star on
8
8
  <img src="bmc-button.svg" alt="Buy me a coffee" width="120">
9
9
  </a>
10
10
 
11
+ ## [0.0.3] - 2025-05-01
12
+
13
+ ### Added
14
+
15
+ - [readme]: Added the possibility to test the hook before confirming the changes in the config editor.
16
+
17
+ ### Changed
18
+
19
+ - [package]: Require matterbridge 3.0.0.
20
+ - [package]: Updated package.
21
+ - [package]: Updated dependencies.
22
+
23
+ <a href="https://www.buymeacoffee.com/luligugithub">
24
+ <img src="bmc-button.svg" alt="Buy me a coffee" width="80">
25
+ </a>
26
+
11
27
  ## [0.0.2] - 2025-03-30
12
28
 
13
29
  ### Added
package/dist/fetch.js CHANGED
@@ -44,7 +44,7 @@ export async function fetch(url, method = 'GET', data = {}, timeout = 5000) {
44
44
  clearTimeout(timeoutId);
45
45
  res.resume();
46
46
  req.destroy();
47
- return reject(new Error(`Request failed with status code: ${res.statusCode}`));
47
+ reject(new Error(`Request failed with status code: ${res.statusCode}`));
48
48
  }
49
49
  res.on('data', (chunk) => {
50
50
  responseData += chunk;
package/dist/platform.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import { bridgedNode, MatterbridgeDynamicPlatform, MatterbridgeEndpoint, onOffLight, onOffOutlet, onOffSwitch } from 'matterbridge';
2
+ import { isValidObject } from 'matterbridge/utils';
2
3
  import { fetch } from './fetch.js';
3
4
  export class Platform extends MatterbridgeDynamicPlatform {
4
5
  webhooks;
5
6
  bridgedDevices = new Map();
6
7
  constructor(matterbridge, log, config) {
7
8
  super(matterbridge, log, config);
8
- if (this.verifyMatterbridgeVersion === undefined || typeof this.verifyMatterbridgeVersion !== 'function' || !this.verifyMatterbridgeVersion('2.2.5')) {
9
- throw new Error(`This plugin requires Matterbridge version >= "2.2.5". Please update Matterbridge to the latest version in the frontend.`);
9
+ if (this.verifyMatterbridgeVersion === undefined || typeof this.verifyMatterbridgeVersion !== 'function' || !this.verifyMatterbridgeVersion('3.0.0')) {
10
+ throw new Error(`This plugin requires Matterbridge version >= "3.0.0". Please update Matterbridge to the latest version in the frontend.`);
10
11
  }
11
12
  this.log.info('Initializing platform:', this.config.name);
12
13
  this.webhooks = this.config.webhooks;
@@ -50,9 +51,33 @@ export class Platform extends MatterbridgeDynamicPlatform {
50
51
  await device.setAttribute('onOff', 'onOff', false, device.log);
51
52
  });
52
53
  }
53
- async onAction(action, value, id) {
54
+ async onAction(action, value, id, formData) {
54
55
  this.log.info('onAction called with action:', action, 'and value:', value ?? 'none', 'and id:', id ?? 'none');
56
+ this.log.debug('onAction called with formData:', formData ?? 'none');
57
+ if (id?.startsWith('root_webhooks_'))
58
+ id = id.replace('root_webhooks_', '');
59
+ if (id?.endsWith('_test'))
60
+ id = id.replace('_test', '');
55
61
  if (action === 'test') {
62
+ if (isValidObject(formData, 1) && isValidObject(formData.webhooks, 1)) {
63
+ const webhooks = formData.webhooks;
64
+ for (const webhookName in webhooks) {
65
+ if (Object.prototype.hasOwnProperty.call(webhooks, webhookName)) {
66
+ const webhook = webhooks[webhookName];
67
+ if (id?.includes(webhookName)) {
68
+ this.log.info(`Testing new webhook ${webhookName} method ${webhook.method} url ${webhook.httpUrl}`);
69
+ fetch(webhook.httpUrl, webhook.method)
70
+ .then(() => {
71
+ this.log.notice(`Webhook test ${webhookName} successful!`);
72
+ })
73
+ .catch((err) => {
74
+ this.log.error(`Webhook test ${webhookName} failed: ${err instanceof Error ? err.message : err}`);
75
+ });
76
+ }
77
+ }
78
+ }
79
+ return;
80
+ }
56
81
  for (const webhookName in this.webhooks) {
57
82
  if (Object.prototype.hasOwnProperty.call(this.webhooks, webhookName)) {
58
83
  const webhook = this.webhooks[webhookName];
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "matterbridge-webhooks",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "matterbridge-webhooks",
9
- "version": "0.0.2",
9
+ "version": "0.0.3",
10
10
  "license": "Apache-2.0",
11
11
  "dependencies": {
12
12
  "node-ansi-logger": "^3.0.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "matterbridge-webhooks",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Matterbridge webhooks plugin",
5
5
  "author": "https://github.com/Luligu",
6
6
  "license": "Apache-2.0",
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ // enable isolatedModules just for ts-jest
5
+ "isolatedModules": true
6
+ },
7
+ "include": ["**/*.spec.ts", "**/*.test.ts", "**/__test__/*"]
8
+ }