Merge pull request #32602 from wingsbob/got-extend

Add support for `got.extend`
This commit is contained in:
Armando Aguirre
2019-04-15 12:52:20 -07:00
committed by GitHub
2 changed files with 95 additions and 38 deletions

View File

@@ -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<any>
got.extend({ json: true })('/example').then(({ body }) => body);
// $ExpectType Promise<any>
got.extend({ baseUrl: 'https://localhost' }).extend({ json: true })('/example').then(({ body }) => body);
// $ExpectType Promise<string>
got.extend({})('/example').then(({ body }) => body);
// Form options:
// $ExpectType Promise<Buffer>
got.extend({ form: true, encoding: null })('/example').then(({ body }) => body);
// $ExpectType Promise<Buffer>
got.extend({ form: true, encoding: null, body: {} })('/example').then(({ body }) => body);
// $ExpectType Promise<string>
got.extend({ form: true, encoding: 'utf8' })('/example').then(({ body }) => body);
// $ExpectType Promise<string>
got.extend({ form: true, encoding: 'utf8', body: {} })('/example').then(({ body }) => body);
// Body options:
// $ExpectType Promise<Buffer>
got.extend({ encoding: null })('/example').then(({ body }) => body);
// $ExpectType Promise<Buffer>
got.extend({ encoding: null, body: '{}' })('/example').then(({ body }) => body);
// $ExpectType Promise<string>
got.extend({ encoding: 'utf8' })('/example').then(({ body }) => body);
// $ExpectType Promise<string>
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', {

55
types/got/index.d.ts vendored
View File

@@ -4,8 +4,9 @@
// Linus Unnebäck <https://github.com/LinusU>
// Konstantin Ikonnikov <https://github.com/ikokostya>
// Stijn Van Nieuwenhuyse <https://github.com/stijnvn>
// Matthew Bull <https://github.com/wingsbob>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// TypeScript Version: 2.8
/// <reference types="node"/>
@@ -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<null>): GotPromise<Buffer>;
}
interface GotJSONFn {
(url: GotUrl): GotPromise<any>;
(url: GotUrl, options: GotJSONOptions): GotPromise<any>;
}
interface GotFormFn<T extends string | null> {
(url: GotUrl): GotPromise<T extends null ? Buffer : string>;
(url: GotUrl, options: GotFormOptions<T>): GotPromise<T extends null ? Buffer : string>;
}
interface GotBodyFn<T extends string | null> {
(url: GotUrl): GotPromise<T extends null ? Buffer : string>;
(url: GotUrl, options: GotBodyOptions<T>): GotPromise<T extends null ? Buffer : string>;
}
type GotInstance<T = GotFn> = 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<GotJSONFn>;
(options: GotFormOptions<string>): GotInstance<GotFormFn<string>>;
(options: GotFormOptions<null>): GotInstance<GotFormFn<null>>;
(options: GotBodyOptions<string>): GotInstance<GotBodyFn<string>>;
(options: GotBodyOptions<null>): GotInstance<GotBodyFn<null>>;
}
type GotStreamFn = (url: GotUrl, options?: GotOptions<string | null>) => GotEmitter & nodeStream.Duplex;
type GotUrl = string | https.RequestOptions | Url | URL;