Skip to content

Commit 098b12c

Browse files
dsp-antclaude
andcommitted
refactor: rename fetchWrapper to middleware and move to client directory
- Renamed fetchWrapper.ts to middleware.ts for cleaner naming - Renamed FetchMiddleware type to Middleware (kept deprecated aliases) - Moved middleware files from src/shared/ to src/client/ since this is client-specific - Updated all imports to reflect new location - Fixed test for short-circuit responses The middleware is now properly located in the client directory where it belongs, separate from any server-side middleware. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent f24a4bf commit 098b12c

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/shared/fetchWrapper.test.ts renamed to src/client/middleware.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {
33
withLogging,
44
applyMiddleware,
55
createMiddleware,
6-
} from "./fetchWrapper.js";
7-
import { OAuthClientProvider } from "../client/auth.js";
8-
import { FetchLike } from "./transport.js";
6+
} from "./middleware.js";
7+
import { OAuthClientProvider } from "./auth.js";
8+
import { FetchLike } from "../shared/transport.js";
99

1010
jest.mock("../client/auth.js", () => {
1111
const actual = jest.requireActual("../client/auth.js");
@@ -16,7 +16,7 @@ jest.mock("../client/auth.js", () => {
1616
};
1717
});
1818

19-
import { auth, extractResourceMetadataUrl } from "../client/auth.js";
19+
import { auth, extractResourceMetadataUrl } from "./auth.js";
2020

2121
const mockAuth = auth as jest.MockedFunction<typeof auth>;
2222
const mockExtractResourceMetadataUrl =
@@ -1091,6 +1091,8 @@ describe("createMiddleware", () => {
10911091

10921092
// Test normal route
10931093
mockFetch.mockResolvedValue(new Response("fresh data", { status: 200 }));
1094+
const normalResponse = await enhancedFetch("https://example.com/normal/data");
1095+
expect(await normalResponse.text()).toBe("fresh data");
10941096
expect(mockFetch).toHaveBeenCalledTimes(1);
10951097
});
10961098

src/shared/fetchWrapper.ts renamed to src/client/middleware.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ import {
33
extractResourceMetadataUrl,
44
OAuthClientProvider,
55
UnauthorizedError,
6-
} from "../client/auth.js";
7-
import { FetchLike } from "./transport.js";
6+
} from "./auth.js";
7+
import { FetchLike } from "../shared/transport.js";
88

99
/**
1010
* Middleware function that wraps and enhances fetch functionality.
1111
* Takes a fetch handler and returns an enhanced fetch handler.
1212
*/
13-
export type FetchMiddleware = (next: FetchLike) => FetchLike;
13+
export type Middleware = (next: FetchLike) => FetchLike;
1414

1515
/**
16-
* @deprecated Use FetchMiddleware instead
16+
* @deprecated Use Middleware instead
1717
*/
18-
export type FetchWrapper = FetchMiddleware;
18+
export type FetchWrapper = Middleware;
19+
20+
/**
21+
* @deprecated Use Middleware instead
22+
*/
23+
export type FetchMiddleware = Middleware;
1924

2025
/**
2126
* Creates a fetch wrapper that handles OAuth authentication automatically.
@@ -44,7 +49,7 @@ export type FetchWrapper = FetchMiddleware;
4449
* @returns A fetch middleware function
4550
*/
4651
export const withOAuth =
47-
(provider: OAuthClientProvider, baseUrl?: string | URL): FetchMiddleware =>
52+
(provider: OAuthClientProvider, baseUrl?: string | URL): Middleware =>
4853
(next) => {
4954
return async (input, init) => {
5055
const makeRequest = async (): Promise<Response> => {
@@ -167,7 +172,7 @@ export type LoggingOptions = {
167172
* @param options - Logging configuration options
168173
* @returns A fetch middleware function
169174
*/
170-
export const withLogging = (options: LoggingOptions = {}): FetchMiddleware => {
175+
export const withLogging = (options: LoggingOptions = {}): Middleware => {
171176
const {
172177
logger,
173178
includeRequestHeaders = false,
@@ -282,8 +287,8 @@ export const withLogging = (options: LoggingOptions = {}): FetchMiddleware => {
282287
* @returns A single composed middleware function
283288
*/
284289
export const applyMiddleware = (
285-
...middleware: FetchMiddleware[]
286-
): FetchMiddleware => {
290+
...middleware: Middleware[]
291+
): Middleware => {
287292
return (next) => {
288293
return middleware.reduce((handler, mw) => mw(handler), next);
289294
};
@@ -353,6 +358,6 @@ export const createMiddleware = (
353358
input: string | URL,
354359
init?: RequestInit,
355360
) => Promise<Response>,
356-
): FetchMiddleware => {
361+
): Middleware => {
357362
return (next) => (input, init) => handler(next, input as string | URL, init);
358363
};

0 commit comments

Comments
 (0)