1 |
|
2 |
|
3 | import {
|
4 | RequestListener,
|
5 | Server,
|
6 | IncomingMessage,
|
7 | ServerResponse
|
8 | } from "http";
|
9 | import { EventEmitter } from "events";
|
10 | import { ListenOptions } from "net";
|
11 |
|
12 | import { Stats } from "fs";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | interface 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 | interface Context extends 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 = () => Promise<void>;
|
39 | type Middleware = ((ctx: Context, next: Next) => Promise<void>);
|
40 |
|
41 | export declare class App extends EventEmitter {
|
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 | type SubRouter<T> = Array<((input: T) => T)>;
|
68 |
|
69 | type Router<T> = {
|
70 | [subrouter: string]: SubRouter<T>;
|
71 | };
|
72 |
|
73 | export declare class Serve {
|
74 | constructor();
|
75 | implementedMethods: ["GET", "PUT", "HEAD"];
|
76 |
|
77 | /**
|
78 | * sugar for
|
79 | * this.implementedMethods.includes(ctx.req.method)
|
80 | *
|
81 | * if (ctx.state.pathname === "/api") {
|
82 | * switch (ctx.state.uriObject.searchParams.get("action")) {
|
83 | * case "list":
|
84 | * case "get-list": return this.getList(ctx);
|
85 | * case "upload": return this.uploadFile(ctx);
|
86 | * }
|
87 | * }
|
88 | *
|
89 | * this.serveFile
|
90 | */
|
91 | [Symbol.iterator](): IterableIterator<Middleware>;
|
92 |
|
93 | _getList(ctx: Context): Promise<void>;
|
94 | _uploadFile(ctx: Context): Promise<void>;
|
95 | _serveFile(ctx: Context): Promise<void>;
|
96 |
|
97 | /**
|
98 | * sugar for _getList with correct `this` reference
|
99 | */
|
100 | getList(ctx: Context): Promise<void>;
|
101 |
|
102 | /**
|
103 | * sugar for _uploadFile with correct `this` reference
|
104 | */
|
105 | uploadFile(ctx: Context): Promise<void>;
|
106 |
|
107 | /**
|
108 | * _serveFile with correct `this` reference.
|
109 | * Will silence errors with status 404
|
110 | */
|
111 | serveFile(ctx: Context): Promise<void>;
|
112 |
|
113 | /**
|
114 | * sugar for
|
115 | * this.pathnameRouter.dir.push(pathname => join(directory, normalize(pathname)));
|
116 | *
|
117 | * this.pathnameRouter.file.push(pathname => join(directory, normalize(pathname)));
|
118 | */
|
119 | mount(directory: string): this;
|
120 |
|
121 | pathnameRouter: Router<string>;
|
122 | fileResHeadersRouter: Router<string>;
|
123 | routeThrough<T>(input: T, ...routers: SubRouter<T>): T;
|
124 |
|
125 | etag(stats: Stats): string;
|
126 | listCache: Map<string, object>;
|
127 | mimeCache: Map<string, string>;
|
128 | } |
\ | No newline at end of file |