forked from nanoexpress/legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanoexpress.d.ts
229 lines (213 loc) · 6.35 KB
/
nanoexpress.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
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
import {
AppOptions as AppOptionsBasic,
TemplatedApp as AppTemplatedApp,
HttpRequest as HttpRequestBasic,
HttpResponse as HttpResponseBasic,
WebSocket as WebSocketBasic
} from 'uWebSockets.js';
import { Ajv, Options as AjvOptions } from 'ajv';
declare namespace nanoexpress {
export interface SwaggerOptions {
[key: string]: SwaggerOptions | string;
}
export interface AppOptions extends AppOptionsBasic {
https?: {
key_file_name: string;
cert_file_name: string;
passphare: string;
};
ajv?: AjvOptions;
configureAjv(ajv: Ajv): Ajv;
swagger?: SwaggerOptions;
}
export interface HttpRequestHeaders {
[key: string]: string;
}
export interface HttpRequestQueries {
[key: string]: string;
}
export interface HttpRequestParams {
[key: string]: string;
}
export interface HttpRequestBody {
[key: string]: string | object | any[];
}
export interface HttpRequestCookies {
[key: string]: string;
}
export interface WebSocket extends WebSocketBasic {
on(name: string, listener: Function);
once(name: string, listener: Function);
off(name: string, listener?: Function);
emit(name: string, ...args: any[]);
}
export interface HttpRequest extends HttpRequestBasic {
path: string;
url: string;
headers: HttpRequestHeaders;
cookies: HttpRequestCookies;
query: HttpRequestQueries;
params: HttpRequestParams;
body?: HttpRequestBody;
__response?: HttpResponse;
}
export interface CookieOptions {
httpOnly?: boolean;
secure?: boolean;
maxAge?: number;
path?: string;
domain?: string;
signed?: boolean;
expires?: number | string;
}
export interface HttpResponse extends HttpResponseBasic {
status(code: number): HttpResponse;
setHeader(key: string, value: string | number): HttpResponse;
hasHeader(key: string): HttpResponse;
removeHeader(key: string): HttpResponse;
applyHeadersAndStatus(): HttpResponse;
setHeaders(headers: HttpRequestHeaders): HttpResponse;
writeHeaderValues(name: string, value: string[]): HttpResponse;
writeHeaders(headers: HttpRequestHeaders): HttpResponse;
writeHead(code: number, headers: HttpRequestHeaders): HttpResponse;
redirect(code: number | string, path?: string): HttpResponse;
send(result: string | object | any[]): HttpResponse;
sendFile(
filename: string,
config?: StaticOptions,
cache?: boolean
): Promise<HttpResponse>;
json(result: object | any[]): HttpResponse;
cork(fn: Function): HttpResponse;
setCookie(
key: string,
value: string,
options?: CookieOptions
): HttpResponse;
cookie(key: string, value: string, options?: CookieOptions): HttpResponse;
hasCookie(key: string): HttpResponse;
removeCookie(key: string, options?: CookieOptions): HttpResponse;
__request?: HttpRequest;
}
type HttpRoute = (
req: HttpRequest,
res: HttpResponse,
next?: Function,
config?: object,
previousMiddlewareResult?: any
) => any;
type WsRoute = (req: HttpRequest, ws: WebSocket) => any;
export interface AppRoute {
get: HttpRoute;
post: HttpRoute;
put: HttpRoute;
patch: HttpRoute;
head: HttpRoute;
delete: HttpRoute;
options: HttpRoute;
trace: HttpRoute;
any: HttpRoute;
ws: WsRoute;
}
interface SchemaValue {
[key: string]: string | SchemaValue;
}
interface Schema {
headers: boolean | SchemaValue;
cookies: boolean | SchemaValue;
query: boolean | SchemaValue;
params: boolean | SchemaValue;
body: string | SchemaValue;
}
interface MiddlewareOption {
schema?: Schema;
isRaw?: boolean;
direct?: boolean;
}
export interface AppRoute {
callback<HttpRequest, HttpResponse>(
req: HttpRequest,
res: HttpResponse
): any;
middlewares?: HttpRoute[];
schema?: Schema;
}
export interface AppRoutes {
[key: string]: AppRoute | AppRoutes | Function;
}
export interface AppConfig {
config: {
set(key: string, value: any): void;
get(key: string): any;
};
}
export interface StaticOptions {
index?: string;
addPrettyUrl?: boolean;
streamConfig?: object;
}
interface Middleware extends MiddlewareOption, HttpRoute {}
interface validationErrorItems {
type: string;
messages: string[];
}
export interface validationErrors {
type: string;
errors: validationErrorItems;
}
interface nanoexpressAppInterface {
host: string | null;
port: number | null;
address: string;
static(
route: string,
folderPath: string,
staticOptions?: StaticOptions
): nanoexpressApp;
use(path: string | Middleware, ...fns: Middleware[]): nanoexpressApp;
get(path: string, ...fns: Middleware[]): nanoexpressApp;
post(path: string, ...fns: Middleware[]): nanoexpressApp;
put(path: string, ...fns: Middleware[]): nanoexpressApp;
patch(path: string, ...fns: Middleware[]): nanoexpressApp;
del(path: string, ...fns: Middleware[]): nanoexpressApp;
options(path: string, ...fns: Middleware[]): nanoexpressApp;
head(path: string, ...fns: Middleware[]): nanoexpressApp;
trace(path: string, ...fns: Middleware[]): nanoexpressApp;
any(path: string | Middleware, ...fns: Middleware[]): nanoexpressApp;
ws(path: string, ...fns: Middleware[]): nanoexpressApp;
listen(port: number, host?: string): Promise<nanoexpressApp>;
close(): boolean;
setErrorHandler(
errorHandlerCallback: (
err: Error,
req: HttpRequest,
res: HttpResponse
) => HttpResponse
): nanoexpressApp;
setNotFoundHandler(
notFoundHandlerCallback: (
res: HttpRequestBasic,
req: HttpRequestBasic
) => HttpRequestBasic
): nanoexpressApp;
setValidationErrorHandler(
validationErrorHandlerCallback: (
errors: validationErrors,
req: HttpRequest,
res: HttpResponse
) => any
): nanoexpressApp;
register(
appModuleRegisterCallback: (app: nanoexpressApp) => any
): nanoexpressApp;
define(prefix: string, routes?: AppRoutes): nanoexpressApp;
config: AppConfig;
}
export interface nanoexpressApp
extends Omit<AppTemplatedApp, keyof nanoexpressAppInterface>,
nanoexpressAppInterface {}
}
declare function nanoexpress(
options?: nanoexpress.AppOptions
): nanoexpress.nanoexpressApp;
export = nanoexpress;