Skip to content

Commit e010146

Browse files
mbalabashEomm
authored andcommitted
Add types definition for TS integration (#55)
1 parent b278223 commit e010146

File tree

6 files changed

+125
-3
lines changed

6 files changed

+125
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ typings/
6464
# lock files
6565

6666
package-lock.json
67+
68+
# misc
69+
.DS_Store

index.d.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// <reference types="node" />
2+
import {
3+
FastifyRequest,
4+
Plugin,
5+
DefaultQuery,
6+
DefaultParams,
7+
DefaultHeaders,
8+
FastifyError,
9+
FastifyReply
10+
} from "fastify";
11+
import { Server, IncomingMessage, ServerResponse } from "http";
12+
import { Http2ServerRequest, Http2ServerResponse } from "http2";
13+
14+
type HttpRequest = IncomingMessage | Http2ServerRequest;
15+
type HttpResponse = ServerResponse | Http2ServerResponse;
16+
17+
declare const fastifyHttpProxy: Plugin<
18+
Server,
19+
IncomingMessage,
20+
ServerResponse,
21+
{
22+
upstream: string;
23+
prefix?: string;
24+
rewritePrefix?: string;
25+
http2?: boolean;
26+
preHandler?: (
27+
request: FastifyRequest<
28+
HttpRequest,
29+
DefaultQuery,
30+
DefaultParams,
31+
DefaultHeaders,
32+
any
33+
>,
34+
reply: FastifyReply<HttpResponse>,
35+
next: (err?: FastifyError | undefined) => void
36+
) => void;
37+
beforeHandler?: (
38+
request: FastifyRequest<
39+
HttpRequest,
40+
DefaultQuery,
41+
DefaultParams,
42+
DefaultHeaders,
43+
any
44+
>,
45+
reply: FastifyReply<HttpResponse>,
46+
next: (err?: FastifyError | undefined) => void
47+
) => void;
48+
config?: Object;
49+
replyOptions?: Object;
50+
}
51+
>;
52+
53+
export = fastifyHttpProxy;

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "proxy http requests, for Fastify",
55
"main": "index.js",
66
"scripts": {
7-
"test": "standard | snazzy && tap test.js"
7+
"test": "standard | snazzy && tap test/test.js",
8+
"lint:typescript": "standard --fix --parser @typescript-eslint/parser --plugin typescript test/types/*.ts",
9+
"typescript": "tsc --project ./test/types/tsconfig.json"
810
},
911
"repository": {
1012
"type": "git",
@@ -22,6 +24,9 @@
2224
},
2325
"homepage": "https://github.com/fastify/fastify-http-proxy#readme",
2426
"devDependencies": {
27+
"@types/node": "^11.13.9",
28+
"@typescript-eslint/parser": "^1.7.0",
29+
"eslint-plugin-typescript": "^0.14.0",
2530
"express": "^4.16.4",
2631
"express-http-proxy": "^1.5.0",
2732
"fastify": "^2.0.0",
@@ -33,7 +38,8 @@
3338
"simple-get": "^3.0.3",
3439
"snazzy": "^8.0.0",
3540
"standard": "^12.0.1",
36-
"tap": "^12.5.2"
41+
"tap": "^12.5.2",
42+
"typescript": "^3.4.5"
3743
},
3844
"peerDependencies": {
3945
"fastify": "2.x"

test.js renamed to test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { tearDown, test } = require('tap')
44
const Fastify = require('fastify')
5-
const proxy = require('.')
5+
const proxy = require('../')
66
const got = require('got')
77
const { Unauthorized } = require('http-errors')
88
const Transform = require('stream').Transform

test/types/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as fastify from "fastify";
2+
import * as fastifyHttpProxy from "../..";
3+
4+
/* eslint-disable */
5+
import { IncomingMessage, ServerResponse } from "http";
6+
import { Http2ServerRequest, Http2ServerResponse } from "http2";
7+
8+
type HttpRequest = IncomingMessage | Http2ServerRequest;
9+
type HttpResponse = ServerResponse | Http2ServerResponse;
10+
/* eslint-enable */
11+
12+
const app = fastify();
13+
14+
app.register(fastifyHttpProxy, {
15+
upstream: "http://origin.asd"
16+
});
17+
18+
app.register(fastifyHttpProxy, {
19+
upstream: "http://origin.asd",
20+
prefix: "/auth",
21+
rewritePrefix: "/u",
22+
http2: false,
23+
config: { key: 1 },
24+
replyOptions: { opt: "a" },
25+
preHandler: (
26+
request: fastify.FastifyRequest<
27+
HttpRequest,
28+
fastify.DefaultQuery,
29+
fastify.DefaultParams,
30+
fastify.DefaultHeaders,
31+
any
32+
>,
33+
reply: fastify.FastifyReply<HttpResponse>
34+
) => {
35+
console.log(request.query);
36+
console.log(reply.context.config);
37+
},
38+
beforeHandler: (
39+
request: fastify.FastifyRequest<
40+
HttpRequest,
41+
fastify.DefaultQuery,
42+
fastify.DefaultParams,
43+
fastify.DefaultHeaders,
44+
any
45+
>,
46+
reply: fastify.FastifyReply<HttpResponse>
47+
) => {
48+
console.log(request.query);
49+
console.log(reply.context.config);
50+
}
51+
});

test/types/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"noEmit": true,
6+
"strict": true
7+
},
8+
"files": ["./index.ts"]
9+
}

0 commit comments

Comments
 (0)