1 |
|
2 |
|
3 | import {
|
4 | RequestListener,
|
5 | Server,
|
6 | IncomingMessage,
|
7 | ServerResponse
|
8 | } from "http";
|
9 |
|
10 | import { ListenOptions } from "net";
|
11 |
|
12 | import { Stats } from "fs";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | type BasicContext = {
|
19 | app: App,
|
20 |
|
21 | throw (status?: number, message?: string): void;
|
22 |
|
23 | assert (shouldBeTruthy: any, status?: number, message?: string): void;
|
24 | }
|
25 |
|
26 | type Context = BasicContext & {
|
27 | req: IncomingMessage,
|
28 | res: ServerResponse,
|
29 | state: {
|
30 | pathname: string,
|
31 | uriObject: URL
|
32 | },
|
33 | url: string,
|
34 | secure: boolean,
|
35 | ip: string
|
36 | }
|
37 |
|
38 | type Next = () => void;
|
39 | type Middleware = ((ctx: Context, next: Next) => Promise<void>)
|
40 |
|
41 | export declare class App {
|
42 | constructor();
|
43 | middlewares: Array<Middleware>;
|
44 | context: BasicContext;
|
45 |
|
46 | /* NOT in koa! */
|
47 | prepend (middleware: Middleware): this
|
48 |
|
49 | use (middleware: Middleware): this
|
50 |
|
51 | callback (): RequestListener
|
52 |
|
53 | /**
|
54 | * a copypasta from net.d.ts
|
55 | */
|
56 | listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): Server;
|
57 | listen(port?: number, hostname?: string, listeningListener?: () => void): Server;
|
58 | listen(port?: number, backlog?: number, listeningListener?: () => void): Server;
|
59 | listen(port?: number, listeningListener?: () => void): Server;
|
60 | listen(path: string, backlog?: number, listeningListener?: () => void): Server;
|
61 | listen(path: string, listeningListener?: () => void): Server;
|
62 | listen(options: ListenOptions, listeningListener?: () => void): Server;
|
63 | listen(handle: any, backlog?: number, listeningListener?: () => void): Server;
|
64 | listen(handle: any, listeningListener?: () => void): Server;
|
65 | }
|
66 |
|
67 | export declare class Serve {
|
68 | constructor();
|
69 | implementedMethods: ["GET", "PUT", "HEAD"];
|
70 |
|
71 | /**
|
72 | * sugar for
|
73 | * this.implementedMethods.includes(ctx.req.method)
|
74 | *
|
75 | * if (ctx.state.pathname === "/api") {
|
76 | * switch (ctx.state.uriObject.searchParams.get("action")) {
|
77 | * case "list":
|
78 | * case "get-list": return this.getList(ctx);
|
79 | * case "upload": return this.uploadFile(ctx);
|
80 | * }
|
81 | * }
|
82 | *
|
83 | * this.serveFile(ctx);
|
84 | */
|
85 | [Symbol.iterator] (): IterableIterator<Middleware>;
|
86 |
|
87 | getList (ctx: Context): Promise<void>;
|
88 | uploadFile (ctx: Context): Promise<void>;
|
89 | serveFile (ctx: Context): Promise<void>;
|
90 |
|
91 | /**
|
92 | * sugar for
|
93 | * this.pathnameRouter.dir.push(pathname => join(directory, normalize(pathname)));
|
94 | *
|
95 | * this.pathnameRouter.file.push(pathname => join(directory, normalize(pathname)));
|
96 | */
|
97 | mount(directory: string): this
|
98 |
|
99 | pathnameRouter: object;
|
100 | fileResHeadersRouter: object;
|
101 | routeThrough<T>(input: T, ...routers: Array<((input: T) => T)>): T;
|
102 |
|
103 | etag (stats: Stats): string;
|
104 | listCache: Map<string, object>;
|
105 | mimeCache: Map<string, string>;
|
106 | } |
\ | No newline at end of file |