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

feat: Support non JSON (aka binary) fetches #267

Merged
merged 2 commits into from
Feb 13, 2020
Merged

feat: Support non JSON (aka binary) fetches #267

merged 2 commits into from
Feb 13, 2020

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Feb 13, 2020

Fixes #266.

Motivation

With Rest Hooks already useful for JSON type data, it should allow for other response types like images, ArrayBuffers, etc.

Solution

Sample FetchShape:

export const photoShape = {
  type: 'read' as const,
  schema: null as ArrayBuffer | null,
  getFetchKey({ userId }: { userId: string }) {
    return `/users/${userId}/photo`;
  },
  fetch: async ({ userId }: { userId: string }) => {
    const response = await fetch(`/users/${userId}/photo`);
    const photoArrayBuffer = await response.arrayBuffer();

    return photoArrayBuffer;
  },
};

Usage:

const photo = useResource(photoShape, { userId });

Open questions

useResource() ensures a response, but we're still stuck with the | null union. This is needed however to have sane defaults. Currently the only distinction is based on entities, which this cannot be so would require some rethinking from normalizr design.

Perhaps a better solution is to simply allow schema to be undefined, and then take the return type of fetch.


return photoArrayBuffer;
},
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This is a great feature!

@ntucker ntucker merged commit fb8e6d8 into master Feb 13, 2020
@ntucker ntucker deleted the arraybuffers branch February 13, 2020 18:49
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

Successfully merging this pull request may close these issues.

Fetching ArrayBuffer via Resource
2 participants