UNPKG

5.91 kBTypeScriptView Raw
1/// <reference types="node" />
2declare module '@ioc:Adonis/Core/BodyParser' {
3 import { Readable } from 'stream';
4 import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
5 /**
6 * Qs module config
7 */
8 type QueryStringConfig = {
9 depth?: number;
10 allowPrototypes?: boolean;
11 plainObjects?: boolean;
12 parameterLimit?: number;
13 arrayLimit?: number;
14 ignoreQueryPrefix?: boolean;
15 delimiter?: RegExp | string;
16 allowDots?: boolean;
17 charset?: string;
18 charsetSentinel?: boolean;
19 interpretNumericEntities?: boolean;
20 parseArrays?: boolean;
21 comma?: boolean;
22 };
23 /**
24 * Base config used by all types
25 */
26 type BodyParserBaseConfig = {
27 encoding: string;
28 limit: string | number;
29 types: string[];
30 };
31 /**
32 * Body parser config for parsing JSON requests
33 */
34 export type BodyParserJSONConfig = BodyParserBaseConfig & {
35 strict: boolean;
36 };
37 /**
38 * Parser config for parsing form data
39 */
40 export type BodyParserFormConfig = BodyParserBaseConfig & {
41 queryString: QueryStringConfig;
42 convertEmptyStringsToNull: boolean;
43 };
44 /**
45 * Parser config for parsing raw body (untouched)
46 */
47 export type BodyParserRawConfig = BodyParserBaseConfig & {
48 queryString: QueryStringConfig;
49 };
50 /**
51 * Parser config for parsing multipart requests
52 */
53 export type BodyParserMultipartConfig = BodyParserBaseConfig & {
54 autoProcess: boolean;
55 maxFields: number;
56 processManually: string[];
57 convertEmptyStringsToNull: boolean;
58 tmpFileName?(): string;
59 };
60 /**
61 * Body parser config for all different types
62 */
63 export type BodyParserConfig = {
64 whitelistedMethods: string[];
65 json: BodyParserJSONConfig;
66 form: BodyParserFormConfig;
67 raw: BodyParserRawConfig;
68 multipart: BodyParserMultipartConfig;
69 };
70 /**
71 * ------------------------------------
72 * Multipart related options
73 * ------------------------------------
74 */
75 /**
76 * Readable stream along with some extra data. This is what
77 * is passed to `onFile` handlers.
78 */
79 export type MultipartStream = Readable & {
80 headers: {
81 [key: string]: string;
82 };
83 name: string;
84 filename: string;
85 bytes: number;
86 file: MultipartFileContract;
87 };
88 /**
89 * The callback handler for a given file part
90 */
91 export type PartHandler = (part: MultipartStream, reportChunk: (chunk: Buffer) => void) => Promise<({
92 filePath?: string;
93 tmpPath?: string;
94 } & {
95 [key: string]: any;
96 }) | void>;
97 /**
98 * Multipart class contract, since it is exposed on the request object,
99 * we need the interface to extend typings.
100 */
101 export interface MultipartContract {
102 state: 'idle' | 'processing' | 'error' | 'success';
103 abort(error: any): void;
104 onFile(name: string, options: Partial<FileValidationOptions & {
105 deferValidations: boolean;
106 }>, callback: PartHandler): this;
107 process(config?: Partial<{
108 limit: string | number;
109 maxFields: number;
110 }>): Promise<void>;
111 }
112 /**
113 * ------------------------------------
114 * Multipart file related options
115 * ------------------------------------
116 */
117 /**
118 * The options that can be used to validate a given
119 * file
120 */
121 export type FileValidationOptions = {
122 size: string | number;
123 extnames: string[];
124 };
125 /**
126 * Error shape for file upload errors
127 */
128 export type FileUploadError = {
129 fieldName: string;
130 clientName: string;
131 message: string;
132 type: 'size' | 'extname' | 'fatal';
133 };
134 /**
135 * Shape of file.toJSON return value
136 */
137 export type FileJSON = {
138 fieldName: string;
139 clientName: string;
140 size: number;
141 filePath?: string;
142 type?: string;
143 extname?: string;
144 subtype?: string;
145 state: 'idle' | 'streaming' | 'consumed' | 'moved';
146 isValid: boolean;
147 validated: boolean;
148 errors: FileUploadError[];
149 meta: any;
150 };
151 /**
152 * Multipart file interface
153 */
154 export interface MultipartFileContract {
155 isMultipartFile: true;
156 fieldName: string;
157 clientName: string;
158 size: number;
159 headers: {
160 [key: string]: string;
161 };
162 tmpPath?: string;
163 filePath?: string;
164 fileName?: string;
165 type?: string;
166 extname?: string;
167 subtype?: string;
168 state: 'idle' | 'streaming' | 'consumed' | 'moved';
169 isValid: boolean;
170 hasErrors: boolean;
171 validated: boolean;
172 errors: FileUploadError[];
173 sizeLimit?: number | string;
174 allowedExtensions?: string[];
175 meta: any;
176 /**
177 * Run validations on the file
178 */
179 validate(): void;
180 /**
181 * Move file from temporary path to a different location. Self consumed
182 * streams cannot be moved unless `tmpPath` is defined explicitly.
183 */
184 move(location: string, options?: {
185 name?: string;
186 overwrite?: boolean;
187 }): Promise<void>;
188 /**
189 * Get JSON representation of file
190 */
191 toJSON(): FileJSON;
192 }
193 /**
194 * Shape of the bodyparser middleware class constructor
195 */
196 export interface BodyParserMiddlewareContract {
197 new (config: BodyParserConfig): {
198 handle(ctx: HttpContextContract, next: () => void): any;
199 };
200 }
201 const BodyParserMiddleware: BodyParserMiddlewareContract;
202 export default BodyParserMiddleware;
203}