From 33a325efc8640ead8084cbeb38256b3f6a0d5bfa Mon Sep 17 00:00:00 2001 From: Alex Brick Date: Mon, 2 Oct 2017 19:38:38 +0200 Subject: [PATCH] [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. --- types/superagent/index.d.ts | 9 +++++++-- types/superagent/superagent-tests.ts | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/types/superagent/index.d.ts b/types/superagent/index.d.ts index 05e1275ca0..fdf23891cb 100644 --- a/types/superagent/index.d.ts +++ b/types/superagent/index.d.ts @@ -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 { 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; diff --git a/types/superagent/superagent-tests.ts b/types/superagent/superagent-tests.ts index 6d7eeb793d..5978b37206 100644 --- a/types/superagent/superagent-tests.ts +++ b/types/superagent/superagent-tests.ts @@ -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);