Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions types/got/got-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import got = require('got');
import cookie = require('cookie');
import FormData = require('form-data');
import * as fs from 'fs';
import * as http from 'http';

let str: string;
let buf: Buffer;
let obj: object;

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 => obj = response.body);
got('todomvc.com', {json: true, body: {}}).then(response => obj = response.body);
got('todomvc.com', {json: true, body: [{}]}).then(response => obj = response.body);
got('todomvc.com', {json: true, form: true}).then(response => obj = response.body);
got('todomvc.com', {json: true, form: true, encoding: null}).then(response => obj = response.body);
got('todomvc.com', {json: true, form: true, encoding: null, hostname: 'todomvc'}).then(response => obj = 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: [{}],
encoding: 'utf8',
hostname: 'todomvc'
}).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;

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;
});
stream.addListener('error', (e, b, r) => {
err = e;
res = r;
});

stream.on('request', (r) => req = r);
stream.on('response', (r) => res = r);
stream.on('redirect', (r, o) => {
res = r;
opts = o;
});
stream.on('error', (e, b, r) => {
err = e;
res = r;
});

stream.once('request', (r) => req = r);
stream.once('response', (r) => res = r);
stream.once('redirect', (r, o) => {
res = r;
opts = o;
});
stream.once('error', (e, b, r) => {
err = e;
res = r;
});

stream.prependListener('request', (r) => req = r);
stream.prependListener('response', (r) => res = r);
stream.prependListener('redirect', (r, o) => {
res = r;
opts = o;
});
stream.prependListener('error', (e, b, r) => {
err = e;
res = r;
});

stream.prependOnceListener('request', (r) => req = r);
stream.prependOnceListener('response', (r) => res = r);
stream.prependOnceListener('redirect', (r, o) => {
res = r;
opts = o;
});
stream.prependOnceListener('error', (e, b, r) => {
err = e;
res = r;
});

stream.removeListener('request', (r) => req = r);
stream.removeListener('response', (r) => res = r);
stream.removeListener('redirect', (r, o) => {
res = r;
opts = o;
});
stream.removeListener('error', (e, b, r) => {
err = e;
res = r;
});

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)`
}
});
155 changes: 155 additions & 0 deletions types/got/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Type definitions for got 7.1
// Project: https://github.com/sindresorhus/got#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2

/// <reference types="node"/>

import * as http from 'http';
import * as nodeStream from 'stream';

export = got;

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>
};

declare namespace got {
// tslint:disable unified-signatures
interface GotFn {
(url: GotUrl): GotPromise<string>;
(url: GotUrl, options: GotJSONOptions): GotPromise<object>;
(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>;
}
// tslint:disable unified-signatures

type GotStreamFn = (url: GotUrl, options?: GotOptions<string | null>) => GotEmitter & nodeStream.Duplex;

type GotUrl = string | http.RequestOptions | URL;

interface GotBodyOptions<E extends string | null> extends GotOptions<E> {
body?: string | Buffer | nodeStream.Readable;
}

interface GotJSONOptions extends GotOptions<string | null> {
body?: object;
form?: boolean;
json: true;
}

interface GotFormOptions<E extends string | null> extends GotOptions<E> {
body?: {[key: string]: any};
form: true;
json?: boolean;
}

type GotOptions<E extends string | null> = http.RequestOptions & {
encoding?: E;
query?: string | object;
timeout?: number | TimeoutOptions;
retries?: number | RetryFunction;
followRedirect?: boolean;
decompress?: boolean;
useElectronNet?: boolean;
};

interface TimeoutOptions {
connect?: number;
socket?: number;
request?: number;
}

type RetryFunction = (retry: number, error: any) => number;

interface Response<B extends Buffer | string | object> extends http.IncomingMessage {
body: B;
url: string;
requestUrl: string;
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>) => void): this;
addListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => 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>) => void): this;
on(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => 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>) => void): this;
once(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => 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>) => void): this;
prependListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => 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>) => void): this;
prependOnceListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => 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>) => void): this;
removeListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this;
}

type GotError = RequestError | ReadError | ParseError | HTTPError | MaxRedirectsError | UnsupportedProtocolError;

interface RequestError extends StdError {
name: 'RequestError';
}

interface ReadError extends StdError {
name: 'ReadError';
}

interface ParseError extends StdError {
name: 'ParseError';
statusCode: number;
statusMessage: string;
}

interface HTTPError extends StdError {
name: 'HTTPError';
statusCode: number;
statusMessage: string;
headers: http.IncomingHttpHeaders;
}

interface MaxRedirectsError extends StdError {
name: 'MaxRedirectsError';
statusCode: number;
statusMessage: string;
redirectUrls: string[];
}

interface UnsupportedProtocolError extends StdError {
name: 'UnsupportedProtocolError';
}

interface StdError extends Error {
code?: string;
host?: string;
hostname?: string;
method?: string;
path?: string;
protocol?: string;
url?: string;
response?: any;
}
}
23 changes: 23 additions & 0 deletions types/got/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"got-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/got/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }