diff --git a/superagent/superagent-tests.ts b/superagent/superagent-tests.ts index 58b6840440..cc270a8ca4 100644 --- a/superagent/superagent-tests.ts +++ b/superagent/superagent-tests.ts @@ -193,6 +193,24 @@ request('/search') var charset: string = res.charset; }); +// Custom parsers +request + .post('/search') + .parse((res, callback) => { + res.setEncoding("binary"); + let data = ""; + res.on("data", (chunk: string) => { + data += chunk; + }); + + res.on("end", () => { + callback(null, new Buffer(data, "base64")); + }); + }) + .end((res: request.Response) => { + res.body.toString("hex"); + }); + var req = request.get('/hoge'); // Aborting requests req.abort(); diff --git a/superagent/superagent.d.ts b/superagent/superagent.d.ts index d16b13584f..9d9f3a8f15 100644 --- a/superagent/superagent.d.ts +++ b/superagent/superagent.d.ts @@ -50,7 +50,7 @@ declare module "superagent" { search(url: string, callback?: CallbackHandler): Req; connect(url: string, callback?: CallbackHandler): Req; - parse(fn: Function): Req; + parse(fn: (res: Response, callback: (err: Error, body: any) => void) => void): this; saveCookies(res: Response): void; attachCookies(req: Req): void; } @@ -107,6 +107,7 @@ declare module "superagent" { withCredentials(): this; write(data: string, encoding?: string): this; write(data: Buffer, encoding?: string): this; + parse(fn: (res: Response, callback: (err: Error, body: any) => void) => void): this; } }