You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
217 lines
5.4 KiB
217 lines
5.4 KiB
'use strict';
|
|
|
|
var _regenerator = require('babel-runtime/regenerator');
|
|
|
|
var _regenerator2 = _interopRequireDefault(_regenerator);
|
|
|
|
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
|
|
|
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
var responseBuilder = require('./response-builder');
|
|
var requestUtils = require('./request-utils');
|
|
var FetchMock = {};
|
|
|
|
var resolve = function () {
|
|
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(response, url, options, request) {
|
|
return _regenerator2.default.wrap(function _callee$(_context) {
|
|
while (1) {
|
|
switch (_context.prev = _context.next) {
|
|
case 0:
|
|
if (!(typeof response === 'function' || typeof response.then === 'function')) {
|
|
_context.next = 10;
|
|
break;
|
|
}
|
|
|
|
if (!(typeof response === 'function')) {
|
|
_context.next = 5;
|
|
break;
|
|
}
|
|
|
|
response = response(url, options, request);
|
|
_context.next = 8;
|
|
break;
|
|
|
|
case 5:
|
|
_context.next = 7;
|
|
return response;
|
|
|
|
case 7:
|
|
response = _context.sent;
|
|
|
|
case 8:
|
|
_context.next = 0;
|
|
break;
|
|
|
|
case 10:
|
|
return _context.abrupt('return', response);
|
|
|
|
case 11:
|
|
case 'end':
|
|
return _context.stop();
|
|
}
|
|
}
|
|
}, _callee, undefined);
|
|
}));
|
|
|
|
return function resolve(_x, _x2, _x3, _x4) {
|
|
return _ref.apply(this, arguments);
|
|
};
|
|
}();
|
|
|
|
FetchMock.fetchHandler = function (url, options, request) {
|
|
var _this = this;
|
|
|
|
var _requestUtils$normali = requestUtils.normalizeRequest(url, options, this.config.Request);
|
|
|
|
url = _requestUtils$normali.url;
|
|
options = _requestUtils$normali.options;
|
|
request = _requestUtils$normali.request;
|
|
|
|
|
|
var route = this.executeRouter(url, options, request);
|
|
|
|
// this is used to power the .flush() method
|
|
var done = void 0;
|
|
this._holdingPromises.push(new this.config.Promise(function (res) {
|
|
return done = res;
|
|
}));
|
|
|
|
// wrapped in this promise to make sure we respect custom Promise
|
|
// constructors defined by the user
|
|
return new this.config.Promise(function (res, rej) {
|
|
if (options && options.signal) {
|
|
var abort = function abort() {
|
|
rej(new Error('URL \'' + url + '\' aborted.'));
|
|
done();
|
|
};
|
|
if (options.signal.aborted) {
|
|
abort();
|
|
}
|
|
options.signal.addEventListener('abort', abort);
|
|
}
|
|
|
|
_this.generateResponse(route, url, options, request).then(res, rej).then(done, done);
|
|
});
|
|
};
|
|
|
|
FetchMock.fetchHandler.isMock = true;
|
|
|
|
FetchMock.executeRouter = function (url, options, request) {
|
|
if (this.config.fallbackToNetwork === 'always') {
|
|
return { response: this.getNativeFetch() };
|
|
}
|
|
|
|
var match = this.router(url, options, request);
|
|
|
|
if (match) {
|
|
return match;
|
|
}
|
|
|
|
if (this.config.warnOnFallback) {
|
|
console.warn('Unmatched ' + (options && options.method || 'GET') + ' to ' + url); // eslint-disable-line
|
|
}
|
|
|
|
this.push({ url: url, options: options, request: request, isUnmatched: true });
|
|
|
|
if (this.fallbackResponse) {
|
|
return { response: this.fallbackResponse };
|
|
}
|
|
|
|
if (!this.config.fallbackToNetwork) {
|
|
throw new Error('fetch-mock: No fallback response defined for ' + (options && options.method || 'GET') + ' to ' + url);
|
|
}
|
|
|
|
return { response: this.getNativeFetch() };
|
|
};
|
|
|
|
FetchMock.generateResponse = function () {
|
|
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(route, url, options, request) {
|
|
var response;
|
|
return _regenerator2.default.wrap(function _callee2$(_context2) {
|
|
while (1) {
|
|
switch (_context2.prev = _context2.next) {
|
|
case 0:
|
|
_context2.next = 2;
|
|
return resolve(route.response, url, options, request);
|
|
|
|
case 2:
|
|
response = _context2.sent;
|
|
|
|
if (!(response.throws && typeof response !== 'function')) {
|
|
_context2.next = 5;
|
|
break;
|
|
}
|
|
|
|
throw response.throws;
|
|
|
|
case 5:
|
|
if (!this.config.Response.prototype.isPrototypeOf(response)) {
|
|
_context2.next = 7;
|
|
break;
|
|
}
|
|
|
|
return _context2.abrupt('return', response);
|
|
|
|
case 7:
|
|
return _context2.abrupt('return', responseBuilder({
|
|
url: url,
|
|
responseConfig: response,
|
|
fetchMock: this,
|
|
route: route
|
|
}));
|
|
|
|
case 8:
|
|
case 'end':
|
|
return _context2.stop();
|
|
}
|
|
}
|
|
}, _callee2, this);
|
|
}));
|
|
|
|
return function (_x5, _x6, _x7, _x8) {
|
|
return _ref2.apply(this, arguments);
|
|
};
|
|
}();
|
|
|
|
FetchMock.router = function (url, options, request) {
|
|
var route = this.routes.find(function (route) {
|
|
return route.matcher(url, options, request);
|
|
});
|
|
|
|
if (route) {
|
|
this.push({
|
|
url: url,
|
|
options: options,
|
|
request: request,
|
|
identifier: route.identifier
|
|
});
|
|
return route;
|
|
}
|
|
};
|
|
|
|
FetchMock.getNativeFetch = function () {
|
|
var func = this.realFetch || this.isSandbox && this.config.fetch;
|
|
if (!func) {
|
|
throw new Error('fetch-mock: Falling back to network only available on gloabl fetch-mock, or by setting config.fetch on sandboxed fetch-mock');
|
|
}
|
|
return func;
|
|
};
|
|
|
|
FetchMock.push = function (_ref3) {
|
|
var url = _ref3.url,
|
|
options = _ref3.options,
|
|
request = _ref3.request,
|
|
isUnmatched = _ref3.isUnmatched,
|
|
identifier = _ref3.identifier;
|
|
|
|
var args = [url, options];
|
|
args.request = request;
|
|
args.identifier = identifier;
|
|
args.isUnmatched = isUnmatched;
|
|
this._calls.push(args);
|
|
};
|
|
|
|
module.exports = FetchMock;
|