diff --git a/types/download/tsconfig.json b/types/download/tsconfig.json index edb8b7f471..809bed13d2 100644 --- a/types/download/tsconfig.json +++ b/types/download/tsconfig.json @@ -14,10 +14,15 @@ ], "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "paths": { + "got": [ + "got/v8" + ] + } }, "files": [ "index.d.ts", "download-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/got/got-tests.ts b/types/got/got-tests.ts index 610ac87c26..934adfc9a8 100644 --- a/types/got/got-tests.ts +++ b/types/got/got-tests.ts @@ -7,6 +7,7 @@ import * as http from 'http'; import * as https from 'https'; import * as url from 'url'; import QuickLRU = require('quick-lru'); +import tough = require('tough-cookie'); let str: string; let buf: Buffer; @@ -260,3 +261,11 @@ got(new url.URL('http://todomvc.com')); got(url.parse('http://todomvc.com')); got('https://todomvc.com', { rejectUnauthorized: false }); + +got('/examples/angularjs', { baseUrl: 'http://todomvc.com' }); +got('http://todomvc.com', { headers: { foo: 'bar'} }); +got('http://todomvc.com', { cookieJar: new tough.CookieJar() }); +got('http://todomvc.com', { retry: 2 }); +got('http://todomvc.com', { retry: { retries: 2, methods: ['GET'], statusCodes: [408, 504], maxRetryAfter: 1 } }); +got('http://todomvc.com', { throwHttpErrors: false }); +got('http://todomvc.com', { hooks: { beforeRequest: [ () => 'foo']} }); diff --git a/types/got/index.d.ts b/types/got/index.d.ts index 775985e581..6b0929fdcf 100644 --- a/types/got/index.d.ts +++ b/types/got/index.d.ts @@ -1,8 +1,9 @@ -// Type definitions for got 8.3 +// Type definitions for got 9.2 // Project: https://github.com/sindresorhus/got#readme // Definitions by: BendingBender // Linus Unnebäck // Konstantin Ikonnikov +// Stijn Van Nieuwenhuyse // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -12,6 +13,7 @@ import { Url, URL } from 'url'; import * as http from 'http'; import * as https from 'https'; import * as nodeStream from 'stream'; +import { CookieJar } from 'tough-cookie'; export = got; @@ -95,8 +97,12 @@ declare namespace got { type GotUrl = string | https.RequestOptions | Url | URL; + type Hook = (options: T) => any; + type Hooks = Record<'beforeRequest', Array>>; + interface GotBodyOptions extends GotOptions { body?: string | Buffer | nodeStream.Readable; + hooks?: Hooks>; } interface GotJSONOptions extends GotOptions { @@ -104,25 +110,29 @@ declare namespace got { body?: object; form?: boolean; json: true; + hooks?: Hooks; } interface GotFormOptions extends GotOptions { body?: {[key: string]: any}; form: true; json?: boolean; + hooks?: Hooks>; } interface GotOptions extends InternalRequestOptions { + baseUrl?: string; + cookieJar?: CookieJar; encoding?: E; query?: string | object; timeout?: number | TimeoutOptions; - retries?: number | RetryFunction; + retry?: number | RetryOptions; followRedirect?: boolean; decompress?: boolean; useElectronNet?: boolean; - cache?: Cache; - agent?: http.Agent | boolean | AgentOptions; throwHttpErrors?: boolean; + agent?: http.Agent | boolean | AgentOptions; + cache?: Cache; } interface TimeoutOptions { @@ -131,13 +141,20 @@ declare namespace got { request?: number; } + type RetryFunction = (retry: number, error: any) => number; + + interface RetryOptions { + retries?: number | RetryFunction; + methods?: Array<'GET' | 'PUT' | 'HEAD' | 'DELETE' | 'OPTIONS' | 'TRACE'>; + statusCodes?: Array<408 | 413 | 429 | 500 | 502 | 503 | 504>; + maxRetryAfter?: number; + } + interface AgentOptions { http: http.Agent; https: https.Agent; } - type RetryFunction = (retry: number, error: any) => number; - interface Cache { set(key: string, value: any, ttl?: number): any; get(key: string): any; diff --git a/types/got/tslint.json b/types/got/tslint.json index 4a4f94c041..08b1465cd6 100644 --- a/types/got/tslint.json +++ b/types/got/tslint.json @@ -1,7 +1,6 @@ { "extends": "dtslint/dt.json", "rules": { - // TODO "unified-signatures": false } } diff --git a/types/got/v8/got-tests.ts b/types/got/v8/got-tests.ts new file mode 100644 index 0000000000..610ac87c26 --- /dev/null +++ b/types/got/v8/got-tests.ts @@ -0,0 +1,262 @@ +import got = require('got'); +import cookie = require('cookie'); +import FormData = require('form-data'); +import Keyv = require('keyv'); +import * as fs from 'fs'; +import * as http from 'http'; +import * as https from 'https'; +import * as url from 'url'; +import QuickLRU = require('quick-lru'); + +let str: string; +let buf: Buffer; + +got('todomvc.com') + .then(response => { + str = response.body; + }) + .catch((error: got.GotError) => { + console.log(error.response.body); + }); + +got('todomvc.com').cancel(); + +got('todomvc.com', {json: true}).then((response) => { + response.body; // $ExpectType any +}); + +got('todomvc.com', {json: true, body: {}}).then((response) => { + response.body; // $ExpectType any +}); + +got('todomvc.com', {json: true, body: [{}]}).then((response) => { + response.body; // $ExpectType any +}); + +got('todomvc.com', {json: true, form: true}).then((response) => { + response.body; // $ExpectType any +}); + +got('todomvc.com', {json: true, form: true, encoding: null}).then((response) => { + response.body; // $ExpectType any +}); + +got('todomvc.com', {json: true, form: true, encoding: null, hostname: 'todomvc'}).then((response) => { + response.body; // $ExpectType any +}); + +got('todomvc.com', {form: true}).then(response => str = response.body); +got('todomvc.com', {form: true, body: {}}).then(response => str = response.body); +got('todomvc.com', {form: true, body: [{}]}).then(response => str = response.body); +got('todomvc.com', {form: true, body: [{}], encoding: null}).then(response => buf = response.body); +got('todomvc.com', {form: true, body: [{}], encoding: 'utf8'}).then(response => str = response.body); +got('todomvc.com', { + form: true, + body: [{}], + encoding: 'utf8', + hostname: 'todomvc' +}).then(response => str = response.body); +got('todomvc.com', { + form: true, + body: [{}], + encoding: 'utf8', + hostname: 'todomvc', + timeout: 2000 +}).then(response => str = response.body); +got('todomvc.com', { + form: true, + body: [{}], + encoding: 'utf8', + hostname: 'todomvc', + timeout: {connect: 20, request: 20, socket: 20} +}).then(response => str = response.body); +// following must lead to type checking error: got('todomvc.com', {form: true, body: ''}).then(response => str = response.body); + +got('todomvc.com', {encoding: null, hostname: 'todomvc'}).then(response => buf = response.body); +got('todomvc.com', {encoding: 'utf8', hostname: 'todomvc'}).then(response => str = response.body); + +got('todomvc.com', {hostname: 'todomvc'}).then(response => str = response.body); + +got.get('todomvc.com', {hostname: 'todomvc'}).then(response => str = response.body); +got.post('todomvc.com', {hostname: 'todomvc'}).then(response => str = response.body); +got.put('todomvc.com', {hostname: 'todomvc'}).then(response => str = response.body); +got.patch('todomvc.com', {hostname: 'todomvc'}).then(response => str = response.body); +got.head('todomvc.com', {hostname: 'todomvc'}).then(response => str = response.body); +got.delete('todomvc.com', {hostname: 'todomvc'}).then(response => str = response.body); + +got.stream('todomvc.com').pipe(fs.createWriteStream('index.html')); + +fs.createReadStream('index.html').pipe(got.stream.get('todomvc.com')); +fs.createReadStream('index.html').pipe(got.stream.post('todomvc.com')); +fs.createReadStream('index.html').pipe(got.stream.put('todomvc.com')); +fs.createReadStream('index.html').pipe(got.stream.patch('todomvc.com')); +fs.createReadStream('index.html').pipe(got.stream.head('todomvc.com')); +fs.createReadStream('index.html').pipe(got.stream.delete('todomvc.com')); + +let req: http.ClientRequest; +let res: http.IncomingMessage | undefined; +let opts: got.GotOptions; +let err: got.GotError; +let href: string | undefined; +let progress: got.Progress; + +const stream = got.stream('todomvc.com'); +stream.addListener('request', (r) => req = r); +stream.addListener('response', (r) => res = r); +stream.addListener('redirect', (r, o) => { + res = r; + opts = o; + href = o.href; +}); +stream.addListener('error', (e, b, r) => { + err = e; + res = r; +}); +stream.addListener('downloadProgress', (p) => { + progress = p; +}); +stream.addListener('uploadProgress', (p) => { + progress = p; +}); + +stream.on('request', (r) => req = r); +stream.on('response', (r) => res = r); +stream.on('redirect', (r, o) => { + res = r; + opts = o; + href = o.href; +}); +stream.on('error', (e, b, r) => { + err = e; + res = r; +}); +stream.on('downloadProgress', (p) => { + progress = p; +}); +stream.on('uploadProgress', (p) => { + progress = p; +}); + +stream.once('request', (r) => req = r); +stream.once('response', (r) => res = r); +stream.once('redirect', (r, o) => { + res = r; + opts = o; + href = o.href; +}); +stream.once('error', (e, b, r) => { + err = e; + res = r; +}); +stream.once('downloadProgress', (p) => { + progress = p; +}); +stream.once('uploadProgress', (p) => { + progress = p; +}); + +stream.prependListener('request', (r) => req = r); +stream.prependListener('response', (r) => res = r); +stream.prependListener('redirect', (r, o) => { + res = r; + opts = o; + href = o.href; +}); +stream.prependListener('error', (e, b, r) => { + err = e; + res = r; +}); +stream.prependListener('downloadProgress', (p) => { + progress = p; +}); +stream.prependListener('uploadProgress', (p) => { + progress = p; +}); + +stream.prependOnceListener('request', (r) => req = r); +stream.prependOnceListener('response', (r) => res = r); +stream.prependOnceListener('redirect', (r, o) => { + res = r; + opts = o; + href = o.href; +}); +stream.prependOnceListener('error', (e, b, r) => { + err = e; + res = r; +}); +stream.prependOnceListener('downloadProgress', (p) => { + progress = p; +}); +stream.prependOnceListener('uploadProgress', (p) => { + progress = p; +}); + +stream.removeListener('request', (r) => req = r); +stream.removeListener('response', (r) => res = r); +stream.removeListener('redirect', (r, o) => { + res = r; + opts = o; + href = o.href; +}); +stream.removeListener('error', (e, b, r) => { + err = e; + res = r; +}); +stream.removeListener('downloadProgress', (p) => { + progress = p; +}); +stream.removeListener('uploadProgress', (p) => { + progress = p; +}); + +got('google.com', { + headers: { + cookie: cookie.serialize('foo', 'bar') + } +}); + +const form = new FormData(); + +form.append('my_file', fs.createReadStream('/foo/bar.jpg')); + +got.post('google.com', { + body: form +}); + +got('todomvc.com', { + headers: { + 'user-agent': `my-module/ (https://github.com/username/my-module)` + } +}); + +got('https://httpbin.org/404') + .catch(err => err instanceof got.HTTPError && err.statusCode === 404); + +got('todomvc', { + throwHttpErrors: false +}); + +got('todomvc', { + agent: { + http: new http.Agent(), + https: new https.Agent() + } +}); + +got('todomvc', { + cache: new Map(), +}).then(res => res.fromCache); + +got('todomvc', { + cache: new Keyv(), +}).then(res => res.fromCache); + +got('todomvc', { + cache: new QuickLRU(), +}).then(res => res.fromCache); + +got(new url.URL('http://todomvc.com')); + +got(url.parse('http://todomvc.com')); + +got('https://todomvc.com', { rejectUnauthorized: false }); diff --git a/types/got/v8/index.d.ts b/types/got/v8/index.d.ts new file mode 100644 index 0000000000..775985e581 --- /dev/null +++ b/types/got/v8/index.d.ts @@ -0,0 +1,208 @@ +// Type definitions for got 8.3 +// Project: https://github.com/sindresorhus/got#readme +// Definitions by: BendingBender +// Linus Unnebäck +// Konstantin Ikonnikov +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +import { Url, URL } from 'url'; +import * as http from 'http'; +import * as https from 'https'; +import * as nodeStream from 'stream'; + +export = got; + +declare class RequestError extends StdError { + name: 'RequestError'; +} + +declare class ReadError extends StdError { + name: 'ReadError'; +} + +declare class ParseError extends StdError { + name: 'ParseError'; + statusCode: number; + statusMessage: string; +} + +declare class HTTPError extends StdError { + name: 'HTTPError'; + statusCode: number; + statusMessage: string; + headers: http.IncomingHttpHeaders; +} + +declare class MaxRedirectsError extends StdError { + name: 'MaxRedirectsError'; + statusCode: number; + statusMessage: string; + redirectUrls: string[]; +} + +declare class UnsupportedProtocolError extends StdError { + name: 'UnsupportedProtocolError'; +} + +declare class CancelError extends StdError { + name: 'CancelError'; +} + +declare class StdError extends Error { + code?: string; + host?: string; + hostname?: string; + method?: string; + path?: string; + protocol?: string; + url?: string; + response?: any; +} + +declare const got: got.GotFn & + Record<'get' | 'post' | 'put' | 'patch' | 'head' | 'delete', got.GotFn> & + { + stream: got.GotStreamFn & Record<'get' | 'post' | 'put' | 'patch' | 'head' | 'delete', got.GotStreamFn>; + RequestError: typeof RequestError; + ReadError: typeof ReadError; + ParseError: typeof ParseError; + HTTPError: typeof HTTPError; + MaxRedirectsError: typeof MaxRedirectsError; + UnsupportedProtocolError: typeof UnsupportedProtocolError; + CancelError: typeof CancelError; + }; + +interface InternalRequestOptions extends https.RequestOptions { + // Redeclare options with `any` type for allow specify types incompatible with http.RequestOptions. + timeout?: any; + agent?: any; +} + +declare namespace got { + interface GotFn { + (url: GotUrl): GotPromise; + (url: GotUrl, options: GotJSONOptions): GotPromise; + (url: GotUrl, options: GotFormOptions): GotPromise; + (url: GotUrl, options: GotFormOptions): GotPromise; + (url: GotUrl, options: GotBodyOptions): GotPromise; + (url: GotUrl, options: GotBodyOptions): GotPromise; + } + + type GotStreamFn = (url: GotUrl, options?: GotOptions) => GotEmitter & nodeStream.Duplex; + + type GotUrl = string | https.RequestOptions | Url | URL; + + interface GotBodyOptions extends GotOptions { + body?: string | Buffer | nodeStream.Readable; + } + + interface GotJSONOptions extends GotOptions { + // Body must be an object or array. See https://github.com/sindresorhus/got/issues/511 + body?: object; + form?: boolean; + json: true; + } + + interface GotFormOptions extends GotOptions { + body?: {[key: string]: any}; + form: true; + json?: boolean; + } + + interface GotOptions extends InternalRequestOptions { + encoding?: E; + query?: string | object; + timeout?: number | TimeoutOptions; + retries?: number | RetryFunction; + followRedirect?: boolean; + decompress?: boolean; + useElectronNet?: boolean; + cache?: Cache; + agent?: http.Agent | boolean | AgentOptions; + throwHttpErrors?: boolean; + } + + interface TimeoutOptions { + connect?: number; + socket?: number; + request?: number; + } + + interface AgentOptions { + http: http.Agent; + https: https.Agent; + } + + type RetryFunction = (retry: number, error: any) => number; + + interface Cache { + set(key: string, value: any, ttl?: number): any; + get(key: string): any; + delete(key: string): any; + } + + interface Response extends http.IncomingMessage { + body: B; + url: string; + requestUrl: string; + fromCache: boolean; + redirectUrls?: string[]; + } + + type GotPromise = Promise> & { cancel(): void }; + + interface GotEmitter { + addListener(event: 'request', listener: (req: http.ClientRequest) => void): this; + addListener(event: 'response', listener: (res: http.IncomingMessage) => void): this; + addListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions & Url) => void): this; + addListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this; + addListener(event: 'downloadProgress', listener: (progress: Progress) => void): this; + addListener(event: 'uploadProgress', listener: (progress: Progress) => void): this; + + on(event: 'request', listener: (req: http.ClientRequest) => void): this; + on(event: 'response', listener: (res: http.IncomingMessage) => void): this; + on(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions & Url) => void): this; + on(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this; + on(event: 'downloadProgress', listener: (progress: Progress) => void): this; + on(event: 'uploadProgress', listener: (progress: Progress) => void): this; + + once(event: 'request', listener: (req: http.ClientRequest) => void): this; + once(event: 'response', listener: (res: http.IncomingMessage) => void): this; + once(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions & Url) => void): this; + once(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this; + once(event: 'downloadProgress', listener: (progress: Progress) => void): this; + once(event: 'uploadProgress', listener: (progress: Progress) => void): this; + + prependListener(event: 'request', listener: (req: http.ClientRequest) => void): this; + prependListener(event: 'response', listener: (res: http.IncomingMessage) => void): this; + prependListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions & Url) => void): this; + prependListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this; + prependListener(event: 'downloadProgress', listener: (progress: Progress) => void): this; + prependListener(event: 'uploadProgress', listener: (progress: Progress) => void): this; + + prependOnceListener(event: 'request', listener: (req: http.ClientRequest) => void): this; + prependOnceListener(event: 'response', listener: (res: http.IncomingMessage) => void): this; + prependOnceListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions & Url) => void): this; + prependOnceListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this; + prependOnceListener(event: 'downloadProgress', listener: (progress: Progress) => void): this; + prependOnceListener(event: 'uploadProgress', listener: (progress: Progress) => void): this; + + removeListener(event: 'request', listener: (req: http.ClientRequest) => void): this; + removeListener(event: 'response', listener: (res: http.IncomingMessage) => void): this; + removeListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions & Url) => void): this; + removeListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this; + removeListener(event: 'downloadProgress', listener: (progress: Progress) => void): this; + removeListener(event: 'uploadProgress', listener: (progress: Progress) => void): this; + } + + type GotError = RequestError | ReadError | ParseError | HTTPError | MaxRedirectsError | UnsupportedProtocolError | CancelError; + + interface Progress { + percent: number; + transferred: number; + total: number | null; + } +} diff --git a/types/got/v8/tsconfig.json b/types/got/v8/tsconfig.json new file mode 100644 index 0000000000..a5ffba23b0 --- /dev/null +++ b/types/got/v8/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "got": [ + "got/v8" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "got-tests.ts" + ] +} diff --git a/types/got/v8/tslint.json b/types/got/v8/tslint.json new file mode 100644 index 0000000000..08b1465cd6 --- /dev/null +++ b/types/got/v8/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "unified-signatures": false + } +}