Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major refactoring #921

Merged
merged 35 commits into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c29d4d3
init
szmarczak Nov 5, 2019
fcc18f8
Big big changes
szmarczak Nov 6, 2019
b803076
fixes
szmarczak Nov 7, 2019
3ceba12
Merge with master
szmarczak Nov 7, 2019
29fee34
remove unnecessary semicolon
szmarczak Nov 7, 2019
d5a2ff2
enhancements
szmarczak Nov 7, 2019
5236d84
rename stream to isStream
szmarczak Nov 7, 2019
c7dbe1e
throw on legacy url input
szmarczak Nov 7, 2019
f1f203a
enhancements
szmarczak Nov 10, 2019
461e8d9
bug fixes
szmarczak Nov 10, 2019
b044037
fixes
szmarczak Nov 10, 2019
22c36bf
fix option merge
szmarczak Nov 11, 2019
aedac8c
more bug fixes
szmarczak Nov 11, 2019
d7c7d53
fixes
szmarczak Nov 11, 2019
507a3cc
make tests pass
szmarczak Nov 11, 2019
9255df7
remove todo
szmarczak Nov 11, 2019
778cf67
Remove got.create() & update docs
szmarczak Nov 12, 2019
e8ff08b
update docs
szmarczak Nov 12, 2019
0841642
another fix
szmarczak Nov 12, 2019
fe76e8c
nitpick
szmarczak Nov 12, 2019
3def303
types
szmarczak Nov 12, 2019
695ebaf
generic cookiejar object
szmarczak Nov 12, 2019
52496df
make tests pass
szmarczak Nov 12, 2019
c6b22e1
Refactor the got() function, aka: fix bugs
szmarczak Nov 14, 2019
ef32a0e
Throw on null value headers
szmarczak Nov 14, 2019
dde0b76
bug fixes
szmarczak Nov 14, 2019
a0850bd
Improve is usage
szmarczak Nov 14, 2019
0d9baf1
remove useless line
szmarczak Nov 14, 2019
42b2605
comments
szmarczak Nov 14, 2019
3b313a7
call beforeRetry hook when retrying in afterResponse hook
szmarczak Nov 14, 2019
518c00d
nitpicks
szmarczak Nov 15, 2019
7f2f477
nitpicks
szmarczak Nov 15, 2019
5bc7319
nitpicks
szmarczak Nov 15, 2019
3ff5ebb
no unnecessary escape
szmarczak Nov 15, 2019
a7e73f2
Update readme.md
sindresorhus Nov 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ const instance = got.extend({
###### responseType

Type: `string`<br>
Default: `text`
Default: `'default'`
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

**Note:** When using streams, this option is ignored.

Parsing method used to retrieve the body from the response. Can be `text`, `json` or `buffer`. The promise has `.json()` and `.buffer()` and `.text()` functions which set this option automatically.
Parsing method used to retrieve the body from the response. Can be `'default'`, `'text'`, `'json'` or `'buffer'`. The promise has `.json()` and `.buffer()` and `.text()` functions which set this option automatically.

Example:

Expand Down
2 changes: 1 addition & 1 deletion source/as-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const parseBody = (body: Response['body'], responseType: NormalizedOptions['resp
return body.toString();
}

if (responseType === '') {
if (responseType === 'default') {
return body;
}

Expand Down
32 changes: 26 additions & 6 deletions source/create.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Merge} from 'type-fest';
import * as errors from './errors';
import {
Options,
Expand All @@ -23,13 +24,34 @@ export type HTTPAlias =
| 'head'
| 'delete';

export type ReturnResponse = (url: URLOrOptions | Options & {isStream?: false}, options?: Options & {isStream?: false}) => CancelableRequest<Response>;
export type ReturnStream = (url: URLOrOptions | Options & {isStream: true}, options?: Options & {isStream: true}) => ProxyStream;
export type GotReturn = ProxyStream | CancelableRequest<Response>;

const getPromiseOrStream = (options: NormalizedOptions): GotReturn => options.isStream ? asStream(options) : asPromise(options);

export interface Got extends Record<HTTPAlias, ReturnResponse> {
type OptionsOfDefaultResponseBody = Options & {isStream?: false; resolveBodyOnly?: false; responseType?: 'default'};
type OptionsOfTextResponseBody = Options & {isStream?: false; resolveBodyOnly?: false; responseType: 'text'};
type OptionsOfJSONResponseBody = Options & {isStream?: false; resolveBodyOnly?: false; responseType: 'json'};
type OptionsOfBufferResponseBody = Options & {isStream?: false; resolveBodyOnly?: false; responseType: 'buffer'};
type BodyOnly = {resolveBodyOnly: true};

interface GotFunctions {
// `asPromise` usage
(url: URLOrOptions | OptionsOfDefaultResponseBody, options?: OptionsOfDefaultResponseBody): CancelableRequest<Response>;
(url: URLOrOptions | OptionsOfTextResponseBody, options?: OptionsOfTextResponseBody): CancelableRequest<Response<string>>;
(url: URLOrOptions | OptionsOfJSONResponseBody, options?: OptionsOfJSONResponseBody): CancelableRequest<Response<object>>;
(url: URLOrOptions | OptionsOfBufferResponseBody, options?: OptionsOfBufferResponseBody): CancelableRequest<Response<Buffer>>;

(url: URLOrOptions | OptionsOfDefaultResponseBody & BodyOnly, options?: OptionsOfDefaultResponseBody & BodyOnly): CancelableRequest<any>;
(url: URLOrOptions | OptionsOfTextResponseBody & BodyOnly, options?: OptionsOfTextResponseBody & BodyOnly): CancelableRequest<string>;
(url: URLOrOptions | OptionsOfJSONResponseBody & BodyOnly, options?: OptionsOfJSONResponseBody & BodyOnly): CancelableRequest<object>;
(url: URLOrOptions | OptionsOfBufferResponseBody & BodyOnly, options?: OptionsOfBufferResponseBody & BodyOnly): CancelableRequest<Buffer>;

// `asStream` usage
(url: URLOrOptions | Options & {isStream: true}, options?: Options & {isStream: true}): ProxyStream;
}

export interface Got extends Merge<Record<HTTPAlias, GotFunctions>, GotFunctions> {
stream: GotStream;
defaults: Defaults | Readonly<Defaults>;
GotError: typeof errors.GotError;
Expand All @@ -43,9 +65,6 @@ export interface Got extends Record<HTTPAlias, ReturnResponse> {
TimeoutError: typeof errors.TimeoutError;
CancelError: typeof errors.CancelError;

(url: URLOrOptions | Options & {isStream?: false}, options?: Options & {isStream?: false}): CancelableRequest<Response>;
(url: URLOrOptions | Options & {isStream: true}, options?: Options & {isStream: true}): ProxyStream;
(url: URLOrOptions, options?: Options): CancelableRequest<Response> | ProxyStream;
extend(...instancesOrOptions: Array<Got | ExtendedOptions>): Got;
mergeInstances(parent: Got, ...instances: Got[]): Got;
mergeOptions<T extends Options>(...sources: T[]): T & {hooks: Partial<Hooks>};
Expand Down Expand Up @@ -149,7 +168,8 @@ const create = (defaults: Defaults): Got => {
got.stream = (url, options) => got(url, {...options, isStream: true});

for (const method of aliases) {
got[method] = (url, options) => got(url, {...options, method});
// @ts-ignore
got[method] = (url: URLOrOptions, options?: Options): GotReturn => got(url, {...options, method});
got.stream[method] = (url, options) => got.stream(url, {...options, method});
}

Expand Down
3 changes: 1 addition & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const defaults: Defaults = {
cache: false,
dnsCache: false,
useElectronNet: false,
responseType: '',
responseType: 'default',
resolveBodyOnly: false,
maxRedirects: 10,
prefixUrl: ''
Expand All @@ -76,7 +76,6 @@ export * from './utils/types';
export {
Got,
GotStream,
ReturnResponse,
ReturnStream,
GotReturn
} from './create';
Expand Down
14 changes: 14 additions & 0 deletions source/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ClientRequest} from 'http';
import {Transform as TransformStream} from 'stream';
import {Socket} from 'net';
import EventEmitter = require('events');
import is from '@sindresorhus/is';

export function downloadProgress(emitter: EventEmitter, downloadBodySize?: number): TransformStream {
let downloadedBytes = 0;
Expand Down Expand Up @@ -71,6 +72,19 @@ export function uploadProgress(request: ClientRequest, emitter: EventEmitter, up
const onSocketConnect = (): void => {
progressInterval = setInterval(() => {
const lastUploadedBytes = uploadedBytes;

/* istanbul ignore next: future versions of Node may not have this property */
if (!is.string((request as any)._header)) {
clearInterval(progressInterval);

const url = new URL('https://github.com/sindresorhus/got/issues/new');
url.searchParams.set('title', '`request._header` is not present');
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
url.searchParams.set('body', 'It causes `uploadProgress` to fail.');

console.warn('`request._header` is not present. Please report this as a bug:\n' + url.href);
return;
}

const headersSize = Buffer.byteLength((request as any)._header);
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
uploadedBytes = socket.bytesWritten - headersSize;

Expand Down
6 changes: 3 additions & 3 deletions source/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export type ErrorCode =
| 'ENETUNREACH'
| 'EAI_AGAIN';

export type ResponseType = 'json' | 'buffer' | 'text' | '';
export type ResponseType = 'json' | 'buffer' | 'text' | 'default';

export interface Response extends http.IncomingMessage {
body: any;
export interface Response<BodyType = any> extends http.IncomingMessage {
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
body: BodyType;
statusCode: number;

/**
Expand Down