@pnp/sp
Version: 
pnp - provides a fluent api for working with SharePoint REST
56 lines • 2.75 kB
JavaScript
import { combine, dateAdd, hOP, isFunc, objectDefinedNotNull } from "@pnp/core";
import { JSONParse } from "@pnp/queryable";
import { extractWebUrl } from "../utils/extract-web-url.js";
import { SPQueryable, spPost } from "../spqueryable.js";
import { BatchNever } from "../batching.js";
function clearExpired(digest) {
    const now = new Date();
    return !objectDefinedNotNull(digest) || (now > digest.expiration) ? null : digest;
}
// allows for the caching of digests across all calls which each have their own IDigestInfo wrapper.
const digests = new Map();
export function RequestDigest(hook) {
    return (instance) => {
        instance.on.pre(async function (url, init, result) {
            // add the request to the auth moment of the timeline
            this.on.auth(async (url, init) => {
                // eslint-disable-next-line max-len
                if (/get/i.test(init.method) || (init.headers && (hOP(init.headers, "X-RequestDigest") || hOP(init.headers, "Authorization") || hOP(init.headers, "X-PnPjs-NoDigest")))) {
                    return [url, init];
                }
                const urlAsString = url.toString();
                const webUrl = extractWebUrl(urlAsString);
                // do we have one in the cache that is still valid
                // from #2186 we need to always ensure the digest we get isn't expired
                let digest = clearExpired(digests.get(webUrl));
                if (!objectDefinedNotNull(digest) && isFunc(hook)) {
                    digest = clearExpired(hook(urlAsString, init));
                }
                if (!objectDefinedNotNull(digest)) {
                    digest = await spPost(SPQueryable([this, combine(webUrl, "_api/contextinfo")]).using(JSONParse(), BatchNever()), {
                        headers: {
                            "Accept": "application/json",
                            "X-PnPjs-NoDigest": "1",
                        },
                    }).then(p => ({
                        expiration: dateAdd(new Date(), "second", p.FormDigestTimeoutSeconds),
                        value: p.FormDigestValue,
                    }));
                }
                if (objectDefinedNotNull(digest)) {
                    // if we got a digest, set it in the headers
                    init.headers = {
                        "X-RequestDigest": digest.value,
                        ...init.headers,
                    };
                    // and cache it for future requests
                    digests.set(webUrl, digest);
                }
                return [url, init];
            });
            return [url, init, result];
        });
        return instance;
    };
}
//# sourceMappingURL=request-digest.js.map