|
| 1 | +// flow-typed signature: 1a33220ead1c6b6e3205a55b2a2ec3a0 |
| 2 | +// flow-typed version: 18b7d8b101/koa_v2.x.x/flow_>=v0.47.x |
| 3 | + |
| 4 | +/* |
| 5 | + * Type def from from source code of koa. |
| 6 | + * this: https://github.com/koajs/koa/commit/08eb1a20c3975230aa1fe1c693b0cd1ac7a0752b |
| 7 | + * previous: https://github.com/koajs/koa/commit/fabf5864c6a5dca0782b867a263b1b0825a05bf9 |
| 8 | + * |
| 9 | + * Changelog |
| 10 | + * breaking: remove unused app.name |
| 11 | + * breaking: ctx.throw([status], [msg], [properties]) (caused by http-errors (#957) ) |
| 12 | +**/ |
| 13 | +declare module 'koa' { |
| 14 | + // Currently, import type doesnt work well ? |
| 15 | + // so copy `Server` from flow/lib/node.js#L820 |
| 16 | + declare class Server extends net$Server { |
| 17 | + listen(port?: number, hostname?: string, backlog?: number, callback?: Function): Server, |
| 18 | + listen(path: string, callback?: Function): Server, |
| 19 | + listen(handle: Object, callback?: Function): Server, |
| 20 | + close(callback?: Function): Server, |
| 21 | + maxHeadersCount: number, |
| 22 | + setTimeout(msecs: number, callback: Function): Server, |
| 23 | + timeout: number, |
| 24 | + } |
| 25 | + declare type ServerType = Server; |
| 26 | + |
| 27 | + declare type JSON = | string | number | boolean | null | JSONObject | JSONArray; |
| 28 | + declare type JSONObject = { [key: string]: JSON }; |
| 29 | + declare type JSONArray = Array<JSON>; |
| 30 | + |
| 31 | + declare type SimpleHeader = { |
| 32 | + 'set-cookie'?: Array<string>, |
| 33 | + [key: string]: string, |
| 34 | + }; |
| 35 | + |
| 36 | + declare type RequestJSON = { |
| 37 | + 'method': string, |
| 38 | + 'url': string, |
| 39 | + 'header': SimpleHeader, |
| 40 | + }; |
| 41 | + declare type RequestInspect = void|RequestJSON; |
| 42 | + declare type Request = { |
| 43 | + app: Application, |
| 44 | + req: http$IncomingMessage, |
| 45 | + res: http$ServerResponse, |
| 46 | + ctx: Context, |
| 47 | + response: Response, |
| 48 | + |
| 49 | + fresh: boolean, |
| 50 | + header: SimpleHeader, |
| 51 | + headers: SimpleHeader, // alias as header |
| 52 | + host: string, |
| 53 | + hostname: string, |
| 54 | + href: string, |
| 55 | + idempotent: boolean, |
| 56 | + ip: string, |
| 57 | + ips: string[], |
| 58 | + method: string, |
| 59 | + origin: string, |
| 60 | + originalUrl: string, |
| 61 | + path: string, |
| 62 | + protocol: string, |
| 63 | + query: {[key: string]: string}, // always string |
| 64 | + querystring: string, |
| 65 | + search: string, |
| 66 | + secure: boolean, // Shorthand for ctx.protocol == "https" to check if a request was issued via TLS. |
| 67 | + socket: net$Socket, |
| 68 | + stale: boolean, |
| 69 | + subdomains: string[], |
| 70 | + type: string, |
| 71 | + url: string, |
| 72 | + |
| 73 | + charset: string|void, |
| 74 | + length: number|void, |
| 75 | + |
| 76 | +// Those functions comes from https://github.com/jshttp/accepts/blob/master/index.js |
| 77 | +// request.js$L445 |
| 78 | +// https://github.com/jshttp/accepts/blob/master/test/type.js |
| 79 | + accepts: ((args: string[]) => string|false)& |
| 80 | + // ToDo: There is an issue https://github.com/facebook/flow/issues/3009 |
| 81 | + // if you meet some error here, temporarily add an additional annotation |
| 82 | + // like: `request.accepts((['json', 'text']:Array<string>))` to fix it. |
| 83 | + ((arg: string, ...args: string[]) => string|false) & |
| 84 | + ( () => string[] ) , // return the old value. |
| 85 | + |
| 86 | +// https://github.com/jshttp/accepts/blob/master/index.js#L153 |
| 87 | +// https://github.com/jshttp/accepts/blob/master/test/charset.js |
| 88 | + acceptsCharsets: ( (args: string[]) => buffer$Encoding|false)& |
| 89 | + // ToDo: https://github.com/facebook/flow/issues/3009 |
| 90 | + // if you meet some error here, see L70. |
| 91 | + ( (arg: string, ...args: string[]) => buffer$Encoding|false ) & |
| 92 | + ( () => string[] ), |
| 93 | + |
| 94 | +// https://github.com/jshttp/accepts/blob/master/index.js#L119 |
| 95 | +// https://github.com/jshttp/accepts/blob/master/test/encoding.js |
| 96 | + acceptsEncodings: ( (args: string[]) => string|false)& |
| 97 | + // ToDo: https://github.com/facebook/flow/issues/3009 |
| 98 | + // if you meet some error here, see L70. |
| 99 | + ( (arg: string, ...args: string[]) => string|false ) & |
| 100 | + ( () => string[] ), |
| 101 | + |
| 102 | +// https://github.com/jshttp/accepts/blob/master/index.js#L185 |
| 103 | +// https://github.com/jshttp/accepts/blob/master/test/language.js |
| 104 | + acceptsLanguages: ( (args: string[]) => string|false) & |
| 105 | + // ToDo: https://github.com/facebook/flow/issues/3009 |
| 106 | + // if you meet some error here, see L70. |
| 107 | + ( (arg: string, ...args: string[]) => string|false ) & |
| 108 | + ( () => string[] ), |
| 109 | + |
| 110 | + get: (field: string) => string, |
| 111 | + |
| 112 | +/* https://github.com/jshttp/type-is/blob/master/test/test.js |
| 113 | +* Check if the incoming request contains the "Content-Type" |
| 114 | +* header field, and it contains any of the give mime `type`s. |
| 115 | +* If there is no request body, `null` is returned. |
| 116 | +* If there is no content type, `false` is returned. |
| 117 | +* Otherwise, it returns the first `type` that matches. |
| 118 | +*/ |
| 119 | + is: ( (args: string[]) => null|false|string)& |
| 120 | + ( (arg: string, ...args: string[]) => null|false|string ) & |
| 121 | + ( () => string ), // should return the mime type |
| 122 | + |
| 123 | + toJSON: () => RequestJSON, |
| 124 | + inspect: () => RequestInspect, |
| 125 | + |
| 126 | + [key: string]: mixed, // props added by middlewares. |
| 127 | + }; |
| 128 | + |
| 129 | + declare type ResponseJSON = { |
| 130 | + 'status': mixed, |
| 131 | + 'message': mixed, |
| 132 | + 'header': mixed, |
| 133 | + }; |
| 134 | + declare type ResponseInspect = { |
| 135 | + 'status': mixed, |
| 136 | + 'message': mixed, |
| 137 | + 'header': mixed, |
| 138 | + 'body': mixed, |
| 139 | + }; |
| 140 | + declare type Response = { |
| 141 | + app: Application, |
| 142 | + req: http$IncomingMessage, |
| 143 | + res: http$ServerResponse, |
| 144 | + ctx: Context, |
| 145 | + request: Request, |
| 146 | + |
| 147 | + // docs/api/response.md#L113. |
| 148 | + body: string|Buffer|stream$Stream|Object|Array<mixed>|null, // JSON contains null |
| 149 | + etag: string, |
| 150 | + header: SimpleHeader, |
| 151 | + headers: SimpleHeader, // alias as header |
| 152 | + headerSent: boolean, |
| 153 | + // can be set with string|Date, but get with Date. |
| 154 | + // set lastModified(v: string|Date), // 0.36 doesn't support this. |
| 155 | + lastModified: Date, |
| 156 | + message: string, |
| 157 | + socket: net$Socket, |
| 158 | + status: number, |
| 159 | + type: string, |
| 160 | + writable: boolean, |
| 161 | + |
| 162 | + // charset: string, // doesn't find in response.js |
| 163 | + length: number|void, |
| 164 | + |
| 165 | + append: (field: string, val: string | string[]) => void, |
| 166 | + attachment: (filename?: string) => void, |
| 167 | + get: (field: string) => string, |
| 168 | + // https://github.com/jshttp/type-is/blob/master/test/test.js |
| 169 | + // https://github.com/koajs/koa/blob/v2.x/lib/response.js#L382 |
| 170 | + is: ( (arg: string[]) => false|string) & |
| 171 | + ( (arg: string, ...args: string[]) => false|string ) & |
| 172 | + ( () => string ), // should return the mime type |
| 173 | + redirect: (url: string, alt?: string) => void, |
| 174 | + remove: (field: string) => void, |
| 175 | + // https://github.com/koajs/koa/blob/v2.x/lib/response.js#L418 |
| 176 | + set: ((field: string, val: string | string[]) => void)& |
| 177 | + ((field: {[key: string]: string | string[]}) => void), |
| 178 | + |
| 179 | + vary: (field: string) => void, |
| 180 | + |
| 181 | + // https://github.com/koajs/koa/blob/v2.x/lib/response.js#L519 |
| 182 | + toJSON(): ResponseJSON, |
| 183 | + inspect(): ResponseInspect, |
| 184 | + |
| 185 | + [key: string]: mixed, // props added by middlewares. |
| 186 | + } |
| 187 | + |
| 188 | + declare type ContextJSON = { |
| 189 | + request: RequestJSON, |
| 190 | + response: ResponseJSON, |
| 191 | + app: ApplicationJSON, |
| 192 | + originalUrl: string, |
| 193 | + req: '<original node req>', |
| 194 | + res: '<original node res>', |
| 195 | + socket: '<original node socket>', |
| 196 | + }; |
| 197 | + // https://github.com/pillarjs/cookies |
| 198 | + declare type CookiesSetOptions = { |
| 199 | + maxAge: number, // milliseconds from Date.now() for expiry |
| 200 | + expires: Date, //cookie's expiration date (expires at the end of session by default). |
| 201 | + path: string, // the path of the cookie (/ by default). |
| 202 | + domain: string, // domain of the cookie (no default). |
| 203 | + secure: boolean, // false by default for HTTP, true by default for HTTPS |
| 204 | + httpOnly: boolean, // a boolean indicating whether the cookie is only to be sent over HTTP(S), |
| 205 | + // and not made available to client JavaScript (true by default). |
| 206 | + signed: boolean, // whether the cookie is to be signed (false by default) |
| 207 | + overwrite: boolean, // whether to overwrite previously set cookies of the same name (false by default). |
| 208 | + }; |
| 209 | + declare type Cookies = { |
| 210 | + get: (name: string, options?: {signed: boolean}) => string|void, |
| 211 | + set: ((name: string, value: string, options?: CookiesSetOptions) => Context)& |
| 212 | + // delete cookie (an outbound header with an expired date is used.) |
| 213 | + ( (name: string) => Context), |
| 214 | + }; |
| 215 | + // The default props of context come from two files |
| 216 | + // `application.createContext` & `context.js` |
| 217 | + declare type Context = { |
| 218 | + accept: $PropertyType<Request, 'accept'>, |
| 219 | + app: Application, |
| 220 | + cookies: Cookies, |
| 221 | + name?: string, // ? |
| 222 | + originalUrl: string, |
| 223 | + req: http$IncomingMessage, |
| 224 | + request: Request, |
| 225 | + res: http$ServerResponse, |
| 226 | + respond?: boolean, // should not be used, allow bypassing koa application.js#L193 |
| 227 | + response: Response, |
| 228 | + state: Object, |
| 229 | + |
| 230 | + // context.js#L55 |
| 231 | + assert: (test: mixed, status: number, message?: string, opts?: mixed) => void, |
| 232 | + // context.js#L107 |
| 233 | + // if (!(err instanceof Error)) err = new Error(`non-error thrown: ${err}`); |
| 234 | + onerror: (err?: mixed) => void, |
| 235 | + // context.md#L88 |
| 236 | + throw: ( status: number, msg?: string, opts?: Object) => void, |
| 237 | + toJSON(): ContextJSON, |
| 238 | + inspect(): ContextJSON, |
| 239 | + |
| 240 | + // ToDo: add const for some props, |
| 241 | + // while the `const props` feature of Flow is landing in future |
| 242 | + // cherry pick from response |
| 243 | + attachment: $PropertyType<Response, 'attachment'>, |
| 244 | + redirect: $PropertyType<Response, 'redirect'>, |
| 245 | + remove: $PropertyType<Response, 'remove'>, |
| 246 | + vary: $PropertyType<Response, 'vary'>, |
| 247 | + set: $PropertyType<Response, 'set'>, |
| 248 | + append: $PropertyType<Response, 'append'>, |
| 249 | + flushHeaders: $PropertyType<Response, 'flushHeaders'>, |
| 250 | + status: $PropertyType<Response, 'status'>, |
| 251 | + message: $PropertyType<Response, 'message'>, |
| 252 | + body: $PropertyType<Response, 'body'>, |
| 253 | + length: $PropertyType<Response, 'length'>, |
| 254 | + type: $PropertyType<Response, 'type'>, |
| 255 | + lastModified: $PropertyType<Response, 'lastModified'>, |
| 256 | + etag: $PropertyType<Response, 'etag'>, |
| 257 | + headerSent: $PropertyType<Response, 'headerSent'>, |
| 258 | + writable: $PropertyType<Response, 'writable'>, |
| 259 | + |
| 260 | + // cherry pick from request |
| 261 | + acceptsLanguages: $PropertyType<Request, 'acceptsLanguages'>, |
| 262 | + acceptsEncodings: $PropertyType<Request, 'acceptsEncodings'>, |
| 263 | + acceptsCharsets: $PropertyType<Request, 'acceptsCharsets'>, |
| 264 | + accepts: $PropertyType<Request, 'accepts'>, |
| 265 | + get: $PropertyType<Request, 'get'>, |
| 266 | + is: $PropertyType<Request, 'is'>, |
| 267 | + querystring: $PropertyType<Request, 'querystring'>, |
| 268 | + idempotent: $PropertyType<Request, 'idempotent'>, |
| 269 | + socket: $PropertyType<Request, 'socket'>, |
| 270 | + search: $PropertyType<Request, 'search'>, |
| 271 | + method: $PropertyType<Request, 'method'>, |
| 272 | + query: $PropertyType<Request, 'query'>, |
| 273 | + path: $PropertyType<Request, 'path'>, |
| 274 | + url: $PropertyType<Request, 'url'>, |
| 275 | + origin: $PropertyType<Request, 'origin'>, |
| 276 | + href: $PropertyType<Request, 'href'>, |
| 277 | + subdomains: $PropertyType<Request, 'subdomains'>, |
| 278 | + protocol: $PropertyType<Request, 'protocol'>, |
| 279 | + host: $PropertyType<Request, 'host'>, |
| 280 | + hostname: $PropertyType<Request, 'hostname'>, |
| 281 | + header: $PropertyType<Request, 'header'>, |
| 282 | + headers: $PropertyType<Request, 'headers'>, |
| 283 | + secure: $PropertyType<Request, 'secure'>, |
| 284 | + stale: $PropertyType<Request, 'stale'>, |
| 285 | + fresh: $PropertyType<Request, 'fresh'>, |
| 286 | + ips: $PropertyType<Request, 'ips'>, |
| 287 | + ip: $PropertyType<Request, 'ip'>, |
| 288 | + |
| 289 | + [key: string]: any, // props added by middlewares. |
| 290 | + } |
| 291 | + |
| 292 | + declare type Middleware = |
| 293 | + (ctx: Context, next: () => Promise<void>) => Promise<void>|void; |
| 294 | + declare type ApplicationJSON = { |
| 295 | + 'subdomainOffset': mixed, |
| 296 | + 'proxy': mixed, |
| 297 | + 'env': string, |
| 298 | + }; |
| 299 | + declare class Application extends events$EventEmitter { |
| 300 | + context: Context, |
| 301 | + // request handler for node's native http server. |
| 302 | + callback: () => (req: http$IncomingMessage, res: http$ServerResponse) => void, |
| 303 | + env: string, |
| 304 | + keys?: Array<string>|Object, // https://github.com/crypto-utils/keygrip |
| 305 | + middleware: Array<Middleware>, |
| 306 | + proxy: boolean, // when true proxy header fields will be trusted |
| 307 | + request: Request, |
| 308 | + response: Response, |
| 309 | + server: Server, |
| 310 | + subdomainOffset: number, |
| 311 | + |
| 312 | + listen: $PropertyType<Server, 'listen'>, |
| 313 | + toJSON(): ApplicationJSON, |
| 314 | + inspect(): ApplicationJSON, |
| 315 | + use(fn: Middleware): this, |
| 316 | + } |
| 317 | + |
| 318 | + declare module.exports: Class<Application>; |
| 319 | +} |
0 commit comments