Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 10x 10x 10x 10x 10x 10x 10x | import { GetHeadersAPI } from "./GetHeadersAPI";
import { GetHeadersEWS } from "./GetHeadersEWS";
import { GetHeadersRest } from "./GetHeadersRest";
import { diagnostics } from "../../Diag";
import { Errors } from "../../Errors";
import { ParentFrame } from "../../ParentFrame";
/*
* GetHeaders.js
*
* Selector for switching between EWS and Rest logic
*/
export class GetHeaders {
public static permissionLevel(): number {
if (typeof (Office) === "undefined") return 0;
if (!Office) return 0;
if (!Office.context) return 0;
if (!Office.context.mailbox) return 0;
// @ts-expect-error early version of initialData
if (Office.context.mailbox._initialData$p$0) return Office.context.mailbox._initialData$p$0._permissionLevel$p$0;
// @ts-expect-error initialData is missing from the type file
if (Office.context.mailbox.initialData) return Office.context.mailbox.initialData.permissionLevel;
return 0;
}
public static sufficientPermission(strict: boolean): boolean {
if (typeof (Office) === "undefined") return false;
if (!Office) return false;
if (!Office.context) return false;
if (!Office.context.mailbox) return false;
// In strict mode, we must find permissions to conclude we have them
// In non-strict mode, if we don't find permissions, we assume we might have them
// Some down level clients (such as we would use EWS on) don't have _initialData$p$0 or initialData at all.
// @ts-expect-error initialData is missing from the type file
if (!Office.context.mailbox._initialData$p$0 && !Office.context.mailbox.initialData) return !strict;
if (GetHeaders.permissionLevel() < 1) return false;
return true;
}
public static canUseAPI(apiType: string, minset: string): boolean {
// if (apiType === "API") { return false; }
// if (apiType === "Rest") { return false; }
if (typeof (Office) === "undefined") { diagnostics.set(`no${apiType}reason`, "Office undefined"); return false; }
if (!Office) { diagnostics.set(`no${apiType}reason`, "Office false"); return false; }
if (!Office.context) { diagnostics.set("noUseRestReason", "context false"); return false; }
if (!Office.context.requirements) { diagnostics.set("noUseRestReason", "requirements false"); return false; }
if (!Office.context.requirements.isSetSupported("Mailbox", minset)) { diagnostics.set(`no${apiType}reason`, "requirements too low"); return false; }
if (!GetHeaders.sufficientPermission(true)) { diagnostics.set(`no${apiType}reason`, "sufficientPermission false"); return false; }
if (!Office.context.mailbox) { diagnostics.set(`no${apiType}reason`, "mailbox false"); return false; }
if (!Office.context.mailbox.getCallbackTokenAsync) { diagnostics.set(`no${apiType}reason`, "getCallbackTokenAsync false"); return false; }
return true;
}
public static validItem(): boolean {
if (typeof (Office) === "undefined") return false;
if (!Office) return false;
if (!Office.context) return false;
if (!Office.context.mailbox) return false;
if (!Office.context.mailbox.item) return false;
if (!Office.context.mailbox.item.itemId) return false;
return true;
}
public static async send(headersLoadedCallback: (_headers: string, apiUsed: string) => void) {
if (!GetHeaders.validItem()) {
ParentFrame.showError(null, "No item selected", true);
return;
}
if (!GetHeaders.sufficientPermission(false)) {
ParentFrame.showError(null, "Insufficient permissions to request headers", false);
return;
}
try {
let headers:string = await GetHeadersAPI.send();
if (headers !== "") {
headersLoadedCallback(headers, "API");
return;
}
Errors.logMessage("API failed, trying REST");
headers = await GetHeadersRest.send();
if (headers !== "") {
headersLoadedCallback(headers, "REST");
return;
}
Errors.logMessage("REST failed, trying EWS");
headers = await GetHeadersEWS.send();
if (headers !== "") {
headersLoadedCallback(headers, "EWS");
return;
}
} catch (e) {
ParentFrame.showError(e, "Could not send header request");
}
}
}
|