UNPKG

3.7 kBTypeScriptView Raw
1/// <reference types="node" resolution-mode="require"/>
2/// <reference types="node" resolution-mode="require"/>
3import { Readable } from 'node:stream';
4import type { MultipartFile } from './multipart/file.js';
5/**
6 * Qs module config
7 */
8type 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?: 'utf-8' | 'iso-8859-1' | undefined;
18 charsetSentinel?: boolean;
19 interpretNumericEntities?: boolean;
20 parseArrays?: boolean;
21 comma?: boolean;
22};
23/**
24 * Base config used by all types
25 */
26type BodyParserBaseConfig = {
27 encoding: string;
28 limit: string | number;
29 types: string[];
30};
31/**
32 * Body parser config for parsing JSON requests
33 */
34export type BodyParserJSONConfig = BodyParserBaseConfig & {
35 strict: boolean;
36 convertEmptyStringsToNull: boolean;
37};
38/**
39 * Parser config for parsing form data
40 */
41export type BodyParserFormConfig = BodyParserBaseConfig & {
42 queryString: QueryStringConfig;
43 convertEmptyStringsToNull: boolean;
44};
45/**
46 * Parser config for parsing raw body (untouched)
47 */
48export type BodyParserRawConfig = BodyParserBaseConfig;
49/**
50 * Parser config for parsing multipart requests
51 */
52export type BodyParserMultipartConfig = BodyParserBaseConfig & {
53 autoProcess: boolean | string[];
54 maxFields: number;
55 processManually: string[];
56 convertEmptyStringsToNull: boolean;
57 fieldsLimit?: number | string;
58 tmpFileName?(): string;
59};
60/**
61 * Body parser config for all supported form types
62 */
63export type BodyParserConfig = {
64 allowedMethods: string[];
65 json: BodyParserJSONConfig;
66 form: BodyParserFormConfig;
67 raw: BodyParserRawConfig;
68 multipart: BodyParserMultipartConfig;
69};
70/**
71 * Body parser config with partial config
72 */
73export type BodyParserOptionalConfig = {
74 allowedMethods?: string[];
75 json?: Partial<BodyParserJSONConfig>;
76 form?: Partial<BodyParserFormConfig>;
77 raw?: Partial<BodyParserRawConfig>;
78 multipart?: Partial<BodyParserMultipartConfig>;
79};
80/**
81 * ------------------------------------
82 * Multipart related options
83 * ------------------------------------
84 */
85/**
86 * Readable stream along with some extra data. This is what
87 * is passed to `onFile` handlers.
88 */
89export type MultipartStream = Readable & {
90 headers: {
91 [key: string]: string;
92 };
93 name: string;
94 filename: string;
95 bytes: number;
96 file: MultipartFile;
97};
98/**
99 * The callback handler for a given file part
100 */
101export type PartHandler = (part: MultipartStream, reportChunk: (chunk: Buffer) => void) => Promise<({
102 filePath?: string;
103 tmpPath?: string;
104} & {
105 [key: string]: any;
106}) | void>;
107/**
108 * ------------------------------------
109 * Multipart file related options
110 * ------------------------------------
111 */
112/**
113 * The options that can be used to validate a given
114 * file
115 */
116export type FileValidationOptions = {
117 size: string | number;
118 extnames: string[];
119};
120/**
121 * Error shape for file upload errors
122 */
123export type FileUploadError = {
124 fieldName: string;
125 clientName: string;
126 message: string;
127 type: 'size' | 'extname' | 'fatal';
128};
129/**
130 * Shape of file.toJSON return value
131 */
132export type FileJSON = {
133 fieldName: string;
134 clientName: string;
135 size: number;
136 filePath?: string;
137 fileName?: string;
138 type?: string;
139 extname?: string;
140 subtype?: string;
141 state: 'idle' | 'streaming' | 'consumed' | 'moved';
142 isValid: boolean;
143 validated: boolean;
144 errors: FileUploadError[];
145 meta: any;
146};
147export { MultipartFile };