forked from davidje13/websocket-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
146 lines (117 loc) · 3.94 KB
/
index.d.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
declare module 'websocket-express' {
import { Server } from 'http';
import * as WebSocket from 'ws';
import {
static,
json,
urlencoded,
Request,
Response,
IRouterHandler,
IRouterMatcher,
NextFunction,
RequestHandler,
IRouter,
RouterOptions,
Express,
} from 'express';
import { Params, ParamsDictionary } from 'express-serve-static-core';
interface NextMessageOptions {
timeout?: number | undefined;
}
interface WebSocketMessage {
data: Buffer;
isBinary: boolean;
}
export type ExtendedWebSocket = WebSocket & {
nextMessage(options?: NextMessageOptions): Promise<WebSocketMessage>;
};
export interface WSResponse extends Response {
accept(): Promise<ExtendedWebSocket>;
reject(code?: number, message?: string | null): void;
abandon(): void;
sendError(
httpStatus: number,
wsStatus?: number | null,
message?: string | null,
): void;
closeAtTime(time: number, code?: number, message?: string): void;
send(message: string): this;
beginTransaction(): void;
endTransaction(): void;
}
export function isWebSocket(res: Response): res is WSResponse;
export interface JWTPayload {
iss?: string;
iat?: number;
nbf?: number;
exp?: number;
sub?: string;
aud?: string;
jti?: string;
scopes?: string[] | { [scope: string]: boolean } | string;
}
export type TokenExtractor<P extends Params = ParamsDictionary> = (
token: string,
authRealm: string,
req: Request<P>,
res: Response,
) => Promise<JWTPayload | null> | JWTPayload | null;
export function requireBearerAuth<P extends Params = ParamsDictionary>(
realm: ((req: Request<P>, res: Response) => string) | string,
extractAndValidateToken: TokenExtractor<P>,
): RequestHandler<P>;
export function requireAuthScope(scope: string): RequestHandler<any>;
export function getAuthData(res: Response): JWTPayload;
export function hasAuthScope(res: Response, scope: string): boolean;
export type WSRequestHandler<P extends Params = ParamsDictionary> = (
req: Request<P>,
res: WSResponse,
next: NextFunction,
) => any;
export type WSErrorRequestHandler<P extends Params = ParamsDictionary> = (
err: any,
req: Request<P>,
res: WSResponse,
next: NextFunction,
) => any;
export type WSRequestHandlerParams<P extends Params = ParamsDictionary> =
| WSRequestHandler<P>
| WSErrorRequestHandler<P>
| (WSRequestHandler<P> | WSErrorRequestHandler<P>)[];
export type PathParams = string | RegExp | (string | RegExp)[];
export interface WSRouterMatcher<T> {
<P extends Params = ParamsDictionary>(
path: PathParams,
...handlers: WSRequestHandler<P>[]
): T;
<P extends Params = ParamsDictionary>(
path: PathParams,
...handlers: WSRequestHandlerParams<P>[]
): T;
}
interface Router extends IRouter {}
class Router {
public constructor(options?: RouterOptions);
public readonly ws: WSRouterMatcher<this>;
public readonly useHTTP: IRouterHandler<this> & IRouterMatcher<this>;
}
interface WebSocketExpress extends Express {}
class WebSocketExpress {
public readonly ws: WSRouterMatcher<this>;
public readonly useHTTP: IRouterHandler<this> & IRouterMatcher<this>;
public attach(server: Server): void;
public detach(server: Server): void;
public createServer(): Server;
public static readonly static: typeof static;
public static readonly json: typeof json;
public static readonly urlencoded: typeof urlencoded;
public static readonly isWebSocket: typeof isWebSocket;
public static readonly Router: typeof Router;
public static readonly requireBearerAuth: typeof requireBearerAuth;
public static readonly requireAuthScope: typeof requireAuthScope;
public static readonly getAuthData: typeof getAuthData;
public static readonly hasAuthScope: typeof hasAuthScope;
}
export { Router, WebSocketExpress };
}