UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

62 lines (61 loc) 3.04 kB
/** * Generated from ChatGPT Query... UNTESTED as of 2024-09-29. * NOTE: * * Usage: getFormDigestValue("https://your-sharepoint-site-url") .then(digestValue => { console.log("Form Digest Value:", digestValue); }); * Fetch Request NOTES: Method: A POST request is sent to retrieve the context information. Headers: The request includes the Accept and Content-Type headers. If you're already on a SharePoint page and have access to the __REQUESTDIGEST hidden input, you can include it, though it's optional for this request. Credentials: Setting credentials: 'include' ensures that cookies (authentication) are sent with the request. * * @param siteUrl * @returns */ import { check4This } from "../../../../../logic/Links/CheckSearch"; import { startPerformOpV2, updatePerformanceEndV2 } from "../../../Performance/functions"; export async function getFormDigestValueNoContext(siteUrl) { var _a; const contextInfoUrl = `${siteUrl}/_api/contextinfo`; const XRequestDigest = document.getElementById("__REQUESTDIGEST"); let thisPerformance = startPerformOpV2({ label: `DIGEST ${siteUrl}`, includeMsStr: true }); try { const response = await fetch(contextInfoUrl, { method: 'POST', headers: { 'Accept': 'application/json;odata=verbose', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'X-RequestDigest': XRequestDigest ? XRequestDigest : '', // Optional, if available in the context }, credentials: 'include' // Include credentials for authentication }); if (!response.ok) { throw new Error(`Error: ${response.statusText}`); } const data = await response.json(); /** * Made this check just to be absolutely sure it gets any FormDigestValue there. * My original SiteEULA code just had data.FormDigestValue. * ChatGPT then said it's under the GetContextWebInformation object */ let FormDigestValue = data.FormDigestValue ? data.FormDigestValue : ''; if (!FormDigestValue && data.GetContextWebInformation) FormDigestValue = data.GetContextWebInformation.FormDigestValue; if (!FormDigestValue && ((_a = data.d) === null || _a === void 0 ? void 0 : _a.GetContextWebInformation)) FormDigestValue = data.d.GetContextWebInformation.FormDigestValue; thisPerformance = updatePerformanceEndV2({ op: thisPerformance, updateMiliseconds: true, count: 1 }); if (check4This(`fpsShowDigestValue=true`) === true) { console.log(`fps-core-v7 COMPLETE: getFormDigestValueNoContext ~ 61`, thisPerformance); } ; return FormDigestValue; } catch (error) { console.error("Failed to get form digest value:", error); return ''; } } //# sourceMappingURL=getFormDigestValueNoContext.js.map