Skip to content

Commit

Permalink
Fix XO errors
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Feb 23, 2020
1 parent 7a0cae4 commit 8302615
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 186 deletions.
9 changes: 5 additions & 4 deletions source/as-promise/core.ts
@@ -1,7 +1,7 @@
import {URL} from 'url';
import is, {assert} from '@sindresorhus/is';
import {Options, NormalizedOptions, Defaults} from './types';
import {Request, knownHookEvents} from '../core';
import Request, {knownHookEvents} from '../core';

if (!knownHookEvents.includes('beforeRetry' as any)) {
knownHookEvents.push('beforeRetry' as any, 'afterResponse' as any);
Expand All @@ -10,11 +10,12 @@ if (!knownHookEvents.includes('beforeRetry' as any)) {
export default class PromisableRequest extends Request {
['constructor']: typeof PromisableRequest;

static normalizeArguments(url?: string | URL, nonNormalizedOptions?: Options, defaults?: Defaults) {
static normalizeArguments(url?: string | URL, nonNormalizedOptions?: Options, defaults?: Defaults): NormalizedOptions {
const options = super.normalizeArguments(url, nonNormalizedOptions, defaults) as NormalizedOptions;

if (!('responseType' in options)) {
options!.responseType = 'text';
// @ts-ignore TypeScript bug - it says `options` is `never`
options.responseType = 'text';
}

assert.any([is.boolean, is.undefined], options.resolveBodyOnly);
Expand Down Expand Up @@ -74,7 +75,7 @@ export default class PromisableRequest extends Request {
if (!is.function_(pagination.shouldContinue)) {
throw new Error('`options._pagination.shouldContinue` must be implemented');
}
PromisableRequest

if (!is.function_(pagination.paginate)) {
throw new Error('`options._pagination.paginate` must be implemented');
}
Expand Down
15 changes: 10 additions & 5 deletions source/as-promise/index.ts
Expand Up @@ -14,7 +14,14 @@ import {
import PromisableRequest from './core';
import proxyEvents from '../utils/proxy-events';

export const knownBodyTypes = ['json', 'buffer', 'text'];

// @ts-ignore The error is: Not all code paths return a value.
const parseBody = (body: Buffer, responseType: NormalizedOptions['responseType'], encoding: NormalizedOptions['encoding']): unknown => {
if (responseType === 'text') {
return body.toString(encoding);
}

if (responseType === 'json') {
return body.length === 0 ? '' : JSON.parse(body.toString());
}
Expand All @@ -23,11 +30,9 @@ const parseBody = (body: Buffer, responseType: NormalizedOptions['responseType']
return Buffer.from(body);
}

if (responseType === 'text') {
return body.toString(encoding);
if (!knownBodyTypes.includes(responseType)) {
throw new TypeError(`Unknown body type '${responseType as string}'`);
}

throw new TypeError(`Unknown body type '${responseType as string}'`);
};

export default function asPromise<T>(options: NormalizedOptions): CancelableRequest<T> {
Expand All @@ -36,7 +41,7 @@ export default function asPromise<T>(options: NormalizedOptions): CancelableRequ
const emitter = new EventEmitter();

const promise = new PCancelable<T>((resolve, reject, onCancel) => {
const makeRequest = () => {
const makeRequest = (): void => {
const request = new PromisableRequest(options.url, options);
onCancel(request.destroy);

Expand Down

0 comments on commit 8302615

Please sign in to comment.