UNPKG

2.32 kBTypeScriptView Raw
1// Type definitions for koa-bodyparser 4.3
2// Project: https://github.com/koajs/body-parser
3// Definitions by: Jerry Chin <https://github.com/hellopao>
4// Anup Kishore <https://github.com/anup-2s>
5// Hiroshi Ioka <https://github.com/hirochachacha>
6// Alexi Maschas <https://github.com/amaschas>
7// Piotr Kuczynski <https://github.com/pkuczynski>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 3.0
10
11/* =================== USAGE ===================
12
13 import bodyParser = require("koa-bodyparser");
14 var Koa = require('koa');
15
16 var app = new Koa();
17 app.use(bodyParser());
18
19 =============================================== */
20
21import * as Koa from 'koa';
22
23declare module 'koa' {
24 interface Request {
25 body: string | Record<string, unknown>;
26 rawBody: string;
27 }
28}
29
30declare function bodyParser(opts?: bodyParser.Options): Koa.Middleware;
31
32declare namespace bodyParser {
33 interface Options {
34 /**
35 * parser will only parse when request type hits enableTypes, default is ['json', 'form'].
36 */
37 enableTypes?: string[];
38
39 /**
40 * requested encoding. Default is utf-8 by co-body
41 */
42 encode?: string;
43
44 /**
45 * limit of the urlencoded body. If the body ends up being larger than this limit
46 * a 413 error code is returned. Default is 56kb
47 */
48 formLimit?: string;
49
50 /**
51 * limit of the json body. Default is 1mb
52 */
53 jsonLimit?: string;
54
55 /**
56 * limit of the text body. Default is 1mb.
57 */
58 textLimit?: string;
59
60 /**
61 * when set to true, JSON parser will only accept arrays and objects. Default is true
62 */
63 strict?: boolean;
64
65 /**
66 * custom json request detect function. Default is null
67 */
68 detectJSON?: (ctx: Koa.Context) => boolean;
69
70 /**
71 * support extend types
72 */
73 extendTypes?: {
74 json?: string[];
75 form?: string[];
76 text?: string[];
77 };
78
79 /**
80 * support custom error handle
81 */
82 onerror?: (err: Error, ctx: Koa.Context) => void;
83 }
84}
85
86export = bodyParser;