|
| 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 | +}; |
0 commit comments