-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipart.d.ts
185 lines (183 loc) · 6.75 KB
/
multipart.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/// <reference types="node" />
import { ReadStream } from "fs";
import { Readable } from "stream";
/**
* Simple and lite of `multipart/form-data` implementation, most similar to `form-data`.
*
* @example
* // buffer style(Synchronous)
* (new Multipart())
* .append('a', 1)
* .append('b', '2')
* .append('c', Buffer.from('31'))
* .append('d', JSON.stringify({}), 'any.json')
* .append('e', require('fs').readFileSync('/path/your/file.jpg'), 'file.jpg')
* .getBuffer();
* // stream style(Asynchronous)
* .pipe(require('fs').createWriteStream('./file3.jpg'));
*/
export class Multipart extends Readable {
/**
* @protected
* @memberof Multipart#
* @prop {object<string,string>} mimeTypes - Built-in mime-type mapping
*/
protected mimeTypes: object;
/**
* @readonly
* @memberof Multipart#
* @prop {Buffer} boundary - The boundary buffer.
*/
readonly boundary: Buffer;
/**
* @protected
* @memberof Multipart#
* @prop {Array<Buffer|ReadStream>} data - The Multipart's instance data storage
*/
protected data: Array<Buffer|ReadStream>;
/**
* @protected
* @memberof Multipart#
* @prop {[string|undefined, number][]} indices - The entities' value indices whose were in {@link Multipart#data}
*/
protected indices: [string|undefined, number][];
/**
* To retrieve the {@link Miltipart#data} buffer
*
* @returns {Buffer} - The payload buffer
*/
getBuffer(): Buffer;
/**
* To retrieve the `Content-Type` multipart/form-data header
*
* @returns {object<string, string>} - The `Content-Type` header With {@link Multipart#boundary}
*/
getHeaders(): object;
/**
* Append a customized {@link Multipart#mimeType}
*
* @example
* .appendMimeTypes({p12: 'application/x-pkcs12'})
* .appendMimeTypes({txt: 'text/plain'})
*
* @param {object<string,string>} things - The mime-type
*
* @returns {Multipart} - The `Multipart` class instance self
*/
appendMimeTypes(things: object): this;
/**
* Append data wrapped by {@link Multipart#boundary}
*
* @param {string} field - The field
* @param {string|Buffer} value - The value
* @param {String} [filename] - Optional filename, when provided, then append the `Content-Type` after of the `Content-Disposition`
*
* @returns {Multipart} - The `Multipart` class instance self
*/
append(field: string, value: string | Buffer | ReadStream, filename?: string): this;
/**
* Formed a named value, a filename reported to the server, when a Buffer or FileStream is passed as the second parameter.
*
* @param {string} name - The field name
* @param {string|Buffer|ReadStream} value - The value
* @param {string} [filename] - Optional filename, when provided, then append the `Content-Type` after of the `Content-Disposition`
*
* @returns {Array<Buffer|ReadStream>} - The part of data
*/
formed(name: string, value: string | Buffer | ReadStream, filename?: string): Array<Buffer | ReadStream>;
/**
* To go through all key/value pairs contained in this {@link Multipart#data} instance
*
* @return {Iterator<[string|undefined, Buffer|ReadStream]>} - An Array Iterator key/value pairs.
*/
entries(): Iterator<[string | undefined, Buffer | ReadStream]>;
/**
* Sets a new value for an existing key inside a {@link Multipart#data} instance, or adds the key/value if it does not already exist.
*
* @param {string} name - The field name
* @param {string|Buffer|ReadStream} value - The value
* @param {string} [filename] - Optional filename, when provided, then append the `Content-Type` after of the `Content-Disposition`
*
* @returns {this} - The Multipart instance
*/
set(name: string, value: string | Buffer | ReadStream, filename?: string): this;
/**
* Returns the first value associated with a given key from within a {@link Multipart#data} instance
*
* @param {string} name - The field name
*
* @return {Buffer|ReadStream|undefined} value - The value, undefined means none named key exists
*/
get(name: string): Buffer | ReadStream | undefined;
/**
* Returns all values associated with a given key from within a {@link Multipart#data} instance
*
* @param {string} name - The field name
*
* @return {(Buffer|ReadStream)[]} value(s) - The value(s)
*/
getAll(name: string): (Buffer | ReadStream)[];
/**
* Returns a boolean stating whether a {@link Multipart#data} instance contains a certain key.
*
* @param {string} name - The field name
*
* @return {boolean} - True for contains
*/
has(name: string): boolean;
/**
* Deletes a key and its value(s) from a {@link Multipart#data} instance
*
* @param {string} name - The field name
*
* @returns {this} - The Multipart instance
*/
delete(name: string): this;
/**
* To go through all keys contained in {@link Multipart#data} instance
*
* @return {Iterator<string|undefined>} - An Array Iterator key pairs.
*/
keys(): Iterator<string | undefined>;
/**
* To go through all values contained in {@link Multipart#data} instance
*
* @returns {Iterator<Array<Buffer|ReadStream>>} - An Array Iterator value pairs.
*/
values(): Iterator<Array<Buffer | ReadStream>>;
/**
* @returns {string} - FormData string
*/
static get [Symbol.toStringTag](): "FormData";
/**
* @returns {string} - FormData string
*/
get [Symbol.toStringTag](): "FormData";
/**
* @returns {string} - FormData string
*/
toString(): "[object FormData]";
/**
* alias of {@link Multipart#entries}
* @returns {Iterator<[string|undefined, Buffer|ReadStream]>} - An Array Iterator key/value pairs.
*/
[Symbol.iterator](): Iterator<[string|undefined, Buffer|ReadStream]>;
_read(): void;
/**
* Pushing {@link Multipart#data} into the readable BufferList
*
* @param {boolean} [end = true] - End the writer when the reader ends. Default: true.
* @returns {Promise<this>} - The Multipart instance
*/
flowing(end?: boolean): Promise<this>;
/**
* Attaches a Writable stream to the {@link Multipart} instance
*
* @param {NodeJS.WritableStream} destination - The destination for writing data
* @param {object} [options] - Pipe options
* @param {boolean} [options.end = true] - End the writer when the reader ends. Default: true.
* @returns {stream.Writable} - The destination, allowing for a chain of pipes
*/
pipe<T extends NodeJS.WritableStream>(destination: T, options?: {end?: boolean}): T;
}
export default Multipart;