@pallasoft/ps-mail 1.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.
Files changed (145) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +33 -0
  3. package/dist/constants/mail-http-config.d.ts +11 -0
  4. package/dist/constants/mail-http-config.js +14 -0
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +2 -0
  7. package/dist/microsoft/constants/graph-mail-body-type.d.ts +4 -0
  8. package/dist/microsoft/constants/graph-mail-body-type.js +8 -0
  9. package/dist/microsoft/constants/graph-mail-config.d.ts +42 -0
  10. package/dist/microsoft/constants/graph-mail-config.js +80 -0
  11. package/dist/microsoft/constants/graph-mail-error-messages.d.ts +2 -0
  12. package/dist/microsoft/constants/graph-mail-error-messages.js +12 -0
  13. package/dist/microsoft/constants/graph-mail-error-reason.d.ts +7 -0
  14. package/dist/microsoft/constants/graph-mail-error-reason.js +11 -0
  15. package/dist/microsoft/constants/graph-mail-folder.d.ts +4 -0
  16. package/dist/microsoft/constants/graph-mail-folder.js +8 -0
  17. package/dist/microsoft/constants/index.d.ts +5 -0
  18. package/dist/microsoft/constants/index.js +21 -0
  19. package/dist/microsoft/index.d.ts +3 -0
  20. package/dist/microsoft/index.js +10 -0
  21. package/dist/microsoft/models/graph-access-token-data.d.ts +4 -0
  22. package/dist/microsoft/models/graph-access-token-data.js +2 -0
  23. package/dist/microsoft/models/graph-collection-response.d.ts +5 -0
  24. package/dist/microsoft/models/graph-collection-response.js +2 -0
  25. package/dist/microsoft/models/graph-credentials.d.ts +6 -0
  26. package/dist/microsoft/models/graph-credentials.js +2 -0
  27. package/dist/microsoft/models/graph-error-detail-data.d.ts +4 -0
  28. package/dist/microsoft/models/graph-error-detail-data.js +2 -0
  29. package/dist/microsoft/models/graph-error-response.d.ts +4 -0
  30. package/dist/microsoft/models/graph-error-response.js +2 -0
  31. package/dist/microsoft/models/graph-mail-attachment-data.d.ts +8 -0
  32. package/dist/microsoft/models/graph-mail-attachment-data.js +2 -0
  33. package/dist/microsoft/models/graph-mail-body-data.d.ts +5 -0
  34. package/dist/microsoft/models/graph-mail-body-data.js +2 -0
  35. package/dist/microsoft/models/graph-mail-categories-request.d.ts +3 -0
  36. package/dist/microsoft/models/graph-mail-categories-request.js +2 -0
  37. package/dist/microsoft/models/graph-mail-connection.d.ts +4 -0
  38. package/dist/microsoft/models/graph-mail-connection.js +2 -0
  39. package/dist/microsoft/models/graph-mail-delta-data.d.ts +6 -0
  40. package/dist/microsoft/models/graph-mail-delta-data.js +2 -0
  41. package/dist/microsoft/models/graph-mail-delta-options.d.ts +4 -0
  42. package/dist/microsoft/models/graph-mail-delta-options.js +2 -0
  43. package/dist/microsoft/models/graph-mail-delta-page-data.d.ts +7 -0
  44. package/dist/microsoft/models/graph-mail-delta-page-data.js +2 -0
  45. package/dist/microsoft/models/graph-mail-draft-data.d.ts +11 -0
  46. package/dist/microsoft/models/graph-mail-draft-data.js +2 -0
  47. package/dist/microsoft/models/graph-mail-error-data.d.ts +7 -0
  48. package/dist/microsoft/models/graph-mail-error-data.js +2 -0
  49. package/dist/microsoft/models/graph-mail-header-data.d.ts +4 -0
  50. package/dist/microsoft/models/graph-mail-header-data.js +2 -0
  51. package/dist/microsoft/models/graph-mail-message-data.d.ts +25 -0
  52. package/dist/microsoft/models/graph-mail-message-data.js +2 -0
  53. package/dist/microsoft/models/graph-mail-outgoing-message-data.d.ts +11 -0
  54. package/dist/microsoft/models/graph-mail-outgoing-message-data.js +2 -0
  55. package/dist/microsoft/models/graph-mail-recipient-data.d.ts +4 -0
  56. package/dist/microsoft/models/graph-mail-recipient-data.js +2 -0
  57. package/dist/microsoft/models/graph-mail-removed-data.d.ts +3 -0
  58. package/dist/microsoft/models/graph-mail-removed-data.js +2 -0
  59. package/dist/microsoft/models/graph-mail-reply-data.d.ts +9 -0
  60. package/dist/microsoft/models/graph-mail-reply-data.js +2 -0
  61. package/dist/microsoft/models/graph-mail-reply-request.d.ts +4 -0
  62. package/dist/microsoft/models/graph-mail-reply-request.js +2 -0
  63. package/dist/microsoft/models/graph-mail-send-request.d.ts +5 -0
  64. package/dist/microsoft/models/graph-mail-send-request.js +2 -0
  65. package/dist/microsoft/models/graph-request-data.d.ts +7 -0
  66. package/dist/microsoft/models/graph-request-data.js +2 -0
  67. package/dist/microsoft/models/graph-token-response.d.ts +5 -0
  68. package/dist/microsoft/models/graph-token-response.js +2 -0
  69. package/dist/microsoft/models/index.d.ts +24 -0
  70. package/dist/microsoft/models/index.js +2 -0
  71. package/dist/microsoft/services/graph-mail-error-service.d.ts +13 -0
  72. package/dist/microsoft/services/graph-mail-error-service.js +153 -0
  73. package/dist/microsoft/services/graph-mail-service.d.ts +32 -0
  74. package/dist/microsoft/services/graph-mail-service.js +352 -0
  75. package/dist/microsoft/services/graph-request-service.d.ts +11 -0
  76. package/dist/microsoft/services/graph-request-service.js +206 -0
  77. package/dist/microsoft/services/graph-token-service.d.ts +10 -0
  78. package/dist/microsoft/services/graph-token-service.js +137 -0
  79. package/dist/microsoft/services/index.d.ts +4 -0
  80. package/dist/microsoft/services/index.js +20 -0
  81. package/dist/models/index.d.ts +5 -0
  82. package/dist/models/index.js +2 -0
  83. package/dist/models/mail-address-data.d.ts +4 -0
  84. package/dist/models/mail-address-data.js +2 -0
  85. package/dist/models/mail-attachment-content-data.d.ts +4 -0
  86. package/dist/models/mail-attachment-content-data.js +2 -0
  87. package/dist/models/mail-attachment-data.d.ts +7 -0
  88. package/dist/models/mail-attachment-data.js +2 -0
  89. package/dist/models/mail-query-parameter.d.ts +4 -0
  90. package/dist/models/mail-query-parameter.js +2 -0
  91. package/dist/models/mail-sent-data.d.ts +5 -0
  92. package/dist/models/mail-sent-data.js +2 -0
  93. package/dist/resend/constants/index.d.ts +2 -0
  94. package/dist/resend/constants/index.js +18 -0
  95. package/dist/resend/constants/resend-mail-config.d.ts +24 -0
  96. package/dist/resend/constants/resend-mail-config.js +27 -0
  97. package/dist/resend/constants/resend-webhook-config.d.ts +16 -0
  98. package/dist/resend/constants/resend-webhook-config.js +19 -0
  99. package/dist/resend/index.d.ts +2 -0
  100. package/dist/resend/index.js +7 -0
  101. package/dist/resend/models/index.d.ts +15 -0
  102. package/dist/resend/models/index.js +2 -0
  103. package/dist/resend/models/resend-attachment-row.d.ts +9 -0
  104. package/dist/resend/models/resend-attachment-row.js +2 -0
  105. package/dist/resend/models/resend-authentication-data.d.ts +5 -0
  106. package/dist/resend/models/resend-authentication-data.js +2 -0
  107. package/dist/resend/models/resend-connection.d.ts +5 -0
  108. package/dist/resend/models/resend-connection.js +2 -0
  109. package/dist/resend/models/resend-error-response.d.ts +5 -0
  110. package/dist/resend/models/resend-error-response.js +2 -0
  111. package/dist/resend/models/resend-list-response.d.ts +3 -0
  112. package/dist/resend/models/resend-list-response.js +2 -0
  113. package/dist/resend/models/resend-mail-error-data.d.ts +4 -0
  114. package/dist/resend/models/resend-mail-error-data.js +2 -0
  115. package/dist/resend/models/resend-received-attachment-data.d.ts +4 -0
  116. package/dist/resend/models/resend-received-attachment-data.js +2 -0
  117. package/dist/resend/models/resend-received-email-data.d.ts +20 -0
  118. package/dist/resend/models/resend-received-email-data.js +2 -0
  119. package/dist/resend/models/resend-received-email-row.d.ts +21 -0
  120. package/dist/resend/models/resend-received-email-row.js +2 -0
  121. package/dist/resend/models/resend-send-attachment-data.d.ts +6 -0
  122. package/dist/resend/models/resend-send-attachment-data.js +2 -0
  123. package/dist/resend/models/resend-send-attachment-request.d.ts +6 -0
  124. package/dist/resend/models/resend-send-attachment-request.js +2 -0
  125. package/dist/resend/models/resend-send-data.d.ts +15 -0
  126. package/dist/resend/models/resend-send-data.js +2 -0
  127. package/dist/resend/models/resend-send-request.d.ts +13 -0
  128. package/dist/resend/models/resend-send-request.js +2 -0
  129. package/dist/resend/models/resend-send-response.d.ts +3 -0
  130. package/dist/resend/models/resend-send-response.js +2 -0
  131. package/dist/resend/models/resend-webhook-headers.d.ts +1 -0
  132. package/dist/resend/models/resend-webhook-headers.js +2 -0
  133. package/dist/resend/services/index.d.ts +3 -0
  134. package/dist/resend/services/index.js +19 -0
  135. package/dist/resend/services/resend-mail-error-service.d.ts +7 -0
  136. package/dist/resend/services/resend-mail-error-service.js +79 -0
  137. package/dist/resend/services/resend-mail-service.d.ts +22 -0
  138. package/dist/resend/services/resend-mail-service.js +322 -0
  139. package/dist/resend/services/resend-webhook-service.d.ts +10 -0
  140. package/dist/resend/services/resend-webhook-service.js +139 -0
  141. package/dist/services/mail-fetch-failure-service.d.ts +3 -0
  142. package/dist/services/mail-fetch-failure-service.js +49 -0
  143. package/dist/services/mail-url-service.d.ts +5 -0
  144. package/dist/services/mail-url-service.js +27 -0
  145. package/package.json +88 -0
@@ -0,0 +1,7 @@
1
+ import type { HttpMethod } from '@pallasoft/ps-utilities';
2
+ export interface GraphRequestData {
3
+ method: HttpMethod;
4
+ url: string;
5
+ body?: unknown;
6
+ prefer?: string[];
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export interface GraphTokenResponse {
2
+ access_token?: string;
3
+ expires_in?: number;
4
+ token_type?: string;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ export type { GraphAccessTokenData } from './graph-access-token-data';
2
+ export type { GraphCollectionResponse } from './graph-collection-response';
3
+ export type { GraphCredentials } from './graph-credentials';
4
+ export type { GraphErrorDetailData } from './graph-error-detail-data';
5
+ export type { GraphErrorResponse } from './graph-error-response';
6
+ export type { GraphMailAttachmentData } from './graph-mail-attachment-data';
7
+ export type { GraphMailBodyData } from './graph-mail-body-data';
8
+ export type { GraphMailCategoriesRequest } from './graph-mail-categories-request';
9
+ export type { GraphMailConnection } from './graph-mail-connection';
10
+ export type { GraphMailDeltaData } from './graph-mail-delta-data';
11
+ export type { GraphMailDeltaOptions } from './graph-mail-delta-options';
12
+ export type { GraphMailDeltaPageData } from './graph-mail-delta-page-data';
13
+ export type { GraphMailDraftData } from './graph-mail-draft-data';
14
+ export type { GraphMailErrorData } from './graph-mail-error-data';
15
+ export type { GraphMailHeaderData } from './graph-mail-header-data';
16
+ export type { GraphMailMessageData } from './graph-mail-message-data';
17
+ export type { GraphMailOutgoingMessageData } from './graph-mail-outgoing-message-data';
18
+ export type { GraphMailRecipientData } from './graph-mail-recipient-data';
19
+ export type { GraphMailRemovedData } from './graph-mail-removed-data';
20
+ export type { GraphMailReplyData } from './graph-mail-reply-data';
21
+ export type { GraphMailReplyRequest } from './graph-mail-reply-request';
22
+ export type { GraphMailSendRequest } from './graph-mail-send-request';
23
+ export type { GraphRequestData } from './graph-request-data';
24
+ export type { GraphTokenResponse } from './graph-token-response';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,13 @@
1
+ import { GraphMailErrorReason } from '../constants/graph-mail-error-reason';
2
+ import type { GraphMailErrorData } from '../models/graph-mail-error-data';
3
+ export declare class GraphMailErrorService {
4
+ static create(reason: GraphMailErrorReason, status: number, graphCode?: string | null, retryAfterSeconds?: number | null): GraphMailErrorData;
5
+ static fromResponse(response: Response): Promise<GraphMailErrorData>;
6
+ static fromFetchFailure(error: unknown): GraphMailErrorData;
7
+ static reasonFor(status: number, graphCode: string | null): GraphMailErrorReason;
8
+ static retryAfterSeconds(response: Response): number | null;
9
+ static isGraphMailError(error: unknown): error is GraphMailErrorData;
10
+ static reason(error: unknown): GraphMailErrorReason | null;
11
+ static isSyncStateExpired(error: unknown): boolean;
12
+ private static _graphCode;
13
+ }
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ var __generator = (this && this.__generator) || function (thisArg, body) {
45
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
46
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
+ function verb(n) { return function (v) { return step([n, v]); }; }
48
+ function step(op) {
49
+ if (f) throw new TypeError("Generator is already executing.");
50
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
52
+ if (y = 0, t) op = [op[0] & 2, t.value];
53
+ switch (op[0]) {
54
+ case 0: case 1: t = op; break;
55
+ case 4: _.label++; return { value: op[1], done: false };
56
+ case 5: _.label++; y = op[1]; op = [0]; continue;
57
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
+ default:
59
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
+ if (t[2]) _.ops.pop();
64
+ _.trys.pop(); continue;
65
+ }
66
+ op = body.call(thisArg, _);
67
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
+ }
70
+ };
71
+ Object.defineProperty(exports, "__esModule", { value: true });
72
+ exports.GraphMailErrorService = void 0;
73
+ var Attributes = __importStar(require("@pallasoft/ps-attributes"));
74
+ var ps_utilities_1 = require("@pallasoft/ps-utilities");
75
+ var mail_http_config_1 = require("../../constants/mail-http-config");
76
+ var mail_fetch_failure_service_1 = require("../../services/mail-fetch-failure-service");
77
+ var graph_mail_config_1 = require("../constants/graph-mail-config");
78
+ var graph_mail_error_messages_1 = require("../constants/graph-mail-error-messages");
79
+ var graph_mail_error_reason_1 = require("../constants/graph-mail-error-reason");
80
+ var GraphMailErrorService = /** @class */ (function () {
81
+ function GraphMailErrorService() {
82
+ }
83
+ GraphMailErrorService.create = function (reason, status, graphCode, retryAfterSeconds) {
84
+ if (graphCode === void 0) { graphCode = null; }
85
+ if (retryAfterSeconds === void 0) { retryAfterSeconds = null; }
86
+ return Object.assign(ps_utilities_1.Common.ErrorResponseService.create(graph_mail_error_messages_1.GraphMailErrorMessages[reason], status), {
87
+ reason: reason,
88
+ graphCode: graphCode,
89
+ retryAfterSeconds: retryAfterSeconds,
90
+ });
91
+ };
92
+ GraphMailErrorService.fromResponse = function (response) {
93
+ return __awaiter(this, void 0, void 0, function () {
94
+ var graphCode, _a;
95
+ return __generator(this, function (_b) {
96
+ switch (_b.label) {
97
+ case 0:
98
+ _a = this._graphCode;
99
+ return [4 /*yield*/, response.text().catch(function () { return ''; })];
100
+ case 1:
101
+ graphCode = _a.apply(this, [_b.sent()]);
102
+ return [2 /*return*/, this.create(this.reasonFor(response.status, graphCode), response.status, graphCode, this.retryAfterSeconds(response))];
103
+ }
104
+ });
105
+ });
106
+ };
107
+ GraphMailErrorService.fromFetchFailure = function (error) {
108
+ return this.create(graph_mail_error_reason_1.GraphMailErrorReason.Failed, mail_fetch_failure_service_1.MailFetchFailureService.status(error));
109
+ };
110
+ GraphMailErrorService.reasonFor = function (status, graphCode) {
111
+ var errorCodes = Attributes.Common.Response.ErrorCodes;
112
+ var syncResetCodes = graph_mail_config_1.GraphMailConfig.syncResetErrorCodes;
113
+ if (status === mail_http_config_1.MailHttpConfig.goneStatus ||
114
+ (graphCode !== null && syncResetCodes.includes(graphCode.toLowerCase())))
115
+ return graph_mail_error_reason_1.GraphMailErrorReason.SyncStateExpired;
116
+ if (status === errorCodes.TooManyRequests || status === errorCodes.ServiceUnavailable)
117
+ return graph_mail_error_reason_1.GraphMailErrorReason.Throttled;
118
+ if (status === errorCodes.Unauthorized || status === errorCodes.Forbidden)
119
+ return graph_mail_error_reason_1.GraphMailErrorReason.Unauthorized;
120
+ if (status === errorCodes.NotFound)
121
+ return graph_mail_error_reason_1.GraphMailErrorReason.NotFound;
122
+ return graph_mail_error_reason_1.GraphMailErrorReason.Failed;
123
+ };
124
+ GraphMailErrorService.retryAfterSeconds = function (response) {
125
+ var header = response.headers.get(ps_utilities_1.Common.HttpHeader.retryAfter);
126
+ var seconds = header === null || header.trim() === '' ? NaN : Number(header);
127
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
128
+ };
129
+ GraphMailErrorService.isGraphMailError = function (error) {
130
+ var reasons = Object.values(graph_mail_error_reason_1.GraphMailErrorReason);
131
+ return (error instanceof Error &&
132
+ 'reason' in error &&
133
+ typeof error.reason === 'string' &&
134
+ reasons.includes(error.reason));
135
+ };
136
+ GraphMailErrorService.reason = function (error) {
137
+ return this.isGraphMailError(error) ? error.reason : null;
138
+ };
139
+ GraphMailErrorService.isSyncStateExpired = function (error) {
140
+ return this.reason(error) === graph_mail_error_reason_1.GraphMailErrorReason.SyncStateExpired;
141
+ };
142
+ GraphMailErrorService._graphCode = function (text) {
143
+ var body = ps_utilities_1.Common.JsonService.read(text);
144
+ if (typeof body !== 'object' || body === null)
145
+ return null;
146
+ var detail = body.error;
147
+ return detail !== undefined && typeof detail.code === 'string' && detail.code !== ''
148
+ ? detail.code
149
+ : null;
150
+ };
151
+ return GraphMailErrorService;
152
+ }());
153
+ exports.GraphMailErrorService = GraphMailErrorService;
@@ -0,0 +1,32 @@
1
+ import type { MailAttachmentContentData } from '../../models/mail-attachment-content-data';
2
+ import type { MailAttachmentData } from '../../models/mail-attachment-data';
3
+ import type { MailSentData } from '../../models/mail-sent-data';
4
+ import type { GraphMailFolder } from '../constants/graph-mail-folder';
5
+ import type { GraphMailConnection } from '../models/graph-mail-connection';
6
+ import type { GraphMailDeltaData } from '../models/graph-mail-delta-data';
7
+ import type { GraphMailDeltaOptions } from '../models/graph-mail-delta-options';
8
+ import type { GraphMailDeltaPageData } from '../models/graph-mail-delta-page-data';
9
+ import type { GraphMailDraftData } from '../models/graph-mail-draft-data';
10
+ import type { GraphMailMessageData } from '../models/graph-mail-message-data';
11
+ import type { GraphMailReplyData } from '../models/graph-mail-reply-data';
12
+ export declare class GraphMailService {
13
+ static deltaPage(connection: GraphMailConnection, folder: GraphMailFolder, link: string | null, options?: GraphMailDeltaOptions): Promise<GraphMailDeltaPageData>;
14
+ static delta(connection: GraphMailConnection, folder: GraphMailFolder, deltaLink: string | null, options?: GraphMailDeltaOptions): Promise<GraphMailDeltaData>;
15
+ static getMessage(connection: GraphMailConnection, messageId: string): Promise<GraphMailMessageData>;
16
+ static listAttachments(connection: GraphMailConnection, messageId: string): Promise<MailAttachmentData[]>;
17
+ static downloadAttachment(connection: GraphMailConnection, messageId: string, attachmentId: string): Promise<MailAttachmentContentData>;
18
+ static sendMail(connection: GraphMailConnection, draft: GraphMailDraftData): Promise<void>;
19
+ static createAndSend(connection: GraphMailConnection, draft: GraphMailDraftData): Promise<MailSentData>;
20
+ static reply(connection: GraphMailConnection, messageId: string, reply: GraphMailReplyData): Promise<MailSentData>;
21
+ static setCategories(connection: GraphMailConnection, messageId: string, categories: string[]): Promise<void>;
22
+ private static _send;
23
+ private static _deltaStart;
24
+ private static _outgoing;
25
+ private static _recipients;
26
+ private static _attachment;
27
+ private static _sent;
28
+ private static _link;
29
+ private static _mailboxPath;
30
+ private static _messagePath;
31
+ private static _select;
32
+ }
@@ -0,0 +1,352 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ var __generator = (this && this.__generator) || function (thisArg, body) {
45
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
46
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
+ function verb(n) { return function (v) { return step([n, v]); }; }
48
+ function step(op) {
49
+ if (f) throw new TypeError("Generator is already executing.");
50
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
52
+ if (y = 0, t) op = [op[0] & 2, t.value];
53
+ switch (op[0]) {
54
+ case 0: case 1: t = op; break;
55
+ case 4: _.label++; return { value: op[1], done: false };
56
+ case 5: _.label++; y = op[1]; op = [0]; continue;
57
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
+ default:
59
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
+ if (t[2]) _.ops.pop();
64
+ _.trys.pop(); continue;
65
+ }
66
+ op = body.call(thisArg, _);
67
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
+ }
70
+ };
71
+ Object.defineProperty(exports, "__esModule", { value: true });
72
+ exports.GraphMailService = void 0;
73
+ var Attributes = __importStar(require("@pallasoft/ps-attributes"));
74
+ var ps_utilities_1 = require("@pallasoft/ps-utilities");
75
+ var mail_http_config_1 = require("../../constants/mail-http-config");
76
+ var mail_url_service_1 = require("../../services/mail-url-service");
77
+ var graph_mail_config_1 = require("../constants/graph-mail-config");
78
+ var graph_mail_error_reason_1 = require("../constants/graph-mail-error-reason");
79
+ var graph_mail_error_service_1 = require("./graph-mail-error-service");
80
+ var graph_request_service_1 = require("./graph-request-service");
81
+ var GraphMailService = /** @class */ (function () {
82
+ function GraphMailService() {
83
+ }
84
+ GraphMailService.deltaPage = function (connection_1, folder_1, link_1) {
85
+ return __awaiter(this, arguments, void 0, function (connection, folder, link, options) {
86
+ var page;
87
+ if (options === void 0) { options = {}; }
88
+ return __generator(this, function (_a) {
89
+ switch (_a.label) {
90
+ case 0: return [4 /*yield*/, graph_request_service_1.GraphRequestService.json(connection, {
91
+ method: ps_utilities_1.HttpMethod.Get,
92
+ url: link !== null ? link : this._deltaStart(connection, folder, options),
93
+ prefer: [
94
+ graph_mail_config_1.GraphMailConfig.prefer.textBody,
95
+ "".concat(graph_mail_config_1.GraphMailConfig.prefer.maxPageSize).concat(typeof options.pageSize === 'number' ? options.pageSize : graph_mail_config_1.GraphMailConfig.deltaPageSize),
96
+ ],
97
+ })];
98
+ case 1:
99
+ page = _a.sent();
100
+ return [2 /*return*/, {
101
+ messages: page.value.filter(function (item) { return item['@removed'] === undefined; }),
102
+ removedIds: page.value
103
+ .filter(function (item) { return item['@removed'] !== undefined; })
104
+ .map(function (item) { return item.id; }),
105
+ nextLink: this._link(page['@odata.nextLink']),
106
+ deltaLink: this._link(page['@odata.deltaLink']),
107
+ }];
108
+ }
109
+ });
110
+ });
111
+ };
112
+ GraphMailService.delta = function (connection_1, folder_1, deltaLink_1) {
113
+ return __awaiter(this, arguments, void 0, function (connection, folder, deltaLink, options) {
114
+ var messages, removedIds, link, page;
115
+ if (options === void 0) { options = {}; }
116
+ return __generator(this, function (_a) {
117
+ switch (_a.label) {
118
+ case 0:
119
+ messages = [];
120
+ removedIds = [];
121
+ link = deltaLink;
122
+ _a.label = 1;
123
+ case 1:
124
+ if (!true) return [3 /*break*/, 3];
125
+ return [4 /*yield*/, this.deltaPage(connection, folder, link, options)];
126
+ case 2:
127
+ page = _a.sent();
128
+ messages.push.apply(messages, page.messages);
129
+ removedIds.push.apply(removedIds, page.removedIds);
130
+ if (page.deltaLink !== null)
131
+ return [2 /*return*/, { messages: messages, removedIds: removedIds, deltaLink: page.deltaLink }];
132
+ if (page.nextLink === null)
133
+ throw graph_mail_error_service_1.GraphMailErrorService.create(graph_mail_error_reason_1.GraphMailErrorReason.Failed, Attributes.Common.Response.ErrorCodes.BadGateway);
134
+ link = page.nextLink;
135
+ return [3 /*break*/, 1];
136
+ case 3: return [2 /*return*/];
137
+ }
138
+ });
139
+ });
140
+ };
141
+ GraphMailService.getMessage = function (connection, messageId) {
142
+ return graph_request_service_1.GraphRequestService.json(connection, {
143
+ method: ps_utilities_1.HttpMethod.Get,
144
+ url: mail_url_service_1.MailUrlService.withQuery(this._messagePath(connection, messageId), [
145
+ this._select(graph_mail_config_1.GraphMailConfig.messageFields),
146
+ ]),
147
+ prefer: [graph_mail_config_1.GraphMailConfig.prefer.textBody],
148
+ });
149
+ };
150
+ GraphMailService.listAttachments = function (connection, messageId) {
151
+ return __awaiter(this, void 0, void 0, function () {
152
+ var attachments, url, page;
153
+ var _this = this;
154
+ return __generator(this, function (_a) {
155
+ switch (_a.label) {
156
+ case 0:
157
+ attachments = [];
158
+ url = mail_url_service_1.MailUrlService.withQuery(mail_url_service_1.MailUrlService.path(this._messagePath(connection, messageId), graph_mail_config_1.GraphMailConfig.paths.attachments), [this._select(graph_mail_config_1.GraphMailConfig.attachmentFields)]);
159
+ _a.label = 1;
160
+ case 1:
161
+ if (!(url !== null)) return [3 /*break*/, 3];
162
+ return [4 /*yield*/, graph_request_service_1.GraphRequestService.json(connection, { method: ps_utilities_1.HttpMethod.Get, url: url })];
163
+ case 2:
164
+ page = _a.sent();
165
+ attachments.push.apply(attachments, page.value.map(function (attachment) { return _this._attachment(attachment); }));
166
+ url = this._link(page['@odata.nextLink']);
167
+ return [3 /*break*/, 1];
168
+ case 3: return [2 /*return*/, attachments];
169
+ }
170
+ });
171
+ });
172
+ };
173
+ GraphMailService.downloadAttachment = function (connection, messageId, attachmentId) {
174
+ return __awaiter(this, void 0, void 0, function () {
175
+ var response, contentType;
176
+ var _a;
177
+ return __generator(this, function (_b) {
178
+ switch (_b.label) {
179
+ case 0: return [4 /*yield*/, graph_request_service_1.GraphRequestService.send(connection, {
180
+ method: ps_utilities_1.HttpMethod.Get,
181
+ url: mail_url_service_1.MailUrlService.path(this._messagePath(connection, messageId), graph_mail_config_1.GraphMailConfig.paths.attachments, encodeURIComponent(attachmentId), graph_mail_config_1.GraphMailConfig.paths.value),
182
+ })];
183
+ case 1:
184
+ response = _b.sent();
185
+ contentType = response.headers.get(ps_utilities_1.Common.HttpHeader.contentType);
186
+ _a = {};
187
+ return [4 /*yield*/, response.arrayBuffer()];
188
+ case 2: return [2 /*return*/, (_a.content = _b.sent(),
189
+ _a.contentType = contentType === null ? ps_utilities_1.Common.HttpContentType.octetStream : contentType,
190
+ _a)];
191
+ }
192
+ });
193
+ });
194
+ };
195
+ GraphMailService.sendMail = function (connection, draft) {
196
+ return __awaiter(this, void 0, void 0, function () {
197
+ var request;
198
+ return __generator(this, function (_a) {
199
+ switch (_a.label) {
200
+ case 0:
201
+ request = {
202
+ message: this._outgoing(draft),
203
+ saveToSentItems: graph_mail_config_1.GraphMailConfig.saveToSentItems,
204
+ };
205
+ return [4 /*yield*/, graph_request_service_1.GraphRequestService.send(connection, {
206
+ method: ps_utilities_1.HttpMethod.Post,
207
+ url: mail_url_service_1.MailUrlService.path(this._mailboxPath(connection), graph_mail_config_1.GraphMailConfig.paths.sendMail),
208
+ body: request,
209
+ })];
210
+ case 1:
211
+ _a.sent();
212
+ return [2 /*return*/];
213
+ }
214
+ });
215
+ });
216
+ };
217
+ GraphMailService.createAndSend = function (connection, draft) {
218
+ return __awaiter(this, void 0, void 0, function () {
219
+ var created;
220
+ return __generator(this, function (_a) {
221
+ switch (_a.label) {
222
+ case 0: return [4 /*yield*/, graph_request_service_1.GraphRequestService.json(connection, {
223
+ method: ps_utilities_1.HttpMethod.Post,
224
+ url: mail_url_service_1.MailUrlService.path(this._mailboxPath(connection), graph_mail_config_1.GraphMailConfig.paths.messages),
225
+ body: this._outgoing(draft),
226
+ })];
227
+ case 1:
228
+ created = _a.sent();
229
+ return [4 /*yield*/, this._send(connection, created.id)];
230
+ case 2:
231
+ _a.sent();
232
+ return [2 /*return*/, this._sent(created)];
233
+ }
234
+ });
235
+ });
236
+ };
237
+ GraphMailService.reply = function (connection, messageId, reply) {
238
+ return __awaiter(this, void 0, void 0, function () {
239
+ var request, created;
240
+ return __generator(this, function (_a) {
241
+ switch (_a.label) {
242
+ case 0:
243
+ request = { message: this._outgoing(reply) };
244
+ return [4 /*yield*/, graph_request_service_1.GraphRequestService.json(connection, {
245
+ method: ps_utilities_1.HttpMethod.Post,
246
+ url: mail_url_service_1.MailUrlService.path(this._messagePath(connection, messageId), graph_mail_config_1.GraphMailConfig.paths.createReply),
247
+ body: request,
248
+ })];
249
+ case 1:
250
+ created = _a.sent();
251
+ return [4 /*yield*/, this._send(connection, created.id)];
252
+ case 2:
253
+ _a.sent();
254
+ return [2 /*return*/, this._sent(created)];
255
+ }
256
+ });
257
+ });
258
+ };
259
+ GraphMailService.setCategories = function (connection, messageId, categories) {
260
+ return __awaiter(this, void 0, void 0, function () {
261
+ var request;
262
+ return __generator(this, function (_a) {
263
+ switch (_a.label) {
264
+ case 0:
265
+ request = { categories: categories };
266
+ return [4 /*yield*/, graph_request_service_1.GraphRequestService.send(connection, {
267
+ method: ps_utilities_1.HttpMethod.Patch,
268
+ url: this._messagePath(connection, messageId),
269
+ body: request,
270
+ })];
271
+ case 1:
272
+ _a.sent();
273
+ return [2 /*return*/];
274
+ }
275
+ });
276
+ });
277
+ };
278
+ GraphMailService._send = function (connection, messageId) {
279
+ return __awaiter(this, void 0, void 0, function () {
280
+ return __generator(this, function (_a) {
281
+ switch (_a.label) {
282
+ case 0: return [4 /*yield*/, graph_request_service_1.GraphRequestService.send(connection, {
283
+ method: ps_utilities_1.HttpMethod.Post,
284
+ url: mail_url_service_1.MailUrlService.path(this._messagePath(connection, messageId), graph_mail_config_1.GraphMailConfig.paths.send),
285
+ })];
286
+ case 1:
287
+ _a.sent();
288
+ return [2 /*return*/];
289
+ }
290
+ });
291
+ });
292
+ };
293
+ GraphMailService._deltaStart = function (connection, folder, options) {
294
+ var parameters = [this._select(graph_mail_config_1.GraphMailConfig.deltaFields)];
295
+ if (typeof options.receivedSince === 'string')
296
+ parameters.push({
297
+ key: graph_mail_config_1.GraphMailConfig.query.filter,
298
+ value: "".concat(graph_mail_config_1.GraphMailConfig.query.receivedSinceFilter).concat(options.receivedSince),
299
+ });
300
+ return mail_url_service_1.MailUrlService.withQuery(mail_url_service_1.MailUrlService.path(this._mailboxPath(connection), graph_mail_config_1.GraphMailConfig.paths.mailFolders, folder, graph_mail_config_1.GraphMailConfig.paths.delta), parameters);
301
+ };
302
+ GraphMailService._outgoing = function (message) {
303
+ var outgoing = { body: message.body };
304
+ if (message.subject !== undefined)
305
+ outgoing.subject = message.subject;
306
+ if (message.to !== undefined)
307
+ outgoing.toRecipients = this._recipients(message.to);
308
+ if (message.cc !== undefined)
309
+ outgoing.ccRecipients = this._recipients(message.cc);
310
+ if (message.replyTo !== undefined)
311
+ outgoing.replyTo = this._recipients(message.replyTo);
312
+ if ('headers' in message && message.headers !== undefined)
313
+ outgoing.internetMessageHeaders = message.headers;
314
+ return outgoing;
315
+ };
316
+ GraphMailService._recipients = function (addresses) {
317
+ return addresses.map(function (emailAddress) { return ({ emailAddress: emailAddress }); });
318
+ };
319
+ GraphMailService._attachment = function (attachment) {
320
+ return {
321
+ id: attachment.id,
322
+ fileName: attachment.name,
323
+ contentType: attachment.contentType,
324
+ sizeBytes: attachment.size,
325
+ inline: attachment.isInline,
326
+ };
327
+ };
328
+ GraphMailService._sent = function (message) {
329
+ return {
330
+ id: message.id,
331
+ conversationId: typeof message.conversationId === 'string' ? message.conversationId : null,
332
+ internetMessageId: typeof message.internetMessageId === 'string' ? message.internetMessageId : null,
333
+ };
334
+ };
335
+ GraphMailService._link = function (link) {
336
+ return typeof link === 'string' ? link : null;
337
+ };
338
+ GraphMailService._mailboxPath = function (connection) {
339
+ return mail_url_service_1.MailUrlService.path('', graph_mail_config_1.GraphMailConfig.paths.users, encodeURIComponent(connection.mailbox));
340
+ };
341
+ GraphMailService._messagePath = function (connection, messageId) {
342
+ return mail_url_service_1.MailUrlService.path(this._mailboxPath(connection), graph_mail_config_1.GraphMailConfig.paths.messages, encodeURIComponent(messageId));
343
+ };
344
+ GraphMailService._select = function (fields) {
345
+ return {
346
+ key: graph_mail_config_1.GraphMailConfig.query.select,
347
+ value: fields.join(mail_http_config_1.MailHttpConfig.listSeparator),
348
+ };
349
+ };
350
+ return GraphMailService;
351
+ }());
352
+ exports.GraphMailService = GraphMailService;
@@ -0,0 +1,11 @@
1
+ import type { GraphCredentials } from '../models/graph-credentials';
2
+ import type { GraphRequestData } from '../models/graph-request-data';
3
+ export declare class GraphRequestService {
4
+ static send(credentials: GraphCredentials, request: GraphRequestData): Promise<Response>;
5
+ static json<Value>(credentials: GraphCredentials, request: GraphRequestData): Promise<Value>;
6
+ static url(pathOrUrl: string): string;
7
+ private static _attempt;
8
+ private static _headers;
9
+ private static _waitSeconds;
10
+ private static _wait;
11
+ }