From f687ec5a6bcd3b5444e7c9bf895d709aa07cc0cf Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Fri, 12 Jan 2018 12:54:20 -0700 Subject: [PATCH] [@types/request] Fix lints (#22821) * [@types/request] Fix lints: ban-types. Replace generic `Function` types with appropriate `(...): `. * [@types/request] Fix lints:no-duplicate-variable. * [@types/request] Fix lints:no-inferrable-types. * [@types/request] Fix lints:no-misused-new. * [@types/request] Fix lints:no-var-keyword. * [@types/request] Fix lints:object-literal-shorthand. * [@types/request] Fix lints:prefer-const. * [@types/request] Fix lints: strict-export-declare-modifiers. * [@types/request] Fix lints: unified-signatures. --- types/request/index.d.ts | 58 +++++++------ types/request/request-tests.ts | 143 ++++++++++++++++----------------- types/request/tslint.json | 13 +-- 3 files changed, 99 insertions(+), 115 deletions(-) diff --git a/types/request/index.d.ts b/types/request/index.d.ts index 91d4e2bb04..b0da48edb1 100644 --- a/types/request/index.d.ts +++ b/types/request/index.d.ts @@ -23,7 +23,7 @@ import tough = require('tough-cookie'); import { Url } from 'url'; declare namespace request { - export interface RequestAPI { + interface RequestAPI { defaults(options: TOptions): RequestAPI; defaults(options: RequiredUriUrl & TOptions): DefaultUriUrlRequestApi; @@ -170,21 +170,21 @@ declare namespace request { interface UrlOptions { url: string | Url; } - export type RequiredUriUrl = UriOptions | UrlOptions; + type RequiredUriUrl = UriOptions | UrlOptions; - export type OptionalUriUrl = RequiredUriUrl | {}; + type OptionalUriUrl = RequiredUriUrl | {}; - export type OptionsWithUri = UriOptions & CoreOptions; - export type OptionsWithUrl = UrlOptions & CoreOptions; - export type Options = OptionsWithUri | OptionsWithUrl; + type OptionsWithUri = UriOptions & CoreOptions; + type OptionsWithUrl = UrlOptions & CoreOptions; + type Options = OptionsWithUri | OptionsWithUrl; - export type RequestCallback = (error: any, response: RequestResponse, body: any) => void; + type RequestCallback = (error: any, response: RequestResponse, body: any) => void; - export type ResponseRequest = CoreOptions & { + type ResponseRequest = CoreOptions & { uri: Url; }; - export interface RequestResponse extends http.IncomingMessage { + interface RequestResponse extends http.IncomingMessage { request: ResponseRequest; body: any; timingStart?: number; @@ -205,7 +205,7 @@ declare namespace request { }; } - export interface HttpArchiveRequest { + interface HttpArchiveRequest { url?: string; method?: string; headers?: NameValuePair[]; @@ -215,12 +215,12 @@ declare namespace request { }; } - export interface NameValuePair { + interface NameValuePair { name: string; value: string; } - export interface Multipart { + interface Multipart { chunked?: boolean; data?: Array<{ 'content-type'?: string, @@ -228,12 +228,12 @@ declare namespace request { }>; } - export interface RequestPart { + interface RequestPart { headers?: Headers; body: any; } - export interface Request extends stream.Stream { + interface Request extends stream.Stream { readable: boolean; writable: boolean; @@ -253,21 +253,19 @@ declare namespace request { oauth(oauth: OAuthOptions): Request; jar(jar: CookieJar): Request; - on(event: string, listener: Function): this; + on(event: string, listener: (...args: any[]) => void): this; on(event: 'request', listener: (req: http.ClientRequest) => void): this; on(event: 'response', listener: (resp: http.IncomingMessage) => void): this; on(event: 'data', listener: (data: Buffer | string) => void): this; on(event: 'error', listener: (e: Error) => void): this; on(event: 'complete', listener: (resp: http.IncomingMessage, body?: string | Buffer) => void): this; - write(buffer: Buffer, cb?: Function): boolean; - write(str: string, cb?: Function): boolean; - write(str: string, encoding: string, cb?: Function): boolean; - write(str: string, encoding?: string, fd?: string): boolean; - end(): void; - end(chunk: Buffer, cb?: Function): void; - end(chunk: string, cb?: Function): void; - end(chunk: string, encoding: string, cb?: Function): void; + write(buffer: Buffer | string, cb?: (err?: Error) => void): boolean; + write(str: string, encoding?: string, cb?: (err?: Error) => void): boolean; + end(cb?: () => void): void; + end(chunk: string | Buffer, cb?: () => void): void; + end(str: string, encoding?: string, cb?: () => void): void; + pause(): void; resume(): void; abort(): void; @@ -275,11 +273,11 @@ declare namespace request { toJSON(): object; } - export interface Headers { + interface Headers { [key: string]: any; } - export interface AuthOptions { + interface AuthOptions { user?: string; username?: string; pass?: string; @@ -288,7 +286,7 @@ declare namespace request { bearer?: string | (() => string); } - export interface OAuthOptions { + interface OAuthOptions { callback?: string; consumer_key?: string; consumer_secret?: string; @@ -299,18 +297,18 @@ declare namespace request { body_hash?: true | string; } - export interface HawkOptions { + interface HawkOptions { credentials: any; } - export interface AWSOptions { + interface AWSOptions { secret: string; bucket?: string; } - export type Cookie = tough.Cookie; + type Cookie = tough.Cookie; - export interface CookieJar { + interface CookieJar { setCookie(cookieOrStr: Cookie | string, uri: string | Url, options?: tough.CookieJar.SetCookieOptions): void; getCookieString(uri: string | Url): string; getCookies(uri: string | Url): Cookie[]; diff --git a/types/request/request-tests.ts b/types/request/request-tests.ts index 481f54473b..5ba0145b64 100644 --- a/types/request/request-tests.ts +++ b/types/request/request-tests.ts @@ -1,31 +1,32 @@ -import request = require('request'); +import FormData = require('form-data'); +import fs = require('fs'); import http = require('http'); +import path = require('path'); +import qs = require('querystring'); +import request = require('request'); import stream = require('stream'); import urlModule = require('url'); -import fs = require('fs'); -import FormData = require('form-data'); -import path = require('path'); -var value: any; -var str: string; -var strOrUndef: string | undefined; -var strOrTrueOrUndef: string | true | undefined; -var buffer: NodeBuffer = new Buffer('foo'); -var num: number = 0; -var bool: boolean; -var date: Date; -var obj: object; -var dest: string = 'foo'; +let value: any; +let str: string; +let strOrUndef: string | undefined; +let strOrTrueOrUndef: string | true | undefined; +const buffer: NodeBuffer = new Buffer('foo'); +const num = 0; +let bool: boolean; +let date: Date; +let obj: object; +const dest = 'foo'; -var uri: string = 'foo-bar'; -var headers: request.Headers = {}; +const uri = 'foo-bar'; +const headers: request.Headers = {}; -var agent: http.Agent; -var write: stream.Writable = new stream.Writable(); -var req: request.Request = request(uri, function callback() {}); -var form1: FormData; +let agent: http.Agent; +let write: stream.Writable = new stream.Writable(); +let req: request.Request = request(uri, function callback() {}); +let form: FormData; -var bodyArr: request.RequestPart[] = [{ +const bodyArr: request.RequestPart[] = [{ body: value }, { body: value @@ -52,7 +53,7 @@ var bodyArr: request.RequestPart[] = [{ obj = req.toJSON(); -var cookie: request.Cookie = request.cookie('foo')!; +let cookie: request.Cookie = request.cookie('foo')!; str = cookie.key; str = cookie.value; date = cookie.expires; @@ -60,16 +61,16 @@ str = cookie.path; str = cookie.toString(); bool = cookie.httpOnly; -var jar: request.CookieJar = request.jar(); +let jar: request.CookieJar = request.jar(); jar.setCookie(cookie, uri); str = jar.getCookieString(uri); -var cookies: request.Cookie[] = jar.getCookies(uri); +const cookies: request.Cookie[] = jar.getCookies(uri); -var aws: request.AWSOptions = { secret: 'foo' }; +const aws: request.AWSOptions = { secret: 'foo' }; str = aws.secret; strOrUndef = aws.bucket; -var oauth: request.OAuthOptions = { body_hash: 'foo' }; +let oauth: request.OAuthOptions = { body_hash: 'foo' }; strOrUndef = oauth.callback; strOrUndef = oauth.consumer_key; strOrUndef = oauth.consumer_secret; @@ -79,14 +80,14 @@ strOrUndef = oauth.transport_method; strOrUndef = oauth.verifier; strOrTrueOrUndef = oauth.body_hash; -var options: request.Options = { +let options: request.Options = { url: str, uri: str, callback: (error: any, response: any, body: any) => {}, jar: value, form: obj, oauth: value, - aws: aws, + aws, qs: obj, json: value, jsonReviver: (key: string, value: any) => {}, @@ -220,7 +221,7 @@ value = request.initParams(options, callback); req = request.forever(value, value); jar = request.jar(); -var r = request.defaults(options); +const r = request.defaults(options); r(str); r.get(str); r.post(str); @@ -279,7 +280,7 @@ http.createServer((req, resp) => { http.createServer((req, resp) => { if (req.url === '/doodle.png') { - var x = request('http://mysite.com/doodle.png'); + const x = request('http://mysite.com/doodle.png'); req.pipe(x); x.pipe(resp); } @@ -301,7 +302,7 @@ request.post('http://service.com/upload').form({key: 'value'}); // or request.post({url: 'http://service.com/upload', form: {key: 'value'}}, (err, httpResponse, body) => { /* ... */ }); -var data = { +const data = { // Pass a simple key-value pair my_field: 'my_value', // Pass data via Buffers @@ -332,8 +333,8 @@ request.post({url: 'http://service.com/upload', formData: data}, function option console.log('Upload successful! Server responded with:', body); }); -var requestMultipart = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {}); -var form = requestMultipart.form(); +const requestMultipart = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {}); +form = requestMultipart.form(); form.append('my_field', 'my_value'); form.append('my_buffer', new Buffer([1, 2, 3])); form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'}); @@ -407,11 +408,11 @@ request.get('http://some.server.com/', { } }); -let username = 'username'; -let password = 'password'; -var url = `http://'${username}:${password}'@some.server.com`; +const username = 'username'; +const password = 'password'; +let url = `http://'${username}:${password}'@some.server.com`; -request({url: url}, (error, response, body) => { +request({url}, (error, response, body) => { // Do more stuff with 'body' here }); @@ -424,7 +425,7 @@ options = { function callback(error: any, response: http.IncomingMessage, body: string) { if (!error && response.statusCode === 200) { - var info = JSON.parse(body); + const info = JSON.parse(body); console.log(info.stargazers_count + " Stars"); console.log(info.forks_count + " Forks"); } @@ -434,7 +435,6 @@ request(options, callback); // OAuth1.0 - 3-legged server side flow (Twitter example) // step 1 -import qs = require('querystring'); const CONSUMER_KEY = 'key'; const CONSUMER_SECRET = 'secret'; oauth = { @@ -445,52 +445,52 @@ oauth = { }; url = 'https://api.twitter.com/oauth/request_token'; -request.post({url: url, oauth: oauth}, (e, r, body) => { +request.post({url, oauth}, (e, r, body) => { // Ideally, you would take the body in the response // and construct a URL that a user clicks on (like a sign in button). // The verifier is only available in the response after a user has // verified with twitter that they are authorizing your app. // step 2 - var req_data = qs.parse(body); - var uri = `https://api.twitter.com/oauth/authenticate?${qs.stringify({oauth_token: req_data.oauth_token})}`; + const req_data = qs.parse(body); + const uri = `https://api.twitter.com/oauth/authenticate?${qs.stringify({oauth_token: req_data.oauth_token})}`; // redirect the user to the authorize uri // step 3 // after the user is redirected back to your server - var auth_data: any = qs.parse(body); - var oauth = { + const auth_data: any = qs.parse(body); + const oauth = { consumer_key: CONSUMER_KEY, consumer_secret: CONSUMER_SECRET, token: auth_data.oauth_token, token_secret: req_data.oauth_token_secret as string, verifier: auth_data.oauth_verifier }; - var url = 'https://api.twitter.com/oauth/access_token'; + const url = 'https://api.twitter.com/oauth/access_token'; - request.post({url: url, oauth: oauth}, (e, r, body) => { + request.post({url, oauth}, (e, r, body) => { // ready to make signed requests on behalf of the user - var perm_data: any = qs.parse(body); - var oauth = { + const perm_data: any = qs.parse(body); + const oauth = { consumer_key: CONSUMER_KEY, consumer_secret: CONSUMER_SECRET, token: perm_data.oauth_token, token_secret: perm_data.oauth_token_secret }; - var url = 'https://api.twitter.com/1.1/users/show.json'; - var query = { + const url = 'https://api.twitter.com/1.1/users/show.json'; + const query = { screen_name: perm_data.screen_name, user_id: perm_data.user_id }; - request.get({url: url, oauth: oauth, qs: query, json: true}, (e, r, user) => { + request.get({url, oauth, qs: query, json: true}, (e, r, user) => { console.log(user); }); }); }); -var certFile = path.resolve(__dirname, 'ssl/client.crt'); -var keyFile = path.resolve(__dirname, 'ssl/client.key'); -var caFile = path.resolve(__dirname, 'ssl/ca.cert.pem'); +const certFile = path.resolve(__dirname, 'ssl/client.crt'); +const keyFile = path.resolve(__dirname, 'ssl/client.key'); +const caFile = path.resolve(__dirname, 'ssl/ca.cert.pem'); options = { url: 'https://api.some-server.com/', @@ -502,9 +502,6 @@ options = { request.get(options); -var certFile = path.resolve(__dirname, 'ssl/client.crt'); -var keyFile = path.resolve(__dirname, 'ssl/client.key'); - options = { url: 'https://api.some-server.com/', agentOptions: { @@ -565,13 +562,13 @@ request({ }); // requests using baseRequest() will set the 'x-token' header -var baseRequest = request.defaults({ +const baseRequest = request.defaults({ headers: {'x-token': 'my-token'} }); // requests using specialRequest() will include the 'x-token' header set in // baseRequest and will also include the 'special' header -var specialRequest = baseRequest.defaults({ +const specialRequest = baseRequest.defaults({ headers: {special: 'special value'} }); @@ -597,7 +594,7 @@ request.get('http://10.255.255.1', {timeout: 1500}, (err) => { process.exit(0); }); -var rand = Math.floor(Math.random() * 100000000).toString(); +const rand = Math.floor(Math.random() * 100000000).toString(); request( { method: 'PUT' , uri: 'http://mikeal.iriscouch.com/testjs/' + rand @@ -640,22 +637,22 @@ request( }); }); -var requestWithJar = request.defaults({jar: true}); +let requestWithJar = request.defaults({jar: true}); requestWithJar('http://www.google.com', () => { requestWithJar('http://images.google.com'); }); -var j = request.jar(); -requestWithJar = request.defaults({jar: j}); +jar = request.jar(); +requestWithJar = request.defaults({jar}); requestWithJar('http://www.google.com', () => { requestWithJar('http://images.google.com'); }); -var j = request.jar(); +jar = request.jar(); cookie = request.cookie('key1=value1')!; -var url = 'http://www.google.com'; -j.setCookie(cookie, url); -request({url: url, jar: j}, () => { +url = 'http://www.google.com'; +jar.setCookie(cookie, url); +request({url, jar}, () => { request('http://images.google.com'); }); @@ -663,15 +660,15 @@ request({url: url, jar: j}, () => { // var FileCookieStore = require('tough-cookie-filestore'); // NOTE - currently the 'cookies.json' file must already exist! // var j = request.jar(new FileCookieStore('cookies.json')); -requestWithJar = request.defaults({ jar : j }); +requestWithJar = request.defaults({ jar }); request('http://www.google.com', () => { request('http://images.google.com'); }); -var j = request.jar(); -request({url: 'http://www.google.com', jar: j}, () => { - var cookie_string = j.getCookieString(url); // "key1=value1; key2=value2; ..." - var cookies = j.getCookies(url); +jar = request.jar(); +request({url: 'http://www.google.com', jar}, () => { + const cookie_string = jar.getCookieString(url); // "key1=value1; key2=value2; ..." + const cookies = jar.getCookies(url); // [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...] }); @@ -696,7 +693,7 @@ request.get({ uri: urlModule.parse('http://example.com') }); -var requestWithOptionalUri = request.defaults({ uri: 'http://example.com' }); +const requestWithOptionalUri = request.defaults({ uri: 'http://example.com' }); requestWithOptionalUri(); diff --git a/types/request/tslint.json b/types/request/tslint.json index 7f1cf14897..f93cf8562a 100644 --- a/types/request/tslint.json +++ b/types/request/tslint.json @@ -1,14 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "ban-types": false, - "no-duplicate-variable": false, - "no-inferrable-types": false, - "no-misused-new": false, - "no-var-keyword": false, - "object-literal-shorthand": false, - "prefer-const": false, - "strict-export-declare-modifiers": false, - "unified-signatures": false - } + "extends": "dtslint/dt.json" }