UNPKG

3.31 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import {
4 RequestListener,
5 Server,
6 IncomingMessage,
7 ServerResponse
8} from "http";
9
10import { ListenOptions } from "net";
11
12import { Stats } from "fs";
13
14/**
15 * the prototype from which ctx is created.
16 * You may add additional properties to ctx by editing app.context
17 */
18type BasicContext = {
19 app: App,
20 /* parameter `properties` not supported */
21 throw (status?: number, message?: string): void;
22 /* parameter `properties` not supported */
23 assert (shouldBeTruthy: any, status?: number, message?: string): void;
24}
25
26type 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
38type Next = () => void;
39type Middleware = ((ctx: Context, next: Next) => Promise<void>)
40
41export 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 /**
52 * Parameters `host` and `protocol` are NOT in koa!
53 * They are only required for providing a right info in
54 * Context#url & Context#state.uriObject & Context#secure,
55 * omitting them would just be fine otherwise
56 */
57 callback (host?: string, protocol?: string): RequestListener
58
59 /**
60 * a copypasta from net.d.ts
61 */
62 listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): Server;
63 listen(port?: number, hostname?: string, listeningListener?: () => void): Server;
64 listen(port?: number, backlog?: number, listeningListener?: () => void): Server;
65 listen(port?: number, listeningListener?: () => void): Server;
66 listen(path: string, backlog?: number, listeningListener?: () => void): Server;
67 listen(path: string, listeningListener?: () => void): Server;
68 listen(options: ListenOptions, listeningListener?: () => void): Server;
69 listen(handle: any, backlog?: number, listeningListener?: () => void): Server;
70 listen(handle: any, listeningListener?: () => void): Server;
71}
72
73export 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(ctx);
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
99 * this.pathnameRouter.dir.push(pathname => join(directory, normalize(pathname)));
100 *
101 * this.pathnameRouter.file.push(pathname => join(directory, normalize(pathname)));
102 */
103 mount(directory: string): this
104
105 pathnameRouter: object;
106 fileResHeadersRouter: object;
107 routeThrough<T>(input: T, ...routers: Array<((input: T) => T)>): T;
108
109 etag (stats: Stats): string;
110 listCache: Map<string, object>;
111 mimeCache: Map<string, string>;
112}
\No newline at end of file