-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-server.d.ts
128 lines (107 loc) · 3.52 KB
/
file-server.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
/// <reference types="node" />
import {
RequestListener,
Server,
IncomingMessage,
ServerResponse
} from "http";
import { EventEmitter } from "events";
import { ListenOptions } from "net";
import { Stats } from "fs";
/**
* the prototype from which ctx is created.
* You may add additional properties to ctx by editing app.context
*/
interface BasicContext {
app: App;
/* parameter `properties` not supported */
throw(status?: number, message?: string): void;
/* parameter `properties` not supported */
assert(shouldBeTruthy: any, status?: number, message?: string): void;
}
interface Context extends BasicContext {
req: IncomingMessage;
res: ServerResponse;
state: {
pathname: string;
uriObject: URL;
};
url: string;
secure: boolean;
ip: string;
}
type Next = () => Promise<void>;
type Middleware = ((ctx: Context, next: Next) => Promise<void>);
export declare class App extends EventEmitter {
constructor();
middlewares: Array<Middleware>;
context: BasicContext;
/* NOT in koa! */
prepend(middleware: Middleware): this;
use(middleware: Middleware): this;
callback(): RequestListener;
/**
* a copypasta from net.d.ts
*/
listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): Server;
listen(port?: number, hostname?: string, listeningListener?: () => void): Server;
listen(port?: number, backlog?: number, listeningListener?: () => void): Server;
listen(port?: number, listeningListener?: () => void): Server;
listen(path: string, backlog?: number, listeningListener?: () => void): Server;
listen(path: string, listeningListener?: () => void): Server;
listen(options: ListenOptions, listeningListener?: () => void): Server;
listen(handle: any, backlog?: number, listeningListener?: () => void): Server;
listen(handle: any, listeningListener?: () => void): Server;
}
type SubRouter<T> = Array<((input: T) => T)>;
type Router<T> = {
[subrouter: string]: SubRouter<T>;
};
export declare class Serve {
constructor();
implementedMethods: ["GET", "PUT", "HEAD"];
/**
* sugar for
* this.implementedMethods.includes(ctx.req.method)
*
* if (ctx.state.pathname === "/api") {
* switch (ctx.state.uriObject.searchParams.get("action")) {
* case "list":
* case "get-list": return this.getList(ctx);
* case "upload": return this.uploadFile(ctx);
* }
* }
*
* this.serveFile
*/
[Symbol.iterator](): IterableIterator<Middleware>;
_getList(ctx: Context): Promise<void>;
_uploadFile(ctx: Context): Promise<void>;
_serveFile(ctx: Context): Promise<void>;
/**
* sugar for _getList with correct `this` reference
*/
getList(ctx: Context): Promise<void>;
/**
* sugar for _uploadFile with correct `this` reference
*/
uploadFile(ctx: Context): Promise<void>;
/**
* _serveFile with correct `this` reference.
* Will silence errors with status 404
*/
serveFile(ctx: Context): Promise<void>;
/**
* sugar for
* this.pathnameRouter.dir.push(pathname => join(directory, normalize(pathname)));
*
* this.pathnameRouter.file.push(pathname => join(directory, normalize(pathname)));
*/
mount(directory: string): this;
pathnameRouter: Router<string>;
fileResHeadersRouter: Router<string>;
routeThrough<T>(input: T, ...routers: SubRouter<T>): T;
etag(stats: Stats): string;
listCache: Map<string, object>;
mimeCache: Map<string, string>;
}