Skip to content

Commit e0bb03d

Browse files
SkeLLLamcollina
authored andcommitted
feat(typescript): add initial types (#39)
* feat(typescript): add initial types * fix(types): fix route options * test(types): add tests * chore: add test script
1 parent 0b98fc0 commit e0bb03d

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

index.d.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/// <reference types="node" />
2+
import * as fastify from 'fastify';
3+
import { IncomingMessage, ServerResponse, Server } from 'http';
4+
import { Plugin } from 'fastify';
5+
import * as WebSocket from 'ws';
6+
import { Duplex } from 'stream';
7+
8+
declare module 'fastify' {
9+
interface RouteShorthandOptions<
10+
HttpServer,
11+
HttpRequest,
12+
HttpResponse,
13+
Query,
14+
Params,
15+
Headers,
16+
Body
17+
> {
18+
websocket?: boolean;
19+
}
20+
interface FastifyInstance<
21+
HttpServer = Server,
22+
HttpRequest = IncomingMessage,
23+
HttpResponse = ServerResponse
24+
> {
25+
get<
26+
Query = DefaultQuery,
27+
Params = DefaultParams,
28+
Headers = DefaultHeaders,
29+
Body = DefaultBody
30+
>(
31+
url: string,
32+
opts: RouteShorthandOptions<
33+
HttpServer,
34+
HttpRequest,
35+
HttpResponse,
36+
Query,
37+
Params,
38+
Headers,
39+
Body
40+
>,
41+
handler?: WebsocketHandler<HttpRequest, HttpResponse>
42+
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>;
43+
}
44+
45+
interface RouteOptions<
46+
HttpServer = Server,
47+
HttpRequest = IncomingMessage,
48+
HttpResponse = ServerResponse,
49+
Query = DefaultQuery,
50+
Params = DefaultParams,
51+
Headers = DefaultHeaders,
52+
Body = DefaultBody
53+
> {
54+
wsHandler?: WebsocketHandler<
55+
HttpRequest,
56+
HttpResponse,
57+
Query,
58+
Params,
59+
Headers,
60+
Body
61+
>;
62+
}
63+
64+
export type WebsocketHandler<
65+
HttpRequest = IncomingMessage,
66+
HttpResponse = ServerResponse,
67+
Query = DefaultQuery,
68+
Params = DefaultParams,
69+
Headers = DefaultHeaders,
70+
Body = DefaultBody
71+
> = (
72+
this: FastifyInstance<Server, HttpRequest, HttpResponse>,
73+
connection: websocketPlugin.SocketStream,
74+
request: FastifyRequest<HttpRequest, Query, Params, Headers, Body>,
75+
params?: { [key: string]: any }
76+
) => void | Promise<any>;
77+
}
78+
79+
declare namespace websocketPlugin {
80+
export interface SocketStream extends Duplex {
81+
socket: WebSocket;
82+
}
83+
84+
export interface PluginOptions {
85+
handle: (connection: SocketStream) => void;
86+
options: WebSocket.ServerOptions;
87+
}
88+
}
89+
90+
declare const websocketPlugin: Plugin<
91+
Server,
92+
IncomingMessage,
93+
ServerResponse,
94+
websocketPlugin.PluginOptions
95+
>;
96+
97+
export = websocketPlugin;

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
"version": "1.0.0",
44
"description": "basic websocket support for fastify",
55
"main": "index.js",
6+
"types": "index.d.ts",
7+
"tsd": {
8+
"directory": "./test/types"
9+
},
610
"scripts": {
711
"lint": "standard | snazzy",
812
"unit": "tap \"test/*.js\"",
9-
"test": "npm run lint && npm run unit"
13+
"test:js": "npm run lint && npm run unit",
14+
"test:ts": "tsd",
15+
"test": "npm run test:js && npm run test:ts"
1016
},
1117
"repository": {
1218
"type": "git",
@@ -23,11 +29,13 @@
2329
},
2430
"homepage": "https://github.com/fastify/fastify-websocket#readme",
2531
"devDependencies": {
32+
"@types/ws": "^6.0.3",
2633
"fastify": "^2.4.1",
2734
"pre-commit": "^1.2.2",
2835
"snazzy": "^8.0.0",
2936
"standard": "^14.0.2",
30-
"tap": "^12.7.0"
37+
"tap": "^12.7.0",
38+
"tsd": "^0.11.0"
3139
},
3240
"dependencies": {
3341
"fastify-plugin": "^1.5.0",

test/types/index.test-d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const wsPlugin = require('../..');
2+
const fastify = require('fastify');
3+
import { SocketStream } from '../..';
4+
import { WebsocketHandler, FastifyRequest, FastifyInstance } from 'fastify';
5+
import { expectType } from 'tsd';
6+
7+
const app: FastifyInstance = fastify();
8+
app.register(wsPlugin);
9+
10+
const handler: WebsocketHandler = (
11+
connection: SocketStream,
12+
req: FastifyRequest,
13+
params
14+
) => {
15+
expectType<SocketStream>(connection);
16+
expectType<{ [key: string]: any } | undefined>(params);
17+
};
18+
19+
app.get('/', { websocket: true }, handler);

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"lib": ["es2015"],
5+
"module": "commonjs",
6+
"noEmit": true,
7+
"strict": true,
8+
"noImplicitAny": true
9+
},
10+
"includes": ["./test/types/*.test-d.ts", "index.d.ts"]
11+
}

0 commit comments

Comments
 (0)