New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#168 Multiple instances/Request option caching in redux for middleware customisation #170
base: master
Are you sure you want to change the base?
Conversation
…instance factory function
Oh, this looks great @mspae! Please allow me a few days to review it thoroughly. Think we're looking at a new major version, since there are no backward compatibility. |
Sure. No hurry. As far as I can tell this PR is actually fully backward compatible. |
...(options.axiosConfig ? options.axiosConfig : {}) | ||
}, | ||
...options | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unpacking options
in the end, wouldn't that override axiosConfig
as specified above, if present in options
? i.e. rendering L237 useless. I don't see a test case for this, maybe that's relevant to add.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. Will change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had a chance to look today. This looks great and I would be very happy to have this merged asap. I would request that you add some documentation of the changes you've made, with some usage examples.
I also think we need more tests to cover the various ways options can be passed now, thoughts?
Nice! Happy to hear that. I will add a more comprehensive documentation and test cases for the different ways to use options once I have some free time. 👍 |
@mspae Appreciate it and love that you're willing to spend a bit of time and effort on this project |
Fixes #168
This PR contains two things (which build on each other) but which for the purpose of explaining what's going on I will seperate:
There are two new actions
API_WILL_REQUEST
andAPI_DID_REQUEST
which cache ALL the request data (endpoint, data, axiosConfig etc.) in the redux store before each request. This happens purely so that middleware can hook in and change stuff. The data is then immediately read out again and used for the request. After the request the request data is deleted again by using the second action. This means request options can be built from multiple sources (by order of superiority):updateResource
uses thePOST
method)There is now a
stateKey
property in the reducer. If this is defined and if there is astateKey
property in the action payload then the action is only applied if they match up. (Since this is part of therequestOptions
(see 1.) customisation middleware can select between instances) This way it is possible to have multiple instancesredux-json-api
if needed. To make this easier to set up this PR adds acreateInstance
function which returns a reducer and actions which are bound to this instance.This should be fully backward compatible for people who want to stick with a single instance and not bother about customisation middleware.
An example of the instance API in use:
Some notes for @egeriis
readEndpoint
function. It was messing with the API action binding. This should not break anythin however, because there are very (strict checks in place)[https://github.com/redux-json-api/redux-json-api/blob/master/src/state-mutation.js#L100] when this is used.… also sorry for the giant PR! 🤕