@@ -20,16 +20,6 @@ import { Graph } from '../Graph';
2020import { chainMiddleware } from '../utils/GraphHelpers' ;
2121import { MockProvider } from './MockProvider' ;
2222
23- /**
24- * The base URL for the mock endpoint
25- */
26- const BASE_URL = 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=' ;
27-
28- /**
29- * The base URL for the graph
30- */
31- const ROOT_GRAPH_URL = 'https://graph.microsoft.com/' ;
32-
3323/**
3424 * MockGraph Instance
3525 *
@@ -50,7 +40,6 @@ export class MockGraph extends Graph {
5040
5141 super (
5242 Client . initWithMiddleware ( {
53- baseUrl : BASE_URL + ROOT_GRAPH_URL ,
5443 middleware : chainMiddleware ( ...middleware )
5544 } )
5645 ) ;
@@ -85,12 +74,13 @@ class MockMiddleware implements Middleware {
8574 */
8675 private _nextMiddleware : Middleware ;
8776
77+ private static _baseUrl ;
78+
8879 // tslint:disable-next-line: completed-docs
8980 public async execute ( context : Context ) : Promise < void > {
9081 try {
91- const url = context . request as string ;
92- const baseLength = BASE_URL . length ;
93- context . request = url . substring ( 0 , baseLength ) + escape ( url . substring ( baseLength ) ) ;
82+ const baseUrl = await MockMiddleware . getBaseUrl ( ) ;
83+ context . request = baseUrl + escape ( context . request as string ) ;
9484 } catch ( error ) {
9585 // ignore error
9686 }
@@ -105,4 +95,19 @@ class MockMiddleware implements Middleware {
10595 public setNext ( next : Middleware ) : void {
10696 this . _nextMiddleware = next ;
10797 }
98+
99+ private static async getBaseUrl ( ) {
100+ if ( ! this . _baseUrl ) {
101+ try {
102+ // get the url we should be using from the endpoint service
103+ let response = await fetch ( 'https://cdn.graph.office.net/en-us/graph/api/proxy/endpoint' ) ;
104+ this . _baseUrl = ( await response . json ( ) ) + '?url=' ;
105+ } catch {
106+ // fallback to hardcoded value
107+ this . _baseUrl = 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=' ;
108+ }
109+ }
110+
111+ return this . _baseUrl ;
112+ }
108113}
0 commit comments