From 240f665b3c1304b2435d5c646fd40d22d8ebeb0b Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 21 Dec 2023 12:05:49 -0500 Subject: [PATCH] Fix issue with form-data not importing correctly for ESM projects (#523) The npm package `form-data` has an issue with importing compatibility between CJS and ESM. Using `import * as FormData` or `import FormData` works only on cjs while `import { default as FormData }` works only on esm. We work around this now by checking if `default` exists on the importing object, and using it if it does, otherwise just use `FormData`. --- CHANGELOG.md | 3 +++ src/resources/messages.ts | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc802c6c..dbecb112 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### 7.0.0-beta.4 / TBD +* Fix issue with form-data not importing correctly for ESM projects + ### 7.0.0-beta.3 / 2023-10-23 * Added support for the messages, drafts, and threads endpoints * Added support for the free-busy endpoint diff --git a/src/resources/messages.ts b/src/resources/messages.ts index 882cac42..9a19f5d6 100644 --- a/src/resources/messages.ts +++ b/src/resources/messages.ts @@ -250,7 +250,13 @@ export class Messages extends Resource { static _buildFormRequest( requestBody: BaseCreateMessage | UpdateDraftRequest ): FormData { - const form = new 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(); + } // Split out the message payload from the attachments const messagePayload = {