Skip to content

Commit

Permalink
Add response headers to promise resolves and redux actions (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnutter authored and ryanashcraft committed Mar 20, 2017
1 parent 76d3d4d commit 65a7995
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
31 changes: 27 additions & 4 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ export const requestStart = (url, body, request, meta, queryKey) => {
};
};

export const requestSuccess = (url, body, status, entities, meta, queryKey, responseBody, responseText) => {
export const requestSuccess = (
url,
body,
status,
entities,
meta,
queryKey,
responseBody,
responseText,
responseHeaders
) => {
return {
type: actionTypes.REQUEST_SUCCESS,
url,
Expand All @@ -20,20 +30,22 @@ export const requestSuccess = (url, body, status, entities, meta, queryKey, resp
entities,
responseBody,
responseText,
responseHeaders,
meta,
queryKey,
time: Date.now(),
};
};

export const requestFailure = (url, body, status, responseBody, meta, queryKey, responseText) => {
export const requestFailure = (url, body, status, responseBody, meta, queryKey, responseText, responseHeaders) => {
return {
type: actionTypes.REQUEST_FAILURE,
url,
body,
status,
responseBody,
responseText,
responseHeaders,
meta,
queryKey,
time: Date.now(),
Expand All @@ -51,28 +63,39 @@ export const mutateStart = (url, body, request, optimisticEntities, queryKey) =>
};
};

export const mutateSuccess = (url, body, status, entities, queryKey, responseBody, responseText) => {
export const mutateSuccess = (url, body, status, entities, queryKey, responseBody, responseText, responseHeaders) => {
return {
type: actionTypes.MUTATE_SUCCESS,
url,
body,
status,
responseBody,
responseText,
responseHeaders,
entities,
queryKey,
time: Date.now(),
};
};

export const mutateFailure = (url, body, status, originalEntities, queryKey, responseBody, responseText) => {
export const mutateFailure = (
url,
body,
status,
originalEntities,
queryKey,
responseBody,
responseText,
responseHeaders
) => {
return {
type: actionTypes.MUTATE_FAILURE,
url,
body,
status,
responseBody,
responseText,
responseHeaders,
originalEntities,
queryKey,
time: Date.now(),
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/superagent.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const superagentNetworkAdapter = (url, method, { body, headers, credentials } =
const resStatus = (response && response.status) || 0;
const resBody = (response && response.body) || undefined;
const resText = (response && response.text) || undefined;
const resHeaders = (response && response.header) || undefined;

cb(err, resStatus, resBody, resText);
cb(err, resStatus, resBody, resText, resHeaders);
});

const abort = () => request.abort();
Expand Down
18 changes: 12 additions & 6 deletions src/middleware/query-advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe

attempts += 1;

request.execute((err, resStatus, resBody, resText) => {
request.execute((err, resStatus, resBody, resText, resHeaders) => {
if (
includes(config.retryableStatusCodes, resStatus) &&
attempts < config.backoff.maxAttempts
Expand All @@ -135,7 +135,8 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe
resBody,
meta,
queryKey,
resText
resText,
resHeaders
));
} else {
const callbackState = getState();
Expand All @@ -150,7 +151,8 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe
meta,
queryKey,
resBody,
resText
resText,
resHeaders
));
}

Expand All @@ -163,6 +165,7 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe
text: resText,
transformed,
entities: newEntities,
headers: resHeaders,
});
});
};
Expand Down Expand Up @@ -207,7 +210,7 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe
// `mutateStart` action as `optimisticEntities`
dispatch(mutateStart(url, body, request, optimisticEntities, queryKey));

request.execute((err, resStatus, resBody, resText) => {
request.execute((err, resStatus, resBody, resText, resHeaders) => {
let transformed;
let newEntities;

Expand All @@ -219,7 +222,8 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe
entities,
queryKey,
resBody,
resText
resText,
resHeaders
));
} else {
transformed = transform(resBody, resText);
Expand All @@ -231,7 +235,8 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe
newEntities,
queryKey,
resBody,
resText
resText,
resHeaders
));
}

Expand All @@ -244,6 +249,7 @@ const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSe
text: resText,
transformed,
entities: newEntities,
headers: resHeaders,
});
});
});
Expand Down

0 comments on commit 65a7995

Please sign in to comment.