Skip to content

Commit

Permalink
Add allowGetBody options
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Feb 23, 2020
1 parent c7cd063 commit 7a0cae4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export interface Options extends Omit<RequestOptions, 'agent' | 'timeout' | 'pat
username?: string;
password?: string;
http2?: boolean;
allowGetBody?: boolean;
}

export interface NormalizedOptions extends Options {
Expand All @@ -141,6 +142,7 @@ export interface NormalizedOptions extends Options {
dnsCache?: CacheableLookup;
cacheableRequest?: (options: string | URL | http.RequestOptions, callback?: (response: http.ServerResponse | ResponseLike) => void) => CacheableRequest.Emitter;
http2: boolean;
allowGetBody: boolean;
[kRequest]: HttpRequestFunction;
[kIsNormalizedAlready]?: boolean;
}
Expand All @@ -161,6 +163,7 @@ export interface Defaults {
cache?: string | CacheableRequest.StorageAdapter;
throwHttpErrors: boolean;
http2: boolean;
allowGetBody: boolean;
}

export interface Progress {
Expand Down Expand Up @@ -544,6 +547,7 @@ export class Request extends Duplex implements RequestEvents<Request> {
assert.any([is.number, is.undefined], options.maxRedirects);
assert.any([is.boolean, is.undefined], options.throwHttpErrors);
assert.any([is.boolean, is.undefined], options.http2);
assert.any([is.boolean, is.undefined], options.allowGetBody);

if (!('followRedirects' in options) && 'followRedirect' in options) {
options.followRedirects = options.followRedirect;
Expand All @@ -557,6 +561,7 @@ export class Request extends Duplex implements RequestEvents<Request> {
options.maxRedirects = options.maxRedirects || 0;
options.throwHttpErrors = Boolean(options.throwHttpErrors);
options.http2 = Boolean(options.http2);
options.allowGetBody = Boolean(options.allowGetBody);

return options;
}
Expand Down Expand Up @@ -655,7 +660,7 @@ export class Request extends Duplex implements RequestEvents<Request> {
const isBody = !is.undefined(options.body);
const hasPayload = isForm || isJSON || isBody;
if (hasPayload) {
if (options.method in withoutBody) {
if (options.method in withoutBody && !(options.method === 'GET' && options.allowGetBody)) {
throw new TypeError(`The \`${options.method}\` method cannot be used with a body`);
}

Expand Down
3 changes: 2 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const defaults: InstanceDefaults = {
methodRewriting: true,
ignoreInvalidCookies: false,
context: {},
http2: false
http2: false,
allowGetBody: false
},
handlers: [defaultHandler],
mutableDefaults: false
Expand Down

0 comments on commit 7a0cae4

Please sign in to comment.