Skip to content

Commit

Permalink
fix(queryHelpers): fix ssr logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael A Tomcal committed Aug 29, 2020
1 parent 40185d0 commit c16229b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 29 deletions.
8 changes: 4 additions & 4 deletions __tests__/makeServerFetchye.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('makeServerFetchye', () => {
"ok": true,
"status": 200,
},
"error": undefined,
"error": null,
}
`);
});
Expand Down Expand Up @@ -132,8 +132,8 @@ describe('makeServerFetchye', () => {
`);
expect(fetchyeRes).toMatchInlineSnapshot(`
Object {
"data": undefined,
"error": [Error: fake error],
"data": null,
"error": "Error: fake error",
}
`);
});
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('makeServerFetchye', () => {
"ok": true,
"status": 200,
},
"error": undefined,
"error": null,
}
`);
});
Expand Down
33 changes: 31 additions & 2 deletions __tests__/isLoading.spec.js → __tests__/queryHelpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* permissions and limitations under the License.
*/

import { isLoading } from '../src/isLoading';
import {
isLoading, getData, getError, coerceSsrField,
} from '../src/queryHelpers';

describe('defaultMapOptionsToKey', () => {
describe('isLoading', () => {
it('should return true if loading cache true', () => {
expect(isLoading(true, {}, {})).toBeTruthy();
});
Expand All @@ -30,3 +32,30 @@ describe('defaultMapOptionsToKey', () => {
expect(isLoading(false, { current: false }, { lazy: false })).toBeFalsy();
});
});

describe('getData', () => {
it('should return data if data exists', () => {
expect(getData({})).toBeTruthy();
});
it('should return initialData data', () => {
expect(getData(undefined, { initialData: { data: { fakeData: true } } })).toBeTruthy();
});
});

describe('getError', () => {
it('should return error if error exists', () => {
expect(getError(new Error('fake error'))).toBeTruthy();
});
it('should return initialData error', () => {
expect(getError(undefined, { initialData: { error: new Error('fake error') } })).toBeTruthy();
});
});

describe('coerceSsrField', () => {
it('should return stringified error', () => {
expect(coerceSsrField(new Error('Fake Error'))).toEqual('Error: Fake Error');
});
it('should return null if undefined', () => {
expect(coerceSsrField(undefined)).toEqual(null);
});
});
2 changes: 1 addition & 1 deletion __tests__/runAsync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('runAsync', () => {
"fake": true,
},
},
"error": null,
"error": undefined,
}
`);
});
Expand Down
15 changes: 12 additions & 3 deletions src/makeServerFetchye.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { computeKey } from './computeKey';
import SimpleCache from './cache/SimpleCache';
import { defaultFetcher } from './defaultFetcher';
import { defaultMapOptionsToKey } from './defaultMapOptionsToKey';
import { coerceSsrField } from './queryHelpers';

export const makeServerFetchye = ({
cache = SimpleCache(),
Expand All @@ -32,18 +33,26 @@ export const makeServerFetchye = ({
const { cacheSelector } = cache;
const computedKey = computeKey(key, defaultMapOptionsToKey(mapOptionsToKey(options)));
if (!getState || !dispatch || !cacheSelector) {
return runAsync({
const res = await runAsync({
dispatch: () => {}, computedKey, fetcher, fetchClient, options,
});
return {
data: coerceSsrField(res.data),
error: coerceSsrField(res.error),
};
}
const state = cacheSelector(getState());
const { data, loading, error } = cache.getCacheByKey(state, computedKey.hash);
if (!data && !error && !loading) {
return runAsync({
const res = await runAsync({
dispatch, computedKey, fetcher, fetchClient, options,
});
return {
data: coerceSsrField(res.data),
error: coerceSsrField(res.error),
};
}
return { data, error };
return { data: coerceSsrField(data), error: coerceSsrField(error) };
};

export default makeServerFetchye;
24 changes: 24 additions & 0 deletions src/isLoading.js → src/queryHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,27 @@ export const isLoading = (loading, isFirstRender, options) => {
}
return false;
};

export const getData = (data, options) => {
if (!data) {
return options?.initialData?.data;
}
return data;
};

export const getError = (error, options) => {
if (!error) {
return options?.initialData?.error;
}
return error;
};

export const coerceSsrField = (field) => {
if (!field) {
return null;
}
if (field instanceof Error) {
return field.toString?.();
}
return field;
};
2 changes: 1 addition & 1 deletion src/runAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const runAsync = async ({
} else {
dispatch(actions.errorAction({ hash: computedKey.hash, error: requestError }));
}
return { data, error: requestError || null };
return { data, error: requestError };
};

export default runAsync;
26 changes: 8 additions & 18 deletions src/useFetchye.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,14 @@
import { useEffect, useRef } from 'react';
import { runAsync } from './runAsync';
import { computeKey } from './computeKey';
import { isLoading } from './isLoading';
import {
isLoading,
getData,
getError,
} from './queryHelpers';
import { useFetchyeContext } from './useFetchyeContext';
import { defaultMapOptionsToKey } from './defaultMapOptionsToKey';

const getData = (data, isFirstRender, options) => {
if (!data && !isFirstRender.current) {
return options?.initialData?.data;
}
return data;
};

const getError = (error, isFirstRender, options) => {
if (!error && !isFirstRender.current) {
return options?.initialData?.error;
}
return error;
};

export const useFetchye = (
key,
{ mapOptionsToKey = (options) => options, ...options } = { },
Expand Down Expand Up @@ -62,9 +52,9 @@ export const useFetchye = (
}
}, [data, loading, error, computedKey, selectedFetcher, options, dispatch, fetchClient]);
return {
isLoading: isLoading(loading, isFirstRender, options),
error: getError(error, isFirstRender, options),
data: getData(data, isFirstRender, options),
isLoading: isLoading(loading, options),
error: getError(error, options),
data: getData(data, options),
run() {
return runAsync({
dispatch, computedKey, fetcher: selectedFetcher, fetchClient, options,
Expand Down

0 comments on commit c16229b

Please sign in to comment.