1 |
|
2 |
|
3 | import { IncomingHttpHeaders } from "http";
|
4 | import Koa, { Context, Request } from "koa";
|
5 | import { ClassReflection, MethodReflection, ParameterReflection } from "tinspector";
|
6 | export declare type HttpMethod = "post" | "get" | "put" | "delete";
|
7 | export declare type KoaMiddleware = (ctx: Context, next: () => Promise<void>) => Promise<any>;
|
8 | export declare type RequestPart = keyof Request;
|
9 | export declare type HeaderPart = keyof IncomingHttpHeaders;
|
10 | export declare type Class = new (...args: any[]) => any;
|
11 | export declare type DefaultConverter = "Boolean" | "Number" | "Date" | "Object" | "Array";
|
12 | export declare type Converters = {
|
13 | default: {
|
14 | [key in DefaultConverter]: ConverterFunction;
|
15 | };
|
16 | converters: Map<Function, ConverterFunction>;
|
17 | };
|
18 | export declare type ConverterFunction = (value: any, path: string[], expectedType: Function | Function[], converters: Converters) => any;
|
19 | export declare type TypeConverter = {
|
20 | type: Class;
|
21 | converter: ConverterFunction;
|
22 | };
|
23 | export declare type ValidatorFunction = (value: string, ctx: Context) => Promise<string | undefined>;
|
24 | export declare type ValidatorStore = {
|
25 | [key: string]: ValidatorFunction;
|
26 | };
|
27 | export interface BindingDecorator {
|
28 | type: "ParameterBinding";
|
29 | process: (ctx: Context) => any;
|
30 | }
|
31 | export interface RouteDecorator {
|
32 | name: "Route";
|
33 | method: HttpMethod;
|
34 | url?: string;
|
35 | }
|
36 | export interface IgnoreDecorator {
|
37 | name: "Ignore";
|
38 | }
|
39 | export interface RootDecorator {
|
40 | name: "Root";
|
41 | url: string;
|
42 | }
|
43 | export interface MiddlewareDecorator {
|
44 | name: "Middleware";
|
45 | value: Middleware[];
|
46 | }
|
47 | export interface AuthDecorator {
|
48 | type: "authorize:public" | "authorize:role";
|
49 | value: string[];
|
50 | }
|
51 | export interface RouteInfo {
|
52 | url: string;
|
53 | method: HttpMethod;
|
54 | action: MethodReflection;
|
55 | controller: ClassReflection;
|
56 | }
|
57 | export interface ValidatorDecorator {
|
58 | type: "ValidatorDecorator";
|
59 | validator: ValidatorFunction | string;
|
60 | }
|
61 | export interface Invocation {
|
62 | context: Readonly<Context>;
|
63 | proceed(): Promise<ActionResult>;
|
64 | }
|
65 | export interface Middleware {
|
66 | execute(invocation: Readonly<Invocation>): Promise<ActionResult>;
|
67 | }
|
68 | export interface Facility {
|
69 | setup(app: Readonly<PlumierApplication>): Promise<void>;
|
70 | }
|
71 | export interface DependencyResolver {
|
72 | resolve(type: (new (...args: any[]) => any)): any;
|
73 | }
|
74 | export interface BodyParserOption {
|
75 | enableTypes?: string[];
|
76 | encode?: string;
|
77 | formLimit?: string;
|
78 | jsonLimit?: string;
|
79 | strict?: boolean;
|
80 | detectJSON?: (ctx: Koa.Context) => boolean;
|
81 | extendTypes?: {
|
82 | json?: string[];
|
83 | form?: string[];
|
84 | text?: string[];
|
85 | };
|
86 | onerror?: (err: Error, ctx: Koa.Context) => void;
|
87 | }
|
88 | export interface ValidationIssue {
|
89 | path: string[];
|
90 | messages: string[];
|
91 | }
|
92 | export interface FileUploadInfo {
|
93 | field: string;
|
94 | fileName: string;
|
95 | originalName: string;
|
96 | mime: string;
|
97 | size: number;
|
98 | encoding: string;
|
99 | }
|
100 | export interface FileParser {
|
101 | save(subDirectory?: string): Promise<FileUploadInfo[]>;
|
102 | }
|
103 | export interface Configuration {
|
104 | mode: "debug" | "production";
|
105 | |
106 |
|
107 |
|
108 | controller: string | Class[] | Class;
|
109 | |
110 |
|
111 |
|
112 | dependencyResolver: DependencyResolver;
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | responseStatus?: Partial<{
|
120 | [key in HttpMethod]: number;
|
121 | }>;
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | converters?: TypeConverter[];
|
131 | |
132 |
|
133 |
|
134 | validator?: (value: any, metadata: ParameterReflection, context: Context, validators?: {
|
135 | [key: string]: ValidatorFunction;
|
136 | }) => Promise<ValidationIssue[]>;
|
137 | |
138 |
|
139 |
|
140 | fileParser?: (ctx: Context) => FileParser;
|
141 | |
142 |
|
143 |
|
144 | validators?: ValidatorStore;
|
145 | }
|
146 | export interface PlumierConfiguration extends Configuration {
|
147 | middleware: Middleware[];
|
148 | facilities: Facility[];
|
149 | }
|
150 | export interface Application {
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | use(middleware: KoaMiddleware): Application;
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | use(middleware: Middleware): Application;
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | set(facility: Facility): Application;
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | set(config: Partial<Configuration>): Application;
|
194 | |
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | initialize(): Promise<Koa>;
|
206 | }
|
207 | export interface PlumierApplication extends Application {
|
208 | readonly koa: Koa;
|
209 | readonly config: Readonly<PlumierConfiguration>;
|
210 | }
|
211 | declare module "koa" {
|
212 | interface Context {
|
213 | route?: Readonly<RouteInfo>;
|
214 | config: Readonly<Configuration>;
|
215 | parameters?: any[];
|
216 | }
|
217 | }
|
218 | export declare namespace MiddlewareUtil {
|
219 | function fromKoa(middleware: KoaMiddleware): Middleware;
|
220 | }
|
221 | export declare class ActionResult {
|
222 | body?: any;
|
223 | status?: number | undefined;
|
224 | static fromContext(ctx: Context): ActionResult;
|
225 | private readonly headers;
|
226 | constructor(body?: any, status?: number | undefined);
|
227 | setHeader(key: string, value: string): this;
|
228 | setStatus(status: number): this;
|
229 | execute(ctx: Context): Promise<void>;
|
230 | }
|
231 | export declare class HttpStatusError extends Error {
|
232 | status: number;
|
233 | constructor(status: number, message?: string);
|
234 | }
|
235 | export declare class ConversionError extends HttpStatusError {
|
236 | issues: ValidationIssue;
|
237 | constructor(issues: ValidationIssue);
|
238 | }
|
239 | export declare class ValidationError extends HttpStatusError {
|
240 | issues: ValidationIssue[];
|
241 | constructor(issues: ValidationIssue[]);
|
242 | }
|
243 | export declare class DefaultDependencyResolver implements DependencyResolver {
|
244 | resolve(type: new (...args: any[]) => any): any;
|
245 | }
|
246 | export declare namespace bind {
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | function ctx(part?: string): (target: any, name: string, index: number) => void;
|
261 | |
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 | function request(part?: RequestPart): (target: any, name: string, index: number) => void;
|
274 | |
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | function body(part?: string): (target: any, name: string, index: number) => void;
|
285 | |
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | function header(key?: HeaderPart): (target: any, name: string, index: number) => void;
|
296 | |
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | function query(name?: string): (target: any, name: string, index: number) => void;
|
307 | |
308 |
|
309 |
|
310 |
|
311 |
|
312 | function user(): (target: any, name: string, index: number) => void;
|
313 | |
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | function file(): (target: any, name: string, index: number) => void;
|
323 | |
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | function custom(process: (ctx: Koa.Context) => any): (target: any, name: string, index: number) => void;
|
331 | }
|
332 | export declare class RouteDecoratorImpl {
|
333 | private decorateRoute;
|
334 | |
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | post(url?: string): (target: any, name: string) => void;
|
362 | |
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | get(url?: string): (target: any, name: string) => void;
|
390 | |
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 | put(url?: string): (target: any, name: string) => void;
|
418 | |
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 | delete(url?: string): (target: any, name: string) => void;
|
446 | |
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 | root(url: string): (target: any) => void;
|
468 | |
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 | ignore(): (target: any, name: string) => void;
|
482 | }
|
483 | export declare const route: RouteDecoratorImpl;
|
484 | export declare namespace middleware {
|
485 | function use(...middleware: (Middleware | KoaMiddleware)[]): (...args: any[]) => void;
|
486 | }
|
487 | export declare function domain(): (target: any) => void;
|
488 | export declare class AuthDecoratorImpl {
|
489 | |
490 |
|
491 |
|
492 | public(): (...args: any[]) => void;
|
493 | |
494 |
|
495 |
|
496 |
|
497 | role(...roles: string[]): (...args: any[]) => void;
|
498 | }
|
499 | export declare const authorize: AuthDecoratorImpl;
|
500 | export declare const DefaultConfiguration: Configuration;
|
501 | export declare namespace errorMessage {
|
502 | const RouteDoesNotHaveBackingParam = "PLUM1000: Route parameters ({0}) doesn't have appropriate backing parameter";
|
503 | const ActionDoesNotHaveTypeInfo = "PLUM1001: Parameter binding skipped because action doesn't have @route decorator";
|
504 | const DuplicateRouteFound = "PLUM1003: Duplicate route found in {0}";
|
505 | const ControllerPathNotFound = "PLUM1004: Controller file or directory {0} not found";
|
506 | const ModelWithoutTypeInformation = "PLUM1005: Parameter binding skipped because {0} doesn't have @domain() decorator";
|
507 | const ArrayWithoutTypeInformation = "PLUM1006: Parameter binding skipped because array field without @array() decorator found in ({0})";
|
508 | const ModelNotFound = "PLUM1007: Domain model not found, no class decorated with @domain() on provided classes";
|
509 | const ModelPathNotFound = "PLUM1007: Domain model not found, no class decorated with @domain() on path {0}";
|
510 | const PublicNotInParameter = "PLUM1008: @authorize.public() can not be applied to parameter";
|
511 | const UnableToInstantiateModel = "PLUM2000: Unable to instantiate model {0}. Domain model should be instantiable using default constructor";
|
512 | const UnableToConvertValue = "Unable to convert \"{0}\" into {1}";
|
513 | const FileSizeExceeded = "File {0} size exceeded the maximum size";
|
514 | const NumberOfFilesExceeded = "Number of files exceeded the maximum allowed";
|
515 | }
|