[superagent] Improving the typing of the #field method (#20102)

There are two major changes here:

1) The value of a field no longer needs to be a string. It also
accepts arrays, buffers, blobs, streams, and booleans.

2) You may pass in an object, which sets all of the field values,
instead of calling once for each field.
This commit is contained in:
Alex Brick
2017-10-02 10:38:38 -07:00
committed by Ryan Cavanaugh
parent 44443f8e49
commit 33a325efc8
2 changed files with 14 additions and 2 deletions
+7 -2
View File
@@ -16,6 +16,10 @@ type CallbackHandler = (err: any, res: request.Response) => void;
type Serializer = (obj: any) => string;
type MultipartValueSingle = Blob | Buffer | fs.ReadStream | string | boolean | number;
type MultipartValue = MultipartValueSingle | MultipartValueSingle[];
declare const request: request.SuperAgentStatic;
declare namespace request {
@@ -103,14 +107,15 @@ declare namespace request {
interface Request extends Promise<Response> {
abort(): void;
accept(type: string): this;
attach(field: string, file: Blob | Buffer | fs.ReadStream | string, filename?: string): this;
attach(field: string, file: MultipartValueSingle, options?: string | { filename?: string; contentType?: string }): this;
auth(user: string, name: string): this;
buffer(val?: boolean): this;
ca(cert: Buffer): this;
cert(cert: Buffer | string): this;
clearTimeout(): this;
end(callback?: CallbackHandler): this;
field(name: string, val: string): this;
field(name: string, val: MultipartValue): this;
field(fields: { [fieldName: string]: MultipartValue }): this;
get(field: string): string;
key(cert: Buffer | string): this;
ok(callback: (res: Response) => boolean): this;
+7
View File
@@ -285,6 +285,7 @@ request
.attach('avatar', 'path/to/tobi.png', 'user.png')
.attach('image', 'path/to/loki.png')
.attach('file', 'path/to/jane.png')
.attach('fileWithOptions', 'path/to/file.png', { filename: 'filename', contentType: 'contentType' })
.attach('blob', blob)
.end(callback);
@@ -293,6 +294,12 @@ request
.post('/upload')
.field('user[name]', 'Tobi')
.field('user[email]', 'tobi@learnboost.com')
.field({
field1: 'value1',
field2: Buffer.from([ 10, 20 ]),
field3: [ 'value1', 'value2' ],
field4: true,
})
.attach('image', 'path/to/tobi.png')
.end(callback);