Skip to content

feat(legacyCreateProxyMiddleware): show migration tips #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2022
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
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);
});
});