Skip to content

Commit

Permalink
feat: adds url replacement as a default (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev authored Jan 9, 2025
1 parent ac2da8f commit acca423
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ParametersNameDecodingHandler } from "../parametersNameDecodingHandler"
import { RetryHandler } from "../retryHandler";
import { UserAgentHandler } from "../userAgentHandler";
import { CompressionHandler } from "../compressionHandler";
import { UrlReplaceHandler } from "../urlReplaceHandler";

/**
*
Expand All @@ -26,6 +27,6 @@ export class MiddlewareFactory {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
public static getDefaultMiddlewares(customFetch: (request: string, init: RequestInit) => Promise<Response> = (...args) => fetch(...args) as any): Middleware[] {
// Browsers handles redirection automatically and do not require the redirectionHandler
return [new RetryHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new CompressionHandler(), new HeadersInspectionHandler(), new CustomFetchHandler(customFetch)];
return [new RetryHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new CompressionHandler(), new HeadersInspectionHandler(), new UrlReplaceHandler(), new CustomFetchHandler(customFetch)];
}
}
3 changes: 2 additions & 1 deletion packages/http/fetch/src/middlewares/middlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RedirectHandler } from "./redirectHandler";
import { RetryHandler } from "./retryHandler";
import { UserAgentHandler } from "./userAgentHandler";
import { CompressionHandler } from "./compressionHandler";
import { UrlReplaceHandler } from "./urlReplaceHandler";

/**
*
Expand All @@ -26,6 +27,6 @@ export class MiddlewareFactory {
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
public static getDefaultMiddlewares(customFetch: (request: string, init: RequestInit) => Promise<Response> = (...args) => fetch(...args) as any): Middleware[] {
return [new RetryHandler(), new RedirectHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new CompressionHandler(), new HeadersInspectionHandler(), new CustomFetchHandler(customFetch)];
return [new RetryHandler(), new RedirectHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new CompressionHandler(), new HeadersInspectionHandler(), new UrlReplaceHandler(), new CustomFetchHandler(customFetch)];
}
}
10 changes: 6 additions & 4 deletions packages/http/fetch/test/browser/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { assert, describe, it } from "vitest";

import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, Middleware, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UserAgentHandler } from "../../src";
import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, Middleware, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src";
import { DummyFetchHandler } from "../common/middleware/dummyFetchHandler";
import { CompressionHandler } from "../../src/middlewares/compressionHandler";

Expand Down Expand Up @@ -42,7 +42,8 @@ describe("browser - HTTPClient.ts", () => {
assert.isTrue(next?.next?.next instanceof UserAgentHandler);
assert.isTrue(next?.next?.next?.next instanceof CompressionHandler);
assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler);
assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
});

it("Should set default middleware array with customFetchHandler if middleware parameter is undefined && customFetch is defined", () => {
Expand All @@ -58,7 +59,8 @@ describe("browser - HTTPClient.ts", () => {
assert.isTrue(next?.next?.next instanceof UserAgentHandler);
assert.isTrue(next?.next?.next?.next instanceof CompressionHandler);
assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler);
assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
});

it("Should set to default fetch handler middleware array if middleware parameter is null && customFetch is undefined", () => {
Expand All @@ -74,7 +76,7 @@ describe("browser - HTTPClient.ts", () => {

assert.isDefined(client["middleware"]);
assert.equal(client["customFetch"], dummyCustomFetch);
assert.isTrue(client["middleware"]?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
assert.isTrue(client["middleware"]?.next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
});
});
});
5 changes: 3 additions & 2 deletions packages/http/fetch/test/browser/kiotaClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { assert, describe, it } from "vitest";

import { CustomFetchHandler, HeadersInspectionHandler, KiotaClientFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UserAgentHandler } from "../../src";
import { CustomFetchHandler, HeadersInspectionHandler, KiotaClientFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src";
import { CompressionHandler } from "../../src/middlewares/compressionHandler";

describe("browser - KiotaClientFactory", () => {
Expand All @@ -22,6 +22,7 @@ describe("browser - KiotaClientFactory", () => {
assert.isTrue(middleware?.next?.next?.next instanceof UserAgentHandler);
assert.isTrue(middleware?.next?.next?.next?.next instanceof CompressionHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next instanceof HeadersInspectionHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
});
});
7 changes: 4 additions & 3 deletions packages/http/fetch/test/browser/middlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
*/

import { assert, describe, it } from "vitest";
import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UserAgentHandler } from "../../src";
import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src";
import { CompressionHandler } from "../../src/middlewares/compressionHandler";

describe("browser - MiddlewareFactory", () => {
it("Should return the default pipeline", () => {
const defaultMiddleWareArray = MiddlewareFactory.getDefaultMiddlewares();
assert.equal(defaultMiddleWareArray.length, 7);
assert.equal(defaultMiddleWareArray.length, 8);
assert.isTrue(defaultMiddleWareArray[0] instanceof RetryHandler);
assert.isTrue(defaultMiddleWareArray[1] instanceof RedirectHandler);
assert.isTrue(defaultMiddleWareArray[2] instanceof ParametersNameDecodingHandler);
assert.isTrue(defaultMiddleWareArray[3] instanceof UserAgentHandler);
assert.isTrue(defaultMiddleWareArray[4] instanceof CompressionHandler);
assert.isTrue(defaultMiddleWareArray[5] instanceof HeadersInspectionHandler);
assert.isTrue(defaultMiddleWareArray[6] instanceof CustomFetchHandler);
assert.isTrue(defaultMiddleWareArray[6] instanceof UrlReplaceHandler);
assert.isTrue(defaultMiddleWareArray[7] instanceof CustomFetchHandler);
});
});
7 changes: 4 additions & 3 deletions packages/http/fetch/test/node/MiddlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

import { assert, describe, it } from "vitest";

import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UserAgentHandler } from "../../src";
import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src";
import { CompressionHandler } from "../../src/middlewares/compressionHandler";

describe("node - MiddlewareFactory", () => {
it("Should return the default pipeline", () => {
const defaultMiddleWareArray = MiddlewareFactory.getDefaultMiddlewares();
assert.equal(defaultMiddleWareArray.length, 7);
assert.equal(defaultMiddleWareArray.length, 8);

assert.isTrue(defaultMiddleWareArray[0] instanceof RetryHandler);
assert.isTrue(defaultMiddleWareArray[1] instanceof RedirectHandler);
assert.isTrue(defaultMiddleWareArray[2] instanceof ParametersNameDecodingHandler);
assert.isTrue(defaultMiddleWareArray[3] instanceof UserAgentHandler);
assert.isTrue(defaultMiddleWareArray[4] instanceof CompressionHandler);
assert.isTrue(defaultMiddleWareArray[5] instanceof HeadersInspectionHandler);
assert.isTrue(defaultMiddleWareArray[6] instanceof CustomFetchHandler);
assert.isTrue(defaultMiddleWareArray[6] instanceof UrlReplaceHandler);
assert.isTrue(defaultMiddleWareArray[7] instanceof CustomFetchHandler);
});
});
8 changes: 5 additions & 3 deletions packages/http/fetch/test/node/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { assert, describe, it } from "vitest";

import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UserAgentHandler } from "../../src";
import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src";
import { DummyFetchHandler } from "../common/middleware/dummyFetchHandler";
import { CompressionHandler } from "../../src/middlewares/compressionHandler";

Expand Down Expand Up @@ -42,7 +42,8 @@ describe("node - HTTPClient.ts", () => {
assert.isTrue(next?.next?.next instanceof UserAgentHandler);
assert.isTrue(next?.next?.next?.next instanceof CompressionHandler);
assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler);
assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
});

it("Should set default middleware array with customFetchHandler if middleware parameter is undefined && customFetch is defined", () => {
Expand All @@ -59,7 +60,8 @@ describe("node - HTTPClient.ts", () => {
assert.isTrue(next?.next?.next instanceof UserAgentHandler);
assert.isTrue(next?.next?.next?.next instanceof CompressionHandler);
assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler);
assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
});

it("Should set to default fetch handler middleware array if middleware parameter is null && customFetch is undefined", () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/http/fetch/test/node/kiotaClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { assert, describe, it } from "vitest";
import { CustomFetchHandler, HeadersInspectionHandler, KiotaClientFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UserAgentHandler } from "../../src";
import { CustomFetchHandler, HeadersInspectionHandler, KiotaClientFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src";
import { CompressionHandler } from "../../src/middlewares/compressionHandler";

describe("browser - KiotaClientFactory", () => {
Expand All @@ -21,7 +21,8 @@ describe("browser - KiotaClientFactory", () => {
assert.isTrue(middleware?.next?.next?.next instanceof UserAgentHandler);
assert.isTrue(middleware?.next?.next?.next?.next instanceof CompressionHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next instanceof HeadersInspectionHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler);
assert.isTrue(middleware?.next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler);
});

it("Should maintain the middleware array order", () => {
Expand Down

0 comments on commit acca423

Please sign in to comment.