From db27e85ab16fa7ed64689852e7dfcb12fd1505f0 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Mon, 23 May 2022 14:07:58 -0700 Subject: [PATCH 1/2] update client options and middleware options --- package-lock.json | 32 ++++++++++---------- package.json | 4 +-- src/Client.ts | 3 +- src/GraphRequest.ts | 11 +++---- src/GraphRequestUtil.ts | 5 ++- src/IClientOptions.ts | 6 +--- src/browser/index.ts | 1 - src/index.ts | 1 - src/middleware/options/IMiddlewareOptions.ts | 14 --------- src/tasks/PageIterator.ts | 15 ++++----- test/DummyHandlerOptions.ts | 10 ++++-- test/browser/middleware/MiddlewareFactory.ts | 7 +++-- test/development/secrets.sample.ts | 7 ----- test/development/workload/PageIterator.ts | 22 +++++++------- test/node/middleware/MiddlewareFactory.ts | 7 +++-- 15 files changed, 62 insertions(+), 83 deletions(-) delete mode 100644 src/middleware/options/IMiddlewareOptions.ts delete mode 100644 test/development/secrets.sample.ts diff --git a/package-lock.json b/package-lock.json index ff0f998d8..3c60e4127 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,8 @@ "version": "4.0.0-Preview.1", "license": "MIT", "dependencies": { - "@microsoft/kiota-abstractions": "^1.0.0-preview.1", - "@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.1", + "@microsoft/kiota-abstractions": "^1.0.0-preview.5", + "@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.6", "tslib": "^2.2.0" }, "devDependencies": { @@ -2106,9 +2106,9 @@ } }, "node_modules/@microsoft/kiota-abstractions": { - "version": "1.0.0-preview.1", - "resolved": "https://registry.npmjs.org/@microsoft/kiota-abstractions/-/kiota-abstractions-1.0.0-preview.1.tgz", - "integrity": "sha512-imx4lMe/8CZkFUPUmuoZiwEClVbPdLXXgjQphwNY0IBVyHnQEwW19nz2UhWZHYp0oV3t40eWrnCzeJOwbu8vXA==", + "version": "1.0.0-preview.5", + "resolved": "https://registry.npmjs.org/@microsoft/kiota-abstractions/-/kiota-abstractions-1.0.0-preview.5.tgz", + "integrity": "sha512-RcqOexHGNOyKLWk75V3bF8l+jvBgF2pkhIHGKXJXj2DxDPwgQTV8HyacBMaSMECQTMDcfTdT8H7Tg2X74YuG+Q==", "dependencies": { "tinyduration": "^3.2.2", "tslib": "^2.3.1", @@ -2117,11 +2117,11 @@ } }, "node_modules/@microsoft/kiota-http-fetchlibrary": { - "version": "1.0.0-preview.1", - "resolved": "https://registry.npmjs.org/@microsoft/kiota-http-fetchlibrary/-/kiota-http-fetchlibrary-1.0.0-preview.1.tgz", - "integrity": "sha512-2iJFbTFYiXAMhuInAWryqW1xiO8L5x6sieh4Uixm39xbiHfr0b8kHDvsASRB8haQYKhrv7Uw4OGL3SXGzfv8QA==", + "version": "1.0.0-preview.6", + "resolved": "https://registry.npmjs.org/@microsoft/kiota-http-fetchlibrary/-/kiota-http-fetchlibrary-1.0.0-preview.6.tgz", + "integrity": "sha512-F/3o2ufbyhGVd4rnuEcYP6Lnm8v62Bz4l3Prg1c5jENO+EUq4nCGY3S71zWxihn5qncep5/2wglpaeJ2DpzEmQ==", "dependencies": { - "@microsoft/kiota-abstractions": "^1.0.0-preview.1", + "@microsoft/kiota-abstractions": "1.0.0-preview.5", "node-fetch": "^2.6.5", "tslib": "^2.3.1" } @@ -15528,9 +15528,9 @@ } }, "@microsoft/kiota-abstractions": { - "version": "1.0.0-preview.1", - "resolved": "https://registry.npmjs.org/@microsoft/kiota-abstractions/-/kiota-abstractions-1.0.0-preview.1.tgz", - "integrity": "sha512-imx4lMe/8CZkFUPUmuoZiwEClVbPdLXXgjQphwNY0IBVyHnQEwW19nz2UhWZHYp0oV3t40eWrnCzeJOwbu8vXA==", + "version": "1.0.0-preview.5", + "resolved": "https://registry.npmjs.org/@microsoft/kiota-abstractions/-/kiota-abstractions-1.0.0-preview.5.tgz", + "integrity": "sha512-RcqOexHGNOyKLWk75V3bF8l+jvBgF2pkhIHGKXJXj2DxDPwgQTV8HyacBMaSMECQTMDcfTdT8H7Tg2X74YuG+Q==", "requires": { "tinyduration": "^3.2.2", "tslib": "^2.3.1", @@ -15539,11 +15539,11 @@ } }, "@microsoft/kiota-http-fetchlibrary": { - "version": "1.0.0-preview.1", - "resolved": "https://registry.npmjs.org/@microsoft/kiota-http-fetchlibrary/-/kiota-http-fetchlibrary-1.0.0-preview.1.tgz", - "integrity": "sha512-2iJFbTFYiXAMhuInAWryqW1xiO8L5x6sieh4Uixm39xbiHfr0b8kHDvsASRB8haQYKhrv7Uw4OGL3SXGzfv8QA==", + "version": "1.0.0-preview.6", + "resolved": "https://registry.npmjs.org/@microsoft/kiota-http-fetchlibrary/-/kiota-http-fetchlibrary-1.0.0-preview.6.tgz", + "integrity": "sha512-F/3o2ufbyhGVd4rnuEcYP6Lnm8v62Bz4l3Prg1c5jENO+EUq4nCGY3S71zWxihn5qncep5/2wglpaeJ2DpzEmQ==", "requires": { - "@microsoft/kiota-abstractions": "^1.0.0-preview.1", + "@microsoft/kiota-abstractions": "1.0.0-preview.5", "node-fetch": "^2.6.5", "tslib": "^2.3.1" } diff --git a/package.json b/package.json index 8099b65a3..79939ac57 100644 --- a/package.json +++ b/package.json @@ -65,8 +65,8 @@ ] }, "dependencies": { - "@microsoft/kiota-abstractions": "^1.0.0-preview.1", - "@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.1", + "@microsoft/kiota-abstractions": "^1.0.0-preview.5", + "@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.6", "tslib": "^2.2.0" }, "devDependencies": { diff --git a/src/Client.ts b/src/Client.ts index cdcf2f9a8..efe18fe29 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -27,6 +27,7 @@ export class Client implements GraphBaseClient { baseUrl: GRAPH_BASE_URL, debugLogging: false, defaultVersion: GRAPH_API_VERSION, + authProvider: undefined }; /** @@ -67,7 +68,7 @@ export class Client implements GraphBaseClient { error.message = "Unable to Create Client, Please provide an authentication provider"; throw error; } - const allowedHosts = updateAndReturnAllAllowedHosts(clientOptions.authProvider, this.config.customHosts); + const allowedHosts = updateAndReturnAllAllowedHosts(clientOptions.authProvider); if (!clientOptions.middleware) { httpClient = new HttpClient(undefined, ...[].concat(getDefaultMiddlewareChain(clientOptions, allowedHosts))); diff --git a/src/GraphRequest.ts b/src/GraphRequest.ts index c5ff96706..019afd9a2 100644 --- a/src/GraphRequest.ts +++ b/src/GraphRequest.ts @@ -8,6 +8,7 @@ /** * @module GraphRequest */ +import { RequestOption } from "@microsoft/kiota-abstractions"; import { HttpClient } from "@microsoft/kiota-http-fetchlibrary"; import { GraphClientError } from "./GraphClientError"; @@ -18,7 +19,6 @@ import { GraphResponseHandler } from "./GraphResponseHandler"; import { ClientOptions } from "./IClientOptions"; import { FetchOptions } from "./IFetchOptions"; import { GraphRequestCallback } from "./IGraphRequestCallback"; -import { MiddlewareOptions } from "./middleware/options/IMiddlewareOptions"; import { RequestMethod } from "./RequestMethod"; import { ResponseType } from "./ResponseType"; /** @@ -62,7 +62,6 @@ export class GraphRequest { */ private httpClient: HttpClient; - /** * @private * A member variable to hold client options @@ -91,7 +90,7 @@ export class GraphRequest { * @private * A member to hold the array of middleware options for a request */ - private _middlewareOptions: MiddlewareOptions[]; + private _middlewareOptions: Record; /** * @private @@ -119,7 +118,7 @@ export class GraphRequest { }; this._headers = {}; this._options = {}; - this._middlewareOptions = []; + this._middlewareOptions = {}; this.parsePath(path); } @@ -368,7 +367,7 @@ export class GraphRequest { this.updateRequestOptions(options); try { - const rawResponse = await this.httpClient.executeFetch(request as string, options); + const rawResponse = await this.httpClient.executeFetch(request as string, options, this._middlewareOptions); const response: any = await GraphResponseHandler.getResponse(rawResponse, this._responseType, callback); return response; @@ -466,7 +465,7 @@ export class GraphRequest { * @param {MiddlewareOptions[]} options - The array of middleware options * @returns The same GraphRequest instance that is being called with */ - public middlewareOptions(options: MiddlewareOptions[]): GraphRequest { + public middlewareOptions(options: Record): GraphRequest { this._middlewareOptions = options; return this; } diff --git a/src/GraphRequestUtil.ts b/src/GraphRequestUtil.ts index c8dfe5ebe..c6b0f9492 100644 --- a/src/GraphRequestUtil.ts +++ b/src/GraphRequestUtil.ts @@ -129,10 +129,9 @@ const isCustomHostValid = (host: string) => { * @param customHosts * @returns */ -export const updateAndReturnAllAllowedHosts = (authProvider: BaseBearerTokenAuthenticationProvider, customHosts: Set): Set => { +export const updateAndReturnAllAllowedHosts = (authProvider: BaseBearerTokenAuthenticationProvider): Set => { const hostsValidator = authProvider.accessTokenProvider.getAllowedHostsValidator(); - const allowedHosts = customHosts ? new Set([...customHosts, ...hostsValidator.getAllowedHosts()]) : new Set(hostsValidator.getAllowedHosts()); - const hostSetWithGraphandCustomHosts = customHosts ? new Set([...allowedHosts, ...GRAPH_URLS, ...customHosts]) : new Set([...allowedHosts, ...GRAPH_URLS]); + const hostSetWithGraphandCustomHosts = new Set([...hostsValidator.getAllowedHosts(), ...GRAPH_URLS]) hostsValidator.setAllowedHosts(hostSetWithGraphandCustomHosts); return hostSetWithGraphandCustomHosts; }; diff --git a/src/IClientOptions.ts b/src/IClientOptions.ts index 7d8e39ea9..7ed696378 100644 --- a/src/IClientOptions.ts +++ b/src/IClientOptions.ts @@ -23,15 +23,11 @@ import { FetchOptions } from "./IFetchOptions"; */ export interface ClientOptions { - authProvider?: BaseBearerTokenAuthenticationProvider; + authProvider: BaseBearerTokenAuthenticationProvider; baseUrl?: string; debugLogging?: boolean; defaultVersion?: string; fetchOptions?: FetchOptions; middleware?: Middleware | Middleware[]; customFetch?: (input: string, init?: RequestInit) => Promise; - /** - * Example - If URL is "https://test_host/v1.0", then set property "customHosts" as "customHosts: Set(["test_host"])" - */ - customHosts?: Set; } diff --git a/src/browser/index.ts b/src/browser/index.ts index f28b75f2c..af1887200 100644 --- a/src/browser/index.ts +++ b/src/browser/index.ts @@ -12,7 +12,6 @@ export { RetryHandler, RedirectHandler, TelemetryHandler, RetryHandlerOptions, H export { BatchRequestBody, RequestData, BatchRequestContent, BatchRequestData, BatchRequestStep } from "../content/BatchRequestContent"; export { BatchResponseBody, BatchResponseContent } from "../content/BatchResponseContent"; -export { MiddlewareOptions } from "../middleware/options/IMiddlewareOptions"; export { FeatureUsageFlag } from "../middleware/Telemetry/TelemetryFeatureUsageFlag"; export { ChaosHandlerOptions } from "../middleware/options/ChaosHandlerOptions"; export { ChaosStrategy } from "../middleware/options/ChaosStrategy"; diff --git a/src/index.ts b/src/index.ts index 58f39eed5..0d4333727 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,6 @@ export { RetryHandler, RedirectHandler, TelemetryHandler, RedirectHandlerOptionK export { BatchRequestBody, RequestData, BatchRequestContent, BatchRequestData, BatchRequestStep } from "./content/BatchRequestContent"; export { BatchResponseBody, BatchResponseContent } from "./content/BatchResponseContent"; -export { MiddlewareOptions } from "./middleware/options/IMiddlewareOptions"; export { FeatureUsageFlag } from "./middleware/Telemetry/TelemetryFeatureUsageFlag"; export { ChaosHandlerOptions } from "./middleware/options/ChaosHandlerOptions"; export { ChaosStrategy } from "./middleware/options/ChaosStrategy"; diff --git a/src/middleware/options/IMiddlewareOptions.ts b/src/middleware/options/IMiddlewareOptions.ts deleted file mode 100644 index ebbf30827..000000000 --- a/src/middleware/options/IMiddlewareOptions.ts +++ /dev/null @@ -1,14 +0,0 @@ -/** - * ------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. - * See License in the project root for license information. - * ------------------------------------------------------------------------------------------- - */ - -/** - * @interface - * Signature representing the middleware options - */ - -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface MiddlewareOptions {} diff --git a/src/tasks/PageIterator.ts b/src/tasks/PageIterator.ts index 7730f1ea2..a3ac63aed 100644 --- a/src/tasks/PageIterator.ts +++ b/src/tasks/PageIterator.ts @@ -9,9 +9,10 @@ * @module PageIterator */ +import { RequestOption } from "@microsoft/kiota-abstractions/src/requestOption"; + import { FetchOptions } from "../IFetchOptions"; -import { Client } from "../index"; -import { MiddlewareOptions } from "../middleware/options/IMiddlewareOptions"; +import { GraphBaseClient } from "../requestBuilderUtils/GraphBaseClient"; /** * Signature representing PageCollection @@ -31,12 +32,12 @@ export interface PageCollection { * Signature to define the request options to be sent during request. * The values of the GraphRequestOptions properties are passed to the Graph Request object. * @property {HeadersInit} headers - the header options for the request - * @property {MiddlewareOptions[]} middlewareoptions - The middleware options for the request + * @property { Record} middlewareoptions - The middleware options for the request * @property {FetchOptions} options - The fetch options for the request */ export interface GraphRequestOptions { - headers?: HeadersInit; - middlewareOptions?: MiddlewareOptions[]; + headers?: Record; + middlewareOptions?: Record; options?: FetchOptions; } @@ -55,7 +56,7 @@ export class PageIterator { * @private * Member holding the GraphClient instance */ - private client: Client; + private client: GraphBaseClient; /** * @private @@ -103,7 +104,7 @@ export class PageIterator { * @param {GraphRequestOptions} requestOptions - The request options * @returns An instance of a PageIterator */ - public constructor(client: Client, pageCollection: PageCollection, callback: PageIteratorCallback, requestOptions?: GraphRequestOptions) { + public constructor(client: GraphBaseClient, pageCollection: PageCollection, callback: PageIteratorCallback, requestOptions?: GraphRequestOptions) { this.client = client; this.collection = pageCollection.value; this.nextLink = pageCollection["@odata.nextLink"]; diff --git a/test/DummyHandlerOptions.ts b/test/DummyHandlerOptions.ts index cdf27eb61..e1c753e2c 100644 --- a/test/DummyHandlerOptions.ts +++ b/test/DummyHandlerOptions.ts @@ -5,19 +5,19 @@ * ------------------------------------------------------------------------------------------- */ +import { RequestOption } from "@microsoft/kiota-abstractions"; + /** * @module DummyHandlerOptions */ -import { MiddlewareOptions } from "../src/middleware/options/IMiddlewareOptions"; - /** * @class * @implements MiddlewareOptions * Class for DummyHandlerOptions */ -export class DummyHandlerOptions implements MiddlewareOptions { +export class DummyHandlerOptions implements RequestOption { /** * @public * A member holding a dummy string @@ -34,4 +34,8 @@ export class DummyHandlerOptions implements MiddlewareOptions { public constructor(dummyString = "dummy") { this.dummyString = dummyString; } + + public getKey(): string { + return "DummyHandlerOptions"; + } } diff --git a/test/browser/middleware/MiddlewareFactory.ts b/test/browser/middleware/MiddlewareFactory.ts index 9844f962b..4a1fb8f78 100644 --- a/test/browser/middleware/MiddlewareFactory.ts +++ b/test/browser/middleware/MiddlewareFactory.ts @@ -5,7 +5,7 @@ * ------------------------------------------------------------------------------------------- */ -import { CustomFetchHandler, RedirectHandler, RetryHandler, TelemetryHandler } from "@microsoft/kiota-http-fetchlibrary"; +import { CustomFetchHandler, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, TelemetryHandler } from "@microsoft/kiota-http-fetchlibrary"; import { assert } from "chai"; import { ClientOptions, SimpleAuthenticationProvider } from "../../../src"; @@ -25,7 +25,8 @@ describe("MiddlewareFactory", () => { assert.isTrue(defaultMiddleWareArray[0] instanceof AuthenticationHandler); assert.isTrue(defaultMiddleWareArray[1] instanceof RetryHandler); - assert.isTrue(defaultMiddleWareArray[2] instanceof TelemetryHandler); - assert.isTrue(defaultMiddleWareArray[3] instanceof CustomFetchHandler); + assert.isTrue(defaultMiddleWareArray[2] instanceof ParametersNameDecodingHandler); + assert.isTrue(defaultMiddleWareArray[3] instanceof TelemetryHandler); + assert.isTrue(defaultMiddleWareArray[4] instanceof CustomFetchHandler); }); }); diff --git a/test/development/secrets.sample.ts b/test/development/secrets.sample.ts deleted file mode 100644 index 304c647cd..000000000 --- a/test/development/secrets.sample.ts +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @file - * Defines access token exporting structure - * - * To use authentication based(making real requests to the graph service) testing populate this access token's value and rename this file as secrets.ts - */ -export const AccessToken = ""; diff --git a/test/development/workload/PageIterator.ts b/test/development/workload/PageIterator.ts index 6c486c1e7..0b71585b4 100644 --- a/test/development/workload/PageIterator.ts +++ b/test/development/workload/PageIterator.ts @@ -8,7 +8,7 @@ import { Event } from "@microsoft/microsoft-graph-types"; import { assert } from "chai"; -import { Client, ClientOptions } from "../../../src"; +import { Client, ClientOptions, SimpleAccessTokenProvider, SimpleAuthenticationProvider } from "../../../src"; import { ChaosHandler } from "../../../src/middleware/ChaosHandler"; import { ChaosHandlerOptions } from "../../../src/middleware/options/ChaosHandlerOptions"; import { ChaosStrategy } from "../../../src/middleware/options/ChaosStrategy"; @@ -21,7 +21,7 @@ describe("PageIterator", () => { const pst = "Pacific Standard Time"; const testURL = "/me/events"; - before(async function() { + before(async function () { this.timeout(20000); const response = await client.api(testURL + "?count=true").get(); @@ -46,11 +46,7 @@ describe("PageIterator", () => { }); it("same headers passed with pageIterator", async () => { - const response = await client - .api(`${testURL}?$top=2`) - .headers(pstHeader) - .select("id,start,end") - .get(); + const response = await client.api(`${testURL}?$top=2`).headers(pstHeader).select("id,start,end").get(); const callback: PageIteratorCallback = (eventResponse) => { const event = eventResponse as Event; @@ -63,7 +59,7 @@ describe("PageIterator", () => { await pageIterator.iterate(); assert.isTrue(pageIterator.isComplete()); } - }).timeout(30 * 1000); + }).timeout(0); it("different headers passed with pageIterator", async () => { const response = await client @@ -90,7 +86,7 @@ describe("PageIterator", () => { await pageIterator.iterate(); assert.isTrue(pageIterator.isComplete()); } - }).timeout(30 * 1000); + }).timeout(0); // TODO - Temporariliy commenting this test. it("setting middleware with pageIterator", async () => { @@ -103,6 +99,9 @@ describe("PageIterator", () => { }; }; const clientOptions: ClientOptions = { + authProvider: new SimpleAuthenticationProvider(async () => { + return "Dummy_Token"; + }), middleware, }; const responseBody = { value: [{ event1: "value1" }, { event2: "value2" }] }; @@ -113,8 +112,9 @@ describe("PageIterator", () => { return true; }; - const middlewareOptions = [new ChaosHandlerOptions(ChaosStrategy.MANUAL, "middleware options for pageIterator", 200, 0, JSON.stringify(responseBody), new Headers({ "Content-Type": "application/json", "content-length": "100" }))]; - const requestOptions = { middlewareOptions }; + const middlewareOptions = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "middleware options for pageIterator", 200, 0, JSON.stringify(responseBody), new Headers({ "Content-Type": "application/json", "content-length": "100" })); + const key = middlewareOptions.getKey(); + const requestOptions: GraphRequestOptions = { middlewareOptions: { [key]: middlewareOptions } }; const client = Client.init(clientOptions); const pageIterator = new PageIterator(client, getPageCollection(), callback, requestOptions); diff --git a/test/node/middleware/MiddlewareFactory.ts b/test/node/middleware/MiddlewareFactory.ts index b8e33ddc1..f7cb9540d 100644 --- a/test/node/middleware/MiddlewareFactory.ts +++ b/test/node/middleware/MiddlewareFactory.ts @@ -5,7 +5,7 @@ * ------------------------------------------------------------------------------------------- */ -import { CustomFetchHandler, RedirectHandler, RetryHandler, TelemetryHandler } from "@microsoft/kiota-http-fetchlibrary"; +import { CustomFetchHandler, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, TelemetryHandler } from "@microsoft/kiota-http-fetchlibrary"; import { assert } from "chai"; import { ClientOptions, SimpleAuthenticationProvider } from "../../../src"; @@ -26,7 +26,8 @@ describe("MiddlewareFactory", () => { assert.isTrue(defaultMiddleWareArray[0] instanceof AuthenticationHandler); assert.isTrue(defaultMiddleWareArray[1] instanceof RetryHandler); assert.isTrue(defaultMiddleWareArray[2] instanceof RedirectHandler); - assert.isTrue(defaultMiddleWareArray[3] instanceof TelemetryHandler); - assert.isTrue(defaultMiddleWareArray[4] instanceof CustomFetchHandler); + assert.isTrue(defaultMiddleWareArray[3] instanceof ParametersNameDecodingHandler); + assert.isTrue(defaultMiddleWareArray[4] instanceof TelemetryHandler); + assert.isTrue(defaultMiddleWareArray[5] instanceof CustomFetchHandler); }); }); From d5bbee54aee79ec2c223e43d41652f587725236b Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Tue, 24 May 2022 01:16:46 -0700 Subject: [PATCH 2/2] restore secrets --- test/development/secrets.sample.ts | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 test/development/secrets.sample.ts diff --git a/test/development/secrets.sample.ts b/test/development/secrets.sample.ts new file mode 100644 index 000000000..304c647cd --- /dev/null +++ b/test/development/secrets.sample.ts @@ -0,0 +1,7 @@ +/** + * @file + * Defines access token exporting structure + * + * To use authentication based(making real requests to the graph service) testing populate this access token's value and rename this file as secrets.ts + */ +export const AccessToken = "";