Skip to content
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

chore(typescript): extract handlers types #603

Merged
merged 3 commits into from
May 12, 2021
Merged
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
61 changes: 47 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type * as express from 'express';
import type * as http from 'http';
import type * as httpProxy from 'http-proxy';
import type * as net from 'net';
import type * as url from 'url';

export interface Request extends express.Request {}
export interface Response extends express.Response {}
Expand All @@ -29,20 +30,14 @@ export interface Options extends httpProxy.ServerOptions {
| ((req: Request) => httpProxy.ServerOptions['target'])
| ((req: Request) => Promise<httpProxy.ServerOptions['target']>);
logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
logProvider?(provider: LogProvider): LogProvider;

onError?(err: Error, req: Request, res: Response): void;
onProxyRes?(proxyRes: http.IncomingMessage, req: Request, res: Response): void;
onProxyReq?(proxyReq: http.ClientRequest, req: Request, res: Response): void;
onProxyReqWs?(
proxyReq: http.ClientRequest,
req: Request,
socket: net.Socket,
options: httpProxy.ServerOptions,
head: any
): void;
onOpen?(proxySocket: net.Socket): void;
onClose?(res: Response, socket: net.Socket, head: any): void;
logProvider?: LogProviderCallback;

onError?: OnErrorCallback;
onProxyRes?: OnProxyResCallback;
onProxyReq?: OnProxyReqCallback;
onProxyReqWs?: OnProxyReqWsCallback;
onOpen?: OnOpenCallback;
onClose?: OnCloseCallback;
}

interface LogProvider {
Expand All @@ -54,3 +49,41 @@ interface LogProvider {
}

type Logger = (...args: any[]) => void;

export type LogProviderCallback = (provider: LogProvider) => LogProvider;

/**
* Use types based on the events listeners from http-proxy
* https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51504fd999031b7f025220fab279f1b2155cbaff/types/http-proxy/index.d.ts
*/
export type OnErrorCallback = (
err: Error,
req: http.IncomingMessage,
res: http.ServerResponse,
target?: string | Partial<url.Url>
) => void;
export type OnProxyResCallback = (
proxyRes: http.IncomingMessage,
req: http.IncomingMessage,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this remain Request (express.Request)?

res: http.ServerResponse
) => void;
export type OnProxyReqCallback = (
proxyReq: http.ClientRequest,
req: http.IncomingMessage,
res: http.ServerResponse,
options: httpProxy.ServerOptions
) => void;
export type OnProxyReqWsCallback = (
proxyReq: http.ClientRequest,
req: http.IncomingMessage,
socket: net.Socket,
options: httpProxy.ServerOptions,
head: any
) => void;
export type OnCloseCallback = (
proxyRes: http.IncomingMessage,
proxySocket: net.Socket,
proxyHead: any
) => void;

export type OnOpenCallback = (proxySocket: net.Socket) => void;