Skip to content

Commit

Permalink
add options to the transformFn so the transformFn can have access to …
Browse files Browse the repository at this point in the history
…the body or other options if needed
  • Loading branch information
mkasper1-godaddy committed May 13, 2021
1 parent 8eb906e commit 938709b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/actionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export function makeSubActionsCreators(apiName, resourceName, reqDesc) {
* @param {RequestDescription} reqDesc - Request description
* @param {Object} subActions - Generated subActions for action creators
* @param {Object} params - Request path and query params
* @param {Object} options - Request options
* @returns {Function[]} dispatchers
* @private
*/
export function createDispatchers(dispatch, reqDesc, subActions, params) {
export function createDispatchers(dispatch, reqDesc, subActions, params, options) {
const dataTransform = reqDesc.dataTransform || defaultTransformer;
const errorTransform = reqDesc.errorTransform || defaultTransformer;

Expand All @@ -88,11 +89,11 @@ export function createDispatchers(dispatch, reqDesc, subActions, params) {
}

function onResolved(data) {
return dispatch(subActions.success(params, dataTransform(data, { params })));
return dispatch(subActions.success(params, dataTransform(data, { params, options })));
}

function onRejected(data) {
return dispatch(subActions.fail(params, errorTransform(data, { params })));
return dispatch(subActions.fail(params, errorTransform(data, { params, options })));
}

return [onStart, onResolved, onRejected];
Expand Down Expand Up @@ -204,11 +205,11 @@ export default function createActionCreators(apiName, apiDesc, apiConfig = {}) {
);
}

const [onStart, onResolved, onRejected] = createDispatchers(dispatch, reqDesc, subActions, params);
const allOptions = getRequestOptions(apiOptions, reqOptions, options, getState);
const [onStart, onResolved, onRejected] = createDispatchers(dispatch, reqDesc, subActions, params, allOptions);

onStart();
const url = transformUrl(getUrlTemplate(reqDesc.url, getState), params);
const allOptions = getRequestOptions(apiOptions, reqOptions, options, getState);
const promise = makeRequest(reqDesc.method, url, { ...allOptions }, apiConfig)
.then(onResolved, onRejected);
promiseKeeper.set(promiseKey, promise);
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ declare module 'reduxful' {
): string;

// reduxful
export type TransformFn = (data: any, context?: { params?: Object }) => any;
export type TransformFn = (data: any, context?: { params?: Object, options?: Object }) => any;

export type UrlTemplateFn = (getState: () => any) => string;

Expand Down

0 comments on commit 938709b

Please sign in to comment.