Skip to content
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

fetchAccessToken tightly couple to Fetch API #5

Open
amacleay opened this issue May 15, 2018 · 3 comments
Open

fetchAccessToken tightly couple to Fetch API #5

amacleay opened this issue May 15, 2018 · 3 comments

Comments

@amacleay
Copy link

amacleay commented May 15, 2018

I am trying to use apollo-link-token-refresh in a typescript project. In this project, we are using a library function to make the access token refresh request, and that library function does not expose the Fetch API underneath:

interface RefreshedSession {
  accessToken: string;
  expirationTimestamp: number;
}
async function refreshSession: Promise<RefreshedSession> {
  const resp = await fetch(...);
  ...
  return {
    accessToken,
    expirationTimestamp;
  };
}

I believe I should be able to use apollo-link-token-refresh without casting to any or using any other type hacks. Perhaps TokenRefreshLink should have an optional type parameter.

new TokenRefreshLink<RefreshedSession>({
  fetchAccessToken: refreshSession,
  handleResponse: (_, __) => response => response.accessToken,
  handleFetch: saveToken,
  handleError: console.error,
  },
});

Workaround: force my session response to be of a modified Response type:

new TokenRefreshLink({
  fetchAccessToken: async () => {
    const refreshedSession = await refreshSession();
    const fakeReq = new Request('uri://fake');
    return Object.assign(fakeReq, refreshedSession);
  },
  handleResponse: (_, __) => (response: Response & RefreshedSession) => response.accessToken,
  handleFetch: saveToken,
  handleError: console.error,
  },
});
@newsiberian
Copy link
Owner

Hi, @amacleay, did you try to do something like this?:

accessTokenField: 'refreshedSession', // Your object name with `accessToken` and 
// `expirationTimestamp` that returned by fetch
fetchAccessToken: fetch(...),
handleResponse: (_, __) => (response: typeYouNeed) => {
  ...
  return {
    accessToken,
    expirationTimestamp
  };
},
handleFetch: ({ accessToken, expirationTimestamp }) => saveToken

@amacleay
Copy link
Author

That would certainly work as I've laid out the issue.

In actual fact, though, I need to use a function in fetchAccessToken that doesn't expose the fetch API at all: my refreshSession function is actually wrapping a call to apollo-client

@newsiberian
Copy link
Owner

Can you make a PR that allows to avoid cases like yours?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants