diff --git a/types/promisify-supertest/index.d.ts b/types/promisify-supertest/index.d.ts index 9f59afdb89..e887c6c915 100644 --- a/types/promisify-supertest/index.d.ts +++ b/types/promisify-supertest/index.d.ts @@ -19,7 +19,7 @@ declare namespace supertest { interface SuperTest extends superagent.SuperAgent { } - interface Test extends superagent.Request { + interface Test extends superagent.SuperAgentRequest { url: string; serverAddress(app: any, path: string): string; expect(status: number, callback?: CallbackHandler): this; diff --git a/types/superagent/index.d.ts b/types/superagent/index.d.ts index df3a7e709f..4e9cdf4812 100644 --- a/types/superagent/index.d.ts +++ b/types/superagent/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/visionmedia/superagent // Definitions by: Nico Zelaya // Michael Ledin +// Pap Lőrinc // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -21,9 +22,9 @@ declare namespace request { interface SuperAgentRequest extends Request { agent(agent?: https.Agent): this; + cookies: string; method: string; url: string; - cookies: string; } interface SuperAgentStatic extends SuperAgent { (url: string): SuperAgentRequest; @@ -34,95 +35,99 @@ declare namespace request { serialize: { [type: string]: Serializer }; } - interface SuperAgent extends stream.Stream { - get(url: string, callback?: CallbackHandler): Req; - post(url: string, callback?: CallbackHandler): Req; - put(url: string, callback?: CallbackHandler): Req; - head(url: string, callback?: CallbackHandler): Req; + interface SuperAgent extends stream.Stream { + attachCookies(req: Req): void; + checkout(url: string, callback?: CallbackHandler): Req; + connect(url: string, callback?: CallbackHandler): Req; + copy(url: string, callback?: CallbackHandler): Req; del(url: string, callback?: CallbackHandler): Req; delete(url: string, callback?: CallbackHandler): Req; - options(url: string, callback?: CallbackHandler): Req; - trace(url: string, callback?: CallbackHandler): Req; - copy(url: string, callback?: CallbackHandler): Req; + get(url: string, callback?: CallbackHandler): Req; + head(url: string, callback?: CallbackHandler): Req; lock(url: string, callback?: CallbackHandler): Req; + merge(url: string, callback?: CallbackHandler): Req; + mkactivity(url: string, callback?: CallbackHandler): Req; mkcol(url: string, callback?: CallbackHandler): Req; move(url: string, callback?: CallbackHandler): Req; - purge(url: string, callback?: CallbackHandler): Req; + notify(url: string, callback?: CallbackHandler): Req; + options(url: string, callback?: CallbackHandler): Req; + parse(fn: (res: Response, callback: (err: Error | null, body: any) => void) => void): this; + patch(url: string, callback?: CallbackHandler): Req; + post(url: string, callback?: CallbackHandler): Req; propfind(url: string, callback?: CallbackHandler): Req; proppatch(url: string, callback?: CallbackHandler): Req; - unlock(url: string, callback?: CallbackHandler): Req; + purge(url: string, callback?: CallbackHandler): Req; + put(url: string, callback?: CallbackHandler): Req; report(url: string, callback?: CallbackHandler): Req; - mkactivity(url: string, callback?: CallbackHandler): Req; - checkout(url: string, callback?: CallbackHandler): Req; - merge(url: string, callback?: CallbackHandler): Req; - // m-search(url: string, callback?: CallbackHandler): Req; - notify(url: string, callback?: CallbackHandler): Req; - subscribe(url: string, callback?: CallbackHandler): Req; - unsubscribe(url: string, callback?: CallbackHandler): Req; - patch(url: string, callback?: CallbackHandler): Req; - 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; + search(url: string, callback?: CallbackHandler): Req; + subscribe(url: string, callback?: CallbackHandler): Req; + trace(url: string, callback?: CallbackHandler): Req; + unlock(url: string, callback?: CallbackHandler): Req; + unsubscribe(url: string, callback?: CallbackHandler): Req; } interface Response extends NodeJS.ReadableStream { - text: string; - body: any; - files: any; - header: any; - type: string; - charset: string; - status: number; - statusType: number; - info: boolean; - ok: boolean; - redirect: boolean; - clientError: boolean; - serverError: boolean; - error: Error; accepted: boolean; - noContent: boolean; badRequest: boolean; - unauthorized: boolean; + body: any; + charset: string; + clientError: boolean; + error: Error; + files: any; + forbidden: boolean; + get(header: string): string; + header: any; + info: boolean; + noContent: boolean; notAcceptable: boolean; notFound: boolean; - forbidden: boolean; + ok: boolean; + redirect: boolean; + serverError: boolean; + status: number; + statusType: number; + text: string; + type: string; + unauthorized: boolean; xhr: XMLHttpRequest; - get(header: string): string; } - interface Request extends Promise /* extends NodeJS.WritableStream */ { + interface Request extends Promise { abort(): void; accept(type: string): this; attach(field: string, file: Blob | Buffer | fs.ReadStream | string, filename?: 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; get(field: string): string; - on(name: string, handler: (event: any) => void): this; + key(cert: Buffer | string): this; + ok(callback: (res: Response) => boolean): this; on(name: 'error', handler: (err: any) => void): this; on(name: 'progress', handler: (event: ProgressEvent) => void): this; + on(name: string, handler: (event: any) => void): this; + parse(fn: (res: Response, callback: (err: Error | null, body: any) => void) => void): this; part(): this; + pfx(cert: Buffer | string): this; pipe(stream: NodeJS.WritableStream, options?: object): stream.Writable; query(val: object | string): this; redirects(n: number): this; responseType(type: string): this; + retry(count?: number): this; send(data?: string | object): this; serialize(serializer: Serializer): this; - set(field: string, val: string): this; set(field: object): this; + set(field: string, val: string): this; timeout(ms: number | { deadline?: number, response?: number }): this; type(val: string): this; unset(field: string): this; use(fn: Plugin): this; withCredentials(): this; write(data: string | Buffer, encoding?: string): this; - parse(fn: (res: Response, callback: (err: Error | null, body: any) => void) => void): this; } type Plugin = (req: Request) => void; diff --git a/types/superagent/superagent-tests.ts b/types/superagent/superagent-tests.ts index f38dcb31ea..99214b2ccd 100644 --- a/types/superagent/superagent-tests.ts +++ b/types/superagent/superagent-tests.ts @@ -12,7 +12,7 @@ const httpsAgent: Agent = new Agent(); request .post('/api/pet') - .send({name: 'Manny', species: 'cat'}) + .send({ name: 'Manny', species: 'cat' }) .set('X-API-Key', 'foobar') .set('Accept', 'application/json') .agent(httpsAgent) @@ -27,7 +27,7 @@ request const agent = request.agent(); agent .post('/api/pet') - .send({name: 'Manny', species: 'cat'}) + .send({ name: 'Manny', species: 'cat' }) .set('X-API-Key', 'foobar') .set('Accept', 'application/json') .end((err, res) => { @@ -81,20 +81,20 @@ request request .get('/search') - .set({'API-Key': 'foobar', Accept: 'application/json'}) + .set({ 'API-Key': 'foobar', Accept: 'application/json' }) .end(callback); // GET requests request .get('/search') - .query({query: 'Manny'}) - .query({range: '1..5'}) - .query({order: 'desc'}) + .query({ query: 'Manny' }) + .query({ range: '1..5' }) + .query({ order: 'desc' }) .end(callback); request .get('/search') - .query({query: 'Manny', range: '1..5', order: 'desc'}) + .query({ query: 'Manny', range: '1..5', order: 'desc' }) .end(callback); request @@ -111,7 +111,7 @@ request // HEAD requests request .head('/users') - .query({email: 'joe@smith.com'}) + .query({ email: 'joe@smith.com' }) .end(callback); // POST / PUT requests @@ -121,12 +121,12 @@ request.post('/user') .end(callback); request.post('/user') - .send({name: 'tj', pet: 'tobi'}) + .send({ name: 'tj', pet: 'tobi' }) .end(callback); request.post('/user') - .send({name: 'tj'}) - .send({pet: 'tobi'}) + .send({ name: 'tj' }) + .send({ pet: 'tobi' }) .end(callback); request.post('/user') @@ -136,8 +136,8 @@ request.post('/user') request.post('/user') .type('form') - .send({name: 'tj'}) - .send({pet: 'tobi'}) + .send({ name: 'tj' }) + .send({ pet: 'tobi' }) .end(callback); // Setting the Content-Type @@ -166,22 +166,22 @@ request.get('/user') // Query strings request .post('/') - .query({format: 'json'}) - .query({dest: '/login'}) - .send({post: 'data', here: 'wahoo'}) + .query({ format: 'json' }) + .query({ dest: '/login' }) + .send({ post: 'data', here: 'wahoo' }) .end(callback); // Custom request serializer function testParser(data: any) { - return JSON.stringify(data); + return JSON.stringify(data); } request - .post('/user') - .serialize(testParser) - .type('json') - .send({ foo: 123 }) - .end(callback); + .post('/user') + .serialize(testParser) + .type('json') + .send({ foo: 123 }) + .end(callback); // Default serialization map @@ -189,15 +189,15 @@ request.serialize['application/xml'] = (obj: any) => 'generated xml here'; // Parsing response bodies request('/search') - .end((res: request.Response) => { - const status: number = res.status; - const body = res.body; - const files: object = res.files; - const text: string = res.text; - const contentLength = res.header['content-length']; - const contentType: string = res.type; - const charset: string = res.charset; - }); + .end((res: request.Response) => { + const status: number = res.status; + const body = res.body; + const files: object = res.files; + const text: string = res.text; + const contentLength = res.header['content-length']; + const contentType: string = res.type; + const charset: string = res.charset; + }); // Custom parsers request @@ -244,15 +244,15 @@ request .redirects(2) .end(callback); -// Piping data -/* -(() => { -const stream = fs.createReadStream('path/to/my.json'); -const req = request.post('/somewhere'); -req.type('json'); -stream.pipe(req); -})(); -*/ +// Retries, from https://github.com/visionmedia/superagent/commit/765de565c505abd221e7aa0765c5dd883a987ef1 +request + .get('/some.png') + .retry() + .timeout({ response: 9000, deadline: 10000 }); +request + .get('http://example.com/search') + .retry(2) + .end(callback); (() => { const stream = fs.createWriteStream('path/to/my.json'); @@ -339,9 +339,34 @@ request // 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((err, res) => { - assert(res.xhr instanceof XMLHttpRequest); - assert(res.xhr.response instanceof Blob); - }); + .get('/blob') + .responseType('blob') + .end((err, res) => { + assert(res.xhr instanceof XMLHttpRequest); + assert(res.xhr.response instanceof Blob); + }); + +// HTTPS request, from: https://github.com/visionmedia/superagent/commit/6158efbf42cb93d77c1a70887284be783dd7dabe +const ca = fs.readFileSync('ca.cert.pem'); +const key = fs.readFileSync('key.pem'); +const cert = fs.readFileSync('cert.pem'); +request + .post('/secure') + .ca(ca) + .key(key) + .cert(cert) + .end(callback); + +const pfx = fs.readFileSync('cert.pfx'); +request + .post('/secure') + .pfx(pfx) + .end(callback); + +// ok, from: https://github.com/visionmedia/superagent/commit/34533bbc29833889090847c45a82b0ea81b2f06d +request + .get('/404') + .ok(res => res.status < 500) + .then(response => { + // reads 404 page as a successful response + }); diff --git a/types/superagent/v2/index.d.ts b/types/superagent/v2/index.d.ts index f598bbd4fe..626d3fb468 100644 --- a/types/superagent/v2/index.d.ts +++ b/types/superagent/v2/index.d.ts @@ -31,7 +31,7 @@ declare namespace request { agent(): SuperAgent; } - interface SuperAgent extends stream.Stream { + interface SuperAgent extends stream.Stream { get(url: string, callback?: CallbackHandler): Req; post(url: string, callback?: CallbackHandler): Req; put(url: string, callback?: CallbackHandler): Req; diff --git a/types/supertest/index.d.ts b/types/supertest/index.d.ts index 206c70defd..4607dd55f1 100644 --- a/types/supertest/index.d.ts +++ b/types/supertest/index.d.ts @@ -13,11 +13,11 @@ declare namespace supertest { interface Response extends superagent.Response { } - interface Request extends superagent.Request { + interface Request extends superagent.SuperAgentRequest { } type CallbackHandler = (err: any, res: Response) => void; - interface Test extends Request { + interface Test extends superagent.SuperAgentRequest { app?: any; url: string; serverAddress(app: any, path: string): string; @@ -34,7 +34,7 @@ declare namespace supertest { function agent(app?: any): SuperTest; - interface SuperTest extends superagent.SuperAgent { + interface SuperTest extends superagent.SuperAgent { } } diff --git a/types/supertest/supertest-tests.ts b/types/supertest/supertest-tests.ts index 5be5558e46..7241aac5c3 100644 --- a/types/supertest/supertest-tests.ts +++ b/types/supertest/supertest-tests.ts @@ -3,9 +3,10 @@ import * as supertest from 'supertest'; import * as express from 'express'; const app = express(); +const request: supertest.SuperTest = supertest(app); -supertest(app) - .get('/user') +(request + .get('/user') as supertest.Test) .expect('Content-Type', /json/) .expect('Content-Length', '20') .expect(201) @@ -14,7 +15,6 @@ supertest(app) }); // cookie scenario -const request = supertest(app); const agent = supertest.agent(); request .post('/login') @@ -22,7 +22,7 @@ request if (err) throw err; agent.saveCookies(res); - const req = request.get('/admin'); + const req = request.get('/admin') as supertest.Test; agent.attachCookies(req); req.expect(200, (err: any, res: supertest.Response) => { if (err) throw err; @@ -36,15 +36,15 @@ client .end((err: any, res: supertest.Response) => { if (err) throw err; - client.get('/admin') + (client.get('/admin') as supertest.Test) .expect(200, (err: any, res: supertest.Response) => { if (err) throw err; }); }); // functional expect -supertest(app) - .get('/') +(request + .get('/') as supertest.Test) .expect(hasPreviousAndNextKeys) .end((err: any, res: supertest.Response) => { if (err) throw err; @@ -56,8 +56,8 @@ function hasPreviousAndNextKeys(res: supertest.Response) { } // object expect -supertest(app) - .get('/') +(request + .get('/') as supertest.Test) .expect(200, { foo: 'bar' }) .end((err: any, res: supertest.Response) => { if (err) throw err;