UNPKG

2.78 kBJavaScriptView Raw
1import { combine, dateAdd, hOP, objectDefinedNotNull } from "@pnp/core";
2import { extractWebUrl } from "../utils/extract-web-url.js";
3function clearExpired(digest) {
4 const now = new Date();
5 return !objectDefinedNotNull(digest) || (now > digest.expiration) ? null : digest;
6}
7// allows for the caching of digests across all calls which each have their own IDigestInfo wrapper.
8const digests = new Map();
9export function RequestDigest(hook) {
10 return (instance) => {
11 instance.on.pre(async function (url, init, result) {
12 // eslint-disable-next-line @typescript-eslint/dot-notation
13 if (/get/i.test(init.method) || (init.headers && (hOP(init.headers, "X-RequestDigest") || hOP(init.headers, "Authorization")))) {
14 return [url, init, result];
15 }
16 // add the request to the auth moment of the timeline
17 this.on.auth(async (url, init) => {
18 const urlAsString = url.toString();
19 const webUrl = extractWebUrl(urlAsString);
20 // do we have one in the cache that is still valid
21 // from #2186 we need to always ensure the digest we get isn't expired
22 let digest = clearExpired(digests.get(webUrl));
23 if (!objectDefinedNotNull(digest) && typeof hook === "function") {
24 digest = clearExpired(hook(urlAsString, init));
25 }
26 if (!objectDefinedNotNull(digest)) {
27 // let's get one from the server
28 digest = await fetch(combine(webUrl, "/_api/contextinfo"), {
29 cache: "no-cache",
30 credentials: "same-origin",
31 headers: {
32 "accept": "application/json",
33 "content-type": "application/json;odata=verbose;charset=utf-8",
34 },
35 method: "POST",
36 }).then(r => r.json()).then(p => ({
37 expiration: dateAdd(new Date(), "second", p.FormDigestTimeoutSeconds),
38 value: p.FormDigestValue,
39 }));
40 }
41 if (objectDefinedNotNull(digest)) {
42 // if we got a digest, set it in the headers
43 init.headers = {
44 "X-RequestDigest": digest.value,
45 ...init.headers,
46 };
47 // and cache it for future requests
48 digests.set(webUrl, digest);
49 }
50 return [url, init];
51 });
52 return [url, init, result];
53 });
54 return instance;
55 };
56}
57//# sourceMappingURL=request-digest.js.map
\No newline at end of file