Skip to content

Commit

Permalink
fix(adapter-fetch): Fix "failed to construct Request" issue
Browse files Browse the repository at this point in the history
Resolves #286.
  • Loading branch information
offirgolan committed Dec 25, 2019
1 parent db9f78a commit c10c44a
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,57 @@ export default class FetchAdapter extends Adapter {
);
this.assert(
'Running concurrent fetch adapters is unsupported, stop any running Polly instances.',
!context.fetch[IS_STUBBED] && !context.Request[IS_STUBBED]
!context.fetch[IS_STUBBED] &&
!context.Request.prototype.constructor[IS_STUBBED]
);

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 c10c44a

Please sign in to comment.