@dxv-systems/mail-graph 0.0.0-stage → 0.1.1
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.
- package/README.md +200 -2
- package/dist/auth.d.ts +41 -0
- package/dist/auth.js +184 -0
- package/dist/client.d.ts +18 -0
- package/dist/client.js +34 -0
- package/dist/errors.d.ts +25 -0
- package/dist/errors.js +35 -0
- package/dist/http.d.ts +44 -0
- package/dist/http.js +182 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +14 -0
- package/dist/limiter.d.ts +7 -0
- package/dist/limiter.js +29 -0
- package/dist/links.d.ts +2 -0
- package/dist/links.js +25 -0
- package/dist/mailbox.d.ts +41 -0
- package/dist/mailbox.js +429 -0
- package/dist/setup/access.d.ts +20 -0
- package/dist/setup/access.js +71 -0
- package/dist/setup/address.d.ts +32 -0
- package/dist/setup/address.js +69 -0
- package/dist/setup/consent.d.ts +40 -0
- package/dist/setup/consent.js +78 -0
- package/dist/setup/constants.d.ts +2 -0
- package/dist/setup/constants.js +5 -0
- package/dist/setup/exchange-graph.d.ts +23 -0
- package/dist/setup/exchange-graph.js +119 -0
- package/dist/setup/exchange-script.d.ts +1 -0
- package/dist/setup/exchange-script.js +190 -0
- package/dist/setup/handlers.d.ts +30 -0
- package/dist/setup/handlers.js +571 -0
- package/dist/setup/index.d.ts +18 -0
- package/dist/setup/index.js +38 -0
- package/dist/setup/it-link.d.ts +17 -0
- package/dist/setup/it-link.js +17 -0
- package/dist/setup/messages.d.ts +22 -0
- package/dist/setup/messages.js +36 -0
- package/dist/setup/react.d.ts +8 -0
- package/dist/setup/react.js +451 -0
- package/dist/setup/script.d.ts +27 -0
- package/dist/setup/script.js +74 -0
- package/dist/setup/signing.d.ts +18 -0
- package/dist/setup/signing.js +56 -0
- package/dist/setup/tenant.d.ts +38 -0
- package/dist/setup/tenant.js +45 -0
- package/dist/setup/types.d.ts +76 -0
- package/dist/setup/types.js +2 -0
- package/dist/types.d.ts +98 -0
- package/dist/types.js +2 -0
- package/dist/util.d.ts +4 -0
- package/dist/util.js +17 -0
- package/package.json +68 -4
package/dist/http.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHttp = createHttp;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
const links_1 = require("./links");
|
|
6
|
+
const limiter_1 = require("./limiter");
|
|
7
|
+
const util_1 = require("./util");
|
|
8
|
+
const ALLOWED_METHODS = new Set(["GET", "POST", "PATCH"]);
|
|
9
|
+
const PREFER_IMMUTABLE = 'IdType="ImmutableId"';
|
|
10
|
+
function createHttp(options) {
|
|
11
|
+
const sleep = options.sleep ?? util_1.defaultSleep;
|
|
12
|
+
const now = options.now ?? Date.now;
|
|
13
|
+
const maxAttempts = Math.max(1, options.maxAttempts ?? 4);
|
|
14
|
+
const maxWait = options.maxRetryWaitMs ?? 20000;
|
|
15
|
+
const baseBackoff = options.baseBackoffMs ?? 1000;
|
|
16
|
+
const timeoutMs = options.requestTimeoutMs ?? 30000;
|
|
17
|
+
const perMailbox = options.maxConcurrentPerMailbox ?? 4;
|
|
18
|
+
const limiters = new Map();
|
|
19
|
+
function limiterFor(mailbox) {
|
|
20
|
+
const key = mailbox.toLowerCase();
|
|
21
|
+
let limiter = limiters.get(key);
|
|
22
|
+
if (!limiter) {
|
|
23
|
+
limiter = new limiter_1.Limiter(perMailbox);
|
|
24
|
+
limiters.set(key, limiter);
|
|
25
|
+
}
|
|
26
|
+
return limiter;
|
|
27
|
+
}
|
|
28
|
+
async function failure(response, retryAfterMs, deltaAware) {
|
|
29
|
+
let code = `HTTP${response.status}`;
|
|
30
|
+
const parsed = (await response.json().catch(() => null));
|
|
31
|
+
if (parsed && typeof parsed.error?.code === "string" && parsed.error.code)
|
|
32
|
+
code = parsed.error.code;
|
|
33
|
+
const init = {
|
|
34
|
+
message: `Graph request failed (${response.status} ${code}).`,
|
|
35
|
+
status: response.status,
|
|
36
|
+
code,
|
|
37
|
+
requestId: response.headers.get("request-id") ?? undefined,
|
|
38
|
+
retryAfterMs: retryAfterMs ?? undefined,
|
|
39
|
+
};
|
|
40
|
+
if (deltaAware && (response.status === 410 || code === "syncStateNotFound" || code === "resyncRequired")) {
|
|
41
|
+
return new errors_1.GraphDeltaExpiredError(init);
|
|
42
|
+
}
|
|
43
|
+
return new errors_1.GraphError(init);
|
|
44
|
+
}
|
|
45
|
+
async function execute(args) {
|
|
46
|
+
const limiter = limiterFor(args.mailbox);
|
|
47
|
+
let refreshedAfter401 = false;
|
|
48
|
+
for (let attempt = 1;; attempt++) {
|
|
49
|
+
const token = args.authenticated ? await options.getToken() : null;
|
|
50
|
+
const headers = {
|
|
51
|
+
Accept: "application/json",
|
|
52
|
+
Prefer: [PREFER_IMMUTABLE, ...args.prefer].join(", "),
|
|
53
|
+
...args.contentHeaders,
|
|
54
|
+
};
|
|
55
|
+
if (token)
|
|
56
|
+
headers.Authorization = `Bearer ${token}`;
|
|
57
|
+
const outcome = await limiter.run(async () => {
|
|
58
|
+
let response;
|
|
59
|
+
try {
|
|
60
|
+
response = await options.fetch(args.url, {
|
|
61
|
+
method: args.method,
|
|
62
|
+
headers,
|
|
63
|
+
body: args.body,
|
|
64
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (cause) {
|
|
68
|
+
throw new errors_1.GraphError({ message: "Graph request failed: network error or timeout.", status: 0, code: "NetworkError", cause });
|
|
69
|
+
}
|
|
70
|
+
if (response.ok) {
|
|
71
|
+
if (args.binary) {
|
|
72
|
+
return { kind: "done", response, data: undefined, bytes: new Uint8Array(await response.arrayBuffer()) };
|
|
73
|
+
}
|
|
74
|
+
const text = await response.text();
|
|
75
|
+
let data;
|
|
76
|
+
if (text !== "") {
|
|
77
|
+
try {
|
|
78
|
+
data = JSON.parse(text);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
throw new errors_1.GraphError({
|
|
82
|
+
message: `Graph returned a non-JSON body (${response.status}).`,
|
|
83
|
+
status: response.status,
|
|
84
|
+
code: "InvalidResponse",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { kind: "done", response, data, bytes: undefined };
|
|
89
|
+
}
|
|
90
|
+
const retryAfterMs = (0, util_1.parseRetryAfter)(response.headers.get("Retry-After"), now());
|
|
91
|
+
const retryable = response.status === 429 || (response.status === 503 && args.retryOn503);
|
|
92
|
+
if (retryable && attempt < maxAttempts && (retryAfterMs === null || retryAfterMs <= maxWait)) {
|
|
93
|
+
await response.body?.cancel().catch(() => undefined);
|
|
94
|
+
return { kind: "retry", waitMs: retryAfterMs ?? Math.min(baseBackoff * 2 ** (attempt - 1), maxWait) };
|
|
95
|
+
}
|
|
96
|
+
if (response.status === 401 && token && !refreshedAfter401) {
|
|
97
|
+
await response.body?.cancel().catch(() => undefined);
|
|
98
|
+
return { kind: "reauth" };
|
|
99
|
+
}
|
|
100
|
+
throw await failure(response, retryAfterMs, args.url.includes("/messages/delta"));
|
|
101
|
+
});
|
|
102
|
+
if (outcome.kind === "reauth") {
|
|
103
|
+
refreshedAfter401 = true;
|
|
104
|
+
options.invalidateToken(token);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (outcome.kind === "retry") {
|
|
108
|
+
await sleep(outcome.waitMs);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
return { status: outcome.response.status, data: outcome.data, bytes: outcome.bytes };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
async request(spec) {
|
|
116
|
+
if (!ALLOWED_METHODS.has(spec.method)) {
|
|
117
|
+
throw new errors_1.GraphUsageError("MethodNotAllowed", `HTTP method ${String(spec.method)} is not allowed; this package only sends GET, POST and PATCH.`);
|
|
118
|
+
}
|
|
119
|
+
if ((spec.path === undefined) === (spec.link === undefined)) {
|
|
120
|
+
throw new errors_1.GraphUsageError("InvalidArgument", "Exactly one of path and link is required.");
|
|
121
|
+
}
|
|
122
|
+
if (spec.path !== undefined && !spec.path.startsWith("/")) {
|
|
123
|
+
throw new errors_1.GraphUsageError("InvalidArgument", "path must start with /.");
|
|
124
|
+
}
|
|
125
|
+
const url = (0, links_1.assertGraphUrl)(spec.link ?? `${links_1.GRAPH_BASE}${spec.path}`);
|
|
126
|
+
const hasBody = spec.body !== undefined;
|
|
127
|
+
return execute({
|
|
128
|
+
mailbox: spec.mailbox,
|
|
129
|
+
method: spec.method,
|
|
130
|
+
url,
|
|
131
|
+
prefer: spec.prefer ?? [],
|
|
132
|
+
body: hasBody ? JSON.stringify(spec.body) : undefined,
|
|
133
|
+
contentHeaders: hasBody ? { "Content-Type": "application/json" } : {},
|
|
134
|
+
authenticated: true,
|
|
135
|
+
binary: spec.binary === true,
|
|
136
|
+
retryOn503: spec.retryOn503 !== false,
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
async deleteMessage(spec) {
|
|
140
|
+
if (!/^\/users\/[^/?#]+\/messages\/[^/?#]+$/.test(spec.path)) {
|
|
141
|
+
throw new errors_1.GraphUsageError("InvalidArgument", "deleteMessage only accepts /users/{mailbox}/messages/{id}.");
|
|
142
|
+
}
|
|
143
|
+
return execute({
|
|
144
|
+
mailbox: spec.mailbox,
|
|
145
|
+
method: "DELETE",
|
|
146
|
+
url: (0, links_1.assertGraphUrl)(`${links_1.GRAPH_BASE}${spec.path}`),
|
|
147
|
+
prefer: [],
|
|
148
|
+
contentHeaders: {},
|
|
149
|
+
authenticated: true,
|
|
150
|
+
binary: false,
|
|
151
|
+
retryOn503: true,
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
async uploadChunk(spec) {
|
|
155
|
+
let target;
|
|
156
|
+
try {
|
|
157
|
+
target = new URL(spec.uploadUrl);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
throw new errors_1.GraphUsageError("InvalidLink", "Upload URL is not a valid URL.");
|
|
161
|
+
}
|
|
162
|
+
if (target.protocol !== "https:" || target.username !== "" || target.password !== "") {
|
|
163
|
+
throw new errors_1.GraphUsageError("InvalidLink", "Upload URL must be an https URL.");
|
|
164
|
+
}
|
|
165
|
+
const end = spec.start + spec.bytes.byteLength - 1;
|
|
166
|
+
return execute({
|
|
167
|
+
mailbox: spec.mailbox,
|
|
168
|
+
method: "PUT",
|
|
169
|
+
url: target.toString(),
|
|
170
|
+
prefer: [],
|
|
171
|
+
body: spec.bytes,
|
|
172
|
+
contentHeaders: {
|
|
173
|
+
"Content-Type": "application/octet-stream",
|
|
174
|
+
"Content-Range": `bytes ${spec.start}-${end}/${spec.total}`,
|
|
175
|
+
},
|
|
176
|
+
authenticated: false,
|
|
177
|
+
binary: false,
|
|
178
|
+
retryOn503: true,
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { createGraphClient } from "./client";
|
|
2
|
+
export type { GraphClient, GraphClientOptions } from "./client";
|
|
3
|
+
export { readTokenIdentity } from "./auth";
|
|
4
|
+
export type { ClientCredentials, FederationRetryOptions, TokenIdentity } from "./auth";
|
|
5
|
+
export { LARGE_ATTACHMENT_THRESHOLD } from "./mailbox";
|
|
6
|
+
export type { MailboxClient } from "./mailbox";
|
|
7
|
+
export { GraphAuthError, GraphDeltaExpiredError, GraphError, GraphUsageError } from "./errors";
|
|
8
|
+
export type { GraphErrorInit, UsageErrorCode } from "./errors";
|
|
9
|
+
export type { AccessCheckResult, AccessStatus, AttachmentInfo, AttachmentKind, BodyType, CreateReplyOptions, DeltaItem, DeltaOptions, DeltaPage, ForwardInput, GetMessageOptions, InternetMessageHeader, MailAddress, MailFolder, Message, MessageBody, MessageSummary, OutgoingAttachment, Recipient, SendMailInput, } from "./types";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GraphUsageError = exports.GraphError = exports.GraphDeltaExpiredError = exports.GraphAuthError = exports.LARGE_ATTACHMENT_THRESHOLD = exports.readTokenIdentity = exports.createGraphClient = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "createGraphClient", { enumerable: true, get: function () { return client_1.createGraphClient; } });
|
|
6
|
+
var auth_1 = require("./auth");
|
|
7
|
+
Object.defineProperty(exports, "readTokenIdentity", { enumerable: true, get: function () { return auth_1.readTokenIdentity; } });
|
|
8
|
+
var mailbox_1 = require("./mailbox");
|
|
9
|
+
Object.defineProperty(exports, "LARGE_ATTACHMENT_THRESHOLD", { enumerable: true, get: function () { return mailbox_1.LARGE_ATTACHMENT_THRESHOLD; } });
|
|
10
|
+
var errors_1 = require("./errors");
|
|
11
|
+
Object.defineProperty(exports, "GraphAuthError", { enumerable: true, get: function () { return errors_1.GraphAuthError; } });
|
|
12
|
+
Object.defineProperty(exports, "GraphDeltaExpiredError", { enumerable: true, get: function () { return errors_1.GraphDeltaExpiredError; } });
|
|
13
|
+
Object.defineProperty(exports, "GraphError", { enumerable: true, get: function () { return errors_1.GraphError; } });
|
|
14
|
+
Object.defineProperty(exports, "GraphUsageError", { enumerable: true, get: function () { return errors_1.GraphUsageError; } });
|
package/dist/limiter.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Limiter = void 0;
|
|
4
|
+
class Limiter {
|
|
5
|
+
constructor(max) {
|
|
6
|
+
this.max = max;
|
|
7
|
+
this.active = 0;
|
|
8
|
+
this.waiting = [];
|
|
9
|
+
}
|
|
10
|
+
async run(fn) {
|
|
11
|
+
if (this.active >= this.max) {
|
|
12
|
+
await new Promise((resolve) => this.waiting.push(resolve));
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
this.active++;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
return await fn();
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
const next = this.waiting.shift();
|
|
22
|
+
if (next)
|
|
23
|
+
next();
|
|
24
|
+
else
|
|
25
|
+
this.active--;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.Limiter = Limiter;
|
package/dist/links.d.ts
ADDED
package/dist/links.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GRAPH_BASE = void 0;
|
|
4
|
+
exports.assertGraphUrl = assertGraphUrl;
|
|
5
|
+
const errors_1 = require("./errors");
|
|
6
|
+
const GRAPH_ORIGIN = "https://graph.microsoft.com";
|
|
7
|
+
exports.GRAPH_BASE = `${GRAPH_ORIGIN}/v1.0`;
|
|
8
|
+
function assertGraphUrl(raw) {
|
|
9
|
+
let url;
|
|
10
|
+
try {
|
|
11
|
+
url = new URL(raw);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
throw new errors_1.GraphUsageError("InvalidLink", "Link is not a valid URL.");
|
|
15
|
+
}
|
|
16
|
+
if (url.protocol !== "https:" ||
|
|
17
|
+
url.hostname !== "graph.microsoft.com" ||
|
|
18
|
+
url.port !== "" ||
|
|
19
|
+
url.username !== "" ||
|
|
20
|
+
url.password !== "" ||
|
|
21
|
+
!url.pathname.startsWith("/v1.0/")) {
|
|
22
|
+
throw new errors_1.GraphUsageError("InvalidLink", "Link must be an https URL on graph.microsoft.com under /v1.0/.");
|
|
23
|
+
}
|
|
24
|
+
return url.toString();
|
|
25
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Http } from "./http";
|
|
2
|
+
import type { AccessCheckResult, AttachmentInfo, CreateReplyOptions, DeltaOptions, DeltaPage, ForwardInput, GetMessageOptions, MailFolder, Message, MessageBody, OutgoingAttachment, SendMailInput } from "./types";
|
|
3
|
+
export declare const LARGE_ATTACHMENT_THRESHOLD: number;
|
|
4
|
+
export interface MailboxClient {
|
|
5
|
+
readonly address: string;
|
|
6
|
+
checkAccess(): Promise<{
|
|
7
|
+
status: AccessCheckResult["status"];
|
|
8
|
+
}>;
|
|
9
|
+
delta(options: DeltaOptions): Promise<DeltaPage>;
|
|
10
|
+
getMessage(id: string, options?: GetMessageOptions): Promise<Message | null>;
|
|
11
|
+
listAttachments(messageId: string): Promise<AttachmentInfo[]>;
|
|
12
|
+
downloadAttachment(messageId: string, attachmentId: string): Promise<Uint8Array>;
|
|
13
|
+
createReply(messageId: string, options?: CreateReplyOptions): Promise<Message>;
|
|
14
|
+
updateDraftBody(draftId: string, body: MessageBody): Promise<void>;
|
|
15
|
+
addAttachment(draftId: string, attachment: OutgoingAttachment): Promise<{
|
|
16
|
+
id?: string;
|
|
17
|
+
}>;
|
|
18
|
+
sendDraft(draftId: string): Promise<void>;
|
|
19
|
+
markRead(messageId: string, isRead?: boolean): Promise<void>;
|
|
20
|
+
setCategories(messageId: string, categories: string[]): Promise<void>;
|
|
21
|
+
listFolders(parent: "inbox" | "root"): Promise<MailFolder[]>;
|
|
22
|
+
getInboxFolderId(): Promise<string>;
|
|
23
|
+
createInboxFolder(displayName: string): Promise<MailFolder>;
|
|
24
|
+
moveMessage(messageId: string, destinationFolderId: string): Promise<{
|
|
25
|
+
id: string;
|
|
26
|
+
}>;
|
|
27
|
+
forward(messageId: string, input: ForwardInput): Promise<void>;
|
|
28
|
+
deleteDraft(draftId: string): Promise<{
|
|
29
|
+
status: "deleted" | "gone" | "not_draft";
|
|
30
|
+
}>;
|
|
31
|
+
sendMail(input: SendMailInput): Promise<void>;
|
|
32
|
+
getMailboxPurpose(): Promise<string | null>;
|
|
33
|
+
listInboxRules(): Promise<InboxRuleInfo[] | null>;
|
|
34
|
+
}
|
|
35
|
+
export interface InboxRuleInfo {
|
|
36
|
+
name: string;
|
|
37
|
+
enabled: boolean;
|
|
38
|
+
movesOut: boolean;
|
|
39
|
+
forwards: boolean;
|
|
40
|
+
}
|
|
41
|
+
export declare function createMailboxClient(http: Http, address: string): MailboxClient;
|