From 7daf686eee316e5fee28082d1486535dce68a048 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:15:53 -0400 Subject: [PATCH 1/4] Fix formdata import for ESM --- src/apiClient.ts | 2 +- src/resources/messages.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/apiClient.ts b/src/apiClient.ts index c1a75002..408c14d1 100644 --- a/src/apiClient.ts +++ b/src/apiClient.ts @@ -7,7 +7,7 @@ import { } from './models/error.js'; import { objKeysToCamelCase, objKeysToSnakeCase } from './utils.js'; import { SDK_VERSION } from './version.js'; -import * as FormData from 'form-data'; +import FormData from 'form-data'; import { snakeCase } from 'change-case'; /** diff --git a/src/resources/messages.ts b/src/resources/messages.ts index d590ffa2..8eabe135 100644 --- a/src/resources/messages.ts +++ b/src/resources/messages.ts @@ -21,7 +21,7 @@ import { SendMessageRequest, UpdateDraftRequest, } from '../models/drafts.js'; -import * as FormData from 'form-data'; +import FormData from 'form-data'; import { encodeAttachmentStreams, objKeysToSnakeCase } from '../utils.js'; import { SmartCompose } from './smartCompose.js'; import APIClient, { RequestOptionsParams } from '../apiClient.js'; From aebf218cec044e52428f05fdf88a7f7ae8f2e2dd Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:16:37 -0400 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b635b01..fca1bf53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Add new webhook trigger types * Change rotate secret endpoint from being a PUT to a POST call * Fix issue where crypto import was causing downstream Jest incompatibilities +* Fix FormData import compatibility issues with ESM ### 7.5.2 / 2024-07-12 * Fix issue where metadata was being incorrectly modified before being sent to the API From 4ef1a1565849c18371c6c25d7105b146b24e5cac Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:00:28 -0400 Subject: [PATCH 3/4] Resolve compatibility issue with formdata import --- src/resources/messages.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/resources/messages.ts b/src/resources/messages.ts index 8eabe135..406f6ba0 100644 --- a/src/resources/messages.ts +++ b/src/resources/messages.ts @@ -311,13 +311,10 @@ export class Messages extends Resource { static _buildFormRequest( requestBody: CreateDraftRequest | UpdateDraftRequest | SendMessageRequest ): FormData { - let form: FormData; // FormData imports are funky, cjs needs to use .default, es6 doesn't - if (typeof (FormData as any).default !== 'undefined') { - form = new (FormData as any).default(); - } else { - form = new FormData(); - } + const FormData = require('form-data'); + const FormDataConstructor = FormData.default || FormData; + const form = new FormDataConstructor(); // Split out the message payload from the attachments const messagePayload = { From c7ee7240f11f27c0e3ba329f4aef60c1ce3c0987 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:04:58 -0400 Subject: [PATCH 4/4] fix linting --- src/resources/messages.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/resources/messages.ts b/src/resources/messages.ts index 406f6ba0..5e2d6006 100644 --- a/src/resources/messages.ts +++ b/src/resources/messages.ts @@ -312,9 +312,9 @@ export class Messages extends Resource { requestBody: CreateDraftRequest | UpdateDraftRequest | SendMessageRequest ): FormData { // FormData imports are funky, cjs needs to use .default, es6 doesn't - const FormData = require('form-data'); - const FormDataConstructor = FormData.default || FormData; - const form = new FormDataConstructor(); + const FD = require('form-data'); + const FormDataConstructor = FD.default || FD; + const form: FormData = new FormDataConstructor(); // Split out the message payload from the attachments const messagePayload = {