Skip to content

Commit

Permalink
chore(typescript): extract handlers types (#603)
Browse files Browse the repository at this point in the history
* chore(typescript): extract handlers types

* chore(typescript): set callback types based on the http-proxy

Co-authored-by: chimurai <[email protected]>
  • Loading branch information
leonardobazico and chimurai authored May 12, 2021
1 parent 3115dae commit c935888
Showing 1 changed file with 47 additions and 14 deletions.
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,
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;

0 comments on commit c935888

Please sign in to comment.