All files / Scripts/ui/getHeaders GetHeadersAPI.ts

26.92% Statements 7/26
0% Branches 0/6
0% Functions 0/5
26.92% Lines 7/26

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 599x 9x 9x 9x 9x                       9x 9x                                                                                  
import { GetHeaders } from "./GetHeaders";
import { diagnostics } from "../../Diag";
import { Errors } from "../../Errors";
import { mhaStrings } from "../../mhaStrings";
import { ParentFrame } from "../../ParentFrame";
 
/*
 * GetHeadersAPI.js
 *
 * This file has all the methods to get PR_TRANSPORT_MESSAGE_HEADERS
 * from the current message via getAllInternetHeadersAsync.
 *
 * Requirement Sets and Permissions
 * getAllInternetHeadersAsync requires 1.9 and ReadItem
 */
 
export class GetHeadersAPI {
    private static minAPISet = "1.9";
 
    public static canUseAPI(): boolean { return GetHeaders.canUseAPI("API", GetHeadersAPI.minAPISet); }
 
    private static async getAllInternetHeaders(item: Office.MessageRead): Promise<string> {
        return new Promise((resolve) => {
            item.getAllInternetHeadersAsync((asyncResult) => {
                if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
                    resolve(asyncResult.value);
                } else {
                    diagnostics.set("getAllInternetHeadersAsyncFailure", JSON.stringify(asyncResult));
                    Errors.log(asyncResult.error, "getAllInternetHeadersAsync failed.\nFallback to Rest.\n" + JSON.stringify(asyncResult, null, 2), true);
                    resolve("");
                }
            });
        });
    }
 
    public static async send(): Promise<string> {
        Iif (!GetHeaders.validItem() || !Office.context.mailbox.item) {
            Errors.logMessage("No item selected (API)");
            return "";
        }
 
        Iif (!GetHeadersAPI.canUseAPI()) {
            return "";
        }
 
        ParentFrame.updateStatus(mhaStrings.mhaRequestSent);
 
        try {
            const headers = await GetHeadersAPI.getAllInternetHeaders(Office.context.mailbox.item);
            return headers;
        }
        catch (e) {
            Errors.log(e, "Failed in getAllInternetHeadersAsync");
        }
 
        return "";
    }
}