Skip to content

onIntercept not re-fetching when RequestToApi object is returned #35

@jjenzz

Description

@jjenzz

I have tried to implement onIntercept. My implementation gets called and refreshes the token correctly but doesn't make another requestToApi with the returned RequestToApi object.

I'm using react-data-fetching@0.2.3.

import React, { Component } from 'react';
import { func, string, object } from 'prop-types';
import Config from 'react-native-config';
import { Fetch as ReactDataFetching } from 'react-data-fetching';
import { authStore } from '@/stores';

class Fetch extends Component {
  handleIntercept = async data => {
    let output = null;

    if (data && data.status === 401) {
      try {
        const token = await authStore.refreshToken();

        if (token) {
          output = {
            ...data.currentParams,
            headers: {
              ...data.currentParams.headers,
              Authorization: `AuthToken ${token}`,
            },
          };
        }
      } catch(error) {
        console.log(error);
      }
    }    

    return output;
  };

  render() {
    const { resource, headers, token, children, ...props } = this.props;
    const url = `${Config.API_URL}${resource}`;

    return (
      <ReactDataFetching
        {...props}
        url={url}
        loader={() => children({ isLoading: true })}
        headers={{ ...headers, Authorization: `AuthToken ${token}` }}
        onIntercept={this.handleIntercept}
      >
        {childProps => children({ isLoading: false, ...childProps })}
      </ReactDataFetching>
    );
  }
}

Fetch.propTypes = {
  resource: string.isRequired,
  children: func.isRequired,
  headers: object,
};

Fetch.defaultProps = {
  headers: {},
};

How do I get this working?

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions