-
Notifications
You must be signed in to change notification settings - Fork 868
/
Copy pathtypes.ts
153 lines (143 loc) · 4.94 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Based on definition by DefinitelyTyped:
* https://github.com/DefinitelyTyped/DefinitelyTyped/blob/6f529c6c67a447190f86bfbf894d1061e41e07b7/types/http-proxy-middleware/index.d.ts
*/
/* eslint-disable @typescript-eslint/no-empty-interface */
import type * as http from 'http';
import type * as httpProxy from 'http-proxy';
import type * as net from 'net';
export type NextFunction<T = (err?: any) => void> = T;
export interface RequestHandler<
TReq = http.IncomingMessage,
TRes = http.ServerResponse,
TNext = NextFunction,
> {
(req: TReq, res: TRes, next?: TNext): Promise<void>;
upgrade: (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => void;
}
export type Filter<TReq = http.IncomingMessage> =
| string
| string[]
| ((pathname: string, req: TReq) => boolean);
export interface Plugin<TReq = http.IncomingMessage, TRes = http.ServerResponse> {
(proxyServer: httpProxy<TReq, TRes>, options: Options<TReq, TRes>): void;
}
export interface OnProxyEvent<TReq = http.IncomingMessage, TRes = http.ServerResponse> {
error?: httpProxy.ErrorCallback<Error, TReq, TRes>;
proxyReq?: httpProxy.ProxyReqCallback<http.ClientRequest, TReq, TRes>;
proxyReqWs?: httpProxy.ProxyReqWsCallback<http.ClientRequest, TReq>;
proxyRes?: httpProxy.ProxyResCallback<TReq, TRes>;
open?: httpProxy.OpenCallback;
close?: httpProxy.CloseCallback<TReq>;
start?: httpProxy.StartCallback<TReq, TRes>;
end?: httpProxy.EndCallback<TReq, TRes>;
econnreset?: httpProxy.EconnresetCallback<Error, TReq, TRes>;
}
export type Logger = Pick<Console, 'info' | 'warn' | 'error'>;
export interface Options<TReq = http.IncomingMessage, TRes = http.ServerResponse>
extends httpProxy.ServerOptions {
/**
* Narrow down requests to proxy or not.
* Filter on {@link http.IncomingMessage.url `pathname`} which is relative to the proxy's "mounting" point in the server.
* Or use the {@link http.IncomingMessage `req`} object for more complex filtering.
* @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathFilter.md
* @since v3.0.0
*/
pathFilter?: Filter<TReq>;
/**
* Modify request paths before requests are send to the target.
* @example
* ```js
* createProxyMiddleware({
* pathRewrite: {
* '^/api/old-path': '/api/new-path', // rewrite path
* }
* });
* ```
* @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathRewrite.md
*/
pathRewrite?:
| { [regexp: string]: string }
| ((path: string, req: TReq) => string | undefined)
| ((path: string, req: TReq) => Promise<string>);
/**
* Access the internal http-proxy server instance to customize behavior
*
* @example
* ```js
* createProxyMiddleware({
* plugins: [(proxyServer, options) => {
* proxyServer.on('error', (error, req, res) => {
* console.error(error);
* });
* }]
* });
* ```
* @link https://github.com/chimurai/http-proxy-middleware#plugins-array
* @since v3.0.0
*/
plugins?: Plugin<TReq, TRes>[];
/**
* Eject pre-configured plugins.
* NOTE: register your own error handlers to prevent server from crashing.
*
* @link https://github.com/chimurai/http-proxy-middleware#ejectplugins-boolean-default-false
* @since v3.0.0
*/
ejectPlugins?: boolean;
/**
* Listen to http-proxy events
* @see {@link OnProxyEvent} for available events
* @example
* ```js
* createProxyMiddleware({
* on: {
* error: (error, req, res, target) => {
* console.error(error);
* }
* }
* });
* ```
* @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/proxy-events.md
* @since v3.0.0
*/
on?: OnProxyEvent<TReq, TRes>;
/**
* Dynamically set the {@link Options.target `options.target`}.
* @example
* ```js
* createProxyMiddleware({
* router: async (req) => {
* return 'http://127:0.0.1:3000';
* }
* });
* ```
* @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/router.md
*/
router?:
| { [hostOrPath: string]: httpProxy.ServerOptions['target'] }
| ((req: TReq) => httpProxy.ServerOptions['target'])
| ((req: TReq) => Promise<httpProxy.ServerOptions['target']>);
/**
* Log information from http-proxy-middleware
* @example
* ```js
* createProxyMiddleware({
* logger: console
* });
* ```
* @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/logger.md
* @since v3.0.0
*/
logger?: Logger | any;
/**
* Configures options for follow-redirects when followRedirects is true.
* @property {number} [maxRedirects] - The maximum number of redirects to follow.
* @property {number} [maxBodyLength] - The maximum allowed size of the response body in bytes.
* @see {@link https://github.com/follow-redirects/follow-redirects?tab=readme-ov-file#global-options}
*/
followRedirectsOpts?: {
maxRedirects?: number;
maxBodyLength?: number;
};
}