From 59913723fd2fada1332afe54c8ea5e5335944fd8 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Tue, 11 Jul 2017 20:46:40 +0300 Subject: [PATCH 1/2] Add http-proxy --- types/http-proxy/http-proxy-tests.ts | 26 +++ types/http-proxy/index.d.ts | 250 +++++++++++++++++++++++++++ types/http-proxy/tsconfig.json | 6 + 3 files changed, 282 insertions(+) create mode 100644 types/http-proxy/http-proxy-tests.ts create mode 100644 types/http-proxy/index.d.ts create mode 100644 types/http-proxy/tsconfig.json diff --git a/types/http-proxy/http-proxy-tests.ts b/types/http-proxy/http-proxy-tests.ts new file mode 100644 index 0000000000..ee8bee00b0 --- /dev/null +++ b/types/http-proxy/http-proxy-tests.ts @@ -0,0 +1,26 @@ +import * as http from "http"; +import * as HttpProxy from "http-proxy"; + +const proxy = new HttpProxy({ + changeOrigin: true +}); + +proxy.on("error", err => { + console.error("An error occured:", err); +}); + +/** + * Test type inference of event listener parameters: + */ +proxy.on("start", (req, res, target) => { + const headers = req.headers; // defined; + const status = res.statusCode; // defined; + const partial = + typeof target === "string" + ? target.substr(0) // defined; + : target.protocol; // defined; +}); + +http.createServer((req, res) => { + proxy.web(req, res); +}); diff --git a/types/http-proxy/index.d.ts b/types/http-proxy/index.d.ts new file mode 100644 index 0000000000..2210f398ce --- /dev/null +++ b/types/http-proxy/index.d.ts @@ -0,0 +1,250 @@ +// Type definitions for node-http-proxy v1.12.0 +// Project: https://github.com/nodejitsu/node-http-proxy +// Definitions by: Maxime LUCE +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module "http-proxy" { + import * as net from "net"; + import * as http from "http"; + import * as https from "https"; + import * as events from "events"; + import * as url from "url"; + + type ProxyTargetUrl = string | url.Url; + + interface ErrorCallback { + ( + err: Error, + req: http.IncomingMessage, + res: http.ServerResponse, + target?: ProxyTargetUrl + ): void; + } + + class Server extends events.EventEmitter { + /** + * Creates the proxy server with specified options. + */ + constructor(); + /** + * Creates the proxy server with specified options. + * @param options - Config object passed to the proxy + */ + constructor(options: Server.ServerOptions); + + /** + * Used for proxying regular HTTP(S) requests + * @param req - Client request. + * @param res - Client response. + */ + public web(req: http.IncomingMessage, res: http.ServerResponse): void; + /** + * Used for proxying regular HTTP(S) requests + * @param req - Client request. + * @param res - Client response. + * @param options - Additionnal options. + */ + public web( + req: http.IncomingMessage, + res: http.ServerResponse, + options: Server.ServerOptions + ): void; + /** + * Used for proxying regular HTTP(S) requests + * @param req - Client request. + * @param res - Client response. + * @param options - Additionnal options. + * @param + */ + public web( + req: http.IncomingMessage, + res: http.ServerResponse, + options: Server.ServerOptions, + callback: ErrorCallback + ): void; + + /** + * Used for proxying regular HTTP(S) requests + * @param req - Client request. + * @param socket - Client socket. + * @param head - Client head. + */ + public ws(req: http.IncomingMessage, socket: any, head: any): void; + /** + * Used for proxying regular HTTP(S) requests + * @param req - Client request. + * @param socket - Client socket. + * @param head - Client head. + * @param options - Additionnal options. + */ + public ws( + req: http.IncomingMessage, + socket: any, + head: any, + options: Server.ServerOptions + ): void; + + /** + * A function that wraps the object in a webserver, for your convenience + * @param port - Port to listen on + */ + public listen(port: number): Server; + + /** + * A function that closes the inner webserver and stops listening on given port + */ + public close(): void; + /** + * A function that closes the inner webserver and stops listening on given port + */ + public close(callback: Function): void; + + /** + * Creates the proxy server. + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createProxyServer(): Server; + /** + * Creates the proxy server with specified options. + * @param options Config object passed to the proxy + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createProxyServer(options: Server.ServerOptions): Server; + + /** + * Creates the proxy server. + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createServer(): Server; + /** + * Creates the proxy server with specified options. + * @param options Config object passed to the proxy + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createServer(options: Server.ServerOptions): Server; + + /** + * Creates the proxy server. + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createProxy(): Server; + /** + * Creates the proxy server with specified options. + * @param options Config object passed to the proxy + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createProxy(options: Server.ServerOptions): Server; + + addListener(event: string, listener: Function): this; + on(event: string, listener: Function): this; + on(event: "error", listener: ErrorCallback): this; + on( + event: "start", + listener: ( + req: http.IncomingMessage, + res: http.ServerResponse, + target: ProxyTargetUrl + ) => void + ): this; + on( + event: "proxyReq", + listener: ( + proxyReq: http.ClientRequest, + req: http.IncomingMessage, + res: http.ServerResponse, + options: Server.ServerOptions + ) => void + ): this; + on( + event: "proxyRes", + listener: ( + proxyRes: http.IncomingMessage, + req: http.IncomingMessage, + res: http.ServerResponse + ) => void + ): this; + on( + event: "proxyReqWs", + listener: ( + proxyReq: http.ClientRequest, + req: http.IncomingMessage, + socket: net.Socket, + options: Server.ServerOptions, + head: any + ) => void + ): this; + on( + event: "econnreset", + listener: ( + err: Error, + req: http.IncomingMessage, + res: http.ServerResponse, + target: ProxyTargetUrl + ) => void + ): this; + on( + event: "end", + listener: ( + req: http.IncomingMessage, + res: http.ServerResponse, + proxyRes: http.IncomingMessage + ) => void + ): this; + on( + event: "close", + listener: ( + proxyRes: http.IncomingMessage, + proxySocket: net.Socket, + proxyHead: any + ) => void + ): this; + + once(event: string, listener: Function): this; + removeListener(event: string, listener: Function): this; + removeAllListeners(event?: string): this; + getMaxListeners(): number; + setMaxListeners(n: number): this; + listeners(event: string): Function[]; + emit(event: string, ...args: any[]): boolean; + listenerCount(type: string): number; + } + + namespace Server { + export interface ServerOptions { + /** URL string to be parsed with the url module. */ + target?: string; + /** URL string to be parsed with the url module. */ + forward?: string; + /** Object to be passed to http(s).request. */ + agent?: any; + /** Object to be passed to https.createServer(). */ + ssl?: any; + /** If you want to proxy websockets. */ + ws?: boolean; + /** Adds x- forward headers. */ + xfwd?: boolean; + /** Verify SSL certificate. */ + secure?: boolean; + /** Explicitly specify if we are proxying to another proxy. */ + toProxy?: boolean; + /** Specify whether you want to prepend the target's path to the proxy path. */ + prependPath?: boolean; + /** Specify whether you want to ignore the proxy path of the incoming request. */ + ignorePath?: boolean; + /** Local interface string to bind for outgoing connections. */ + localAddress?: boolean; + /** Changes the origin of the host header to the target URL. */ + changeOrigin?: boolean; + /** Basic authentication i.e. 'user:password' to compute an Authorization header. */ + auth?: string; + /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */ + hostRewrite?: string; + /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */ + autoRewrite?: boolean; + /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */ + protocolRewrite?: string; + } + } + + export = Server; +} diff --git a/types/http-proxy/tsconfig.json b/types/http-proxy/tsconfig.json new file mode 100644 index 0000000000..5c99a9c3cc --- /dev/null +++ b/types/http-proxy/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "strictNullChecks": true, + "noImplicitAny": true + } +} From 971abae2253377bef58137842af9717d887290e1 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Wed, 12 Jul 2017 05:42:17 +0300 Subject: [PATCH 2/2] Fix dtslint issues --- types/http-proxy/http-proxy-tests.ts | 2 +- types/http-proxy/index.d.ts | 425 ++++++++++++--------------- types/http-proxy/tsconfig.json | 22 +- types/http-proxy/tslint.json | 3 + types/http-status/tsconfig.json | 2 +- 5 files changed, 212 insertions(+), 242 deletions(-) create mode 100644 types/http-proxy/tslint.json diff --git a/types/http-proxy/http-proxy-tests.ts b/types/http-proxy/http-proxy-tests.ts index ee8bee00b0..e691b48ed0 100644 --- a/types/http-proxy/http-proxy-tests.ts +++ b/types/http-proxy/http-proxy-tests.ts @@ -5,7 +5,7 @@ const proxy = new HttpProxy({ changeOrigin: true }); -proxy.on("error", err => { +proxy.on("error", (err) => { console.error("An error occured:", err); }); diff --git a/types/http-proxy/index.d.ts b/types/http-proxy/index.d.ts index 2210f398ce..5ff6d5f32c 100644 --- a/types/http-proxy/index.d.ts +++ b/types/http-proxy/index.d.ts @@ -1,250 +1,201 @@ -// Type definitions for node-http-proxy v1.12.0 +// Type definitions for node-http-proxy 1.12 // Project: https://github.com/nodejitsu/node-http-proxy // Definitions by: Maxime LUCE // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare module "http-proxy" { - import * as net from "net"; - import * as http from "http"; - import * as https from "https"; - import * as events from "events"; - import * as url from "url"; +/// - type ProxyTargetUrl = string | url.Url; +import * as net from "net"; +import * as http from "http"; +import * as https from "https"; +import * as events from "events"; +import * as url from "url"; - interface ErrorCallback { - ( +type ProxyTargetUrl = string | url.Url; + +type ErrorCallback = ( + err: Error, + req: http.IncomingMessage, + res: http.ServerResponse, + target?: ProxyTargetUrl +) => void; + +declare class Server extends events.EventEmitter { + /** + * Creates the proxy server with specified options. + * @param options - Config object passed to the proxy + */ + constructor(options?: Server.ServerOptions); + + /** + * Used for proxying regular HTTP(S) requests + * @param req - Client request. + * @param res - Client response. + * @param options - Additionnal options. + * @param + */ + web( + req: http.IncomingMessage, + res: http.ServerResponse, + options?: Server.ServerOptions, + callback?: ErrorCallback + ): void; + + /** + * Used for proxying regular HTTP(S) requests + * @param req - Client request. + * @param socket - Client socket. + * @param head - Client head. + * @param options - Additionnal options. + */ + ws( + req: http.IncomingMessage, + socket: any, + head: any, + options?: Server.ServerOptions + ): void; + + /** + * A function that wraps the object in a webserver, for your convenience + * @param port - Port to listen on + */ + listen(port: number): Server; + + /** + * A function that closes the inner webserver and stops listening on given port + */ + close(callback?: () => void): void; + + /** + * Creates the proxy server with specified options. + * @param options Config object passed to the proxy + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createProxyServer(options?: Server.ServerOptions): Server; + + /** + * Creates the proxy server with specified options. + * @param options Config object passed to the proxy + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createServer(options?: Server.ServerOptions): Server; + + /** + * Creates the proxy server with specified options. + * @param options Config object passed to the proxy + * @returns Proxy object with handlers for `ws` and `web` requests + */ + static createProxy(options?: Server.ServerOptions): Server; + + addListener(event: string, listener: () => void): this; + on(event: string, listener: () => void): this; + on(event: "error", listener: ErrorCallback): this; + on( + event: "start", + listener: ( + req: http.IncomingMessage, + res: http.ServerResponse, + target: ProxyTargetUrl + ) => void + ): this; + on( + event: "proxyReq", + listener: ( + proxyReq: http.ClientRequest, + req: http.IncomingMessage, + res: http.ServerResponse, + options: Server.ServerOptions + ) => void + ): this; + on( + event: "proxyRes", + listener: ( + proxyRes: http.IncomingMessage, + req: http.IncomingMessage, + res: http.ServerResponse + ) => void + ): this; + on( + event: "proxyReqWs", + listener: ( + proxyReq: http.ClientRequest, + req: http.IncomingMessage, + socket: net.Socket, + options: Server.ServerOptions, + head: any + ) => void + ): this; + on( + event: "econnreset", + listener: ( err: Error, req: http.IncomingMessage, res: http.ServerResponse, - target?: ProxyTargetUrl - ): void; - } - - class Server extends events.EventEmitter { - /** - * Creates the proxy server with specified options. - */ - constructor(); - /** - * Creates the proxy server with specified options. - * @param options - Config object passed to the proxy - */ - constructor(options: Server.ServerOptions); - - /** - * Used for proxying regular HTTP(S) requests - * @param req - Client request. - * @param res - Client response. - */ - public web(req: http.IncomingMessage, res: http.ServerResponse): void; - /** - * Used for proxying regular HTTP(S) requests - * @param req - Client request. - * @param res - Client response. - * @param options - Additionnal options. - */ - public web( + target: ProxyTargetUrl + ) => void + ): this; + on( + event: "end", + listener: ( req: http.IncomingMessage, res: http.ServerResponse, - options: Server.ServerOptions - ): void; - /** - * Used for proxying regular HTTP(S) requests - * @param req - Client request. - * @param res - Client response. - * @param options - Additionnal options. - * @param - */ - public web( - req: http.IncomingMessage, - res: http.ServerResponse, - options: Server.ServerOptions, - callback: ErrorCallback - ): void; + proxyRes: http.IncomingMessage + ) => void + ): this; + on( + event: "close", + listener: ( + proxyRes: http.IncomingMessage, + proxySocket: net.Socket, + proxyHead: any + ) => void + ): this; - /** - * Used for proxying regular HTTP(S) requests - * @param req - Client request. - * @param socket - Client socket. - * @param head - Client head. - */ - public ws(req: http.IncomingMessage, socket: any, head: any): void; - /** - * Used for proxying regular HTTP(S) requests - * @param req - Client request. - * @param socket - Client socket. - * @param head - Client head. - * @param options - Additionnal options. - */ - public ws( - req: http.IncomingMessage, - socket: any, - head: any, - options: Server.ServerOptions - ): void; - - /** - * A function that wraps the object in a webserver, for your convenience - * @param port - Port to listen on - */ - public listen(port: number): Server; - - /** - * A function that closes the inner webserver and stops listening on given port - */ - public close(): void; - /** - * A function that closes the inner webserver and stops listening on given port - */ - public close(callback: Function): void; - - /** - * Creates the proxy server. - * @returns Proxy object with handlers for `ws` and `web` requests - */ - static createProxyServer(): Server; - /** - * Creates the proxy server with specified options. - * @param options Config object passed to the proxy - * @returns Proxy object with handlers for `ws` and `web` requests - */ - static createProxyServer(options: Server.ServerOptions): Server; - - /** - * Creates the proxy server. - * @returns Proxy object with handlers for `ws` and `web` requests - */ - static createServer(): Server; - /** - * Creates the proxy server with specified options. - * @param options Config object passed to the proxy - * @returns Proxy object with handlers for `ws` and `web` requests - */ - static createServer(options: Server.ServerOptions): Server; - - /** - * Creates the proxy server. - * @returns Proxy object with handlers for `ws` and `web` requests - */ - static createProxy(): Server; - /** - * Creates the proxy server with specified options. - * @param options Config object passed to the proxy - * @returns Proxy object with handlers for `ws` and `web` requests - */ - static createProxy(options: Server.ServerOptions): Server; - - addListener(event: string, listener: Function): this; - on(event: string, listener: Function): this; - on(event: "error", listener: ErrorCallback): this; - on( - event: "start", - listener: ( - req: http.IncomingMessage, - res: http.ServerResponse, - target: ProxyTargetUrl - ) => void - ): this; - on( - event: "proxyReq", - listener: ( - proxyReq: http.ClientRequest, - req: http.IncomingMessage, - res: http.ServerResponse, - options: Server.ServerOptions - ) => void - ): this; - on( - event: "proxyRes", - listener: ( - proxyRes: http.IncomingMessage, - req: http.IncomingMessage, - res: http.ServerResponse - ) => void - ): this; - on( - event: "proxyReqWs", - listener: ( - proxyReq: http.ClientRequest, - req: http.IncomingMessage, - socket: net.Socket, - options: Server.ServerOptions, - head: any - ) => void - ): this; - on( - event: "econnreset", - listener: ( - err: Error, - req: http.IncomingMessage, - res: http.ServerResponse, - target: ProxyTargetUrl - ) => void - ): this; - on( - event: "end", - listener: ( - req: http.IncomingMessage, - res: http.ServerResponse, - proxyRes: http.IncomingMessage - ) => void - ): this; - on( - event: "close", - listener: ( - proxyRes: http.IncomingMessage, - proxySocket: net.Socket, - proxyHead: any - ) => void - ): this; - - once(event: string, listener: Function): this; - removeListener(event: string, listener: Function): this; - removeAllListeners(event?: string): this; - getMaxListeners(): number; - setMaxListeners(n: number): this; - listeners(event: string): Function[]; - emit(event: string, ...args: any[]): boolean; - listenerCount(type: string): number; - } - - namespace Server { - export interface ServerOptions { - /** URL string to be parsed with the url module. */ - target?: string; - /** URL string to be parsed with the url module. */ - forward?: string; - /** Object to be passed to http(s).request. */ - agent?: any; - /** Object to be passed to https.createServer(). */ - ssl?: any; - /** If you want to proxy websockets. */ - ws?: boolean; - /** Adds x- forward headers. */ - xfwd?: boolean; - /** Verify SSL certificate. */ - secure?: boolean; - /** Explicitly specify if we are proxying to another proxy. */ - toProxy?: boolean; - /** Specify whether you want to prepend the target's path to the proxy path. */ - prependPath?: boolean; - /** Specify whether you want to ignore the proxy path of the incoming request. */ - ignorePath?: boolean; - /** Local interface string to bind for outgoing connections. */ - localAddress?: boolean; - /** Changes the origin of the host header to the target URL. */ - changeOrigin?: boolean; - /** Basic authentication i.e. 'user:password' to compute an Authorization header. */ - auth?: string; - /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */ - hostRewrite?: string; - /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */ - autoRewrite?: boolean; - /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */ - protocolRewrite?: string; - } - } - - export = Server; + once(event: string, listener: () => void): this; + removeListener(event: string, listener: () => void): this; + removeAllListeners(event?: string): this; + getMaxListeners(): number; + setMaxListeners(n: number): this; + listeners(event: string): Array<() => void>; + emit(event: string, ...args: any[]): boolean; + listenerCount(type: string): number; } + +declare namespace Server { + interface ServerOptions { + /** URL string to be parsed with the url module. */ + target?: string; + /** URL string to be parsed with the url module. */ + forward?: string; + /** Object to be passed to http(s).request. */ + agent?: any; + /** Object to be passed to https.createServer(). */ + ssl?: any; + /** If you want to proxy websockets. */ + ws?: boolean; + /** Adds x- forward headers. */ + xfwd?: boolean; + /** Verify SSL certificate. */ + secure?: boolean; + /** Explicitly specify if we are proxying to another proxy. */ + toProxy?: boolean; + /** Specify whether you want to prepend the target's path to the proxy path. */ + prependPath?: boolean; + /** Specify whether you want to ignore the proxy path of the incoming request. */ + ignorePath?: boolean; + /** Local interface string to bind for outgoing connections. */ + localAddress?: boolean; + /** Changes the origin of the host header to the target URL. */ + changeOrigin?: boolean; + /** Basic authentication i.e. 'user:password' to compute an Authorization header. */ + auth?: string; + /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */ + hostRewrite?: string; + /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */ + autoRewrite?: boolean; + /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */ + protocolRewrite?: string; + } +} + +export = Server; diff --git a/types/http-proxy/tsconfig.json b/types/http-proxy/tsconfig.json index 5c99a9c3cc..fe722af931 100644 --- a/types/http-proxy/tsconfig.json +++ b/types/http-proxy/tsconfig.json @@ -1,6 +1,22 @@ { "compilerOptions": { - "strictNullChecks": true, - "noImplicitAny": true - } + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "http-proxy-tests.ts" + ] } diff --git a/types/http-proxy/tslint.json b/types/http-proxy/tslint.json new file mode 100644 index 0000000000..d88586e5bd --- /dev/null +++ b/types/http-proxy/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/http-status/tsconfig.json b/types/http-status/tsconfig.json index 11a8a8d963..42d5e2ed18 100644 --- a/types/http-status/tsconfig.json +++ b/types/http-status/tsconfig.json @@ -19,4 +19,4 @@ "index.d.ts", "http-status-tests.ts" ] -} \ No newline at end of file +}