Skip to content

Commit

Permalink
Add support for custom headers field for Drafts and Messages (#553)
Browse files Browse the repository at this point in the history
This PR adds support to the custom headers field used to attach headers to message and draft objects. Not to be confused with custom headers to Nylas API requests, completed by #552.
  • Loading branch information
mrashed-dev committed Apr 8, 2024
1 parent 618d8bd commit 5157373
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
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

0 comments on commit 5157373

Please sign in to comment.