Skip to content

Commit

Permalink
Fix issue with form-data not importing correctly for ESM projects (#523)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
mrashed-dev authored Dec 21, 2023
1 parent f9f18e5 commit 240f665
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/resources/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 240f665

Please sign in to comment.