Is the tutorial here up to date? If I follow this tutorial to add the Chaos Handler in the next middleware chain the authProvider fails to load. (config.authProvider). The instance of httpClient.middleware.authenticationProvider remains. Logged from graphClient in the code below. Noted out sections are the attempt.
The Chaos handler will appear in the nextMiddleware Chain but as indicated the authProvider is no longer loaded.
const msalApplication = new Msal.UserAgentApplication(msalConfig);
msalApplication.navigateToLoginRequestUrl = true;
const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);
const authenticationHandler = new MicrosoftGraph.AuthenticationHandler(authProvider);
const chaosProvider = new MicrosoftGraph.ChaosHandlerOptions(MicrosoftGraph.ChaosStrategy.MANUAL, '429', 429, 100, undefined, undefined);
const chaosHandler = new MicrosoftGraph.ChaosHandler(chaosProvider);
//authenticationHandler.setNext(chaosHandler);
//const clientOptions = { middleware: authenticationHandler };
const clientOptions = { authProvider };
try
{
graphClient = await MicrosoftGraph.Client.initWithMiddleware(clientOptions);
console.log(graphClient);
return graphClient;
}
catch (error)
{
logOutput("Graph Authorization:" + error);
}
As a followup it appears as though providing a authProvider and a middleware chain is a one or the other scenario. From the client library.
private constructor(clientOptions: ClientOptions) {
validatePolyFilling();
for (const key in clientOptions) {
if (Object.prototype.hasOwnProperty.call(clientOptions, key)) {
this.config[key] = clientOptions[key];
}
}
let httpClient: HTTPClient;
if (clientOptions.authProvider !== undefined && clientOptions.middleware !== undefined) {
const error = new Error();
error.name = "AmbiguityInInitialization";
error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain not both";
throw error;
} else if (clientOptions.authProvider !== undefined) {
httpClient = HTTPClientFactory.createWithAuthenticationProvider(clientOptions.authProvider);
} else if (clientOptions.middleware !== undefined) {
httpClient = new HTTPClient(...[].concat(clientOptions.middleware));
} else {
const error = new Error();
error.name = "InvalidMiddlewareChain";
error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain";
throw error;
}
this.httpClient = httpClient;
}
After client initWithMiddleware(clientOptions) you can inject the middleware with graphClient.httpClient.setMiddleware() with the handler middleware chain and get the expected result. But there appears to be a conflict with the MSAL authentication library as any attempt to use the .api() function with middleware fails miserably.