-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webhook): Add allowed methods to webhooks
- Loading branch information
Showing
12 changed files
with
256 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"ha-webhook": { | ||
"label": { | ||
"allowed_methods": "Allowed Methods", | ||
"put": "PUT", | ||
"post": "POST", | ||
"get": "GET", | ||
"head": "HEAD" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import OutputController, { | ||
OutputControllerOptions, | ||
} from '../../common/controllers/OutputController'; | ||
import { IntegrationEvent } from '../../common/integration/Integration'; | ||
import { NodeMessage } from '../../types/nodes'; | ||
import { WebhookNode } from '.'; | ||
|
||
interface WebhookResponse { | ||
payload: any; | ||
headers: Record<string, any>; | ||
params: Record<string, any>; | ||
} | ||
|
||
type WebhookNodeOptions = OutputControllerOptions<WebhookNode>; | ||
|
||
export default class WebhookController extends OutputController<WebhookNode> { | ||
constructor(props: WebhookNodeOptions) { | ||
super(props); | ||
|
||
this.node.addListener( | ||
IntegrationEvent.Trigger, | ||
this.#onReceivedMessage.bind(this) | ||
); | ||
} | ||
|
||
#onReceivedMessage(data: WebhookResponse) { | ||
const message: NodeMessage = {}; | ||
try { | ||
this.setCustomOutputs(this.node.config.outputProperties, message, { | ||
config: this.node.config, | ||
data: data.payload, | ||
headers: data.headers, | ||
params: data.params, | ||
}); | ||
} catch (e) { | ||
this.node.error(e); | ||
this.status.setFailed('error'); | ||
return; | ||
} | ||
|
||
this.status.setSuccess('home-assistant.status.received'); | ||
this.node.send(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import BidirectionalIntegration, { | ||
DiscoveryBaseMessage, | ||
} from '../../common/integration/BidrectionalIntegration'; | ||
import { MessageType } from '../../common/integration/Integration'; | ||
import { WebhookNodeProperties } from '.'; | ||
|
||
export interface WebhookDiscoveryPayload extends DiscoveryBaseMessage { | ||
type: MessageType.Webhook; | ||
server_id: string; | ||
webhook_id: string; | ||
name: string; | ||
allowed_methods: string[]; | ||
} | ||
|
||
export default class WebhookIntegration extends BidirectionalIntegration { | ||
protected getDiscoveryPayload( | ||
config: WebhookNodeProperties | ||
): WebhookDiscoveryPayload { | ||
const methods = [ | ||
'method_post', | ||
'method_get', | ||
'method_put', | ||
'method_head', | ||
] as const; | ||
|
||
const allowedMethods = methods.reduce((acc, method) => { | ||
if (config[method]) { | ||
acc.push(method.replace('method_', '').toUpperCase()); | ||
} | ||
return acc; | ||
}, [] as string[]); | ||
|
||
return { | ||
type: MessageType.Webhook, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
server_id: config.server!, | ||
webhook_id: config.webhookId, | ||
name: config.id, | ||
allowed_methods: allowedMethods, | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.