@pnp/sp
Version:
pnp - provides a fluent api for working with SharePoint REST
45 lines • 2.37 kB
JavaScript
import { combine, dateAdd, isUrlAbsolute } from "@pnp/core";
import { BrowserFetchWithRetry, DefaultParse } from "@pnp/queryable";
import { DefaultHeaders, DefaultInit } from "./defaults.js";
import { RequestDigest } from "./request-digest.js";
export function SPFxToken(context) {
return (instance) => {
instance.on.auth.replace(async function (url, init) {
const provider = await context.aadTokenProviderFactory.getTokenProvider();
const token = await provider.getToken(`${url.protocol}//${url.hostname}`);
// eslint-disable-next-line @typescript-eslint/dot-notation
init.headers["Authorization"] = `Bearer ${token}`;
return [url, init];
});
return instance;
};
}
export function SPFx(context) {
return (instance) => {
instance.using(DefaultHeaders(), DefaultInit(), BrowserFetchWithRetry(), DefaultParse(),
// remove SPFx Token in default due to issues #2570, #2571
// SPFxToken(context),
RequestDigest((url) => {
var _a, _b, _c;
const sameWeb = (new RegExp(`^${combine(context.pageContext.web.absoluteUrl, "/_api")}`, "i")).test(url);
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)) {
const creationDateFromDigest = new Date(context.pageContext.legacyPageContext.formDigestValue.split(",")[1]);
// account for page lifetime in timeout #2304 & others
// account for tab sleep #2550
return {
value: context.pageContext.legacyPageContext.formDigestValue,
expiration: dateAdd(creationDateFromDigest, "second", ((_c = context.pageContext.legacyPageContext) === null || _c === void 0 ? void 0 : _c.formDigestTimeoutSeconds) - 15 || 1585),
};
}
}));
// we want to fix up the url first
instance.on.pre.prepend(async (url, init, result) => {
if (!isUrlAbsolute(url)) {
url = combine(context.pageContext.web.absoluteUrl, url);
}
return [url, init, result];
});
return instance;
};
}
//# sourceMappingURL=spfx.js.map