@memberjunction/communication-ms-graph 3.4.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,284 @@
1
+ import { BaseCommunicationProvider, CreateDraftParams, CreateDraftResult, ForwardMessageParams, ForwardMessageResult, GetMessagesParams, GetMessagesResult, GetSingleMessageParams, GetSingleMessageResult, DeleteMessageParams, DeleteMessageResult, MoveMessageParams, MoveMessageResult, ListFoldersParams, ListFoldersResult, MarkAsReadParams, MarkAsReadResult, ArchiveMessageParams, ArchiveMessageResult, SearchMessagesParams, SearchMessagesResult, ListAttachmentsParams, ListAttachmentsResult, DownloadAttachmentParams, DownloadAttachmentResult, ProviderOperation, MessageResult, ProcessedMessage, ProviderCredentialsBase, ReplyToMessageParams, ReplyToMessageResult } from "@memberjunction/communication-types";
2
+ import { Client } from '@microsoft/microsoft-graph-client';
3
+ import { Message } from "@microsoft/microsoft-graph-types";
4
+ /**
5
+ * Credentials for Microsoft Graph (Azure AD) email provider.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // Use with SendSingleMessage
10
+ * await provider.SendSingleMessage(message, {
11
+ * tenantId: 'your-tenant-id',
12
+ * clientId: 'your-client-id',
13
+ * clientSecret: 'your-client-secret',
14
+ * accountEmail: 'user@domain.com'
15
+ * });
16
+ *
17
+ * // Override only accountEmail (use env vars for auth)
18
+ * await provider.SendSingleMessage(message, {
19
+ * accountEmail: 'different-mailbox@domain.com'
20
+ * });
21
+ *
22
+ * // Disable environment fallback
23
+ * await provider.SendSingleMessage(message, {
24
+ * tenantId: 'your-tenant-id',
25
+ * clientId: 'your-client-id',
26
+ * clientSecret: 'your-client-secret',
27
+ * accountEmail: 'user@domain.com',
28
+ * disableEnvironmentFallback: true
29
+ * });
30
+ * ```
31
+ */
32
+ export interface MSGraphCredentials extends ProviderCredentialsBase {
33
+ /**
34
+ * Azure AD tenant ID (GUID).
35
+ * If not provided, falls back to AZURE_TENANT_ID environment variable.
36
+ */
37
+ tenantId?: string;
38
+ /**
39
+ * Azure AD application (client) ID (GUID).
40
+ * If not provided, falls back to AZURE_CLIENT_ID environment variable.
41
+ */
42
+ clientId?: string;
43
+ /**
44
+ * Azure AD application client secret.
45
+ * If not provided, falls back to AZURE_CLIENT_SECRET environment variable.
46
+ */
47
+ clientSecret?: string;
48
+ /**
49
+ * Email address of the mailbox to send from.
50
+ * If not provided, falls back to AZURE_ACCOUNT_EMAIL environment variable.
51
+ * Can also be overridden via message.From.
52
+ */
53
+ accountEmail?: string;
54
+ }
55
+ /**
56
+ * Implementation of the MS Graph provider for sending and receiving messages.
57
+ *
58
+ * @remarks
59
+ * Microsoft Graph provides full mailbox access. This provider supports:
60
+ * - Sending messages
61
+ * - Fetching messages from inbox
62
+ * - Forwarding messages
63
+ * - Replying to messages
64
+ * - Creating drafts
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * // Using environment credentials (default)
69
+ * await engine.SendSingleMessage('Microsoft Graph', 'Standard Email', message);
70
+ *
71
+ * // Using per-request credentials
72
+ * await engine.SendSingleMessage('Microsoft Graph', 'Standard Email', message, undefined, false, {
73
+ * tenantId: 'customer-tenant-id',
74
+ * clientId: 'customer-client-id',
75
+ * clientSecret: 'customer-client-secret',
76
+ * accountEmail: 'customer@domain.com'
77
+ * });
78
+ * ```
79
+ */
80
+ export declare class MSGraphProvider extends BaseCommunicationProvider {
81
+ private HTMLConverter;
82
+ private clientCache;
83
+ constructor();
84
+ /**
85
+ * Resolves MS Graph credentials from request and environment.
86
+ */
87
+ private resolveCredentials;
88
+ /**
89
+ * Gets or creates a Graph client for the given credentials.
90
+ * Uses cached client if credentials match environment (default case).
91
+ */
92
+ private getGraphClient;
93
+ /**
94
+ * Gets the API URI for Graph API calls.
95
+ */
96
+ private getApiUri;
97
+ /**
98
+ * Sends a single email message via MS Graph.
99
+ *
100
+ * @requires MS Graph Scope: Mail.Send (Application)
101
+ */
102
+ SendSingleMessage(message: ProcessedMessage, credentials?: MSGraphCredentials): Promise<MessageResult>;
103
+ /**
104
+ * Replies to an email message via MS Graph.
105
+ *
106
+ * @requires MS Graph Scope: Mail.Send (Application)
107
+ */
108
+ ReplyToMessage(params: ReplyToMessageParams, credentials?: MSGraphCredentials): Promise<ReplyToMessageResult>;
109
+ /**
110
+ * Retrieves email messages from a mailbox via MS Graph.
111
+ *
112
+ * @requires MS Graph Scope: Mail.Read (Application), or Mail.ReadWrite if using ContextData.MarkAsRead option
113
+ */
114
+ GetMessages(params: GetMessagesParams<Record<string, unknown>>, credentials?: MSGraphCredentials): Promise<GetMessagesResult<Message>>;
115
+ /**
116
+ * Forwards an email message via MS Graph.
117
+ *
118
+ * @requires MS Graph Scope: Mail.Send (Application)
119
+ */
120
+ ForwardMessage(params: ForwardMessageParams, credentials?: MSGraphCredentials): Promise<ForwardMessageResult>;
121
+ /**
122
+ * Gets headers for a specific message using email address directly.
123
+ * This is the preferred method for new code as it avoids an extra API call to look up the user.
124
+ *
125
+ * @protected
126
+ * @requires MS Graph Scope: Mail.Read (Application)
127
+ */
128
+ protected GetHeadersWithEmail(client: Client, emailAddress: string, params: GetMessagesParams, messageID: string | undefined): Promise<Record<string, string>>;
129
+ /**
130
+ * Marks a message as read using email address directly.
131
+ * This is the preferred method for new code as it avoids an extra API call to look up the user.
132
+ *
133
+ * @protected
134
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
135
+ */
136
+ protected MarkMessageAsReadWithEmail(client: Client, emailAddress: string, messageID: string): Promise<boolean>;
137
+ /**
138
+ * Gets the user account information by making an API call to MS Graph.
139
+ * This method looks up the full User object from the Graph API.
140
+ *
141
+ * @protected
142
+ * @requires MS Graph Scope: User.Read.All (Application) - Required to look up user information
143
+ */
144
+ protected GetServiceAccountWithClient(client: Client, email: string): Promise<{
145
+ id?: string;
146
+ userPrincipalName?: string;
147
+ } | null>;
148
+ /**
149
+ * Gets the service account using default client.
150
+ * Caches the result for subsequent calls.
151
+ *
152
+ * @protected
153
+ * @requires MS Graph Scope: User.Read.All (Application) - Required to look up user information
154
+ */
155
+ protected GetServiceAccount(email?: string): Promise<{
156
+ id?: string;
157
+ userPrincipalName?: string;
158
+ } | null>;
159
+ /**
160
+ * Gets headers for a message using User object (requires prior user lookup).
161
+ * For better performance, consider using GetHeadersWithEmail() instead.
162
+ *
163
+ * @protected
164
+ * @requires MS Graph Scope: Mail.Read (Application)
165
+ */
166
+ protected GetHeadersWithClient(client: Client, params: GetMessagesParams, user: {
167
+ id?: string;
168
+ userPrincipalName?: string;
169
+ }, messageID: string | undefined): Promise<Record<string, string>>;
170
+ /**
171
+ * Gets headers using default client.
172
+ *
173
+ * @protected
174
+ * @requires MS Graph Scope: Mail.Read (Application)
175
+ */
176
+ protected GetHeaders(params: GetMessagesParams, user: {
177
+ id?: string;
178
+ userPrincipalName?: string;
179
+ }, messageID: string | undefined): Promise<Record<string, string>>;
180
+ /**
181
+ * Marks a message as read using User ID.
182
+ * For better performance, consider using MarkMessageAsReadWithEmail() instead.
183
+ *
184
+ * @protected
185
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
186
+ */
187
+ protected MarkMessageAsReadWithClient(client: Client, userID: string, messageID: string): Promise<boolean>;
188
+ /**
189
+ * Marks a message as read using default client.
190
+ *
191
+ * @protected
192
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
193
+ */
194
+ protected MarkMessageAsRead(userID: string, messageID: string): Promise<boolean>;
195
+ /**
196
+ * Creates a draft email message via MS Graph.
197
+ *
198
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
199
+ */
200
+ CreateDraft(params: CreateDraftParams, credentials?: MSGraphCredentials): Promise<CreateDraftResult>;
201
+ /**
202
+ * Returns the list of operations supported by MS Graph provider.
203
+ * MS Graph supports all mailbox operations.
204
+ */
205
+ getSupportedOperations(): ProviderOperation[];
206
+ /**
207
+ * Gets a single message by ID from MS Graph.
208
+ *
209
+ * @requires MS Graph Scope: Mail.Read (Application)
210
+ */
211
+ GetSingleMessage(params: GetSingleMessageParams, credentials?: MSGraphCredentials): Promise<GetSingleMessageResult>;
212
+ /**
213
+ * Deletes a message using MS Graph.
214
+ * If PermanentDelete is false, moves to Deleted Items folder.
215
+ *
216
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
217
+ */
218
+ DeleteMessage(params: DeleteMessageParams, credentials?: MSGraphCredentials): Promise<DeleteMessageResult>;
219
+ /**
220
+ * Moves a message to a different folder using MS Graph.
221
+ *
222
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
223
+ */
224
+ MoveMessage(params: MoveMessageParams, credentials?: MSGraphCredentials): Promise<MoveMessageResult>;
225
+ /**
226
+ * Lists mail folders using MS Graph.
227
+ *
228
+ * @requires MS Graph Scope: Mail.Read (Application)
229
+ */
230
+ ListFolders(params: ListFoldersParams, credentials?: MSGraphCredentials): Promise<ListFoldersResult>;
231
+ /**
232
+ * Marks messages as read or unread using MS Graph.
233
+ *
234
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
235
+ */
236
+ MarkAsRead(params: MarkAsReadParams, credentials?: MSGraphCredentials): Promise<MarkAsReadResult>;
237
+ /**
238
+ * Archives a message by moving it to the Archive folder using MS Graph.
239
+ *
240
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
241
+ */
242
+ ArchiveMessage(params: ArchiveMessageParams, credentials?: MSGraphCredentials): Promise<ArchiveMessageResult>;
243
+ /**
244
+ * Searches messages using MS Graph search or filter.
245
+ *
246
+ * @requires MS Graph Scope: Mail.Read (Application)
247
+ */
248
+ SearchMessages(params: SearchMessagesParams, credentials?: MSGraphCredentials): Promise<SearchMessagesResult>;
249
+ /**
250
+ * Lists attachments on a message using MS Graph.
251
+ *
252
+ * @requires MS Graph Scope: Mail.Read (Application)
253
+ */
254
+ ListAttachments(params: ListAttachmentsParams, credentials?: MSGraphCredentials): Promise<ListAttachmentsResult>;
255
+ /**
256
+ * Downloads an attachment from a message using MS Graph.
257
+ *
258
+ * @requires MS Graph Scope: Mail.Read (Application)
259
+ */
260
+ DownloadAttachment(params: DownloadAttachmentParams, credentials?: MSGraphCredentials): Promise<DownloadAttachmentResult>;
261
+ /**
262
+ * Finds a system folder by well-known name.
263
+ *
264
+ * @private
265
+ * @requires MS Graph Scope: Mail.Read (Application)
266
+ */
267
+ private findSystemFolder;
268
+ /**
269
+ * Creates a new mail folder.
270
+ *
271
+ * @private
272
+ * @requires MS Graph Scope: Mail.ReadWrite (Application)
273
+ */
274
+ private createMailFolder;
275
+ /**
276
+ * Checks if a folder name is a system folder.
277
+ */
278
+ private isSystemFolder;
279
+ /**
280
+ * Maps folder display name to system folder type.
281
+ */
282
+ private mapSystemFolderType;
283
+ }
284
+ //# sourceMappingURL=MSGraphProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MSGraphProvider.d.ts","sourceRoot":"","sources":["../src/MSGraphProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,yBAAyB,EACzB,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,sBAAsB,EACtB,sBAAsB,EACtB,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,EACrB,wBAAwB,EACxB,wBAAwB,EAGxB,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,uBAAuB,EACvB,oBAAoB,EACpB,oBAAoB,EAGvB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAG3D,OAAO,EAAE,OAAO,EAAE,MAAM,kCAAkC,CAAC;AAO3D;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,WAAW,kBAAmB,SAAQ,uBAAuB;IAC/D;;;OAGG;IACH,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