-
Notifications
You must be signed in to change notification settings - Fork 24
/
httpdispatcher.d.ts
142 lines (117 loc) · 5.3 KB
/
httpdispatcher.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
// Type definitions for httpdispatcher
// Project: https://github.com/alberto-bottarini/httpdispatcher
// Definitions by: Chris Barth <https://github.com/cjbarth>
import Http = require('http');
export = HttpDispatcher;
declare class HttpDispatcher {
constructor();
/**
* Generic function to set up request listener. Prefer onGet and onPost instead.
* @param method - The HTTP method to response to: "get" or "post"
* @param url - String, RegExp, or Function that will match and/or return true with a provided URL string.
* @param callback - The function that will be called on match.
*/
on(method:string, url:string|RegExp|HttpDispatcher.UrlMatcher, callback:HttpDispatcher.Callback):void;
/**
* Generic function to set up request filters. Prefer beforeFilter and afterFilter instead.
* @param method - Should this filter be applied "before" or "after" the request is processed?
* @param url - String, RegExp, or Function that will match and/or return true with a provided URL string.
* @param callback - The function that will be called on match.
*/
filter(method:string, url:string|RegExp|HttpDispatcher.UrlMatcher, callback:HttpDispatcher.Callback):void;
/**
* What to do when a GET reqeust matches _url_.
* @param url - String, RegExp, or Function that will match and/or return true with a provided URL string.
* @param callback - The function that will be called on match.
*/
onGet(url:string|RegExp|HttpDispatcher.UrlMatcher, callback:HttpDispatcher.Callback):void;
/**
* What to do when a POST request matches _url_.
* @param url - String, RegExp, or Function that will match and/or return true with a provided URL string.
* @param callback - The function that will be called on match.
*/
onPost(url:string|RegExp|HttpDispatcher.UrlMatcher, callback:HttpDispatcher.Callback):void;
/**
* What function should be called when there is an error.
* @param callback - The function that will be called on match.
*/
onError(callback:HttpDispatcher.Callback):void;
/**
* Set the virtual folder for the static resources.
* @param folder - Relative path in URL to static resources.
*/
setStatic(folder:string):void;
/**
* Set the physical/local folder for the static resources.
* @param dirname - Relative path in file system to static resources.
*/
setStaticDirname(dirname:string):void;
/**
* Called before a route is handeled; can modify the request and response.
* @param url - String, RegExp, or Function that will match and/or return true with a provided URL string.
* @param callback - The function that will be called on match.
*/
beforeFilter(url:string|RegExp|HttpDispatcher.UrlMatcher, callback:HttpDispatcher.ChainCallback):void;
/**
* Called after a route is handeled; can modify the request and response.
* @param url - String, RegExp, or Function that will match and/or return true with a provided URL string.
* @param callback - The function that will be called on match.
*/
afterFilter(url:string|RegExp|HttpDispatcher.UrlMatcher, callback:HttpDispatcher.ChainCallback):void;
/**
* Main entry point for httpdispatcher. Http.CreateServer would call this.
* @param request - A ClientRequest object from NodeJS _Http_ module.
* @param response - A ClientResponse object from NodeJS _Http_ module.
*/
dispatch(request:Http.IncomingMessage, response:Http.ServerResponse):void;
/**
* Listen to requests for static assests and serve them from the file system.
* @param request - A ClientRequest object from NodeJS _Http_ module.
* @param response - A ClientResponse object from NodeJS _Http_ module.
*/
staticListener(request:Http.IncomingMessage, response:Http.ServerResponse):void;
/**
* Return the Callback that matches the URL and method requested.
* @param url - The URL requested.
* @param method - The method, "get" or "post", that the URL was requested with.
* @returns Callback
*/
getListener(url:string, method:string):HttpDispatcher.Callback;
/**
* Return the Callback filter that matches the URL and method requested.
* @param url - The URL requested.
* @param type - The type of the filter, "before" or "after, for which the Callback should be returned.
* @returns Callback
*/
getFilters(url:string, type:string):HttpDispatcher.Callback;
/**
* Will determine if there is a match for a _url_ given a _config_.
* @param config - String, RegExp, or Function that will match and/or return true with a provided URL string.
* @param url - The string that will be passed to config() or compared (==) with config or matched with config.test() to return a boolean.
*/
urlMatches(config:string|RegExp|HttpDispatcher.UrlMatcher, url:string):boolean;
}
declare namespace HttpDispatcher {
export interface ClientRequest extends Http.IncomingMessage {
params: object,
/// available only on POST requests
body?: string,
/// available only on POST requests
bodyBuffer?: Buffer,
}
export class HttpChain {
constructor();
add(callback: ChainCallback): void;
addAll(callbacks: ChainCallback[]): void;
next(req: ClientRequest, res: Http.ServerResponse): void;
}
export interface Callback {
(request: ClientRequest, response: Http.ServerResponse): void;
}
export interface ChainCallback {
(request: ClientRequest, response: Http.ServerResponse, chain: HttpChain): void;
}
export interface UrlMatcher {
(url:string): boolean;
}
}