Skip to content

Commit

Permalink
fix: allow credentials option in Image (#2130)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Muracciole <diegomuracciole@gmail.com>
Co-authored-by: Dmitry Ivakhnenko <jeetiss@yandex.ru>
  • Loading branch information
3 people authored Oct 23, 2023
1 parent d698681 commit 4a55c1b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/four-shirts-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@react-pdf/image": patch
"@react-pdf/types": patch
---

fix: allow credentials option in Image
9 changes: 7 additions & 2 deletions packages/image/src/resolve.js
Original file line number Diff line number Diff line change
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,
});

const extension = getImageFormat(data);

Expand Down
17 changes: 17 additions & 0 deletions packages/image/tests/resolve.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 }
type SourceURLObject = { uri: string; method: HTTPMethod; body: any; headers: any; credentials?: 'omit' | 'same-origin' | 'include' }

type Source =
| SourceURL
Expand Down

0 comments on commit 4a55c1b

Please sign in to comment.