Skip to content

Commit ba3391f

Browse files
chimuraiSteven Chim
andauthored
chore(typescript): add more types (#514)
Co-authored-by: Steven Chim <[email protected]>
1 parent 1515ba3 commit ba3391f

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/context-matcher.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
import type { Filter, Request } from './types';
12
import * as isGlob from 'is-glob';
23
import * as micromatch from 'micromatch';
34
import * as url from 'url';
45
import { ERRORS } from './errors';
56

6-
export function match(context, uri, req) {
7+
export function match(context: Filter, uri: string, req: Request): boolean {
78
// single path
8-
if (isStringPath(context)) {
9-
return matchSingleStringPath(context, uri);
9+
if (isStringPath(context as string)) {
10+
return matchSingleStringPath(context as string, uri);
1011
}
1112

1213
// single glob path
13-
if (isGlobPath(context)) {
14-
return matchSingleGlobPath(context, uri);
14+
if (isGlobPath(context as string)) {
15+
return matchSingleGlobPath(context as string[], uri);
1516
}
1617

1718
// multi path
@@ -20,7 +21,7 @@ export function match(context, uri, req) {
2021
return matchMultiPath(context, uri);
2122
}
2223
if (context.every(isGlobPath)) {
23-
return matchMultiGlobPath(context, uri);
24+
return matchMultiGlobPath(context as string[], uri);
2425
}
2526

2627
throw new Error(ERRORS.ERR_CONTEXT_MATCHER_INVALID_ARRAY);
@@ -40,18 +41,18 @@ export function match(context, uri, req) {
4041
* @param {String} uri 'http://example.org/api/b/c/d.html'
4142
* @return {Boolean}
4243
*/
43-
function matchSingleStringPath(context, uri) {
44+
function matchSingleStringPath(context: string, uri: string) {
4445
const pathname = getUrlPathName(uri);
4546
return pathname.indexOf(context) === 0;
4647
}
4748

48-
function matchSingleGlobPath(pattern, uri) {
49+
function matchSingleGlobPath(pattern: string | string[], uri: string) {
4950
const pathname = getUrlPathName(uri);
5051
const matches = micromatch([pathname], pattern);
5152
return matches && matches.length > 0;
5253
}
5354

54-
function matchMultiGlobPath(patternList, uri) {
55+
function matchMultiGlobPath(patternList: string | string[], uri: string) {
5556
return matchSingleGlobPath(patternList, uri);
5657
}
5758

@@ -60,7 +61,7 @@ function matchMultiGlobPath(patternList, uri) {
6061
* @param {String} uri 'http://example.org/api/b/c/d.html'
6162
* @return {Boolean}
6263
*/
63-
function matchMultiPath(contextList, uri) {
64+
function matchMultiPath(contextList: string[], uri: string) {
6465
let isMultiPath = false;
6566

6667
for (const context of contextList) {
@@ -79,14 +80,14 @@ function matchMultiPath(contextList, uri) {
7980
* @param {String} uri from req.url
8081
* @return {String} RFC 3986 path
8182
*/
82-
function getUrlPathName(uri) {
83+
function getUrlPathName(uri: string) {
8384
return uri && url.parse(uri).pathname;
8485
}
8586

86-
function isStringPath(context) {
87+
function isStringPath(context: string) {
8788
return typeof context === 'string' && !isGlob(context);
8889
}
8990

90-
function isGlobPath(context) {
91+
function isGlobPath(context: string) {
9192
return isGlob(context);
9293
}

src/handlers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type * as express from 'express';
2+
import type { Options } from './types';
3+
import type * as httpProxy from 'http-proxy';
24
import camelcase = require('camelcase');
35
import { getInstance } from './logger';
46
const logger = getInstance();
57

6-
export function init(proxy, option) {
8+
export function init(proxy: httpProxy, option: Options): void {
79
const handlers = getHandlers(option);
810

911
for (const eventName of Object.keys(handlers)) {
@@ -13,7 +15,7 @@ export function init(proxy, option) {
1315
logger.debug('[HPM] Subscribed to http-proxy events:', Object.keys(handlers));
1416
}
1517

16-
export function getHandlers(options) {
18+
export function getHandlers(options: Options) {
1719
// https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
1820
const proxyEvents = ['error', 'proxyReq', 'proxyReqWs', 'proxyRes', 'open', 'close'];
1921
const handlers: any = {};

src/http-proxy-middleware.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as https from 'https';
2-
import * as express from 'express';
1+
import type * as https from 'https';
2+
import type * as express from 'express';
3+
import type { Filter, Request, RequestHandler, Response, Options } from './types';
34
import * as httpProxy from 'http-proxy';
45
import { createConfig, Config } from './config-factory';
56
import * as contextMatcher from './context-matcher';
67
import * as handlers from './handlers';
78
import { getArrow, getInstance } from './logger';
89
import * as PathRewriter from './path-rewriter';
910
import * as Router from './router';
10-
import { Filter, Request, RequestHandler, Response, Options } from './types';
1111

1212
export class HttpProxyMiddleware {
1313
private logger = getInstance();
@@ -109,7 +109,7 @@ export class HttpProxyMiddleware {
109109
* @param {Object} req [description]
110110
* @return {Boolean}
111111
*/
112-
private shouldProxy = (context, req: Request) => {
112+
private shouldProxy = (context, req: Request): boolean => {
113113
const path = req.originalUrl || req.url;
114114
return contextMatcher.match(context, path, req);
115115
};

test/unit/context-matcher.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import type { Request } from '../../src/types';
12
import * as contextMatcher from '../../src/context-matcher';
23

34
describe('Context Matching', () => {
4-
const fakeReq = {};
5+
const fakeReq = {} as Request;
56

67
describe('String path matching', () => {
78
let result;

0 commit comments

Comments
 (0)