forked from fastify/fastify-multipart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
134 lines (109 loc) · 3.26 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as busboy from "busboy";
import { FastifyPlugin } from "fastify";
import { Readable } from 'stream';
import { FastifyError } from "fastify-error";
type MultipartHandler = (
field: string,
file: any,
filename: string,
encoding: string,
mimetype: string,
) => void;
interface BodyEntry {
data: Buffer,
filename: string,
encoding: string,
mimetype: string,
limit: false
}
export interface MultipartFields {
[x: string]: Multipart | Multipart[];
}
export type Multipart<T = true> = T extends true ? MultipartFile : MultipartValue<T>;
interface MultipartFile {
toBuffer: () => Promise<Buffer>,
file: NodeJS.ReadableStream,
filepath: string,
fieldname: string,
filename: string,
encoding: string,
mimetype: string,
fields: MultipartFields
}
interface MultipartValue<T> {
value: T
}
interface MultipartErrors {
PartsLimitError: FastifyError,
FilesLimitError: FastifyError,
FieldsLimitError: FastifyError,
PrototypeViolationError: FastifyError,
InvalidMultipartContentTypeError: FastifyError,
RequestFileTooLargeError: FastifyError
}
declare module "fastify" {
interface FastifyRequest {
isMultipart: () => boolean;
parts: (options?: busboy.BusboyConfig) => AsyncIterableIterator<Multipart>
// legacy
multipart: (handler: MultipartHandler, next: (err: Error) => void, options?: busboy.BusboyConfig) => busboy.Busboy;
// promise api
multipartIterator: (options?: busboy.BusboyConfig) => AsyncIterableIterator<Multipart>
// Stream mode
file: (options?: busboy.BusboyConfig) => Promise<Multipart>
files: (options?: busboy.BusboyConfig) => AsyncIterableIterator<Multipart>
// Disk mode
saveRequestFiles: (options?: busboy.BusboyConfig) => Promise<Array<Multipart>>
cleanRequestFiles: () => Promise<void>
tmpUploads: Array<Multipart>
}
interface FastifyInstance {
multipartErrors: MultipartErrors
}
}
export interface FastifyMultipartOptions {
/**
* Append the multipart parameters to the body object
*/
addToBody?: boolean;
/**
* Only valid in the promise api. Append the multipart parameters to the body object.
*/
attachFieldsToBody?: boolean
/**
* Add a shared schema to validate the input fields
*/
sharedSchemaId?: string;
/**
* Manage the file stream like you need
*/
onFile?: (fieldName: string, stream: Readable, filename: string, encoding: string, mimetype: string, body: Record<string, BodyEntry>) => void | Promise<void>;
limits?: {
/**
* Max field name size in bytes
*/
fieldNameSize?: number;
/**
* Max field value size in bytes
*/
fieldSize?: number;
/**
* Max number of non-file fields
*/
fields?: number;
/**
* For multipart forms, the max file size
*/
fileSize?: number;
/**
* Max number of file fields
*/
files?: number;
/**
* Max number of header key=>value pairs
*/
headerPairs?: number;
}
}
declare const fastifyMultipart: FastifyPlugin<FastifyMultipartOptions>;
export default fastifyMultipart;