UNPKG

2.37 kBJavaScriptView Raw
1import { combine, dateAdd, isUrlAbsolute } from "@pnp/core";
2import { BrowserFetchWithRetry, DefaultParse } from "@pnp/queryable";
3import { DefaultHeaders, DefaultInit } from "./defaults.js";
4import { RequestDigest } from "./request-digest.js";
5export function SPFxToken(context) {
6 return (instance) => {
7 instance.on.auth.replace(async function (url, init) {
8 const provider = await context.aadTokenProviderFactory.getTokenProvider();
9 const token = await provider.getToken(`${url.protocol}//${url.hostname}`);
10 // eslint-disable-next-line @typescript-eslint/dot-notation
11 init.headers["Authorization"] = `Bearer ${token}`;
12 return [url, init];
13 });
14 return instance;
15 };
16}
17export function SPFx(context) {
18 return (instance) => {
19 instance.using(DefaultHeaders(), DefaultInit(), BrowserFetchWithRetry(), DefaultParse(),
20 // remove SPFx Token in default due to issues #2570, #2571
21 // SPFxToken(context),
22 RequestDigest((url) => {
23 var _a, _b, _c;
24 const sameWeb = (new RegExp(`^${combine(context.pageContext.web.absoluteUrl, "/_api")}`, "i")).test(url);
25 if (sameWeb && ((_b = (_a = context === null || context === void 0 ? void 0 : context.pageContext) === null || _a === void 0 ? void 0 : _a.legacyPageContext) === null || _b === void 0 ? void 0 : _b.formDigestValue)) {
26 const creationDateFromDigest = new Date(context.pageContext.legacyPageContext.formDigestValue.split(",")[1]);
27 // account for page lifetime in timeout #2304 & others
28 // account for tab sleep #2550
29 return {
30 value: context.pageContext.legacyPageContext.formDigestValue,
31 expiration: dateAdd(creationDateFromDigest, "second", ((_c = context.pageContext.legacyPageContext) === null || _c === void 0 ? void 0 : _c.formDigestTimeoutSeconds) - 15 || 1585),
32 };
33 }
34 }));
35 // we want to fix up the url first
36 instance.on.pre.prepend(async (url, init, result) => {
37 if (!isUrlAbsolute(url)) {
38 url = combine(context.pageContext.web.absoluteUrl, url);
39 }
40 return [url, init, result];
41 });
42 return instance;
43 };
44}
45//# sourceMappingURL=spfx.js.map
\No newline at end of file