Skip to content

Commit

Permalink
Adding from and scheduled_id to message schema (#541)
Browse files Browse the repository at this point in the history
* Adding from and scheduled_id to message schema

* remove BaseCreateMessage to avoid confusion

we had 2 levels of inheritance... leading to code smell

* add additional missing fields to message

* TW-2703 Add missing from field for sending message

---------

Co-authored-by: Mostafa Rashed <[email protected]>
  • Loading branch information
kraju3 and mrashed-dev authored Feb 27, 2024
1 parent 9efd4b2 commit f4e01f6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 39 deletions.
47 changes: 45 additions & 2 deletions src/models/drafts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
import { BaseMessage, BaseCreateMessage } from './messages.js';
import { BaseMessage } from './messages.js';
import { ListQueryParams } from './listQueryParams.js';
import { EmailName } from './events.js';
import { CreateAttachmentRequest } from './attachments.js';

/**
* Interface representing a request to create a draft.
*/
export interface CreateDraftRequest extends BaseCreateMessage {
export interface CreateDraftRequest {
/**
* An array of name/email address pairs that the message was sent from. This is usually one pair only, but can be many.
*/
from?: EmailName[];
/**
* An array of message recipients.
*/
to: EmailName[];
/**
* An array of bcc recipients.
*/
bcc?: EmailName[];
/**
* An array of cc recipients.
*/
cc?: EmailName[];
/**
* An array of name and email pairs that override the sent reply-to headers.
*/
replyTo?: EmailName[];
/**
* An array of files to attach to the message.
*/
attachments?: CreateAttachmentRequest[];
/**
* The message subject.
*/
subject?: string;
/**
* The full HTML message body.
* Messages with only plain-text representations are up-converted to HTML.
*/
body?: string;
/**
* Whether or not the message has been starred by the user.
*/
starred?: boolean;
/**
* Unix timestamp to send the message at.
*/
Expand All @@ -23,6 +62,10 @@ export interface CreateDraftRequest extends BaseCreateMessage {
* Interface representing a request to send a message.
*/
export interface SendMessageRequest extends CreateDraftRequest {
/**
* An array of message senders.
*/
from?: EmailName[];
/**
* Whether or not to use draft support.
* This is primarily used when dealing with large attachments.
Expand Down
71 changes: 37 additions & 34 deletions src/models/messages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import { EmailName } from './events.js';
import { ListQueryParams } from './listQueryParams.js';
import { Attachment, CreateAttachmentRequest } from './attachments.js';
import { Attachment } from './attachments.js';

/**
* @internal Internal interface for creating a message.
* @internal Internal interface for a message.
*/
export interface BaseCreateMessage {
export interface BaseMessage {
/**
* The unique identifier for the message.
*/
id: string;
/**
* Grant ID of the Nylas account.
*/
grantId: string;
/**
* Unix timestamp of when the message was received by the mail server.
* This may be different from the unverified Date header in raw message object.
*/
date: number;
/**
* Unix timestamp of when the message was created.
*/
createdAt: number;
/**
* The ID of the folder(s) the message appears in.
*/
folders: string[];
/**
* An array of message recipients.
*/
Expand All @@ -22,10 +43,6 @@ export interface BaseCreateMessage {
* An array of name and email pairs that override the sent reply-to headers.
*/
replyTo?: EmailName[];
/**
* An array of files to attach to the message.
*/
attachments?: CreateAttachmentRequest[];
/**
* The message subject.
*/
Expand All @@ -39,33 +56,6 @@ export interface BaseCreateMessage {
* Whether or not the message has been starred by the user.
*/
starred?: boolean;
}

/**
* @internal Internal interface for a message.
*/
export interface BaseMessage extends Omit<BaseCreateMessage, 'attachments'> {
/**
* The unique identifier for the message.
*/
id: string;
/**
* Grant ID of the Nylas account.
*/
grantId: string;
/**
* Unix timestamp of when the message was received by the mail server.
* This may be different from the unverified Date header in raw message object.
*/
date: number;
/**
* Unix timestamp of when the message was created.
*/
createdAt: number;
/**
* The ID of the folder(s) the message appears in.
*/
folders: string[];
/**
* An array of message senders.
*/
Expand Down Expand Up @@ -107,6 +97,19 @@ export interface Message extends BaseMessage {
* A list of key-value pairs storing additional data.
*/
metadata?: Record<string, unknown>;
/**
* The unique identifier for the scheduled message.
*/
scheduleId?: string;
/**
* Unix timestamp to send the message at.
*/
sendAt?: number;
/**
* Whether or not to use draft support.
* This is primarily used when dealing with large attachments.
*/
useDraft?: boolean;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/resources/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AsyncListResponse, Resource } from './resource.js';
import {
BaseCreateMessage,
FindMessageQueryParams,
ListMessagesQueryParams,
Message,
Expand All @@ -15,7 +14,11 @@ import {
NylasListResponse,
NylasResponse,
} from '../models/response.js';
import { SendMessageRequest, UpdateDraftRequest } from '../models/drafts.js';
import {
CreateDraftRequest,
SendMessageRequest,
UpdateDraftRequest,
} from '../models/drafts.js';
import * as FormData from 'form-data';
import { objKeysToSnakeCase } from '../utils.js';
import { SmartCompose } from './smartCompose.js';
Expand Down Expand Up @@ -250,7 +253,7 @@ export class Messages extends Resource {
}

static _buildFormRequest(
requestBody: BaseCreateMessage | UpdateDraftRequest
requestBody: CreateDraftRequest | UpdateDraftRequest | SendMessageRequest
): FormData {
let form: FormData;
// FormData imports are funky, cjs needs to use .default, es6 doesn't
Expand Down

0 comments on commit f4e01f6

Please sign in to comment.