question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use API Facade instead of request url

See original GitHub issue

In my project, I use API Facades to fetch data and saga looks like

export const handleGetItem: Saga = function*({
  payload,
}: PayloadAction<typeof ActionEnum.API_GET_REQUEST, Payload>): IterableIterator<any> {
  const { id }: Payload = payload;
  const facade: Facade = yield getContext(SagaContextEnum.facade);

  try {
    const storeItem: IItem = yield call(() => facade.getItem(id));
    yield put(actions.successGetItem(storeItem));
  } catch (err) {
    yield put(actions.errorGetItem(err));
  }
};

How can I pass facade.getItem(id) to the redux-saga-request action creator?? Is it possible to implement this feature in this library??

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
klis87commented, Sep 1, 2019

@strdr4605 I am surprised as

const x = () => {
  return new Promise(resolve => resolve(1));
};

does not show any TS errors.

Anyway, I am closing this issue in favour of #244

1reaction
klis87commented, Aug 13, 2019

@strdr4605 of course, if you dispatch request action like yield put(getItems(id)), then id can be from yield select ( I know you mentioned u dont wanna have id in action payload, for this see below methods)

personally though, If I had some id which is part of many request actions, I would just use redux thunk, which makes your sagas clean, or works if you dispatch actions from react too:

function getItems() {
  return (dispatch, getState) => {
    const id = idSelector(getState());
    
    return dispatch({
      type: 'GET_ITEMS',
      request: {
        url: `/${id}/list`
      }
    };
  };
}

In my apps I sometimes do also another thing, kind of magical but I find it very convenient:

import { mapRequest } from 'redux-saga-requests/lib/helpers';

const interpolatePlaceholders = (config, { user }) => {
  if (config.url.includes('{user}')) {
    config = { ...config, url: config.url.replace('{user}', user) };
  }

  return config;
};


function* onRequest(request) {
  const user = yield select(userIdSelector);

  return mapRequest(request, config =>
    interpolatePlaceholders(config, { user }),
  );
}

By using request interceptor, I can then define actions like:

const getItems = () => ({
  type: 'GET_ITEMS',
  request: {
    url: '/{user}/list'
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use API Facade instead of request url · Issue #270
In my project, I use API Facades to fetch data and saga looks like export const handleGetItem: Saga = function*({ payload, } ...
Read more >
API Facade Pattern
Implementing an API façade pattern involves three basic steps. 1 - Design the ideal API – design the URLs, request parameters and responses,....
Read more >
Implement a Custom API for a Façade REST Service
To create a complete custom API using Oracle Mobile Hub. ... with the New API button. Click the API you already created, or...
Read more >
Why REST Api do not follow the Facade design pattern
I think objects are only built correctly around coherent behaviors and not around data. I will provoke and say that data is almost ......
Read more >
Unlocking Data from Existing Systems with a Serverless ...
When building modern APIs for existing systems, you can use an architecture pattern called API Facade. This pattern creates a layer that exposes ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found