UNPKG

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