Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FormData import compatibility issues with ESM #586

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down
11 changes: 4 additions & 7 deletions src/resources/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 FD = require('form-data');
const FormDataConstructor = FD.default || FD;
const form: FormData = new FormDataConstructor();

// Split out the message payload from the attachments
const messagePayload = {
Expand Down
Loading