Skip to content

Commit c5bad33

Browse files
authored
Send cookies with fetch (#120)
* Adding credentials: include to fetchFromAPI so that cookies are sent with each API call. * bump version
1 parent 20e3f0a commit c5bad33

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-storefront",
3-
"version": "8.13.0",
3+
"version": "8.14.0",
44
"description": "Build and deploy e-commerce progressive web apps (PWAs) in record time.",
55
"module": "./index.js",
66
"license": "Apache-2.0",

src/props/fetchFromAPI.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ export default function fetchFromAPI({ req, asPath, pathname }) {
5757

5858
const url = `${protocol}${host}${uri}`
5959

60-
return fetch(url, { headers }).then(res => res.json())
60+
return fetch(url, { credentials: 'include', headers }).then(res => res.json())
6161
}

test/props/fetchFromAPI.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ describe('fetchFromAPI', () => {
1414
fetchFromAPI({
1515
asPath: '/p/1',
1616
})
17-
expect(fetchMock).toHaveBeenCalledWith('/api/p/1', { headers })
17+
expect(fetchMock).toHaveBeenCalledWith('/api/p/1', { credentials: 'include', headers })
1818
})
1919

2020
it('should call /api when the path is /', () => {
2121
fetchFromAPI({
2222
asPath: '/',
2323
})
24-
expect(fetchMock).toHaveBeenCalledWith('/api', { headers })
24+
expect(fetchMock).toHaveBeenCalledWith('/api', { credentials: 'include', headers })
2525
})
2626

2727
it('should append query params directly to api if the root with query params is called', () => {
2828
fetchFromAPI({
2929
asPath: '/?test=1',
3030
})
31-
expect(fetchMock).toHaveBeenCalledWith('/api?test=1', { headers })
31+
expect(fetchMock).toHaveBeenCalledWith('/api?test=1', { credentials: 'include', headers })
3232
})
3333
})
3434

@@ -50,6 +50,7 @@ describe('fetchFromAPI', () => {
5050
},
5151
})
5252
expect(fetchMock).toHaveBeenCalledWith('https://www.domain.com/api/p/1?_includeAppData=1', {
53+
credentials: 'include',
5354
headers: {
5455
...headers,
5556
'x-next-page': '/api/p/[productId]',
@@ -71,6 +72,7 @@ describe('fetchFromAPI', () => {
7172
})
7273

7374
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3001/api/p/1?_includeAppData=1', {
75+
credentials: 'include',
7476
headers: {
7577
...headers,
7678
'x-next-page': '/api/p/[productId]',
@@ -92,6 +94,7 @@ describe('fetchFromAPI', () => {
9294
},
9395
})
9496
expect(fetchMock).toHaveBeenCalledWith('http://localhost/api/p/1?_includeAppData=1', {
97+
credentials: 'include',
9598
headers: {
9699
...headers,
97100
host: 'localhost',
@@ -111,6 +114,7 @@ describe('fetchFromAPI', () => {
111114
expect(fetchMock).toHaveBeenCalledWith(
112115
'https://www.domain.com/api/foo?x=1&_includeAppData=1',
113116
{
117+
credentials: 'include',
114118
headers: {
115119
...headers,
116120
'x-next-page': '/api/foo',

0 commit comments

Comments
 (0)