Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/path-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function matchPathFilter<TReq extends http.IncomingMessage = http.Incomin
// custom matching
if (typeof pathFilter === 'function') {
const pathname = getUrlPathName(uri) as string;
return pathFilter(pathname, req as TReq);
return Boolean(pathFilter(pathname, req as TReq));
}

throw new Error(ERRORS.ERR_CONTEXT_MATCHER_GENERIC);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface RequestHandler<
export type Filter<TReq extends http.IncomingMessage = http.IncomingMessage> =
| string
| string[]
| ((pathname: string, req: TReq) => boolean);
| ((pathname: string, req: TReq) => boolean | string | RegExpMatchArray | null);

/**
* @see {@link https://github.com/chimurai/http-proxy-middleware/tree/master#defineplugin-helper `definePlugin()`} to define a http-proxy-middleware plugin.
Expand Down
18 changes: 18 additions & 0 deletions test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ describe('http-proxy-middleware TypeScript Types', () => {
expect(proxy).toBeDefined();
});

it('should create proxy with truthy filter', () => {
const proxy = middleware({
...options,
pathFilter: (path, req) => path.match('^/api') && req.method === 'GET',
});
expect(proxy).toBeDefined();
});

it('should create proxy with truthy regex match filter', () => {
const proxy = middleware({ ...options, pathFilter: (path, req) => path.match('^/api') });
expect(proxy).toBeDefined();
});

it('should create proxy with truthy regex test filter', () => {
const proxy = middleware({ ...options, pathFilter: (path, req) => /^\/api/.test(path) });
expect(proxy).toBeDefined();
});
Comment thread
chimurai marked this conversation as resolved.

it('should create proxy with manual websocket upgrade function', () => {
const proxy = middleware({ ...options, pathFilter: (path, req) => true });
expect(proxy.upgrade).toBeDefined();
Expand Down
Loading