Merge pull request #29710 from stijnvn/got

@types/got: update options
This commit is contained in:
Daniel Rosenwasser
2018-10-30 12:50:17 -07:00
committed by GitHub
8 changed files with 544 additions and 9 deletions
+7 -2
View File
@@ -14,10 +14,15 @@
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"paths": {
"got": [
"got/v8"
]
}
},
"files": [
"index.d.ts",
"download-tests.ts"
]
}
}
+9
View File
@@ -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']} });
+23 -6
View File
@@ -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 <https://github.com/BendingBender>
// Linus Unnebäck <https://github.com/LinusU>
// Konstantin Ikonnikov <https://github.com/ikokostya>
// Stijn Van Nieuwenhuyse <https://github.com/stijnvn>
// 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<T> = (options: T) => any;
type Hooks<T> = Record<'beforeRequest', Array<Hook<T>>>;
interface GotBodyOptions<E extends string | null> extends GotOptions<E> {
body?: string | Buffer | nodeStream.Readable;
hooks?: Hooks<GotBodyOptions<E>>;
}
interface GotJSONOptions extends GotOptions<string | null> {
@@ -104,25 +110,29 @@ declare namespace got {
body?: object;
form?: boolean;
json: true;
hooks?: Hooks<GotJSONOptions>;
}
interface GotFormOptions<E extends string | null> extends GotOptions<E> {
body?: {[key: string]: any};
form: true;
json?: boolean;
hooks?: Hooks<GotFormOptions<E>>;
}
interface GotOptions<E extends string | null> 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;
-1
View File
@@ -1,7 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
// TODO
"unified-signatures": false
}
}
+262
View File
@@ -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<string | null>;
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 });
+208
View File
@@ -0,0 +1,208 @@
// Type definitions for got 8.3
// Project: https://github.com/sindresorhus/got#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Linus Unnebäck <https://github.com/LinusU>
// Konstantin Ikonnikov <https://github.com/ikokostya>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="node"/>
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<string>;
(url: GotUrl, options: GotJSONOptions): GotPromise<any>;
(url: GotUrl, options: GotFormOptions<string>): GotPromise<string>;
(url: GotUrl, options: GotFormOptions<null>): GotPromise<Buffer>;
(url: GotUrl, options: GotBodyOptions<string>): GotPromise<string>;
(url: GotUrl, options: GotBodyOptions<null>): GotPromise<Buffer>;
}
type GotStreamFn = (url: GotUrl, options?: GotOptions<string | null>) => GotEmitter & nodeStream.Duplex;
type GotUrl = string | https.RequestOptions | Url | URL;
interface GotBodyOptions<E extends string | null> extends GotOptions<E> {
body?: string | Buffer | nodeStream.Readable;
}
interface GotJSONOptions extends GotOptions<string | null> {
// Body must be an object or array. See https://github.com/sindresorhus/got/issues/511
body?: object;
form?: boolean;
json: true;
}
interface GotFormOptions<E extends string | null> extends GotOptions<E> {
body?: {[key: string]: any};
form: true;
json?: boolean;
}
interface GotOptions<E extends string | null> 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<B extends Buffer | string | object> extends http.IncomingMessage {
body: B;
url: string;
requestUrl: string;
fromCache: boolean;
redirectUrls?: string[];
}
type GotPromise<B extends Buffer | string | object> = Promise<Response<B>> & { 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<string | null> & 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<string | null> & 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<string | null> & 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<string | null> & 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<string | null> & 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<string | null> & 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;
}
}
+29
View File
@@ -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"
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"unified-signatures": false
}
}