1 | import { combine, dateAdd, isUrlAbsolute } from "@pnp/core";
|
2 | import { BrowserFetchWithRetry, DefaultParse } from "@pnp/queryable";
|
3 | import { DefaultHeaders, DefaultInit } from "./defaults.js";
|
4 | import { RequestDigest } from "./request-digest.js";
|
5 | export 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 |
|
11 | init.headers["Authorization"] = `Bearer ${token}`;
|
12 | return [url, init];
|
13 | });
|
14 | return instance;
|
15 | };
|
16 | }
|
17 | export function SPFx(context) {
|
18 | return (instance) => {
|
19 | instance.using(DefaultHeaders(), DefaultInit(), BrowserFetchWithRetry(), DefaultParse(),
|
20 |
|
21 |
|
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 |
|
28 |
|
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 |
|
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 |
|
\ | No newline at end of file |