diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 670a3ff..0000000 --- a/index.d.ts +++ /dev/null @@ -1,43 +0,0 @@ -import * as Http from 'http'; -import * as Https from 'https'; -import { Stream } from 'pump'; -import * as Undici from 'undici'; - -interface QueryStringModule { - stringify(value: any): string; - parse(value: string): any; -} - -interface Options { - base?: string; - http2?: boolean; - undici?: Undici.Pool.Options; - cacheURLs?: number; - requests?: { - http?: Http.Agent, - https?: Https.Agent - }; - keepAliveMsecs?: number; - maxSockets?: number; - rejectUnauthorized?: boolean; - queryString?: QueryStringModule; -} - -declare function fastProxy(options?: Options): { - proxy( - originReq: Http.IncomingMessage, - originRes: Http.ServerResponse, - source: string, - opts?: { - base?: string; - onResponse?(req: Http.IncomingMessage, res: Http.ServerResponse, stream: Stream): void; - rewriteRequestHeaders?(req: Http.IncomingMessage, headers: Http.IncomingHttpHeaders): Http.IncomingHttpHeaders; - rewriteHeaders?(headers: Http.OutgoingHttpHeaders): Http.OutgoingHttpHeaders; - request?: Http.RequestOptions; - queryString?: string; - } - ): void; - close(): void; -} - -export default fastProxy diff --git a/package.json b/package.json index e201832..5ac5bce 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,12 @@ "version": "2.1.0", "description": "Forward your HTTP request to another server.", "main": "index.js", - "types": "index.d.ts", + "types": "types/index.d.ts", "scripts": { - "lint": "npx standard", - "test": "nyc mocha test/*.test.js", + "lint": "standard", + "test": "npm run test:unit && npm run test:typescript", + "test:unit": "nyc mocha test/*.test.js", + "test:typescript": "tsd", "bench": "( node benchmark/service.js & node benchmark/fast-proxy-0http-gateway.js & (sleep 5 && wrk -t8 -c8 -d30s http://127.0.0.1:8080/service/hi) )" }, "repository": { @@ -22,7 +24,7 @@ "files": [ "LICENSE", "README.md", - "index.d.ts", + "types/index.d.ts", "index.js", "lib/" ], @@ -33,6 +35,7 @@ }, "homepage": "https://github.com/fastify/fast-proxy", "devDependencies": { + "@types/node": "^18.11.10", "0http": "^3.1.1", "body-parser": "^1.19.0", "chai": "^4.3.4", @@ -45,7 +48,8 @@ "restana": "^4.9.1", "self-cert": "^2.0.0", "standard": "^17.0.0", - "supertest": "^6.1.6" + "supertest": "^6.1.6", + "tsd": "^0.25.0" }, "dependencies": { "end-of-stream": "^1.4.4", diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..0d7f858 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,50 @@ +import * as Http from 'http'; +import * as Https from 'https'; +import * as Undici from 'undici'; + +type Stream = ReadableStream | WritableStream + +type FastProxy = (options?: fastProxy.FastProxyOptions) => { + proxy( + originReq: Http.IncomingMessage, + originRes: Http.ServerResponse, + source: string, + opts?: { + base?: string; + onResponse?(req: Http.IncomingMessage, res: Http.ServerResponse, stream: Stream): void; + rewriteRequestHeaders?(req: Http.IncomingMessage, headers: Http.IncomingHttpHeaders): Http.IncomingHttpHeaders; + rewriteHeaders?(headers: Http.OutgoingHttpHeaders): Http.OutgoingHttpHeaders; + request?: Http.RequestOptions; + queryString?: string; + } + ): void; + close(): void; +} + +declare namespace fastProxy { + interface QueryStringModule { + stringify(value: any): string; + parse(value: string): any; + } + + export interface FastProxyOptions { + base?: string; + http2?: boolean; + undici?: Undici.Pool.Options; + cacheURLs?: number; + requests?: { + http?: Http.Agent, + https?: Https.Agent + }; + keepAliveMsecs?: number; + maxSockets?: number; + rejectUnauthorized?: boolean; + queryString?: QueryStringModule; + } + + export const fastProxy: FastProxy + export { fastProxy as default } +} + +declare function fastProxy(...params: Parameters): ReturnType +export = fastProxy diff --git a/types/index.test-d.ts b/types/index.test-d.ts new file mode 100644 index 0000000..1c31fc7 --- /dev/null +++ b/types/index.test-d.ts @@ -0,0 +1,13 @@ +import fastProxy, { fastProxy as fastProxyNamed} from '..' +import { expectAssignable, expectType } from 'tsd' + +const proxy = fastProxy({ + base: 'http://127.0.0.1:3000' +}) +fastProxyNamed({ + base: 'http://127.0.0.1:3000' +}) + +expectType(fastProxy) +expectType<() => void>(proxy.close) +expectAssignable(proxy.proxy)