redux-saga / saga-query

Data synchronization using a middleware system for front-end apps
64 stars 4 forks source link

feat: support multiple endpoints with the same name #27

Closed neurosnap closed 1 year ago

neurosnap commented 1 year ago

Previously the name provieded to api.get('/the-name') had to be unique.

Internally we used that name for a few internal maps that assumed they would always be unique.

However, we want to support the ability for createApi to support scenariors where you have the same name HTTP endpoint defined multiple times. Why would someone want that? Because we have special sagas that can be used to provide additional functionality to our endpoints -- like polling -- we want to ensure they can use them without colliding with more basic endpoints.

const api = createApi();
const action = api.get('/');
const pollAction = api.get('/', { saga: poll(5 * 1000) });

dispatch(pollAction());
dispatch(action());

Here we have a case where we want to duplicate the name because it relates to the API endpoint we want to hit.

To fix this we now support the ability to provide an array for the name of the endpoint.

const api = createApi();
const action = api.get('/');
const pollAction = api.get(['/', 'poller'], { saga: poll(5 * 1000) });

dispatch(pollAction());
dispatch(action());

The first part is the string used to generate the url with our urlParser middleware. The remaining parts are used for making the name unique and are otherwise not used.

Although other middleware could be creative with using the array of strings.