diff --git a/types/superagent/index.d.ts b/types/superagent/index.d.ts index fe6a797675..b53feed20d 100644 --- a/types/superagent/index.d.ts +++ b/types/superagent/index.d.ts @@ -21,7 +21,7 @@ declare namespace request { method: string; url: string; cookies: string; - } + } interface SuperAgentStatic extends SuperAgent { (url: string): SuperAgentRequest; (method: string, url: string): SuperAgentRequest; @@ -58,9 +58,9 @@ declare namespace request { search(url: string, callback?: CallbackHandler): Req; connect(url: string, callback?: CallbackHandler): Req; - parse(fn: (res: Response, callback: (err: Error | null, body: any) => void) => void): this; - saveCookies(res: Response): void; - attachCookies(req: Req): void; + parse(fn: (res: Response, callback: (err: Error | null, body: any) => void) => void): this; + saveCookies(res: Response): void; + attachCookies(req: Req): void; } interface Response extends NodeJS.ReadableStream { @@ -90,35 +90,35 @@ declare namespace request { } interface Request extends Promise /* extends NodeJS.WritableStream */ { - abort(): void; - accept(type: string): this; - attach(field: string, file: string, filename?: string): this; - auth(user: string, name: string): this; - buffer(val?: boolean): this; - clearTimeout(): this; - end(callback?: CallbackHandler): this; - field(name: string, val: string): this; - get(field: string): string; - on(name: string, handler: Function): this; - on(name: 'error', handler: (err: any) => void): this; - part(): this; - pipe(stream: NodeJS.WritableStream, options?: Object): stream.Writable; - query(val: Object): this; - redirects(n: number): this; - responseType(type: string): this; - send(data: string): this; - send(data: Object): this; - send(): this; - set(field: string, val: string): this; - set(field: Object): this; - timeout(ms: number): this; - type(val: string): this; - unset(field: string): this; - use(fn: Function): this; - withCredentials(): this; - write(data: string, encoding?: string): this; - write(data: Buffer, encoding?: string): this; - parse(fn: (res: Response, callback: (err: Error | null, body: any) => void) => void): this; + abort(): void; + accept(type: string): this; + attach(field: string, file: string, filename?: string): this; + auth(user: string, name: string): this; + buffer(val?: boolean): this; + clearTimeout(): this; + end(callback?: CallbackHandler): this; + field(name: string, val: string): this; + get(field: string): string; + on(name: string, handler: Function): this; + on(name: 'error', handler: (err: any) => void): this; + part(): this; + pipe(stream: NodeJS.WritableStream, options?: Object): stream.Writable; + query(val: Object): this; + redirects(n: number): this; + responseType(type: string): this; + send(data: string): this; + send(data: Object): this; + send(): this; + set(field: string, val: string): this; + set(field: Object): this; + timeout(ms: number): this; + type(val: string): this; + unset(field: string): this; + use(fn: Function): this; + withCredentials(): this; + write(data: string, encoding?: string): this; + write(data: Buffer, encoding?: string): this; + parse(fn: (res: Response, callback: (err: Error | null, body: any) => void) => void): this; } } diff --git a/types/superagent/superagent-tests.ts b/types/superagent/superagent-tests.ts index da60314b23..cfac121640 100644 --- a/types/superagent/superagent-tests.ts +++ b/types/superagent/superagent-tests.ts @@ -1,9 +1,9 @@ // via: http://visionmedia.github.io/superagent/ -import * as request from 'superagent'; -import * as fs from 'fs'; -import * as assert from 'assert'; -import { Agent } from 'https'; +import * as request from 'superagent' +import * as fs from 'fs' +import * as assert from 'assert' +import {Agent} from 'https' // Examples taken from https://github.com/visionmedia/superagent/blob/gh-pages/docs/index.md @@ -12,207 +12,208 @@ import { Agent } from 'https'; const httpsAgent: Agent = new Agent(); request - .post('/api/pet') - .send({ name: 'Manny', species: 'cat' }) - .set('X-API-Key', 'foobar') - .set('Accept', 'application/json') - .agent(httpsAgent) - .end((err, res) => { - if (res.ok) { - console.log('yay got ' + JSON.stringify(res.body)); - } else { - console.log('Oh no! error ' + res.text); - } - }); + .post('/api/pet') + .send({name: 'Manny', species: 'cat'}) + .set('X-API-Key', 'foobar') + .set('Accept', 'application/json') + .agent(httpsAgent) + .end((err, res) => { + if (res.ok) { + console.log('yay got ' + JSON.stringify(res.body)); + } else { + console.log('Oh no! error ' + res.text); + } + }); var agent = request.agent(); agent - .post('/api/pet') - .send({ name: 'Manny', species: 'cat' }) - .set('X-API-Key', 'foobar') - .set('Accept', 'application/json') - .end((err, res) => { - if (res.error) { - console.log('oh no ' + res.error.message); - } else { - console.log('got ' + res.status + ' response'); - } - }); + .post('/api/pet') + .send({name: 'Manny', species: 'cat'}) + .set('X-API-Key', 'foobar') + .set('Accept', 'application/json') + .end((err, res) => { + if (res.error) { + console.log('oh no ' + res.error.message); + } else { + console.log('got ' + res.status + ' response'); + } + }); // Plugins var nocache = require('superagent-no-cache'); var prefix = require('superagent-prefix')('/static'); request - .get('/some-url') - .use(prefix) // Prefixes *only* this request - .use(nocache) // Prevents caching of *only* this request - .end(function(err, res){ - // Do something - }); + .get('/some-url') + .use(prefix) // Prefixes *only* this request + .use(nocache) // Prevents caching of *only* this request + .end(function (err, res) { + // Do something + }); -var callback = (err: any, res: request.Response) => {}; +var callback = (err: any, res: request.Response) => { +}; // Request basics request - .get('/search') - .end(callback); + .get('/search') + .end(callback); request('GET', '/search') - .end(callback); + .end(callback); request - .get('http://example.com/search') - .end(callback); + .get('http://example.com/search') + .end(callback); request - .head('/favicon.ico') - .end(callback); + .head('/favicon.ico') + .end(callback); request - .del('/user/1') - .end(callback); + .del('/user/1') + .end(callback); request - .delete('/user/1') - .end(callback); + .delete('/user/1') + .end(callback); request - .delete('/user/1') - .send() - .end(callback); + .delete('/user/1') + .send() + .end(callback); request('/search') - .end(callback); + .end(callback); // Setting header fields request - .get('/search') - .set('API-Key', 'foobar') - .set('Accept', 'application/json') - .end(callback); + .get('/search') + .set('API-Key', 'foobar') + .set('Accept', 'application/json') + .end(callback); request - .get('/search') - .set({ 'API-Key': 'foobar', Accept: 'application/json' }) - .end(callback); + .get('/search') + .set({'API-Key': 'foobar', Accept: 'application/json'}) + .end(callback); // GET requests request - .get('/search') - .query({ query: 'Manny' }) - .query({ range: '1..5' }) - .query({ order: 'desc' }) - .end(callback); + .get('/search') + .query({query: 'Manny'}) + .query({range: '1..5'}) + .query({order: 'desc'}) + .end(callback); request - .get('/search') - .query({ query: 'Manny', range: '1..5', order: 'desc' }) - .end(callback); + .get('/search') + .query({query: 'Manny', range: '1..5', order: 'desc'}) + .end(callback); request - .get('/querystring') - .query('search=Manny&range=1..5') - .end(callback); + .get('/querystring') + .query('search=Manny&range=1..5') + .end(callback); request - .get('/querystring') - .query('search=Manny') - .query('range=1..5') - .end(callback); + .get('/querystring') + .query('search=Manny') + .query('range=1..5') + .end(callback); // HEAD requests request - .head('/users') - .query({ email: 'joe@smith.com' }) - .end(callback); + .head('/users') + .query({email: 'joe@smith.com'}) + .end(callback); // POST / PUT requests request.post('/user') - .set('Content-Type', 'application/json') - .send('{"name":"tj","pet":"tobi"}') - .end(callback); + .set('Content-Type', 'application/json') + .send('{"name":"tj","pet":"tobi"}') + .end(callback); request.post('/user') - .send({ name: 'tj', pet: 'tobi' }) - .end(callback); + .send({name: 'tj', pet: 'tobi'}) + .end(callback); request.post('/user') - .send({ name: 'tj' }) - .send({ pet: 'tobi' }) - .end(callback); + .send({name: 'tj'}) + .send({pet: 'tobi'}) + .end(callback); request.post('/user') - .send('name=tj') - .send('pet=tobi') - .end(callback); + .send('name=tj') + .send('pet=tobi') + .end(callback); request.post('/user') - .type('form') - .send({ name: 'tj' }) - .send({ pet: 'tobi' }) - .end(callback); + .type('form') + .send({name: 'tj'}) + .send({pet: 'tobi'}) + .end(callback); // Setting the Content-Type request.post('/user') - .set('Content-Type', 'application/json'); + .set('Content-Type', 'application/json'); request.post('/user') - .type('application/json'); + .type('application/json'); request.post('/user') - .type('json'); + .type('json'); request.post('/user') - .type('png'); + .type('png'); // Setting Accept request.get('/user') - .accept('application/json'); + .accept('application/json'); request.get('/user') - .accept('json'); + .accept('json'); request.get('/user') - .accept('png'); + .accept('png'); // Query strings request - .post('/') - .query({ format: 'json' }) - .query({ dest: '/login' }) - .send({ post: 'data', here: 'wahoo' }) - .end(callback); + .post('/') + .query({format: 'json'}) + .query({dest: '/login'}) + .send({post: 'data', here: 'wahoo'}) + .end(callback); // Parsing response bodies request('/search') - .end((res: request.Response) => { - var status: number = res.status; - var body = res.body; - var files: Object = res.files; - var text: string = res.text; - var contentLength = res.header['content-length']; - var contentType: string = res.type; - var charset: string = res.charset; - }); + .end((res: request.Response) => { + var status: number = res.status; + var body = res.body; + var files: Object = res.files; + var text: string = res.text; + var contentLength = res.header['content-length']; + var contentType: string = res.type; + 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; - }); + .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")); + res.on("end", () => { + callback(null, new Buffer(data, "base64")); + }); + }) + .end((res: request.Response) => { + res.body.toString("hex"); }); - }) - .end((res: request.Response) => { - res.body.toString("hex"); - }); var req = request.get('/hoge'); // Aborting requests @@ -230,94 +231,98 @@ console.log(reqMethod + ' request to ' + reqUrl + ' cookies ' + reqCookies); // Basic authentication request.get('http://tobi:learnboost@local').end(callback); request - .get('http://local') - .auth('tobo', 'learnboost') - .end(callback); + .get('http://local') + .auth('tobo', 'learnboost') + .end(callback); // Following redirects request - .get('/some.png') - .redirects(2) - .end(callback); + .get('/some.png') + .redirects(2) + .end(callback); // Piping data /* -(function() { -var stream = fs.createReadStream('path/to/my.json'); -var req = request.post('/somewhere'); -req.type('json'); -stream.pipe(req); -})(); -*/ + (function() { + var stream = fs.createReadStream('path/to/my.json'); + var req = request.post('/somewhere'); + req.type('json'); + stream.pipe(req); + })(); + */ -(function() { -var stream = fs.createWriteStream('path/to/my.json'); -var req = request.get('/some.json'); -req.pipe(stream); +(function () { + var stream = fs.createWriteStream('path/to/my.json'); + var req = request.get('/some.json'); + req.pipe(stream); })(); // Multipart requests -(function() { -var req = request.post('/upload'); +(function () { + var req = request.post('/upload'); -req.part() - .set('Content-Type', 'image/png') - .set('Content-Disposition', 'attachment; filename="myimage.png"') - .write('some image data') - .write('some more image data'); + req.part() + .set('Content-Type', 'image/png') + .set('Content-Disposition', 'attachment; filename="myimage.png"') + .write('some image data') + .write('some more image data'); -req.part() - .set('Content-Disposition', 'form-data; name="name"') - .set('Content-Type', 'text/plain') - .write('tobi'); + req.part() + .set('Content-Disposition', 'form-data; name="name"') + .set('Content-Type', 'text/plain') + .write('tobi'); -req.end(callback); + req.end(callback); })(); // Attaching files request - .post('/upload') - .attach('avatar', 'path/to/tobi.png', 'user.png') - .attach('image', 'path/to/loki.png') - .attach('file', 'path/to/jane.png') - .end(callback); + .post('/upload') + .attach('avatar', 'path/to/tobi.png', 'user.png') + .attach('image', 'path/to/loki.png') + .attach('file', 'path/to/jane.png') + .end(callback); // Field values request - .post('/upload') - .field('user[name]', 'Tobi') - .field('user[email]', 'tobi@learnboost.com') - .attach('image', 'path/to/tobi.png') - .end(callback); + .post('/upload') + .field('user[name]', 'Tobi') + .field('user[email]', 'tobi@learnboost.com') + .attach('image', 'path/to/tobi.png') + .end(callback); // CORS request - .get('http://localhost:4001/') - .withCredentials() - .end(callback); + .get('http://localhost:4001/') + .withCredentials() + .end(callback); // Error handling request - .post('/upload') - .attach('image', 'path/to/tobi.png') - .end((err: any, res: request.Response): void => {}); + .post('/upload') + .attach('image', 'path/to/tobi.png') + .end((err: any, res: request.Response): void => { + }); request - .post('/upload') - .attach('image', 'path/to/tobi.png') - .on('error', (err: any) => {}) - .end(callback); + .post('/upload') + .attach('image', 'path/to/tobi.png') + .on('error', (err: any) => { + }) + .end(callback); //Promise request - .get('/search') - .then((response) => {}) - .catch((error) => {}); + .get('/search') + .then((response) => { + }) + .catch((error) => { + }); // Requesting binary data. // adapted from: https://github.com/visionmedia/superagent/blob/v2.0.0/test/client/request.js#L110 request - .get('/blob') - .responseType('blob') - .end(function (err, res) { - assert(res.xhr instanceof XMLHttpRequest) - assert(res.xhr.response instanceof Blob); - }); + .get('/blob') + .responseType('blob') + .end(function (err, res) { + assert(res.xhr instanceof XMLHttpRequest) + assert(res.xhr.response instanceof Blob); + }); diff --git a/types/superagent/tsconfig.json b/types/superagent/tsconfig.json index 1a20530571..b1f13095f7 100644 --- a/types/superagent/tsconfig.json +++ b/types/superagent/tsconfig.json @@ -20,4 +20,4 @@ "index.d.ts", "superagent-tests.ts" ] -} \ No newline at end of file +}