-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.d.ts
201 lines (179 loc) · 5.34 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
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
import http from 'http';
import {
Request as ExpressRequest,
Response as ExpressResponse,
NextFunction as ExpressNextFunction,
} from 'express';
declare global {
// Allow application-specific manner to extend Springpress interfaces.
namespace Springpress {
interface RouteMetadata {}
}
}
export abstract class Controller {
/**
* Gets the modular router path of the controller instance.
* @returns a modular router path
*/
getPath(): string;
/**
* Returns the router structure with handler and metadata of the controller.
* for converting to the modular express router.
* @returns The router structure (array of {@link Route})
*/
getRouter(): Route[];
}
export abstract class Middleware {
/**
* Returns a function that have access to the request object,
* the response object, and the next middleware function.
* @param routeMetadata - A route metadata
*/
abstract getHandler(routeMetadata: RouteMetadata): RouteHandler;
/**
* Returns a boolean represent to the middleware register condition.
* @param routeMetadata - A route metadata for conditioning
* @returns True if a route matches the middleware register condition, otherwise returns false
*/
getRegisterCondition(routeMetadata: RouteMetadata): boolean;
}
export class ControllerRegistry {
constructor(app: Express);
/**
* Registers a controller instance (a class extended {@link Controller})
* into the controller registry for routing system.
*
* @remarks
* The middleware run in the sequence as passed in the array.
*
* @param controller - A controller instance
* @param middleware - An array of middleware
*/
register(controller: Controller, middleware?: Middleware[]): void;
/**
* Registers global middleware in the application routing.
* @param middleware - A middleware or an array of middleware to register globally
*/
registerGlobalMiddleware(middleware: Middleware | Middleware[]): void;
/**
* @returns A count of registered controller
*/
size(): number;
}
/**
* Binds the decorated controller as a modular route handler.
* @param path - A root route path
*/
export function ControllerMapping(path: string): ClassDecorator;
/**
* Controls the incoming request body of decorated route.
* If the body does not match the requirement will throw {@link BadRequestException}.
* @param keys - Required request body (starts with "?" for optional key)
*/
export function RequestBody(...keys: string[]): MethodDecorator;
/**
* Binds the decorated method as a route handler.
* @param path - A route path
*/
export function RouteMapping(path: string, method: Methods): MethodDecorator;
export abstract class HttpException extends Error {
/**
* @returns A number of http response code
*/
abstract getStatusCode(): number;
}
/**
* Throws a http response code 400
*/
export class BadRequestException extends HttpException { }
/**
* Throws a http response code 403
*/
export class ForbiddenException extends HttpException { }
/**
* Throws a http response code 404
*/
export class NotFoundException extends HttpException { }
/**
* Throws a http response code 401
*/
export class UnauthorizedException extends HttpException { }
/**
* Accepted http request methods
*/
export enum Methods {
GET = 'GET',
HEAD = 'HEAD',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
CONNECT = 'CONNECT',
OPTIONS = 'OPTIONS',
TRACE = 'TRACE',
PATCH = 'PATCH',
}
export type RouteHandler = (req: Request, res: Response, next: NextFunction) => Promise<void>;
export interface RouteMetadata extends Springpress.RouteMetadata {
path: string;
method: Methods;
};
export type Route = {
handler: RouteHandler,
metadata: RouteMetadata,
};
export type RequestParam = {
[key: string]: string,
};
export type RequestQuery = {
[key: string]: undfined | string | string[],
};
export interface ApiSchema {
/**
* Key-value pairs of incoming data from the request body
*/
body?: Object;
/**
* An object containing properties mapped to the route parameter
*/
param?: RequestParam;
/**
* An object containing a property for each query string parameter
*/
query?: RequestQuery;
/**
* An object represents the response format
*/
res?: Object;
};
export type Request<T extends ApiSchema = ApiSchema> = ExpressRequest<T["param"], any, T["body"], T["query"]>;
export type Response<T extends ApiSchema = ApiSchema> = ExpressResponse<T["res"]>;
/**
* For constructing API route schema by following the {@link ApiSchema} type.
*/
export type CreateApiSchema<T extends ApiSchema> = T;
export interface NextFunction extends ExpressNextFunction { }
export class RouteUtil {
/**
* Add data into the route metadata.
* @param data - A key-value pairs of data to merge into the route metadata
*/
static addRouteMetadata(data: Record<string, any>): MethodDecorator;
}
export abstract class Springpress {
constructor(port: number);
abstract onStartup(): Promise<void>;
/**
* Binds the http server and return nodejs net server.
* When invoking this method will invoke {@link Springpress.onStartup} first
* @returns A promise of nodejs net server
*/
listen(): Promise<http.Server>;
/**
* @returns A port that binds this server
*/
getPort(): number;
/**
* @returns The controller registry instance
*/
getControllerRegistry(): ControllerRegistry;
}