Skip to content

Commit

Permalink
Merge pull request #3039 from patrick-rodgers/version-4
Browse files Browse the repository at this point in the history
updates to better handle graph default url logic
  • Loading branch information
juliemturner authored May 22, 2024
2 parents 56de29b + a70e187 commit 5269842
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 37 deletions.
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

0 comments on commit 5269842

Please sign in to comment.