1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-11-11 12:26:30 +03:00

Reuse webhooks.

This commit is contained in:
Ricardo Espinoza 2020-02-28 10:06:05 -05:00
parent 44f5d4b528
commit 50ecb8d0a9

View File

@ -17,6 +17,7 @@ import {
ITypeformAnswerField, ITypeformAnswerField,
ITypeformDefinition, ITypeformDefinition,
} from './GenericFunctions'; } from './GenericFunctions';
import { Webhook } from '../Webhook.node';
export class TypeformTrigger implements INodeType { export class TypeformTrigger implements INodeType {
@ -89,24 +90,23 @@ export class TypeformTrigger implements INodeType {
default: { default: {
async checkExists(this: IHookFunctions): Promise<boolean> { async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
if (webhookData.webhookId === undefined) {
// No webhook id is set so no webhook can exist
return false;
}
const formId = this.getNodeParameter('formId') as string; const formId = this.getNodeParameter('formId') as string;
const endpoint = `forms/${formId}/webhooks/${webhookData.webhookId}`; const endpoint = `forms/${formId}/webhooks`;
try { const { items } = await apiRequest.call(this, 'GET', endpoint, {});
const body = {};
await apiRequest.call(this, 'POST', endpoint, body); for (const item of items) {
} catch (e) { if (item.form_id === formId
return false; && item.url === webhookUrl) {
webhookData.webhookId = item.tag;
return true;
}
} }
return true; return false;
}, },
async create(this: IHookFunctions): Promise<boolean> { async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default'); const webhookUrl = this.getNodeWebhookUrl('default');
@ -138,14 +138,12 @@ export class TypeformTrigger implements INodeType {
if (webhookData.webhookId !== undefined) { if (webhookData.webhookId !== undefined) {
const endpoint = `forms/${formId}/webhooks/${webhookData.webhookId}`; const endpoint = `forms/${formId}/webhooks/${webhookData.webhookId}`;
try { try {
const body = {}; const body = {};
await apiRequest.call(this, 'DELETE', endpoint, body); await apiRequest.call(this, 'DELETE', endpoint, body);
} catch (e) { } catch (e) {
return false; return false;
} }
// Remove from the static workflow data so that it is clear // Remove from the static workflow data so that it is clear
// that no webhooks are registred anymore // that no webhooks are registred anymore
delete webhookData.webhookId; delete webhookData.webhookId;