Skip to content

Commit

Permalink
fix(adapter-fetch): Fix "failed to construct Request" issue (#287)
Browse files Browse the repository at this point in the history
* fix(adapter-fetch): Fix "failed to construct Request" issue

Resolves #286.
  • Loading branch information
offirgolan authored Dec 25, 2019
1 parent db9f78a commit d17ab9b
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,50 @@ export default class FetchAdapter extends Adapter {
this.nativeFetch = context.fetch;
this.NativeRequest = context.Request;

const NativeRequest = this.NativeRequest;

/*
Patch the Request class so we can store all the passed in options. This
allows us the access the `body` directly instead of having to do
Patch the Request constructor so we can store all the passed in options.
This allows us to access the `body` directly instead of having to do
`await req.blob()` as well as not having to hard code each option we want
to extract from the Request instance.
*/
class ExtendedRequest extends context.Request {
constructor(url, options) {
super(url, options);

let args;

options = options || {};

/*
The Request constructor can receive another Request instance as
the first argument so we use its arguments and merge it with the
new options.
*/
if (url instanceof ExtendedRequest) {
const reqArgs = url[REQUEST_ARGUMENTS];
context.Request = function Request(url, options) {
const request = new NativeRequest(url, options);
let args;

options = options || {};

/*
The Request constructor can receive another Request instance as
the first argument so we use its arguments and merge it with the
new options.
*/
if (typeof url === 'object' && url[REQUEST_ARGUMENTS]) {
const reqArgs = url[REQUEST_ARGUMENTS];

args = { ...reqArgs, options: { ...reqArgs.options, ...options } };
} else {
args = { url, options };
}

args = { ...reqArgs, options: { ...reqArgs.options, ...options } };
} else {
args = { url, options };
}
defineProperty(request, REQUEST_ARGUMENTS, { value: args });

defineProperty(this, REQUEST_ARGUMENTS, { value: args });
}
// Override the clone method to use our overridden constructor
request.clone = function clone() {
return new context.Request(request);
};

clone() {
return new ExtendedRequest(this);
}
}
return request;
};

context.Request = ExtendedRequest;
defineProperty(context.Request, IS_STUBBED, { value: true });

context.fetch = (url, options = {}) => {
let respond;

// Support Request object
if (url instanceof ExtendedRequest) {
if (typeof url === 'object' && url[REQUEST_ARGUMENTS]) {
const req = url;
const reqArgs = req[REQUEST_ARGUMENTS];

Expand Down

0 comments on commit d17ab9b

Please sign in to comment.