Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript - isomorphic setup, first set of middlewares #728

Merged
merged 46 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
fd28d27
adding client factory, context and middleware files
nikithauc Sep 1, 2021
1e1a966
Adding middlewarefactory
nikithauc Sep 2, 2021
476532d
introduce custom fetch
nikithauc Sep 14, 2021
5ddd92e
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Sep 20, 2021
0f4d006
removed middlewareoption getkey, httpclient factory,
nikithauc Sep 22, 2021
454f0a6
adding MiddlewareControl
nikithauc Sep 26, 2021
9b09cdc
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Oct 3, 2021
1528921
adding shims
nikithauc Oct 4, 2021
c3a31b5
adding fetch definitions
nikithauc Oct 6, 2021
02d5812
Adding dom.d.ts, url utils
nikithauc Oct 7, 2021
9b9c1b0
splitting tsconfig
nikithauc Oct 8, 2021
090c201
adding redirect and middlewareutil tests
nikithauc Oct 9, 2021
64640dc
add karma tests
nikithauc Oct 12, 2021
de1dfdc
installing rollup, http client tests node
nikithauc Oct 12, 2021
cf02bd8
setting the browser tests
nikithauc Oct 14, 2021
2929fb0
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Oct 14, 2021
5aaca76
adding abstraction tests
nikithauc Oct 15, 2021
a43fbe8
moving shims, import from node-fetch
nikithauc Oct 15, 2021
8c2f25e
adding eslint
nikithauc Oct 15, 2021
21be144
adding eslint to abstractions
nikithauc Oct 16, 2021
a3089a7
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Oct 16, 2021
ed1580f
middleware return type, getkey definition
nikithauc Oct 26, 2021
e6dcdce
updating tests
nikithauc Oct 28, 2021
b65651e
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Oct 28, 2021
f634353
adding url util
nikithauc Oct 28, 2021
4d2728c
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Nov 8, 2021
af9a3c8
removing url utils
nikithauc Nov 9, 2021
7decd35
minor updates
nikithauc Nov 9, 2021
472f924
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Feb 2, 2022
0274947
deleting the context
nikithauc Feb 2, 2022
2725c92
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Feb 4, 2022
d5acde3
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Feb 5, 2022
889c87a
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Mar 15, 2022
142b70f
removing url util abstractions
nikithauc Mar 15, 2022
e0992a8
using fetch definitions only when required
nikithauc Mar 15, 2022
970f6f6
splitting test src tsconfigs, adding null checks
nikithauc Mar 17, 2022
f4fc008
update tests
nikithauc Mar 18, 2022
3db1f8c
resetting abstractions
nikithauc Mar 18, 2022
e34194d
Apply suggestions from code review
nikithauc Mar 18, 2022
3f5b400
resetting abstractions
nikithauc Mar 18, 2022
c7c654b
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Mar 18, 2022
aead7b0
update version, use set, collapse function
nikithauc Mar 21, 2022
c3667ae
Merge branch 'main' into enhancement/nikithauc-client-middleware-setup
nikithauc Mar 21, 2022
a6fb496
update change logi=
nikithauc Mar 21, 2022
973a69c
Correct case
nikithauc Mar 21, 2022
be93925
version bump, case sensitive check
nikithauc Mar 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
using fetch definitions only when required
  • Loading branch information
nikithauc committed Mar 15, 2022
commit e0992a8cc50de04b9e999f06ebe1d01091b3707d
9,464 changes: 8,573 additions & 891 deletions http/typescript/fetch/package-lock.json

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions http/typescript/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@
"test:es": " npm run build:es && mocha 'dist/es/test/common/**/*.js' --require esm && mocha 'dist/es/test/node/**/*.js' --require esm"
},
"dependencies": {
"@microsoft/kiota-abstractions": "file:microsoft-kiota-abstractions-1.0.21.tgz",
"@microsoft/kiota-abstractions": "1.0.31",
"node-fetch": "^2.6.5",
"tslib": "2.3.1"
},
"devDependencies": {
"@microsoft/kiota-abstractions": "^1.0.30",
"@rollup/plugin-commonjs": "^21.0.0",
"@rollup/plugin-node-resolve": "^13.0.5",
"@types/chai": "^4.2.22",
"@types/mocha": "^9.0.0",
"@types/node": "^17.0.13",
"@types/sinon": "^10.0.4",
"chai": "^4.3.4",
"@microsoft/kiota-abstractions": "^1.0.30",
"cross-fetch": "^3.1.5",
"typescript": "^4.6.2",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"chai": "^4.3.4",
"cross-fetch": "^3.1.5",
"eslint": "^8.11.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
Expand All @@ -72,13 +72,14 @@
"karma-mocha": "^2.0.1",
"karma-typescript": "^5.5.2",
"mocha": "^8.4.0",
"prettier": "^1.19.1",
"prettier": "^2.5.1",
"rollup": "^2.58.0",
"rollup-plugin-terser": "^7.0.2",
"sinon": "^11.1.2",
"tslib": "^2.3.1"
"tslib": "^2.3.1",
"typescript": "^4.6.2"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
}
}
}
5 changes: 3 additions & 2 deletions http/typescript/fetch/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
export * from "../fetchRequestAdapter";
export * from "../httpClient";
export * from "../middlewares/middleware";
export * from "../middlewares/browser/defaultFetchHandler";
export * from "../middlewares/customFetchHandler";
export * from "../middlewares/retryHandler";
export * from "../middlewares/options/retryHandlerOptions";
export * from "../middlewares/middlewareContext";
export * from "../middlewares/browser/middlewareFactory";

export * from "../middlewares/telemetryHandler";

export * from "../utils/headersUtil";
2 changes: 1 addition & 1 deletion http/typescript/fetch/src/fetchRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class FetchRequestAdapter implements RequestAdapter {
await this.authenticationProvider.authenticateRequest(requestInfo);

const request = this.getRequestFromRequestInformation(requestInfo);
return await this.httpClient.executeFetch(requestInfo.URL, request);
return await this.httpClient.executeFetch(requestInfo.URL, request, requestInfo.getRequestOptions());
};
private setBaseUrlForRequestInformation = (requestInfo: RequestInformation): void => {
requestInfo.pathParameters["baseurl"] = this.baseUrl;
Expand Down
25 changes: 9 additions & 16 deletions http/typescript/fetch/src/httpClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type RequestOption } from "@microsoft/kiota-abstractions";

import { CustomFetchHandler } from "./middlewares/customFetchHandler";
import { DefaultFetchHandler } from "./middlewares/defaultFetchHandler";
import { Middleware } from "./middlewares/middleware";
import { MiddlewareFactory } from "./middlewares/middlewareFactory";
import { FetchRequestInfo, FetchRequestInit, FetchResponse } from "./utils/fetchDefinitions";

export class HttpClient {
private middleware: Middleware;
Expand All @@ -14,29 +14,22 @@ export class HttpClient {
* If middlewares param is undefined, the httpClient instance will use the default array of middlewares.
* Set middlewares to `null` if you do not wish to use middlewares.
* If custom fetch is undefined, the httpClient instance uses the `DefaultFetchHandler`
* @param {(request: FetchRequestInfo, init?: FetchRequestInit) => Promise < FetchResponse >} custom fetch function - a Fetch API implementation
* @param {(request: string, init?: RequestInit) => Promise < Response >} custom fetch function - a Fetch API implementation
*
*/
public constructor(private customFetch?: (request: FetchRequestInfo, init?: FetchRequestInit) => Promise<FetchResponse>, ...middlewares: Middleware[]) {
public constructor(private customFetch?: (request: string, init?: RequestInit) => Promise<Response>, ...middlewares: Middleware[]) {
// Use default middleware chain if middlewares and custom fetch function are undefined
if (!middlewares.length) {
if (!middlewares.length || middlewares[0] === null) {
if (this.customFetch) {
this.setMiddleware(...MiddlewareFactory.getDefaultMiddlewareChain(customFetch));
} else {
this.setMiddleware(...MiddlewareFactory.getDefaultMiddlewareChain());
}
} else {
if (middlewares[0] === null) {
if (!customFetch) {
this.setMiddleware(...MiddlewareFactory.getDefaultMiddlewareChain());
}
return;
if (this.customFetch) {
this.setMiddleware(...middlewares, new CustomFetchHandler(customFetch));
} else {
if (this.customFetch) {
this.setMiddleware(...middlewares, new CustomFetchHandler(customFetch));
} else {
this.setMiddleware(...middlewares);
}
this.setMiddleware(...middlewares);
}
}
}
Expand Down Expand Up @@ -79,7 +72,7 @@ export class HttpClient {
* @param options request options.
* @returns the promise resolving the response.
*/
public async executeFetch(url: string, requestInit?: FetchRequestInit, requestOptions?: RequestOption[]): Promise<FetchResponse> {
public async executeFetch(url: string, requestInit?: RequestInit, requestOptions?: Record<string, RequestOption>): Promise<Response> {
if (this.customFetch && !this.middleware) {
return this.customFetch(url, requestInit);
}
Expand Down
5 changes: 3 additions & 2 deletions http/typescript/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
export * from "./fetchRequestAdapter";
export * from "./httpClient";
export * from "./middlewares/middleware";
export * from "./middlewares/defaultFetchHandler";
export * from "./middlewares/customFetchHandler";
export * from "./middlewares/redirectHandler";
export * from "./middlewares/retryHandler";
export * from "./middlewares/options/redirectHandlerOptions";
export * from "./middlewares/options/retryHandlerOptions";
export * from "./middlewares/middlewareContext";
export * from "./middlewares/options/telemetryHandlerOptions";
export * from "./middlewares/telemetryHandler";

export * from "./middlewares/middlewareFactory";
export * from "./utils/headersUtil";
export * from "./utils/fetchDefinitions";
14 changes: 0 additions & 14 deletions http/typescript/fetch/src/middleware.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
* @module MiddlewareFactory
*/

import { FetchRequestInfo, FetchRequestInit, FetchResponse } from "../../utils/fetchDefinitions";
import { CustomFetchHandler } from "../customFetchHandler";
import { Middleware } from "../middleware";
import { RetryHandlerOptions } from "../options/retryHandlerOptions";
import { RetryHandler } from "../retryHandler";
import { DefaultFetchHandler } from "./defaultFetchHandler";

/**
* @class
Expand All @@ -28,7 +26,7 @@ export class MiddlewareFactory {
* @param {AuthenticationProvider} authProvider - The authentication provider instance
* @returns an array of the middleware handlers of the default middleware chain
*/
public static getDefaultMiddlewareChain(customFetch?: (request: FetchRequestInfo, init?: FetchRequestInit) => Promise<FetchResponse>): Middleware[] {
public static getDefaultMiddlewareChain(customFetch?: (request: string, init?: RequestInit) => Promise<Response>): Middleware[] {
// Browsers handles redirection automatically and do not require the redirectionHandler

const middlewareArray: Middleware[] = [];
Expand All @@ -37,7 +35,7 @@ export class MiddlewareFactory {
if (customFetch) {
middlewareArray.push(new CustomFetchHandler(customFetch));
} else {
middlewareArray.push(new CustomFetchHandler(fetch as any));
middlewareArray.push(new CustomFetchHandler(fetch));
}
return middlewareArray;
}
Expand Down
5 changes: 2 additions & 3 deletions http/typescript/fetch/src/middlewares/customFetchHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @module FetchHandler
*/

import { FetchRequestInfo, FetchRequestInit, FetchResponse } from "../utils/fetchDefinitions";
import { Middleware } from "./middleware";

/**
Expand All @@ -25,7 +24,7 @@ export class CustomFetchHandler implements Middleware {
*/
next: Middleware;

constructor(private customFetch?: (input: FetchRequestInfo, init: FetchRequestInit) => Promise<FetchResponse>) {}
constructor(private customFetch?: (input: string, init: RequestInit) => Promise<Response>) {}

/**
* @public
Expand All @@ -34,7 +33,7 @@ export class CustomFetchHandler implements Middleware {
* @param {Context} context - The request context object
* @returns A promise that resolves to nothing
*/
public async execute(url: string, requestInit: FetchRequestInit): Promise<FetchResponse> {
public async execute(url: string, requestInit: RequestInit): Promise<Response> {
return await this.customFetch(url, requestInit);
}
}
5 changes: 2 additions & 3 deletions http/typescript/fetch/src/middlewares/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import {RequestOption} from "@microsoft/kiota-abstractions"
import { FetchRequestInit, FetchResponse } from "../utils/fetchDefinitions";
import {RequestOption} from "@microsoft/kiota-abstractions";
// use import types
/** Defines the contract for a middleware in the request execution pipeline. */
export interface Middleware {
Expand All @@ -18,5 +17,5 @@ export interface Middleware {
* @param url The URL of the request.
* @return A promise that resolves to the response object.
*/
execute(url: string, requestInit: FetchRequestInit, requestOptions?: Record<string, RequestOption>): Promise<FetchResponse>;
execute(url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>): Promise<Response>;
}
5 changes: 2 additions & 3 deletions http/typescript/fetch/src/middlewares/middlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
/**
* @module MiddlewareFactory
*/
import fetch from "node-fetch";

import fetch from "node-fetch";
import { FetchRequestInfo, FetchRequestInit, FetchResponse } from "../utils/fetchDefinitions";
import { CustomFetchHandler } from "./customFetchHandler";
import { Middleware } from "./middleware";
import { RedirectHandlerOptions } from "./options/redirectHandlerOptions";
Expand All @@ -30,7 +29,7 @@ export class MiddlewareFactory {
* @param {AuthenticationProvider} authProvider - The authentication provider instance
* @returns an array of the middleware handlers of the default middleware chain
*/
public static getDefaultMiddlewareChain(customFetch?: (request: FetchRequestInfo, init?: FetchRequestInit) => Promise<FetchResponse>): Middleware[] {
public static getDefaultMiddlewareChain(customFetch?: (request: string, init?: RequestInit) => Promise<Response>): Middleware[] {
const middlewareArray: Middleware[] = [];
const retryHandler = new RetryHandler(new RetryHandlerOptions());
middlewareArray.push(retryHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

import { RequestOption } from "@microsoft/kiota-abstractions";

import { FetchRequestInit, FetchResponse } from "../../utils/fetchDefinitions";
import { FetchResponse } from "../../utils/fetchDefinitions";

/**
* @type
* A type declaration for shouldRetry callback
*/
export type ShouldRetry = (delay: number, attempt: number, request: string, options: FetchRequestInit | undefined, response: FetchResponse) => boolean;
export type ShouldRetry = (delay: number, attempt: number, request: string, options: RequestInit | undefined, response: FetchResponse) => boolean;

export const RetryHandlerOptionKey = "RetryHandlerOptionKey";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RequestOption } from "@microsoft/kiota-abstractions";

export interface TelemetryHandlerOptions extends RequestOption {
telemetryConfigurator: (url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>, telemetryInfomation?: unknown) => void;
telemetryInfomation: unknown;
}
24 changes: 6 additions & 18 deletions http/typescript/fetch/src/middlewares/redirectHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
* @module RedirectHandler
*/

import { HttpMethod } from "@microsoft/kiota-abstractions";
import { type RequestOption, HttpMethod } from "@microsoft/kiota-abstractions";

import { FetchRequestInit, FetchResponse } from "../utils/fetchDefinitions";
import { Middleware } from "./middleware";
import { MiddlewareContext } from "./middlewareContext";
import { RedirectHandlerOptionKey, RedirectHandlerOptions } from "./options/redirectHandlerOptions";

/**
Expand Down Expand Up @@ -139,17 +138,6 @@ export class RedirectHandler implements Middleware {
return typeof requestAuthority !== "undefined" && typeof redirectAuthority !== "undefined" && requestAuthority !== redirectAuthority;
}

/**
* @private
* @async
* To update a request url with the redirect url
* @param {string} redirectUrl - The redirect url value
* @param {Context} context - The context object value
* @returns Nothing
*/
private updateRequestUrl(redirectUrl: string, context: MiddlewareContext) {
context.requestUrl = redirectUrl;
}

/**
* @private
Expand All @@ -161,7 +149,7 @@ export class RedirectHandler implements Middleware {
* @returns A promise that resolves to nothing
*/
private async executeWithRedirect(url: string, fetchRequestInit: FetchRequestInit, redirectCount: number, requestOptions?: Record<string, RequestOption>): Promise<FetchResponse> {
const response = await this.next.execute(url, fetchRequestInit, requestOptions);
const response = await this.next.execute(url, fetchRequestInit as RequestInit, requestOptions);
if (redirectCount < this.options.maxRedirects && this.isRedirect(response) && this.hasLocationHeader(response) && this.options.shouldRedirect(response)) {
++redirectCount;
if (response.status === RedirectHandler.STATUS_CODE_SEE_OTHER) {
Expand All @@ -187,13 +175,13 @@ export class RedirectHandler implements Middleware {
* @param {Context} context - The context object of the request
* @returns A Promise that resolves to nothing
*/
public execute(url: string, requestInit: FetchRequestInit, requestOptions?: Record<string, RequestOption>): Promise<FetchResponse> {
public execute(url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>): Promise<FetchResponse> {
const redirectCount = 0;
if (requestOptions && requestOptions[RedirectHandlerOptionKey]))
if (requestOptions && requestOptions[RedirectHandlerOptionKey])
{
this.options = requestOptions[RedirectHandlerOptionKey] as RedirectHandlerOptions;
}
requestInit.redirect = RedirectHandler.MANUAL_REDIRECT;
return this.executeWithRedirect(url, requestInit, redirectCount, requestOptions);
(requestInit as FetchRequestInit).redirect = RedirectHandler.MANUAL_REDIRECT;
return this.executeWithRedirect(url, requestInit as FetchRequestInit, redirectCount, requestOptions);
}
}
Loading