UNPKG

3.48 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import {
4 RequestListener,
5 Server,
6 IncomingMessage,
7 ServerResponse
8} from "http";
9import { EventEmitter } from "events";
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 = () => Promise<void>;
39type Middleware = ((ctx: Context, next: Next) => Promise<void>)
40
41export 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
67export 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
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 _getList with correct `this` reference
93 */
94 getList (ctx: Context): Promise<void>;
95
96 /**
97 * sugar for _uploadFile with correct `this` reference
98 */
99 uploadFile (ctx: Context): Promise<void>;
100
101 /**
102 * _serveFile with correct `this` reference
103 * will silence errors with status 404
104 */
105 serveFile (ctx: Context): Promise<void>;
106
107 /**
108 * sugar for
109 * this.pathnameRouter.dir.push(pathname => join(directory, normalize(pathname)));
110 *
111 * this.pathnameRouter.file.push(pathname => join(directory, normalize(pathname)));
112 */
113 mount(directory: string): this
114
115 pathnameRouter: object;
116 fileResHeadersRouter: object;
117 routeThrough<T>(input: T, ...routers: Array<((input: T) => T)>): T;
118
119 etag (stats: Stats): string;
120 listCache: Map<string, object>;
121 mimeCache: Map<string, string>;
122}
\No newline at end of file