From 86356424c8a5a1ba237db1d03d1772755cfde454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Wed, 22 Feb 2023 11:04:26 +0100 Subject: [PATCH] refactor(WhatsApp Node): Delete duplicate code --- .../nodes/WhatsApp/MediaFunctions.ts | 52 ++++++++++--------- .../nodes/WhatsApp/MessageFunctions.ts | 37 ++----------- 2 files changed, 33 insertions(+), 56 deletions(-) diff --git a/packages/nodes-base/nodes/WhatsApp/MediaFunctions.ts b/packages/nodes-base/nodes/WhatsApp/MediaFunctions.ts index c2f1624228048..9d8d43d19e5ab 100644 --- a/packages/nodes-base/nodes/WhatsApp/MediaFunctions.ts +++ b/packages/nodes-base/nodes/WhatsApp/MediaFunctions.ts @@ -3,38 +3,42 @@ import { NodeOperationError } from 'n8n-workflow'; import FormData from 'form-data'; -export async function setupUpload( +export async function getUploadFormData( this: IExecuteSingleFunctions, - requestOptions: IHttpRequestOptions, -) { - const mediaPropertyName = this.getNodeParameter('mediaPropertyName') as string; - if (!mediaPropertyName) { - return requestOptions; - } - if (this.getInputData().binary?.[mediaPropertyName] === undefined || !mediaPropertyName.trim()) { - throw new NodeOperationError(this.getNode(), 'Could not find file in node input data', { - description: `There’s no key called '${mediaPropertyName}' with binary data in it`, - }); - } - const binaryFile = this.getInputData().binary![mediaPropertyName]!; +): Promise<{ fileName: string; formData: FormData }> { + const mediaPropertyName = ((this.getNodeParameter('mediaPropertyName') as string) || '').trim(); + if (!mediaPropertyName) + throw new NodeOperationError(this.getNode(), 'Parameter "mediaPropertyName" is not defined'); + + const { binary: binaryData } = this.getInputData(); + if (!binaryData) throw new NodeOperationError(this.getNode(), 'Binary data missing in input'); + + const binaryFile = binaryData[mediaPropertyName]; + if (binaryFile === undefined) + throw new NodeOperationError(this.getNode(), 'Could not find file in node input data'); + const mediaFileName = (this.getNodeParameter('additionalFields') as IDataObject).mediaFileName as | string | undefined; - const binaryFileName = binaryFile.fileName; - if (!mediaFileName && !binaryFileName) { + + const fileName = mediaFileName || binaryFile.fileName; + if (!fileName) throw new NodeOperationError(this.getNode(), 'No file name given for media upload.'); - } - const mimeType = binaryFile.mimeType; const buffer = await this.helpers.getBinaryDataBuffer(mediaPropertyName); - const data = new FormData(); - data.append('file', buffer, { - contentType: mimeType, - filename: mediaFileName || binaryFileName, - }); - data.append('messaging_product', 'whatsapp'); + const formData = new FormData(); + formData.append('file', buffer, { contentType: binaryFile.mimeType, filename: fileName }); + formData.append('messaging_product', 'whatsapp'); - requestOptions.body = data; + return { fileName, formData }; +} + +export async function setupUpload( + this: IExecuteSingleFunctions, + requestOptions: IHttpRequestOptions, +) { + const uploadData = await getUploadFormData.call(this); + if (uploadData) requestOptions.body = uploadData.formData; return requestOptions; } diff --git a/packages/nodes-base/nodes/WhatsApp/MessageFunctions.ts b/packages/nodes-base/nodes/WhatsApp/MessageFunctions.ts index 09aafd989c394..ec0e217b546ea 100644 --- a/packages/nodes-base/nodes/WhatsApp/MessageFunctions.ts +++ b/packages/nodes-base/nodes/WhatsApp/MessageFunctions.ts @@ -1,5 +1,4 @@ import set from 'lodash.set'; -import { BinaryDataManager } from 'n8n-core'; import type { IDataObject, IExecuteSingleFunctions, @@ -8,9 +7,9 @@ import type { INodeExecutionData, JsonObject, } from 'n8n-workflow'; -import { NodeApiError, NodeOperationError } from 'n8n-workflow'; +import { NodeApiError } from 'n8n-workflow'; -import FormData from 'form-data'; +import { getUploadFormData } from './MediaFunctions'; interface WhatsAppApiError { error: { @@ -62,29 +61,7 @@ export async function mediaUploadFromItem( this: IExecuteSingleFunctions, requestOptions: IHttpRequestOptions, ) { - const mediaPropertyName = this.getNodeParameter('mediaPropertyName') as string; - if (this.getInputData().binary?.[mediaPropertyName] === undefined) { - throw new NodeOperationError( - this.getNode(), - `The binary property "${mediaPropertyName}" does not exist. So no file can be written!`, - ); - } - const binaryFile = this.getInputData().binary![mediaPropertyName]!; - const mediaFileName = (this.getNodeParameter('additionalFields') as IDataObject).mediaFilename as - | string - | undefined; - const binaryFileName = binaryFile.fileName; - if (!mediaFileName && !binaryFileName) { - throw new NodeOperationError(this.getNode(), 'No file name given for media upload.'); - } - const mimeType = binaryFile.mimeType; - - const data = new FormData(); - data.append('file', await BinaryDataManager.getInstance().retrieveBinaryData(binaryFile), { - contentType: mimeType, - filename: mediaFileName || binaryFileName, - }); - data.append('messaging_product', 'whatsapp'); + const uploadData = await getUploadFormData.call(this); const phoneNumberId = this.getNodeParameter('phoneNumberId') as string; @@ -92,7 +69,7 @@ export async function mediaUploadFromItem( url: `/${phoneNumberId}/media`, baseURL: requestOptions.baseURL, method: 'POST', - body: data, + body: uploadData.formData, })) as IDataObject; const operation = this.getNodeParameter('messageType') as string; @@ -101,11 +78,7 @@ export async function mediaUploadFromItem( } set(requestOptions.body as IDataObject, `${operation}.id`, result.id); if (operation === 'document') { - set( - requestOptions.body as IDataObject, - `${operation}.filename`, - mediaFileName || binaryFileName, - ); + set(requestOptions.body as IDataObject, `${operation}.filename`, uploadData.fileName); } return requestOptions;