Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates to better handle graph default url logic #3039

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/graph/behaviors/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { combine, isUrlAbsolute, TimelinePipe } from "@pnp/core";
import { InjectHeaders, Queryable, RejectOnError, ResolveOnData } from "@pnp/queryable";
import { Telemetry } from "./telemetry.js";
import { DEFAULT_GRAPH_URL } from "../index.js";

export function DefaultInit(graphUrl = "https://graph.microsoft.com/v1.0"): TimelinePipe<Queryable> {
export function DefaultInit(graphUrl = DEFAULT_GRAPH_URL): TimelinePipe<Queryable> {

if (!isUrlAbsolute(graphUrl)) {
throw Error("Graph baseUrl must be absolute.");
}

return (instance: Queryable) => {

Expand Down
25 changes: 7 additions & 18 deletions packages/graph/behaviors/graphbrowser.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import { combine, isUrlAbsolute, TimelinePipe } from "@pnp/core";
import { TimelinePipe } from "@pnp/core";
import { BrowserFetchWithRetry, DefaultParse, Queryable } from "@pnp/queryable";
import { DefaultHeaders, DefaultInit } from "./defaults.js";
import { DEFAULT_GRAPH_URL } from "../index.js";

export interface IGraphBrowserProps {
baseUrl?: string;
}

export function GraphBrowser(props?: IGraphBrowserProps): TimelinePipe<Queryable> {

if (props?.baseUrl && !isUrlAbsolute(props.baseUrl)) {
throw Error("GraphBrowser props.baseUrl must be absolute when supplied.");
}
const { baseUrl } = {
baseUrl: DEFAULT_GRAPH_URL,
...props,
};

return (instance: Queryable) => {

instance.using(
DefaultHeaders(),
DefaultInit(),
DefaultInit(baseUrl),
BrowserFetchWithRetry(),
DefaultParse());

if (props?.baseUrl) {

// we want to fix up the url first
instance.on.pre.prepend(async (url, init, result) => {

if (!isUrlAbsolute(url)) {
url = combine(props.baseUrl, url);
}

return [url, init, result];
});
}

return instance;
};
}
2 changes: 2 additions & 0 deletions packages/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export * from "./behaviors/graphbrowser.js";
export * from "./behaviors/paged.js";
export * from "./behaviors/telemetry.js";
export * from "./behaviors/spfx.js";

export const DEFAULT_GRAPH_URL = "https://graph.microsoft.com/v1.0";
27 changes: 9 additions & 18 deletions packages/nodejs/behaviors/graphdefault.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Configuration } from "@azure/msal-node";
import { combine, isUrlAbsolute, TimelinePipe } from "@pnp/core";
import { combine, TimelinePipe } from "@pnp/core";
import { DefaultParse, Queryable } from "@pnp/queryable";
import { DefaultHeaders, DefaultInit } from "@pnp/graph";
import { DEFAULT_GRAPH_URL, DefaultHeaders, DefaultInit } from "@pnp/graph";
import { NodeFetchWithRetry } from "./fetch.js";
import { MSAL } from "./msal.js";

Expand All @@ -20,30 +20,21 @@ export interface IGraphDefaultProps {
*/
export function GraphDefault(props?: IGraphDefaultProps): TimelinePipe<Queryable> {

if (props?.baseUrl && !isUrlAbsolute(props?.baseUrl)) {
throw Error("GraphDefault props.baseUrl must be absolute when supplied.");
}

const { baseUrl, msal } = {
baseUrl: "https://graph.microsoft.com/",
baseUrl: DEFAULT_GRAPH_URL,
...props,
};

return (instance: Queryable) => {
const behaviors: TimelinePipe<any>[] = [DefaultHeaders(), DefaultInit(), NodeFetchWithRetry(), DefaultParse()];
if(props?.msal){
behaviors.push(MSAL(msal.config, msal?.scopes || [combine(baseUrl, ".default")]));
}
instance.using(...behaviors);

instance.on.pre(async (url, init, result) => {
const behaviors: TimelinePipe<any>[] = [DefaultHeaders(), DefaultInit(baseUrl), NodeFetchWithRetry(), DefaultParse()];

if (!isUrlAbsolute(url)) {
url = combine(baseUrl, url);
}
if (props?.msal) {
const u = new URL(baseUrl);
behaviors.push(MSAL(msal.config, msal?.scopes || [combine(`${u.protocol}//${u.host}`, ".default")]));
}

return [url, init, result];
});
instance.using(...behaviors);

return instance;
};
Expand Down