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
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,14 @@ Default: `'default'`

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

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.
Parsing method used to retrieve the body from the response.

- `'default'` - if `options.encoding` is `null`, the body will be a Buffer. Otherwise it will be a string unless it's overwritten in a `afterResponse` hook,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the null thing to get buffer as the user can just do responseType: 'buffer' instead? Which is also much clearer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll send a PR.

- `'text'` - will always give a string, no matter what's the `options.encoding` or if the body is a custom object,
- `'json'` - will always give an object, unless it's invalid JSON - then it will throw.
- `'buffer'` - will always give a Buffer, no matter what's the `options.encoding`. It will throw if the body is a custom object.

The promise has `.json()` and `.buffer()` and `.text()` functions which set this option automatically.

Example:

Expand Down
10 changes: 5 additions & 5 deletions source/as-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import requestAsEventEmitter, {proxyEvents} from './request-as-event-emitter';
import {normalizeArguments, mergeOptions} from './normalize-arguments';

const parseBody = (body: Response['body'], responseType: NormalizedOptions['responseType'], statusCode: Response['statusCode']) => {
if (responseType === 'json') {
if (responseType === 'json' && is.string(body)) {
return statusCode === 204 ? '' : JSON.parse(body);
}

if (responseType === 'buffer') {
if (responseType === 'buffer' && is.string(body)) {
return Buffer.from(body);
}

if (responseType === 'text') {
return body.toString();
return String(body);
}

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

throw new Error(`Failed to parse body of type '${responseType}'`);
throw new Error(`Failed to parse body of type '${typeof body}' as '${responseType}'`);
};

export default function asPromise(options: NormalizedOptions) {
const proxy = new EventEmitter();
let finalResponse: Pick<Response, 'body' | 'statusCode'>;

// @ts-ignore `.json()`, `.buffer()` and `.text()` are added later
const promise = new PCancelable<IncomingMessage>((resolve, reject, onCancel) => {
const promise = new PCancelable<IncomingMessage | Response['body']>((resolve, reject, onCancel) => {
const emitter = requestAsEventEmitter(options);
onCancel(emitter.abort);

Expand Down
22 changes: 11 additions & 11 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type HTTPAlias =
| 'head'
| 'delete';

export type ReturnStream = (url: URLOrOptions | Options & {isStream: true}, options?: Options & {isStream: true}) => ProxyStream;
export type ReturnStream = (url: string | 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);
Expand All @@ -33,22 +33,22 @@ type OptionsOfDefaultResponseBody = Options & {isStream?: false; resolveBodyOnly
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};
type ResponseBodyOnly = {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: string | OptionsOfDefaultResponseBody, options?: OptionsOfDefaultResponseBody): CancelableRequest<Response>;
(url: string | OptionsOfTextResponseBody, options?: OptionsOfTextResponseBody): CancelableRequest<Response<string>>;
(url: string | OptionsOfJSONResponseBody, options?: OptionsOfJSONResponseBody): CancelableRequest<Response<object>>;
(url: string | 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>;
(url: string | OptionsOfDefaultResponseBody & ResponseBodyOnly, options?: OptionsOfDefaultResponseBody & ResponseBodyOnly): CancelableRequest<any>;
(url: string | OptionsOfTextResponseBody & ResponseBodyOnly, options?: OptionsOfTextResponseBody & ResponseBodyOnly): CancelableRequest<string>;
(url: string | OptionsOfJSONResponseBody & ResponseBodyOnly, options?: OptionsOfJSONResponseBody & ResponseBodyOnly): CancelableRequest<object>;
(url: string | OptionsOfBufferResponseBody & ResponseBodyOnly, options?: OptionsOfBufferResponseBody & ResponseBodyOnly): CancelableRequest<Buffer>;

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

export interface Got extends Merge<Record<HTTPAlias, GotFunctions>, GotFunctions> {
Expand Down
2 changes: 1 addition & 1 deletion source/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type ErrorCode =

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

export interface Response<BodyType = any> extends http.IncomingMessage {
export interface Response<BodyType = unknown> extends http.IncomingMessage {
body: BodyType;
statusCode: number;

Expand Down
3 changes: 2 additions & 1 deletion test/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ test('does not remove user headers from `url` object argument', withServer, asyn
}
})).body;

t.is(headers.accept, 'application/json');
// TODO: The response is not typed, so we need to cast as any
t.is((headers as any).accept, 'application/json');
t.is(headers['user-agent'], 'got (https://github.com/sindresorhus/got)');
t.is(headers['accept-encoding'], supportsBrotli ? 'gzip, deflate, br' : 'gzip, deflate');
t.is(headers['x-request-id'], 'value');
Expand Down
2 changes: 1 addition & 1 deletion test/response-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('works if promise has been already resolved', withServer, async (t, server,
test('throws an error on invalid response type', withServer, async (t, server, got) => {
server.get('/', defaultHandler);

const error = await t.throwsAsync(got({responseType: 'invalid'}), /^Failed to parse body of type 'invalid'/);
const error = await t.throwsAsync(got({responseType: 'invalid'}), /^Failed to parse body of type 'string\' as 'invalid'/);
// @ts-ignore
t.true(error.message.includes(error.options.url.hostname));
// @ts-ignore
Expand Down