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

Add support for custom headers field for Drafts and Messages #553

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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### Unreleased
* Add support for adding custom headers to outgoing requests
* Add support for custom headers field for drafts and messages
* Rename incorrect `type` field in `When` models to `object`

### 7.2.1 / 2024-03-05
* Improved message sending and draft create/update performance
* Change default timeout to match API (90 seconds)
Expand Down
15 changes: 15 additions & 0 deletions src/models/drafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { ListQueryParams } from './listQueryParams.js';
import { EmailName } from './events.js';
import { CreateAttachmentRequest } from './attachments.js';

export interface CustomHeader {
/**
* The name of the custom header.
*/
name: string;
/**
* The value of the custom header.
*/
value: string;
}

/**
* Interface representing a request to create a draft.
*/
Expand Down Expand Up @@ -56,6 +67,10 @@ export interface CreateDraftRequest {
* Options for tracking opens, links, and thread replies.
*/
trackingOptions?: TrackingOptions;
/**
* An array of custom headers to add to the message.
*/
customHeaders?: CustomHeader[];
}

/**
Expand Down
95 changes: 95 additions & 0 deletions tests/resources/drafts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import APIClient from '../../src/apiClient';
import { Drafts } from '../../src/resources/drafts';
import { createReadableStream, MockedFormData } from '../testUtils';
import { CreateAttachmentRequest } from '../../src/models/attachments';
import { objKeysToCamelCase } from '../../src/utils';
jest.mock('../src/apiClient');

// Mock the FormData constructor
Expand Down Expand Up @@ -33,6 +34,100 @@ describe('Drafts', () => {
apiClient.request.mockResolvedValue({});
});

describe('deserializing', () => {
it('should return a draft object as expected', () => {
const apiDraft = {
body: 'Hello, I just sent a message using Nylas!',
cc: [
{
email: 'arya.stark@example.com',
},
],
attachments: [
{
content_type: 'text/calendar',
id: '4kj2jrcoj9ve5j9yxqz5cuv98',
size: 1708,
},
],
folders: ['8l6c4d11y1p4dm4fxj52whyr9', 'd9zkcr2tljpu3m4qpj7l2hbr0'],
from: [
{
name: 'Daenerys Targaryen',
email: 'daenerys.t@example.com',
},
],
grant_id: '41009df5-bf11-4c97-aa18-b285b5f2e386',
id: '5d3qmne77v32r8l4phyuksl2x',
object: 'draft',
reply_to: [
{
name: 'Daenerys Targaryen',
email: 'daenerys.t@example.com',
},
],
snippet: 'Hello, I just sent a message using Nylas!',
starred: true,
subject: 'Hello from Nylas!',
thread_id: '1t8tv3890q4vgmwq6pmdwm8qgsaer',
to: [
{
name: 'Jon Snow',
email: 'j.snow@example.com',
},
],
date: 1705084742,
created_at: 1705084926,
};

const draft = objKeysToCamelCase(apiDraft);

expect(draft).toEqual({
body: 'Hello, I just sent a message using Nylas!',
cc: [
{
email: 'arya.stark@example.com',
},
],
attachments: [
{
contentType: 'text/calendar',
id: '4kj2jrcoj9ve5j9yxqz5cuv98',
size: 1708,
},
],
folders: ['8l6c4d11y1p4dm4fxj52whyr9', 'd9zkcr2tljpu3m4qpj7l2hbr0'],
from: [
{
name: 'Daenerys Targaryen',
email: 'daenerys.t@example.com',
},
],
grantId: '41009df5-bf11-4c97-aa18-b285b5f2e386',
id: '5d3qmne77v32r8l4phyuksl2x',
object: 'draft',
replyTo: [
{
name: 'Daenerys Targaryen',
email: 'daenerys.t@example.com',
},
],
snippet: 'Hello, I just sent a message using Nylas!',
starred: true,
subject: 'Hello from Nylas!',
threadId: '1t8tv3890q4vgmwq6pmdwm8qgsaer',
to: [
{
name: 'Jon Snow',
email: 'j.snow@example.com',
},
],
date: 1705084742,
createdAt: 1705084926,
});
});
});

describe('list', () => {
it('should call apiClient.request with the correct params', async () => {
await drafts.list({
Expand Down
Loading