-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
134 lines (83 loc) · 3.88 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
import * as http from 'http'
export type Object = { [key: string]: any }
export interface DragonrendRequest {
headers: Object
url: string
method: string
raw: http.ClientRequest
path: string
query(): Promise<Object>
body(): Promise<any>
}
export interface DragonrendResponse {
raw: http.ServerResponse
header(key: string, value: string): DragonrendResponse
status(statusCode: number): DragonrendResponse
json(data: string): DragonrendResponse
text(data: string): DragonrendResponse
html(data: string): DragonrendResponse
send(data?: string): DragonrendResponse
}
export interface BaseContext {
request: DragonrendRequest
response: DragonrendResponse
}
export type Middleware = <Context extends BaseContext>(ctx: Context, next?: () => Promise<any>) => any
export interface RoutingOptions<Context extends BaseContext> {
prefix?: string
notFoundHandler?: (ctx: Context) => any
}
export interface Dragonrend<Context extends BaseContext> {
MIDDLEWARE: (...fns: Middleware[]) => Dragonrend<Context>
CONTEXT: (ctx: Partial<Context>) => Dragonrend<Context>
PARSER: (contentType: string, handler: (body: string) => any) => Dragonrend<Context>
CATCH_ERROR: (handler: (error: any, ctx: Context) => any) => Dragonrend<Context>
START: (portOrOptions: number | Object, cb?: (err: any) => any) => void
STOP: (cb?: () => any) => Promise<any> | void
middleware(...fns: Middleware[]): Dragonrend<Context>
context(ctx: Partial<Context>): Dragonrend<Context>
addContentTypeParser(contentType: string, handler: (body: string) => any): Dragonrend<Context>
setErrorHandler(handler: (error: any, ctx: Context) => any): Dragonrend<Context>
start(portOrOptions: number | Object, cb?: (err: any) => any): void
stop(cb?: () => any): Promise<any> | void
}
interface Router<Context extends BaseContext> {
GET: (path: string, ...handlers: Middleware[]) => Router<Context>
POST: (path: string, ...handlers: Middleware[]) => Router<Context>
PUT: (path: string, ...handlers: Middleware[]) => Router<Context>
PATCH: (path: string, ...handlers: Middleware[]) => Router<Context>
HEAD: (path: string, ...handlers: Middleware[]) => Router<Context>
OPTIONS: (path: string, ...handlers: Middleware[]) => Router<Context>
DELETE: (path: string, ...handlers: Middleware[]) => Router<Context>
MERGE: (...routers: Router<Context>[]) => Router<Context>
NOT_FOUND: (handler: (ctx: Context) => any) => Router<Context>
get(path: string, ...handlers: Middleware[]): Router<Context>
post(path: string, ...handlers: Middleware[]): Router<Context>
put(path: string, ...handlers: Middleware[]): Router<Context>
patch(path: string, ...handlers: Middleware[]): Router<Context>
head(path: string, ...handlers: Middleware[]): Router<Context>
options(path: string, ...handlers: Middleware[]): Router<Context>
delete(path: string, ...handlers: Middleware[]): Router<Context>
merge(...routers: Router<Context>[]): Router<Context>
setNotFoundHandler(handler: (ctx: Context) => any): Router<Context>
}
export function dragonrend<Context extends BaseContext>(options?: {
server?: boolean,
https?: boolean,
http2?: boolean,
noDelay?: boolean,
routing?: RoutingOptions<Context>,
autoIncluding?: boolean,
errorHandler?: (e: any, ctx: Context) => any,
}): Dragonrend<Context>
export function routing<Context extends BaseContext>(options?: RoutingOptions<Context>): Router<Context>
export function json(body: Object)
export function json(status: number, body: Object)
export function json(status: number, headers: Object, body: Object)
export function text(body: string)
export function text(status: number, body: string)
export function text(status: number, headers: Object, body: string)
export function html(body: string)
export function html(status: number, body: string)
export function html(status: number, headers: Object, body: string)
export function returnable(fn: Middleware)