Skip to content

Commit

Permalink
fix(camelcase): remove camelcase dependency (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai authored Apr 6, 2021
1 parent ba3391f commit 40e8763
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## next

- refactor(dependency): remove `camelcase`
- fix(option): optional `target` when `router` is used ([#512](https://github.com/chimurai/http-proxy-middleware/pull/512))

## [v1.1.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.0)
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-proxy-middleware",
"version": "1.1.0",
"version": "1.1.1-alpha.1",
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -78,7 +78,6 @@
},
"dependencies": {
"@types/http-proxy": "^1.17.5",
"camelcase": "^6.2.0",
"http-proxy": "^1.18.1",
"is-glob": "^4.0.1",
"is-plain-obj": "^3.0.0",
Expand Down
20 changes: 14 additions & 6 deletions src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type * as express from 'express';
import type { Options } from './types';
import type * as httpProxy from 'http-proxy';
import camelcase = require('camelcase');
import { getInstance } from './logger';
const logger = getInstance();

Expand All @@ -15,20 +14,29 @@ export function init(proxy: httpProxy, option: Options): void {
logger.debug('[HPM] Subscribed to http-proxy events:', Object.keys(handlers));
}

type HttpProxyEventName = 'error' | 'proxyReq' | 'proxyReqWs' | 'proxyRes' | 'open' | 'close';

export function getHandlers(options: Options) {
// https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
const proxyEvents = ['error', 'proxyReq', 'proxyReqWs', 'proxyRes', 'open', 'close'];
const proxyEventsMap: Record<HttpProxyEventName, string> = {
error: 'onError',
proxyReq: 'onProxyReq',
proxyReqWs: 'onProxyReqWs',
proxyRes: 'onProxyRes',
open: 'onOpen',
close: 'onClose',
};

const handlers: any = {};

for (const event of proxyEvents) {
for (const [eventName, onEventName] of Object.entries(proxyEventsMap)) {
// all handlers for the http-proxy events are prefixed with 'on'.
// loop through options and try to find these handlers
// and add them to the handlers object for subscription in init().
const eventName = camelcase('on ' + event);
const fnHandler = options ? options[eventName] : null;
const fnHandler = options ? options[onEventName] : null;

if (typeof fnHandler === 'function') {
handlers[event] = fnHandler;
handlers[eventName] = fnHandler;
}
}

Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1691,11 +1691,6 @@ camelcase@^6.0.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.0.0.tgz#5259f7c30e35e278f1bdc2a4d91230b37cad981e"
integrity sha512-8KMDF1Vz2gzOq54ONPJS65IvTUaB1cHJ2DMM7MbPmLZljDH1qpzzLsWdiN9pHh6qvkRVDTi/07+eNGch/oLU4w==

camelcase@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809"
integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==

capture-exit@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4"
Expand Down

0 comments on commit 40e8763

Please sign in to comment.