UNPKG

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