UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

73 lines 2.74 kB
import { normalizeGuid } from "../../helpers/strings"; import { isNullOrEmptyString } from "../../helpers/typecheckers"; import { GetJsonSync, GetJson } from "../rest"; import { hasGlobalContext, GetRestBaseUrl } from "./common"; import { GetSiteIdSync, GetSiteId } from "./web"; export function getFormDigest(serverRelativeWebUrl, async = false) { if (async) { return GetContextWebInformation(serverRelativeWebUrl).then(contextWebInformation => { return contextWebInformation && contextWebInformation.FormDigestValue || null; }); } else { let contextWebInformation = GetContextWebInformationSync(serverRelativeWebUrl); return contextWebInformation && contextWebInformation.FormDigestValue || null; } } export function GetContextWebInformationSync(siteUrl) { var siteId = null; if (hasGlobalContext() && _spPageContextInfo && _spPageContextInfo.isAppWeb) { //inside an app web you can't get the contextinfo for any other site siteUrl = _spPageContextInfo.webServerRelativeUrl; siteId = _spPageContextInfo.siteId; } else { siteId = GetSiteIdSync(siteUrl); if (isNullOrEmptyString(siteId)) { return null; } } let result = GetJsonSync(`${GetRestBaseUrl(siteUrl)}/contextinfo`, null, { method: "POST", maxAge: 5 * 60, includeDigestInPost: false, allowCache: true, postCacheKey: `GetContextWebInformation_${normalizeGuid(siteId)}`, spWebUrl: siteUrl //allow getDigest to work when not in SharePoint }); if (result && result.success) { return result.result.d.GetContextWebInformation; } else { return null; } } export async function GetContextWebInformation(siteUrl) { var siteId = null; if (hasGlobalContext() && _spPageContextInfo && _spPageContextInfo.isAppWeb) { //inside an app web you can't get the contextinfo for any other site siteUrl = _spPageContextInfo.webServerRelativeUrl; siteId = _spPageContextInfo.siteId; } else { siteId = await GetSiteId(siteUrl); if (isNullOrEmptyString(siteId)) { return null; } } try { let result = await GetJson(`${GetRestBaseUrl(siteUrl)}/contextinfo`, null, { method: "POST", maxAge: 5 * 60, includeDigestInPost: false, allowCache: true, postCacheKey: `GetContextWebInformation_${normalizeGuid(siteId)}`, spWebUrl: siteUrl //allow getDigest to work when not in SharePoint }); return result.d.GetContextWebInformation; } catch { return null; } } //# sourceMappingURL=digest.js.map