All files / Scripts/ui/getHeaders GetHeaders.ts

7.52% Statements 7/93
0% Branches 0/33
0% Functions 0/5
11.86% Lines 7/59

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 1019x 9x 9x 9x 9x 9x               9x                                                                                                                                                                              
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 {
        Iif (typeof (Office) === "undefined") return 0;
        Iif (!Office) return 0;
        Iif (!Office.context) return 0;
        Iif (!Office.context.mailbox) return 0;
        // @ts-expect-error early version of initialData
        Iif (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
        Iif (Office.context.mailbox.initialData) return Office.context.mailbox.initialData.permissionLevel;
        return 0;
    }
 
    public static sufficientPermission(strict: boolean): boolean {
        Iif (typeof (Office) === "undefined") return false;
        Iif (!Office) return false;
        Iif (!Office.context) return false;
        Iif (!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
        Iif (!Office.context.mailbox._initialData$p$0 && !Office.context.mailbox.initialData) return !strict;
        Iif (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; }
        Iif (typeof (Office) === "undefined") { diagnostics.set(`no${apiType}reason`, "Office undefined"); return false; }
        Iif (!Office) { diagnostics.set(`no${apiType}reason`, "Office false"); return false; }
        Iif (!Office.context) { diagnostics.set("noUseRestReason", "context false"); return false; }
        Iif (!Office.context.requirements) { diagnostics.set("noUseRestReason", "requirements false"); return false; }
        Iif (!Office.context.requirements.isSetSupported("Mailbox", minset)) { diagnostics.set(`no${apiType}reason`, "requirements too low"); return false; }
        Iif (!GetHeaders.sufficientPermission(true)) { diagnostics.set(`no${apiType}reason`, "sufficientPermission false"); return false; }
        Iif (!Office.context.mailbox) { diagnostics.set(`no${apiType}reason`, "mailbox false"); return false; }
        Iif (!Office.context.mailbox.getCallbackTokenAsync) { diagnostics.set(`no${apiType}reason`, "getCallbackTokenAsync false"); return false; }
        return true;
    }
 
    public static validItem(): boolean {
        Iif (typeof (Office) === "undefined") return false;
        Iif (!Office) return false;
        Iif (!Office.context) return false;
        Iif (!Office.context.mailbox) return false;
        Iif (!Office.context.mailbox.item) return false;
        Iif (!Office.context.mailbox.item.itemId) return false;
        return true;
    }
 
    public static async send(headersLoadedCallback: (_headers: string, apiUsed: string) => void) {
        Iif (!GetHeaders.validItem()) {
            ParentFrame.showError(null, "No item selected", true);
            return;
        }
 
        Iif (!GetHeaders.sufficientPermission(false)) {
            ParentFrame.showError(null, "Insufficient permissions to request headers", false);
            return;
        }
 
        try {
            let headers:string = await GetHeadersAPI.send();
            Iif (headers !== "") {
                headersLoadedCallback(headers, "API");
                return;
            }
 
            Errors.logMessage("API failed, trying REST");
            headers = await GetHeadersRest.send();
            Iif (headers !== "") {
                headersLoadedCallback(headers, "REST");
                return;
            }
 
            Errors.logMessage("REST failed, trying EWS");
            headers = await GetHeadersEWS.send();
            Iif (headers !== "") {
                headersLoadedCallback(headers, "EWS");
                return;
            }
        } catch (e) {
            ParentFrame.showError(e, "Could not send header request");
        }
    }
}