Skip to content

Commit be42e2e

Browse files
authored
Merge pull request #367 from microsoftgraph/feat/auth-middleware-factory
feat: add authorization middleware support and factory overload
2 parents 96dc536 + f7bb892 commit be42e2e

File tree

6 files changed

+112
-4
lines changed

6 files changed

+112
-4
lines changed

.release-please-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
".": "1.0.0-preview.14"
3-
}
3+
}

src/adapter/BaseGraphRequestAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "@microsoft/kiota-abstractions";
88
import { HttpClient, type ObservabilityOptions, ObservabilityOptionsImpl } from "@microsoft/kiota-http-fetchlibrary";
99
import { DefaultRequestAdapter } from "@microsoft/kiota-bundle";
10-
import { GraphHttpClient } from "../http/GraphHttpClient.js";
10+
import { createGraphClientFactory } from "../http/GraphClientFactory.js";
1111

1212
/**
1313
* Base request adapter for graph clients. Bootstraps telemetry and other aspects.
@@ -29,7 +29,7 @@ export class BaseGraphRequestAdapter extends DefaultRequestAdapter {
2929
authenticationProvider: AuthenticationProvider,
3030
parseNodeFactory: ParseNodeFactory = ParseNodeFactoryRegistry.defaultInstance,
3131
serializationWriterFactory: SerializationWriterFactory = SerializationWriterFactoryRegistry.defaultInstance,
32-
httpClient: HttpClient = new GraphHttpClient({
32+
httpClient: HttpClient = createGraphClientFactory({
3333
graphServiceTargetVersion,
3434
graphServiceLibraryClientVersion,
3535
}),

src/http/GraphClientFactory.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { BaseBearerTokenAuthenticationProvider } from "@microsoft/kiota-abstractions";
2+
import { Middleware } from "@microsoft/kiota-http-fetchlibrary";
3+
import { GraphHttpClient } from "./GraphHttpClient.js";
4+
import { getDefaultMiddlewares, GraphTelemetryOption } from "../middleware";
5+
6+
/**
7+
* Creates an instance of `GraphHttpClient`, with the provided middlewares and custom fetch implementation both parameters are optional.
8+
* if no middlewares are provided, the default middlewares will be used.
9+
* @param {GraphTelemetryOption} graphTelemetryOption - The telemetry options for the Graph client.
10+
* @param {(request: string, init: RequestInit) => Promise<Response>} customFetch - The custom fetch function to use for HTTP requests.
11+
* @param {BaseBearerTokenAuthenticationProvider} [authenticationProvider] - Optional authentication provider for bearer token.
12+
* @param {Middleware[]} [middlewares] - Optional array of middleware to use in the HTTP pipeline.
13+
* @returns {GraphHttpClient} - A new instance of `GraphHttpClient`.
14+
* @example
15+
* ```Typescript
16+
* // Example usage of createGraphClientFactory method with graphTelemetryOption , customFetch and middlewares parameters provided
17+
* createGraphClientFactory(graphTelemetryOption, customFetch, [middleware1, middleware2]);
18+
* ```
19+
* @example
20+
* ```Typescript
21+
* // Example usage of createGraphClientFactory method with only graphTelemetryOption and customFetch parameter provided
22+
* createGraphClientFactory(graphTelemetryOption, customFetch);
23+
* ```
24+
* @example
25+
* ```Typescript
26+
* // Example usage of createGraphClientFactory method with only graphTelemetryOption and middlewares parameter provided
27+
* createGraphClientFactory(graphTelemetryOption, undefined, [middleware1, middleware2]);
28+
* ```
29+
* @example
30+
* ```Typescript
31+
* // Example usage of createGraphClientFactory method with only graphTelemetryOption parameter provided
32+
* createGraphClientFactory(graphTelemetryOption);
33+
* ```
34+
*/
35+
export const createGraphClientFactory = (
36+
graphTelemetryOption: GraphTelemetryOption,
37+
customFetch?: (request: string, init: RequestInit) => Promise<Response>,
38+
authenticationProvider?: BaseBearerTokenAuthenticationProvider,
39+
middlewares?: Middleware[],
40+
): GraphHttpClient => {
41+
const middleware =
42+
middlewares ||
43+
getDefaultMiddlewares(
44+
{
45+
customFetch,
46+
graphTelemetryOption,
47+
},
48+
authenticationProvider,
49+
);
50+
return new GraphHttpClient(graphTelemetryOption, customFetch, ...middleware);
51+
};

src/http/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./GraphClientFactory.js";
12
export * from "./GraphHttpClient.js";

src/middleware/MiddlewareFactory.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ import {
33
MiddlewareFactory,
44
UrlReplaceHandler,
55
UrlReplaceHandlerOptions,
6+
AuthorizationHandler,
67
} from "@microsoft/kiota-http-fetchlibrary";
78
import { GraphTelemetryOption } from "./GraphTelemetryOption.js";
89
import { GraphTelemetryHandler } from "./GraphTelemetryHandler.js";
910
import { defaultUrlReplacementPairs } from "../utils/Constants.js";
11+
import { BaseBearerTokenAuthenticationProvider } from "@microsoft/kiota-abstractions";
1012

11-
export const getDefaultMiddlewares = (options: MiddlewareFactoryOptions = { customFetch: fetch }): Middleware[] => {
13+
export const getDefaultMiddlewares = (
14+
options: MiddlewareFactoryOptions = { customFetch: fetch },
15+
authenticationProvider?: BaseBearerTokenAuthenticationProvider | null,
16+
): Middleware[] => {
1217
let kiotaChain = MiddlewareFactory.getDefaultMiddlewares(options?.customFetch);
18+
if (authenticationProvider) {
19+
kiotaChain.unshift(new AuthorizationHandler(authenticationProvider));
20+
}
1321
const additionalMiddleware: Middleware[] = [
1422
new UrlReplaceHandler(
1523
new UrlReplaceHandlerOptions({

test/http/GraphHttpClient.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { assert, describe, it } from "vitest";
2+
import { GraphHttpClient, GraphTelemetryOption } from "../../src";
3+
import { BaseBearerTokenAuthenticationProvider } from "@microsoft/kiota-abstractions";
4+
import type { Middleware } from "@microsoft/kiota-http-fetchlibrary";
5+
import { createGraphClientFactory } from "../../src/http/GraphClientFactory";
6+
7+
const graphTelemetryOption: GraphTelemetryOption = {};
8+
9+
/**
10+
* Counts the number of middlewares in a linked list of middlewares.
11+
*
12+
* @param {Middleware} middleware - The starting middleware.
13+
* @returns {number} The count of middlewares.
14+
*/
15+
const countMiddlewares = (middleware: Middleware): number => {
16+
let count = 0;
17+
while (middleware.next) {
18+
count++;
19+
middleware = middleware.next;
20+
}
21+
return count;
22+
};
23+
24+
describe("GraphHttpClient tests", () => {
25+
describe("Constructor", () => {
26+
it("Should create instance of graph http client", () => {
27+
const client = new GraphHttpClient(graphTelemetryOption);
28+
assert.isNotNull(client, "Client is null");
29+
});
30+
31+
it("Should create instance of graph http client with middleware", () => {
32+
const middleware = {} as Middleware;
33+
const client = new GraphHttpClient(graphTelemetryOption, undefined, middleware);
34+
assert.isNotNull(client, "Client is null");
35+
});
36+
37+
it("Should add auth middleware when provider is given", () => {
38+
const client = new GraphHttpClient(graphTelemetryOption);
39+
const count = countMiddlewares((client as any)["middleware"] as Middleware);
40+
assert.equal(8, count);
41+
42+
const authenticationProvider = new BaseBearerTokenAuthenticationProvider({} as any);
43+
const clientWithProvider = createGraphClientFactory(graphTelemetryOption, undefined, authenticationProvider);
44+
const count2 = countMiddlewares((clientWithProvider as any)["middleware"] as Middleware);
45+
assert.equal(9, count2);
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)