forked from flow-typed/flow-typed
/
express_v4.x.x.js
280 lines (256 loc) · 10.6 KB
/
express_v4.x.x.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
declare type express$RouterOptions = {
caseSensitive?: boolean,
mergeParams?: boolean,
strict?: boolean,
...
};
declare class express$RequestResponseBase {
app: express$Application<any, any>;
get(field: string): string | void;
}
declare type express$RangeParserOptions = {|
+combine?: boolean,
|};
declare type express$RangeParserRange = {|
+start: number,
+end: number,
|};
declare type express$RequestParams = { [param: string]: string, ... }
/*
NOTE: Use caution when extending `express$Request` or `express$Response`. When
a request first hits the server, its `req` and `res` will not have any
additional properties, even if you explicitly type them with your custom
subclass. Subsequent middleware may assign these properties, but you must be
cognizant of this ordering. One way to handle this is marking all properties
as optional to force refinement every time a property is accessed. Therefore,
we advise that you always mark properties as _optional_ in `express$Request`
and `express$Response` subclasses.
You may decide not to do this, in which case the typings will be unsound and
the behavior will be similar to how arrays work in Flow. See here for more
information: https://flow.org/en/docs/types/arrays/#toc-array-access-is-unsafe
See #3578 and #3337 for additional discussion. If you have ideas on how to
improve these typings, please share them in #3578 or open a new issue.
**BAD**
declare class test_express$CustomRequest extends express$Request {
foo: string;
}
**GOOD**
declare class test_express$CustomRequest extends express$Request {
foo: string | void;
}
*/
declare class express$Request extends http$IncomingMessage mixins express$RequestResponseBase {
baseUrl: string;
body: mixed;
cookies: { [cookie: string]: string, ... };
connection: net$Socket;
fresh: boolean;
hostname: string;
ip: string;
ips: Array<string>;
method: string;
originalUrl: string;
params: express$RequestParams;
path: string;
protocol: 'https' | 'http';
query: { [name: string]: string | Array<string>, ... };
route: string;
secure: boolean;
signedCookies: { [signedCookie: string]: string, ... };
stale: boolean;
subdomains: Array<string>;
xhr: boolean;
accepts(types: string): string | false;
accepts(types: Array<string>): string | false;
acceptsCharsets(...charsets: Array<string>): string | false;
acceptsEncodings(...encoding: Array<string>): string | false;
acceptsLanguages(...lang: Array<string>): string | false;
header(field: string): string | void;
is(type: string): string | false;
param(name: string, defaultValue?: string): string | void;
range(size: number, options?: express$RangeParserOptions): Array<express$RangeParserRange> | number | void;
}
declare type express$CookieOptions = {
domain?: string,
encode?: (value: string) => string,
expires?: Date,
httpOnly?: boolean,
maxAge?: number,
path?: string,
secure?: boolean,
signed?: boolean,
...
};
declare type express$Path = string | RegExp;
declare type express$RenderCallback = (err: Error | null, html?: string) => mixed;
declare type express$SendFileOptions = {
maxAge?: number,
root?: string,
lastModified?: boolean,
headers?: { [name: string]: string, ... },
dotfiles?: 'allow' | 'deny' | 'ignore',
...
};
declare class express$Response extends http$ServerResponse mixins express$RequestResponseBase {
headersSent: boolean;
locals: { [name: string]: mixed, ... };
append(field: string, value?: string): this;
attachment(filename?: string): this;
cookie(name: string, value: string, options?: express$CookieOptions): this;
clearCookie(name: string, options?: express$CookieOptions): this;
download(path: string, filename?: string, callback?: (err?: ?Error) => void): this;
format(typesObject: { [type: string]: Function, ... }): this;
json(body?: mixed): this;
jsonp(body?: mixed): this;
links(links: { [name: string]: string, ... }): this;
location(path: string): this;
redirect(url: string, ...args: Array<void>): this;
redirect(status: number, url: string, ...args: Array<void>): this;
render(view: string, locals?: { [name: string]: mixed, ... }, callback?: express$RenderCallback): this;
render(view: string, callback?: express$RenderCallback): this;
send(body?: mixed): this;
sendFile(path: string, options?: express$SendFileOptions, callback?: (err?: ?Error) => mixed): this;
sendStatus(statusCode: number): this;
header(field: string, value?: string): this;
header(headers: { [name: string]: string, ... }): this;
set(field: string, value?: string|string[]): this;
set(headers: { [name: string]: string, ... }): this;
status(statusCode: number): this;
type(type: string): this;
vary(field: string): this;
req: express$Request;
}
declare type express$NextFunction = (err?: ?Error | 'route') => mixed;
declare type express$Middleware<
Req: express$Request = express$Request,
Res: express$Response = express$Response,
> =
((req: Req, res: Res, next: express$NextFunction) => mixed) |
((error: Error, req: Req, res: Res, next: express$NextFunction) => mixed);
declare interface express$RouteMethodType<
T,
Req: express$Request = express$Request,
Res: express$Response = express$Response,
> {
(middleware: express$Middleware<Req, Res>): T;
(...middleware: Array<express$Middleware<Req, Res>>): T;
(
path: express$Path | $ReadOnlyArray<express$Path>,
...middleware: Array<express$Middleware<Req, Res>>
): T;
}
declare class express$Route<
Req: express$Request = express$Request,
Res: express$Response = express$Response,
> {
all: express$RouteMethodType<this, Req, Res>;
get: express$RouteMethodType<this, Req, Res>;
post: express$RouteMethodType<this, Req, Res>;
put: express$RouteMethodType<this, Req, Res>;
head: express$RouteMethodType<this, Req, Res>;
delete: express$RouteMethodType<this, Req, Res>;
options: express$RouteMethodType<this, Req, Res>;
trace: express$RouteMethodType<this, Req, Res>;
copy: express$RouteMethodType<this, Req, Res>;
lock: express$RouteMethodType<this, Req, Res>;
mkcol: express$RouteMethodType<this, Req, Res>;
move: express$RouteMethodType<this, Req, Res>;
purge: express$RouteMethodType<this, Req, Res>;
propfind: express$RouteMethodType<this, Req, Res>;
proppatch: express$RouteMethodType<this, Req, Res>;
unlock: express$RouteMethodType<this, Req, Res>;
report: express$RouteMethodType<this, Req, Res>;
mkactivity: express$RouteMethodType<this, Req, Res>;
checkout: express$RouteMethodType<this, Req, Res>;
merge: express$RouteMethodType<this, Req, Res>;
// @TODO Missing 'm-search' but get flow illegal name error.
notify: express$RouteMethodType<this, Req, Res>;
subscribe: express$RouteMethodType<this, Req, Res>;
unsubscribe: express$RouteMethodType<this, Req, Res>;
patch: express$RouteMethodType<this, Req, Res>;
search: express$RouteMethodType<this, Req, Res>;
connect: express$RouteMethodType<this, Req, Res>;
}
declare type express$IncomingMessage = http$IncomingMessage<tls$TLSSocket> | http$IncomingMessage<>;
declare class express$Router<
Req: express$Request = express$Request,
Res: express$Response = express$Response,
> extends express$Route<Req, Res> {
constructor(options?: express$RouterOptions): void;
route(path: string): express$Route<Req, Res>;
static <Req2: express$Request, Res2: express$Response>(
options?: express$RouterOptions,
): express$Router<Req2, Res2>;
use(middleware: express$Middleware<Req, Res>): this;
use(...middleware: Array<express$Middleware<Req, Res>>): this;
use(
path: express$Path | $ReadOnlyArray<express$Path>,
...middleware: Array<express$Middleware<Req, Res>>
): this;
use(path: string, router: express$Router<Req, Res>): this;
handle(req: express$IncomingMessage, res: http$ServerResponse, next: express$NextFunction): void;
param(
param: string,
callback: (
req: Req,
res: Res,
next: express$NextFunction,
value: string,
paramName: string,
) => mixed
): void;
(req: express$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
}
declare class express$Application<
Req: express$Request = express$Request,
Res: express$Response = express$Response,
> extends express$Router<Req, Res> mixins events$EventEmitter {
constructor(): void;
locals: { [name: string]: mixed, ... };
mountpath: string;
listen(port: number, hostname?: string, backlog?: number, callback?: (err?: ?Error) => mixed): ?http$Server;
listen(port: number, hostname?: string, callback?: (err?: ?Error) => mixed): ?http$Server;
listen(port: number, callback?: (err?: ?Error) => mixed): ?http$Server;
listen(path: string, callback?: (err?: ?Error) => mixed): ?http$Server;
listen(handle: Object, callback?: (err?: ?Error) => mixed): ?http$Server;
disable(name: string): void;
disabled(name: string): boolean;
enable(name: string): this;
enabled(name: string): boolean;
engine(name: string, callback: Function): void;
/**
* Mixed will not be taken as a value option. Issue around using the GET http method name and the get for settings.
*/
// get(name: string): mixed;
set(name: string, value: mixed): mixed;
render(name: string, options: { [name: string]: mixed, ... }, callback: express$RenderCallback): void;
render(name: string, callback: express$RenderCallback): void;
handle(req: express$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
// callable signature is not inherited
(req: express$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
}
declare module 'express' {
declare export type RouterOptions = express$RouterOptions;
declare export type CookieOptions = express$CookieOptions;
declare export type Middleware<
Req: express$Request = express$Request,
Res: express$Response = express$Response,
> = express$Middleware<Req, Res>;
declare export type NextFunction = express$NextFunction;
declare export type RequestParams = express$RequestParams;
declare export type $Response = express$Response;
declare export type $Request = express$Request;
declare export type $Application<
Req: express$Request = express$Request,
Res: express$Response = express$Response,
> = express$Application<Req, Res>;
declare module.exports: {
// If you try to call like a function, it will use this signature
<Req: express$Request, Res: express$Response>(): express$Application<Req, Res>,
// `static` property on the function
static: <Req: express$Request, Res: express$Response>(root: string, options?: Object) => express$Middleware<Req, Res>,
// `Router` property on the function
Router: typeof express$Router,
...
};
}