[request] Specify types allowed for Multipart body (#38190)

The request library allows anything to be put in a Multipart body as
long as it can be passed to `Buffer.from()`: https://github.com/request/request/blob/master/lib/multipart.js#L74

However the current implementation allows only `string` to be used as
the multipart body. In my use-case I needed to pass a `Buffer` instance
directly.

This change defines a type alias for `MultipartBody` that includes
`string`, `Buffer`, `ArrayBuffer` or `UInt8Array`.
This commit is contained in:
Eitan Levi
2019-09-08 17:24:44 -07:00
committed by Mine Starks
parent 3803cbd988
commit 4fe80e3fcc
2 changed files with 41 additions and 1 deletions

View File

@@ -183,6 +183,8 @@ declare namespace request {
type OptionsWithUrl = UrlOptions & CoreOptions;
type Options = OptionsWithUri | OptionsWithUrl;
type MultipartBody = string | Buffer | ArrayBuffer | Uint8Array;
type RequestCallback = (error: any, response: Response, body: any) => void;
interface HttpArchiveRequest {
@@ -204,7 +206,7 @@ declare namespace request {
chunked?: boolean;
data?: Array<{
'content-type'?: string,
body: string
body: MultipartBody
}>;
}

View File

@@ -649,6 +649,44 @@ request(
}
);
request(
{
method: 'PUT',
uri: 'http://mikeal.iriscouch.com/testjs/' + rand,
multipart: {
data: [
{
'content-type': 'application/json; charset=utf-8',
body: JSON.stringify({
_attachments: {
'dot.png': {
follows: true,
length: 269,
content_type: 'image/png',
},
},
}),
},
{
'content-type': 'image/png',
body: Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAA3NCSVQICAjb4U/gAAAAX3pUWHRSYXcgcHJvZmlsZSB0eXBlIEFQUDEAAAiZ40pPzUstykxWKCjKT8vMSeVSAANjEy4TSxNL' +
'o0QDAwMLAwgwNDAwNgSSRkC2OVQo0QAFmJibpQGhuVmymSmIzwUAT7oVaBst2IwAAAAWSURBVAiZY/z//z8DAwMTAwMDAwMDACQGAwGaMKL7AAAAAElFTkSuQmCC',
),
},
],
},
},
(error, response, body) => {
if (response.statusCode === 201) {
console.log('image saved as http://mikeal.iriscouch.com/testjs/' + rand);
} else {
console.log('error: ' + response.statusCode);
console.log(body);
}
},
);
request(
{ method: 'GET'
, uri: 'http://www.google.com'