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

fix: allow credentials option in Image #2130

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/image/src/resolve.js
Expand Up @@ -144,12 +144,17 @@ const getImageFormat = body => {
};

const resolveImageFromUrl = async src => {
const { uri, body, headers, method = 'GET' } = src;
const { uri, body, headers, method = 'GET', credentials } = src;

const data =
!BROWSER && getAbsoluteLocalPath(uri)
? await fetchLocalFile(uri)
: await fetchRemoteFile(uri, { body, headers, method });
: await fetchRemoteFile(uri, {
body,
headers,
method,
...(credentials && { credentials }),
diegomura marked this conversation as resolved.
Show resolved Hide resolved
});

const extension = getImageFormat(data);

Expand Down
17 changes: 17 additions & 0 deletions packages/image/tests/resolve.test.js
Expand Up @@ -50,6 +50,23 @@ describe('image resolveImage', () => {
expect(fetch.mock.calls[0][1].body).toEqual(body);
});

test('Should fetch remote image using passed credentials', async () => {
fetch.once(localJPGImage);

const credentials = 'include';
await resolveImage({ uri: jpgImageUrl, credentials });

expect(fetch.mock.calls[0][1].credentials).toBe(credentials);
});

test('Should not include credentials if not exist', async () => {
fetch.once(localJPGImage);

await resolveImage({ uri: jpgImageUrl });

expect(fetch.mock.calls[0][1].credentials).toBeUndefined();
});

test('Should render a jpeg image over http', async () => {
fetch.once(localJPGImage);

Expand Down
2 changes: 1 addition & 1 deletion packages/types/image.d.ts
Expand Up @@ -6,7 +6,7 @@ type SourceBuffer = Buffer

type SourceDataBuffer = { data: Buffer; format: 'png' | 'jpg' }

type SourceURLObject = { uri: string; method: HTTPMethod; body: any; headers: any }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Info:
image

type SourceURLObject = { uri: string; method: HTTPMethod; body: any; headers: any; credentials?: 'omit' | 'same-origin' | 'include' }

type Source =
| SourceURL
Expand Down