diff --git a/types/got/got-tests.ts b/types/got/got-tests.ts index 467211a87d..8aae7f300d 100644 --- a/types/got/got-tests.ts +++ b/types/got/got-tests.ts @@ -21,35 +21,35 @@ got('todomvc.com') got('todomvc.com').cancel(); -got('todomvc.com', {json: true}).then((response) => { +got('todomvc.com', { json: true }).then((response) => { response.body; // $ExpectType any }); -got('todomvc.com', {json: true, body: {}}).then((response) => { +got('todomvc.com', { json: true, body: {} }).then((response) => { response.body; // $ExpectType any }); -got('todomvc.com', {json: true, body: [{}]}).then((response) => { +got('todomvc.com', { json: true, body: [{}] }).then((response) => { response.body; // $ExpectType any }); -got('todomvc.com', {json: true, form: true}).then((response) => { +got('todomvc.com', { json: true, form: true }).then((response) => { response.body; // $ExpectType any }); -got('todomvc.com', {json: true, form: true, encoding: null}).then((response) => { +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) => { +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 }).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: [{}], @@ -68,21 +68,21 @@ got('todomvc.com', { body: [{}], encoding: 'utf8', hostname: 'todomvc', - timeout: {connect: 20, request: 20, socket: 20} + 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', { 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('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.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')); @@ -258,9 +258,39 @@ 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', { headers: { foo: 'bar' } }); got('http://todomvc.com', { cookieJar: new tough.CookieJar() }); +// Test extension +got.extend({ method: 'POST' })('/example'); +got.extend({ method: 'POST' }).extend({ headers: {} }).stream('/example'); + +// JSON options: +// $ExpectType Promise +got.extend({ json: true })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({ baseUrl: 'https://localhost' }).extend({ json: true })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({})('/example').then(({ body }) => body); +// Form options: +// $ExpectType Promise +got.extend({ form: true, encoding: null })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({ form: true, encoding: null, body: {} })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({ form: true, encoding: 'utf8' })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({ form: true, encoding: 'utf8', body: {} })('/example').then(({ body }) => body); +// Body options: +// $ExpectType Promise +got.extend({ encoding: null })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({ encoding: null, body: '{}' })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({ encoding: 'utf8' })('/example').then(({ body }) => body); +// $ExpectType Promise +got.extend({ encoding: 'utf8', body: '{}' })('/example').then(({ body }) => body); + // Test retry options. got('http://todomvc.com', { retry: 2 }); got('http://todomvc.com', { @@ -284,7 +314,7 @@ got('http://todomvc.com', { got('http://todomvc.com', { throwHttpErrors: false }); // Test timeout options. -got('http://todomvc.com', {timeout: 1}); +got('http://todomvc.com', { timeout: 1 }); got('http://todomvc.com', { timeout: { lookup: 1, @@ -298,7 +328,7 @@ got('http://todomvc.com', { }); // Test got.TimeoutError. -got('http://todomvc.com', {timeout: 1}).catch((err) => err instanceof got.TimeoutError); +got('http://todomvc.com', { timeout: 1 }).catch((err) => err instanceof got.TimeoutError); // Test hooks. got('example.com', { diff --git a/types/got/index.d.ts b/types/got/index.d.ts index a9f289b831..8fb6a71e63 100644 --- a/types/got/index.d.ts +++ b/types/got/index.d.ts @@ -4,8 +4,9 @@ // Linus Unnebäck // Konstantin Ikonnikov // Stijn Van Nieuwenhuyse +// Matthew Bull // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.8 /// @@ -69,19 +70,7 @@ declare class StdError extends Error { 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; - TimeoutError: typeof TimeoutError; - }; +declare const got: got.GotInstance; interface InternalRequestOptions extends https.RequestOptions { // Redeclare options with `any` type for allow specify types incompatible with http.RequestOptions. @@ -99,6 +88,44 @@ declare namespace got { (url: GotUrl, options: GotBodyOptions): GotPromise; } + interface GotJSONFn { + (url: GotUrl): GotPromise; + (url: GotUrl, options: GotJSONOptions): GotPromise; + } + + interface GotFormFn { + (url: GotUrl): GotPromise; + (url: GotUrl, options: GotFormOptions): GotPromise; + } + + interface GotBodyFn { + (url: GotUrl): GotPromise; + (url: GotUrl, options: GotBodyOptions): GotPromise; + } + + type GotInstance = T & + Record<'get' | 'post' | 'put' | 'patch' | 'head' | 'delete', T> & + { + stream: GotStreamFn & Record<'get' | 'post' | 'put' | 'patch' | 'head' | 'delete', GotStreamFn>; + extend: GotExtend; + RequestError: typeof RequestError; + ReadError: typeof ReadError; + ParseError: typeof ParseError; + HTTPError: typeof HTTPError; + MaxRedirectsError: typeof MaxRedirectsError; + UnsupportedProtocolError: typeof UnsupportedProtocolError; + CancelError: typeof CancelError; + TimeoutError: typeof TimeoutError; + }; + + interface GotExtend { + (options: GotJSONOptions): GotInstance; + (options: GotFormOptions): GotInstance>; + (options: GotFormOptions): GotInstance>; + (options: GotBodyOptions): GotInstance>; + (options: GotBodyOptions): GotInstance>; + } + type GotStreamFn = (url: GotUrl, options?: GotOptions) => GotEmitter & nodeStream.Duplex; type GotUrl = string | https.RequestOptions | Url | URL;