@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
78 lines • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFormDigest = getFormDigest;
exports.GetContextWebInformationSync = GetContextWebInformationSync;
exports.GetContextWebInformation = GetContextWebInformation;
const strings_1 = require("../../helpers/strings");
const typecheckers_1 = require("../../helpers/typecheckers");
const rest_1 = require("../rest");
const common_1 = require("./common");
const web_1 = require("./web");
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;
}
}
function GetContextWebInformationSync(siteUrl) {
var siteId = null;
if ((0, common_1.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 = (0, web_1.GetSiteIdSync)(siteUrl);
if ((0, typecheckers_1.isNullOrEmptyString)(siteId)) {
return null;
}
}
let result = (0, rest_1.GetJsonSync)(`${(0, common_1.GetRestBaseUrl)(siteUrl)}/contextinfo`, null, {
method: "POST",
maxAge: 5 * 60,
includeDigestInPost: false,
allowCache: true,
postCacheKey: `GetContextWebInformation_${(0, strings_1.normalizeGuid)(siteId)}`,
spWebUrl: siteUrl //allow getDigest to work when not in SharePoint
});
if (result && result.success) {
return result.result.d.GetContextWebInformation;
}
else {
return null;
}
}
async function GetContextWebInformation(siteUrl) {
var siteId = null;
if ((0, common_1.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 (0, web_1.GetSiteId)(siteUrl);
if ((0, typecheckers_1.isNullOrEmptyString)(siteId)) {
return null;
}
}
try {
let result = await (0, rest_1.GetJson)(`${(0, common_1.GetRestBaseUrl)(siteUrl)}/contextinfo`, null, {
method: "POST",
maxAge: 5 * 60,
includeDigestInPost: false,
allowCache: true,
postCacheKey: `GetContextWebInformation_${(0, strings_1.normalizeGuid)(siteId)}`,
spWebUrl: siteUrl //allow getDigest to work when not in SharePoint
});
return result.d.GetContextWebInformation;
}
catch {
return null;
}
}
//# sourceMappingURL=digest.js.map