Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Feb 29, 2020
1 parent cd4b8f5 commit 9361722
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
49 changes: 24 additions & 25 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,11 @@ const errors = {

const {normalizeArguments} = PromisableRequest;

export type HTTPAlias =
| 'get'
| 'post'
| 'put'
| 'patch'
| 'head'
| 'delete';

export type GotReturn = Request | CancelableRequest;

interface GotStreamFunction {
(url: string | URL, options?: Options & {isStream?: true}): Request;
(options?: Options & {isStream?: true}): Request;
}
export type HandlerFunction = <T extends GotReturn>(options: NormalizedOptions, next: (options: NormalizedOptions) => T) => T | Promise<T>;

const getPromiseOrStream = (options: NormalizedOptions): GotReturn => options.isStream ? new Request(options.url, options) : asPromise(options);

export type HandlerFunction = <T extends GotReturn>(options: NormalizedOptions, next: (options: NormalizedOptions) => T) => T | Promise<T>;

export interface ExtendOptions extends Options {
handlers?: HandlerFunction[];
mutableDefaults?: boolean;
Expand All @@ -79,7 +65,7 @@ type Except<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Ex
export type OptionsOfTextResponseBody = Options & {isStream?: false; resolveBodyOnly?: false; responseType?: 'text'};
export type OptionsOfJSONResponseBody = Options & {isStream?: false; resolveBodyOnly?: false; responseType: 'json'};
export type OptionsOfBufferResponseBody = Options & {isStream?: false; resolveBodyOnly?: false; responseType: 'buffer'};
export type GotStrictOptions = Except<Options, 'isStream' | 'responseType' | 'resolveBodyOnly'>;
export type StrictOptions = Except<Options, 'isStream' | 'responseType' | 'resolveBodyOnly'>;
type ResponseBodyOnly = {resolveBodyOnly: true};

export interface GotPaginate {
Expand Down Expand Up @@ -124,6 +110,28 @@ export interface GotRequest {
(options: Options): CancelableRequest | Request;
}

export type HTTPAlias =
| 'get'
| 'post'
| 'put'
| 'patch'
| 'head'
| 'delete';

const aliases: readonly HTTPAlias[] = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];

interface GotStreamFunction {
(url: string | URL, options?: Options & {isStream?: true}): Request;
(options?: Options & {isStream?: true}): Request;
}

export type GotStream = GotStreamFunction & Record<HTTPAlias, GotStreamFunction>;

export interface Got extends Record<HTTPAlias, GotRequest>, GotRequest {
Expand All @@ -144,15 +152,6 @@ export interface Got extends Record<HTTPAlias, GotRequest>, GotRequest {
mergeOptions(...sources: Options[]): NormalizedOptions;
}

const aliases: readonly HTTPAlias[] = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];

export const defaultHandler: HandlerFunction = (options, next) => next(options);

export const mergeOptions = (...sources: Options[]): NormalizedOptions => {
Expand Down
12 changes: 11 additions & 1 deletion test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {parse, URL, URLSearchParams} from 'url';
import test from 'ava';
import {Handler} from 'express';
import pEvent = require('p-event');
import got from '../source';
import got, {StrictOptions} from '../source';
import withServer from './helpers/with-server';

const echoUrl: Handler = (request, response) => {
Expand Down Expand Up @@ -409,3 +409,13 @@ test('fallbacks to native http if `request(...)` returns undefined', withServer,

t.is(body, '/');
});

test('strict options', withServer, async (t, server, got) => {
server.get('/', echoUrl);

const options: StrictOptions = {};

const {body} = await got(options);

t.is(body, '/');
});

0 comments on commit 9361722

Please sign in to comment.