-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7da610a
commit 84e2e1a
Showing
6 changed files
with
239 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { CacheManager } from "./shared-types"; | ||
|
||
type RequestMiddlewareOptions = { | ||
fetchOptions: RequestInit; | ||
ttl?: number; // seconds | ||
cache: CacheManager; | ||
}; | ||
|
||
type BeforeMiddlewaresResult = { | ||
url: string, | ||
fetchOptions: RequestInit, | ||
ttl?: number | ||
}; | ||
|
||
export type BeforeMiddlewareResult = { | ||
url?: string; | ||
ttl?: number; | ||
fetchOptions?: RequestInit; | ||
}; | ||
|
||
export type BeforeMiddleware = (url: string, requestOptions: RequestMiddlewareOptions) => | ||
BeforeMiddlewareResult | undefined | null; | ||
|
||
export type AfterMiddleware = <T>(serverData: T) => T; | ||
|
||
export type Middleware = BeforeMiddleware | AfterMiddleware; | ||
|
||
const middlewares = { | ||
before: [] as BeforeMiddleware[], | ||
after: [] as AfterMiddleware[] | ||
}; | ||
|
||
export function addMiddleware(type: "before" | "after", middleware: Middleware): void { | ||
if (type === 'before') { | ||
middlewares.before.push(middleware as BeforeMiddleware); | ||
} | ||
|
||
if (type === 'after') { | ||
middlewares.after.push(middleware as AfterMiddleware); | ||
} | ||
} | ||
|
||
export function removeMiddleware(middleware: Middleware): Middleware | undefined { | ||
let index = middlewares.before.indexOf(middleware as BeforeMiddleware); | ||
if (index !== -1) { | ||
return middlewares.before.splice(index, 1)[0]; | ||
} | ||
|
||
index = middlewares.after.indexOf(middleware as AfterMiddleware); | ||
if (index !== -1) { | ||
return middlewares.after.splice(index, 1)[0]; | ||
} | ||
} | ||
|
||
export function runBeforeMiddlewares( | ||
url: string, | ||
{ fetchOptions, ttl, cache }: RequestMiddlewareOptions | ||
): BeforeMiddlewaresResult { | ||
return middlewares.before.reduce<BeforeMiddlewaresResult>((params, middleware) => { | ||
const result = middleware( | ||
params.url, { | ||
fetchOptions: params.fetchOptions, | ||
ttl: params.ttl, | ||
cache | ||
} | ||
); | ||
|
||
return { | ||
url: result?.url ?? params.url, | ||
fetchOptions: result?.fetchOptions ?? params.fetchOptions, | ||
ttl: result?.ttl ?? params.ttl | ||
} as BeforeMiddlewaresResult; | ||
}, { url, fetchOptions, ttl }); | ||
} | ||
|
||
export function runAfterMiddlewares<T>(serverData: T): T { | ||
return middlewares.after.reduce((data, middleware) => middleware(data), serverData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { get, post, addMiddleware, removeMiddleware } from '../src/fetcher'; | ||
import { AfterMiddleware, BeforeMiddleware, BeforeMiddlewareResult } from '../src/middleware-manager'; | ||
|
||
const BASE_URL = 'https://localhost'; | ||
|
||
describe("Fetcher", () => { | ||
|
||
describe('Middlewares', ()=> { | ||
|
||
it('Should add "before" middlewares', async () => { | ||
const initialId = '15'; | ||
const updatedId = '23'; | ||
const mockChangeUrlMiddleware = jest.fn() as jest.MockedFunction<BeforeMiddleware>; | ||
mockChangeUrlMiddleware.mockImplementation((url, _requestOptions) => ({ | ||
url: url.replace(initialId, updatedId) | ||
})); | ||
const mockAddHeaderMiddleware = jest.fn() as jest.MockedFunction<BeforeMiddleware>; | ||
mockAddHeaderMiddleware.mockImplementation((_url, requestOptions) => ({ | ||
fetchOptions: { | ||
...requestOptions.fetchOptions, | ||
headers: { | ||
...requestOptions.fetchOptions.headers, | ||
'X-Custom-Header': 'custom-value' | ||
} | ||
} | ||
})); | ||
addMiddleware('before', mockChangeUrlMiddleware); | ||
addMiddleware('before', mockAddHeaderMiddleware); | ||
|
||
const user = await get(`${BASE_URL}/api/user/${initialId}`); | ||
|
||
const [url1] = mockChangeUrlMiddleware.mock.calls[0]; | ||
const [url2] = mockAddHeaderMiddleware.mock.calls[0]; | ||
expect(url1).toContain(initialId); | ||
expect(url2).toContain(updatedId); | ||
|
||
expect(user).toHaveProperty('id', Number(updatedId)); | ||
expect(user).toHaveProperty('meta', 'custom-value'); | ||
}); | ||
|
||
it('Should add "after" middlewares', async () => { | ||
const avgPoints = 50; | ||
const avgRebounds = 10; | ||
const mockAddMetaAvgPointsMiddleware = jest.fn() as jest.MockedFunction<AfterMiddleware>; | ||
mockAddMetaAvgPointsMiddleware.mockImplementation((serverData: any) => ({ | ||
...serverData, | ||
meta: { avgPoints } | ||
})); | ||
const mockAddMetaAvgReboundsMiddleware = jest.fn() as jest.MockedFunction<AfterMiddleware>; | ||
mockAddMetaAvgReboundsMiddleware.mockImplementation((serverData: any) => ({ | ||
...serverData, | ||
meta: { | ||
...serverData.meta, | ||
avgRebounds | ||
} | ||
})); | ||
|
||
addMiddleware('after', mockAddMetaAvgPointsMiddleware); | ||
addMiddleware('after', mockAddMetaAvgReboundsMiddleware); | ||
|
||
const user = await get(`${BASE_URL}/api/user/15`); | ||
|
||
expect(user).toHaveProperty('meta.avgPoints', avgPoints); | ||
expect(user).toHaveProperty('meta.avgRebounds', avgRebounds); | ||
}); | ||
|
||
it('Should add both "before" and "after" middlewares', async () => { | ||
const initialId = '15'; | ||
const updatedId = '23'; | ||
const avgPoints = 50; | ||
const mockChangeUrlMiddleware = jest.fn() as jest.MockedFunction<BeforeMiddleware>; | ||
mockChangeUrlMiddleware.mockImplementation((url, _requestOptions) => ({ | ||
url: url.replace(initialId, updatedId) | ||
})); | ||
const mockAddMetaAvgPointsMiddleware = jest.fn() as jest.MockedFunction<AfterMiddleware>; | ||
mockAddMetaAvgPointsMiddleware.mockImplementation((serverData: any) => ({ | ||
...serverData, | ||
meta: { avgPoints } | ||
})); | ||
|
||
addMiddleware('before', mockChangeUrlMiddleware); | ||
addMiddleware('after', mockAddMetaAvgPointsMiddleware); | ||
|
||
const user = await get(`${BASE_URL}/api/user/${initialId}`); | ||
|
||
expect(user).toHaveProperty('id', Number(updatedId)); | ||
expect(user).toHaveProperty('meta.avgPoints', avgPoints); | ||
}); | ||
|
||
it('Should remove middlewares', async () => { | ||
const mockBeforeMiddleware = jest.fn(); | ||
const mockAfterMiddleware = jest.fn((serverData) => serverData); | ||
|
||
addMiddleware('before', mockBeforeMiddleware); | ||
addMiddleware('after', mockAfterMiddleware); | ||
|
||
await get(`${BASE_URL}/api/user/15`); | ||
expect(mockBeforeMiddleware).toHaveBeenCalled(); | ||
expect(mockAfterMiddleware).toHaveBeenCalled(); | ||
|
||
removeMiddleware(mockBeforeMiddleware); | ||
await get(`${BASE_URL}/api/user/15`); | ||
expect(mockBeforeMiddleware).toHaveBeenCalledTimes(1); | ||
expect(mockAfterMiddleware).toHaveBeenCalledTimes(2); | ||
|
||
removeMiddleware(mockAfterMiddleware); | ||
await get(`${BASE_URL}/api/user/15`); | ||
expect(mockBeforeMiddleware).toHaveBeenCalledTimes(1); | ||
expect(mockAfterMiddleware).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
import { rest } from 'msw'; | ||
|
||
type RequestBody = { | ||
id?: string; | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
const BASE_URL = 'https://localhost'; | ||
|
||
export const handlers = [ | ||
|
@@ -75,12 +69,14 @@ export const handlers = [ | |
} | ||
|
||
if (req.params.userId === '23') { | ||
const customHeader = req.headers.get('X-Custom-Header'); | ||
return res( | ||
ctx.status(200), | ||
ctx.json({ | ||
id: 23, | ||
name: 'Michael', | ||
email: '[email protected]' | ||
email: '[email protected]', | ||
meta: customHeader | ||
}) | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters