Skip to content

Commit

Permalink
neaten some function signatires
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed May 23, 2020
1 parent 842783d commit 1cca5da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
5 changes: 2 additions & 3 deletions docs/_api-inspection/lastResponse.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ description: |-
If `.lastResponse()` is called before fetch has been resolved then it will return `undefined`
{: .warning}
To obtain json/text responses await the `.json()/.text()` methods of the response
{: .info}
{: .info}
---

51 changes: 28 additions & 23 deletions src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,42 @@ const resolve = async (
}
};

FetchMock.fetchHandler = function (url, options, request) {
FetchMock.needsAsyncBodyExtraction = function ({ request }) {
return request && this.routes.some(({ usesBody }) => usesBody);
};

FetchMock.fetchHandler = function (url, options) {
setDebugPhase('handle');
const debug = getDebug('fetchHandler()');
debug('fetch called with:', url, options);

const normalizedRequest = requestUtils.normalizeRequest(
url,
options,
this.config.Request
);

({ url, options, request } = normalizedRequest);

const { signal } = normalizedRequest;

debug('Request normalised');
debug(' url', url);
debug(' options', options);
debug(' request', request);
debug(' signal', signal);
debug(' url', normalizedRequest.url);
debug(' options', normalizedRequest.options);
debug(' request', normalizedRequest.request);
debug(' signal', normalizedRequest.signal);

if (request && this.routes.some(({ usesBody }) => usesBody)) {
if (this.needsAsyncBodyExtraction(normalizedRequest)) {
debug(
'Need to wait for Body to be streamed before calling router: switching to async mode'
);
return this._asyncFetchHandler(url, options, request, signal);
return this._extractBodyThenHandle(normalizedRequest);
}
return this._fetchHandler(url, options, request, signal);
return this._fetchHandler(normalizedRequest);
};

FetchMock._asyncFetchHandler = async function (url, options, request, signal) {
options.body = await options.body;
return this._fetchHandler(url, options, request, signal);
FetchMock._extractBodyThenHandle = async function (normalizedRequest) {
normalizedRequest.options.body = await normalizedRequest.options.body;
return this._fetchHandler(normalizedRequest);
};

FetchMock._fetchHandler = function (url, options, request, signal) {
FetchMock._fetchHandler = function ({ url, options, request, signal }) {
const { route, callLog } = this.executeRouter(url, options, request);

this.recordCall(callLog);
Expand All @@ -111,7 +112,9 @@ FetchMock._fetchHandler = function (url, options, request, signal) {
debug('signal exists - enabling fetch abort');
const abort = () => {
debug('aborting fetch');
// note that DOMException is not available in node.js; even node-fetch uses a custom error class: https://github.com/bitinn/node-fetch/blob/master/src/abort-error.js
// note that DOMException is not available in node.js;
// even node-fetch uses a custom error class:
// https://github.com/bitinn/node-fetch/blob/master/src/abort-error.js
rej(
typeof DOMException !== 'undefined'
? new DOMException('The operation was aborted.', 'AbortError')
Expand All @@ -126,7 +129,7 @@ FetchMock._fetchHandler = function (url, options, request, signal) {
signal.addEventListener('abort', abort);
}

this.generateResponse(route, url, options, request, callLog)
this.generateResponse({ route, url, options, request, callLog })
.then(res, rej)
.then(done, done)
.then(() => {
Expand All @@ -147,8 +150,10 @@ FetchMock.executeRouter = function (url, options, request) {
);
return {
route: { response: this.getNativeFetch(), responseIsFetch: true },
// BUG - this callLog never used to get sent. Discovered the bug but can't
// fix outside a major release as it will potentially cause too much disruption
// BUG - this callLog never used to get sent. Discovered the bug
// but can't fix outside a major release as it will potentially
// cause too much disruption
//
// callLog,
};
}
Expand Down Expand Up @@ -192,13 +197,13 @@ FetchMock.executeRouter = function (url, options, request) {
};
};

FetchMock.generateResponse = async function (
FetchMock.generateResponse = async function ({
route,
url,
options,
request,
callLog = {}
) {
callLog = {},
}) {
const debug = getDebug('generateResponse()');
const response = await resolve(route, url, options, request);

Expand Down

0 comments on commit 1cca5da

Please sign in to comment.