1 | import * as Koa from "koa";
|
2 |
|
3 | declare module "koa" {
|
4 | interface Request {
|
5 | body: any;
|
6 | }
|
7 | interface Context {
|
8 | body: any;
|
9 | }
|
10 | }
|
11 |
|
12 | declare namespace koaBody {
|
13 | interface IKoaBodyFormidableOptions {
|
14 | /**
|
15 | * {Integer} The expected number of bytes in this form, default null
|
16 | */
|
17 | bytesExpected?: number
|
18 |
|
19 | /**
|
20 | * {Integer} Limits the number of fields that the querystring parser will decode, default 1000
|
21 | */
|
22 | maxFields?: number;
|
23 |
|
24 | /**
|
25 | * {Integer} Limits the amount of memory all fields together (except files) can allocate in bytes.
|
26 | * If this value is exceeded, an 'error' event is emitted, default 2mb (2 * 1024 * 1024)
|
27 | */
|
28 | maxFieldsSize?: number;
|
29 |
|
30 | /**
|
31 | * {String} Sets the directory for placing file uploads in, default os.tmpDir()
|
32 | */
|
33 | uploadDir?: string;
|
34 |
|
35 | /**
|
36 | * {Boolean} Files written to uploadDir will include the extensions of the original files, default false
|
37 | */
|
38 | keepExtensions?: boolean;
|
39 |
|
40 | /**
|
41 | * {String} If you want checksums calculated for incoming files, set this to either 'sha1' or 'md5', default false
|
42 | */
|
43 | hash?: string;
|
44 |
|
45 | /**
|
46 | * {Boolean} Multiple file uploads or no, default true
|
47 | */
|
48 | multiples?: boolean;
|
49 | /**
|
50 | * {Function} Special callback on file begin. The function is executed directly by formidable.
|
51 | * It can be used to rename files before saving them to disk. See https://github.com/felixge/node-formidable#filebegin
|
52 | */
|
53 | onFileBegin?: (name: string, file: any) => void;
|
54 | }
|
55 | interface IKoaBodyOptions {
|
56 | /**
|
57 | * {Boolean} Patch request body to Node's ctx.req, default false
|
58 | *
|
59 | * Note: You can patch request body to Node or Koa in same time if you want.
|
60 | */
|
61 | patchNode?: boolean;
|
62 |
|
63 | /**
|
64 | * {Boolean} Patch request body to Koa's ctx.request, default true
|
65 | *
|
66 | * Note: You can patch request body to Node or Koa in same time if you want.
|
67 | */
|
68 | patchKoa?: boolean;
|
69 |
|
70 | /**
|
71 | * {String|Integer} The byte (if integer) limit of the JSON body, default 1mb
|
72 | */
|
73 | jsonLimit?: string|number;
|
74 |
|
75 | /**
|
76 | * {String|Integer} The byte (if integer) limit of the form body, default 56kb
|
77 | */
|
78 | formLimit?: string|number;
|
79 |
|
80 | /**
|
81 | * {String|Integer} The byte (if integer) limit of the text body, default 56kb
|
82 | */
|
83 | textLimit?: string|number;
|
84 |
|
85 | /**
|
86 | * {String} Sets encoding for incoming form fields, default utf-8
|
87 | */
|
88 | encoding?: string;
|
89 |
|
90 | /**
|
91 | * {Boolean} Parse multipart bodies, default false
|
92 | */
|
93 | multipart?: boolean;
|
94 |
|
95 | /**
|
96 | * {Boolean} Parse urlencoded bodies, default true
|
97 | */
|
98 | urlencoded?: boolean;
|
99 |
|
100 | /**
|
101 | * {Boolean} Parse text bodies, default true
|
102 | */
|
103 | text?: boolean;
|
104 |
|
105 | /**
|
106 | * {Boolean} Parse json bodies, default true
|
107 | */
|
108 | json?: boolean;
|
109 |
|
110 | /**
|
111 | * {Object} Options to pass to the formidable multipart parser
|
112 | */
|
113 | formidable?: IKoaBodyFormidableOptions;
|
114 |
|
115 | /**
|
116 | * {Function} Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw
|
117 | */
|
118 | onError?: (err: Error, ctx: Koa.Context) => void;
|
119 |
|
120 | /**
|
121 | * {Boolean} If enabled, don't parse GET, HEAD, DELETE requests, default true
|
122 | *
|
123 | * GET, HEAD, and DELETE requests have no defined semantics for the request body,
|
124 | * but this doesn't mean they may not be valid in certain use cases.
|
125 | * koa-body is strict by default
|
126 | *
|
127 | * see http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.3
|
128 | */
|
129 | strict?: boolean;
|
130 | }
|
131 | }
|
132 |
|
133 | declare function koaBody (options?: koaBody.IKoaBodyOptions): Koa.Middleware;
|
134 |
|
135 | export = koaBody;
|