Skip to content

Commit 3503836

Browse files
authored
fix(makeServerFetchye): mask errors from server side fetching (#49)
* fix(makeServerFetchye): mask errors from server side fetching * fix(copyright): add copyright to new file * test(ssrFetcher): Make assersion more specific correct copyright year
1 parent f150f6d commit 3503836

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2021 American Express Travel Related Services Company, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
* or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
17+
import { ssrFetcher } from '../src/ssrFetcher';
18+
import { defaultFetcher } from '../src/defaultFetcher';
19+
20+
jest.mock('../src/defaultFetcher', () => ({
21+
defaultFetcher: jest.fn(() => ({
22+
payload: 'payloadMock',
23+
error: 'errorMock',
24+
})),
25+
}));
26+
27+
describe('ssrFetcher', () => {
28+
it('forward all params to the default fetcher, and return the payload from the default fetcher with error: null', async () => {
29+
const parameters = ['paramMock1', 'paramMock2', 'paramMock3'];
30+
const response = await ssrFetcher(...parameters);
31+
expect(response).toEqual({
32+
payload: 'payloadMock',
33+
error: null,
34+
});
35+
36+
expect(defaultFetcher.mock.calls[0]).toEqual(parameters);
37+
});
38+
});

packages/fetchye-core/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export * from './actions';
1919
export { defaultEqualityChecker } from './defaultEqualityChecker';
2020
export { FetchyeContext } from './FetchyeContext';
2121
export { defaultFetcher } from './defaultFetcher';
22+
export { ssrFetcher } from './ssrFetcher';
2223
export { default as useSubscription } from './useSubscription';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2021 American Express Travel Related Services Company, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
* or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
17+
import { defaultFetcher } from './defaultFetcher';
18+
19+
export const ssrFetcher = async (...params) => {
20+
const { payload } = await defaultFetcher(...params);
21+
return {
22+
payload,
23+
error: null,
24+
};
25+
};
26+
27+
export default ssrFetcher;

packages/fetchye/__tests__/makeServerFetchye.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('makeServerFetchye', () => {
113113
}
114114
`);
115115
});
116-
it('should return error in error state', async () => {
116+
it('should return null in the error state', async () => {
117117
const fetchClient = jest.fn(async () => {
118118
throw new Error('fake error');
119119
});
@@ -133,7 +133,7 @@ describe('makeServerFetchye', () => {
133133
expect(fetchyeRes).toMatchInlineSnapshot(`
134134
Object {
135135
"data": null,
136-
"error": "Error: fake error",
136+
"error": null,
137137
}
138138
`);
139139
});

packages/fetchye/src/makeServerFetchye.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
// eslint-disable-next-line import/no-unresolved
18-
import { defaultFetcher } from 'fetchye-core';
18+
import { ssrFetcher } from 'fetchye-core';
1919
import SimpleCache from './SimpleCache';
2020
import { runAsync } from './runAsync';
2121
import { computeKey } from './computeKey';
@@ -29,7 +29,7 @@ const makeServerFetchye = ({
2929
}) => async (
3030
key,
3131
{ mapOptionsToKey = (options) => options, ...options } = { },
32-
fetcher = defaultFetcher
32+
fetcher = ssrFetcher
3333
) => {
3434
const { cacheSelector } = cache;
3535
const computedKey = computeKey(key, defaultMapOptionsToKey(mapOptionsToKey(options)));

0 commit comments

Comments
 (0)