Skip to content

Commit

Permalink
feat(legacyCreateProxyMiddleware): show migration tips when legacy fe…
Browse files Browse the repository at this point in the history
…atures are used
  • Loading branch information
chimurai committed Apr 19, 2022
1 parent cdfe934 commit aa6cd6f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/legacy/options-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as url from 'url';
import { Filter, Options } from '..';
import { LegacyOptions } from './types';
import { Debug } from '../debug';
import { getLogger } from '../logger';
import { Logger } from '../types';

const debug = Debug.extend('legacy-options-adapter');

Expand All @@ -23,6 +25,7 @@ export function legacyOptionsAdapter(
legacyOptions: LegacyOptions
): Options {
let options: LegacyOptions;
let logger: Logger;

// https://github.com/chimurai/http-proxy-middleware/pull/716
if (typeof legacyContext === 'string' && !!url.parse(legacyContext).host) {
Expand All @@ -37,8 +40,21 @@ export function legacyOptionsAdapter(
if (legacyContext && legacyOptions) {
debug('map legacy context/filter to options.pathFilter');
options = { ...legacyOptions, pathFilter: legacyContext as Filter };
logger = getLegacyLogger(options);

logger.warn(
`[http-proxy-middleware] Legacy "context" argument is deprecated. Migrate your "context" to "options.pathFilter":
const options = {
pathFilter: '${legacyContext}',
}
More details: https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md
`
);
} else if (legacyContext && !legacyOptions) {
options = { ...(legacyContext as Options) };
logger = getLegacyLogger(options);
}

// map old event names to new event names
Expand All @@ -48,6 +64,19 @@ export function legacyOptionsAdapter(
options.on = { ...options.on };
options.on[proxyEventName] = options[legacyEventName];
debug('map legacy event "%s" to "on.%s"', legacyEventName, proxyEventName);

logger.warn(
`[http-proxy-middleware] Legacy "${legacyEventName}" is deprecated. Migrate to "options.on.${proxyEventName}":
const options = {
on: {
${proxyEventName}: () => {},
},
}
More details: https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md
`
);
}
});

Expand All @@ -59,9 +88,27 @@ export function legacyOptionsAdapter(
debug('legacy logProvider: %O', logProvider);

if (typeof logLevel === 'string' && logLevel !== 'silent') {
options.logger = logProvider;
debug('map "logProvider" to "logger"');

logger.warn(
`[http-proxy-middleware] Legacy "logLevel" and "logProvider" are deprecated. Migrate to "options.logger":
const options = {
logger: console,
}
More details: https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md
`
);
}

return options;
}

function getLegacyLogger(options): Logger {
const legacyLogger = options.logProvider && options.logProvider();
if (legacyLogger) {
options.logger = legacyLogger;
}
return getLogger(options);
}
1 change: 1 addition & 0 deletions test/legacy/http-proxy-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@ describe('legacyCreateProxyMiddleware()', () => {
expect(response.text).toBe('my legacy error');

expect(mockLogger.error).toHaveBeenCalledTimes(1);
expect(mockLogger.warn).toHaveBeenCalledTimes(2);
});
});

0 comments on commit aa6cd6f

Please sign in to comment.