Skip to content

Commit 4a55c1b

Browse files
PoornakumarRasirajudiegomurajeetiss
authored
fix: allow credentials option in Image (#2130)
Co-authored-by: Diego Muracciole <diegomuracciole@gmail.com> Co-authored-by: Dmitry Ivakhnenko <jeetiss@yandex.ru>
1 parent d698681 commit 4a55c1b

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

.changeset/four-shirts-refuse.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@react-pdf/image": patch
3+
"@react-pdf/types": patch
4+
---
5+
6+
fix: allow credentials option in Image

packages/image/src/resolve.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,17 @@ const getImageFormat = body => {
144144
};
145145

146146
const resolveImageFromUrl = async src => {
147-
const { uri, body, headers, method = 'GET' } = src;
147+
const { uri, body, headers, method = 'GET', credentials } = src;
148148

149149
const data =
150150
!BROWSER && getAbsoluteLocalPath(uri)
151151
? await fetchLocalFile(uri)
152-
: await fetchRemoteFile(uri, { body, headers, method });
152+
: await fetchRemoteFile(uri, {
153+
body,
154+
headers,
155+
method,
156+
credentials,
157+
});
153158

154159
const extension = getImageFormat(data);
155160

packages/image/tests/resolve.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ describe('image resolveImage', () => {
5050
expect(fetch.mock.calls[0][1].body).toEqual(body);
5151
});
5252

53+
test('Should fetch remote image using passed credentials', async () => {
54+
fetch.once(localJPGImage);
55+
56+
const credentials = 'include';
57+
await resolveImage({ uri: jpgImageUrl, credentials });
58+
59+
expect(fetch.mock.calls[0][1].credentials).toBe(credentials);
60+
});
61+
62+
test('Should not include credentials if not exist', async () => {
63+
fetch.once(localJPGImage);
64+
65+
await resolveImage({ uri: jpgImageUrl });
66+
67+
expect(fetch.mock.calls[0][1].credentials).toBeUndefined();
68+
});
69+
5370
test('Should render a jpeg image over http', async () => {
5471
fetch.once(localJPGImage);
5572

packages/types/image.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type SourceBuffer = Buffer
66

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

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

1111
type Source =
1212
| SourceURL

0 commit comments

Comments
 (0)