From 7889ab819bf2c1c2bfdb5b54406e54f5ededaa6d Mon Sep 17 00:00:00 2001 From: Maurice de Beijer Date: Mon, 10 Jun 2019 21:10:11 +0200 Subject: [PATCH] Fix linting errors --- src/fetchData.ts | 50 +++++++++++++++++++++++++++------------------- src/index.test.tsx | 8 ++++---- src/index.ts | 12 +++++------ src/isJSON.test.ts | 4 ++-- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/fetchData.ts b/src/fetchData.ts index 1bc767fe..208e5bd5 100644 --- a/src/fetchData.ts +++ b/src/fetchData.ts @@ -13,12 +13,14 @@ const fetchData = async ( let rsp: Response | null = null; try { - setState((oldState: FetchState) => ({ - data: null, - loading: oldState.loading + 1, - error: null, - controller - })); + setState( + (oldState: FetchState): FetchState => ({ + data: null, + loading: oldState.loading + 1, + error: null, + controller + }) + ); rsp = await fetch(url, actualInit); const contentTypeHeader = rsp.headers.get('content-type'); @@ -31,16 +33,20 @@ const fetchData = async ( } else { data = await rsp.text(); } - setState((oldState: FetchState) => ({ - ...oldState, - data, - loading: oldState.loading - 1 - })); + setState( + (oldState: FetchState): FetchState => ({ + ...oldState, + data, + loading: oldState.loading - 1 + }) + ); } else { - setState((oldState: FetchState) => ({ - ...oldState, - loading: oldState.loading - 1 - })); + setState( + (oldState: FetchState): FetchState => ({ + ...oldState, + loading: oldState.loading - 1 + }) + ); } if (!rsp.ok) { @@ -53,12 +59,14 @@ const fetchData = async ( const error = err.name !== 'AbortError' ? err : null; - setState((oldState: FetchState) => ({ - ...oldState, - error, - // Only decrease the loading counter if there is no repsonse - loading: rsp ? oldState.loading : oldState.loading - 1 - })); + setState( + (oldState: FetchState): FetchState => ({ + ...oldState, + error, + // Only decrease the loading counter if there is no repsonse + loading: rsp ? oldState.loading : oldState.loading - 1 + }) + ); } }; diff --git a/src/index.test.tsx b/src/index.test.tsx index c49edfc6..9d17e1f9 100644 --- a/src/index.test.tsx +++ b/src/index.test.tsx @@ -24,7 +24,7 @@ const LoadData = (): JSX.Element => { ); }; -test('Renders the data after success', async () => { +test('Renders the data after success', async (): Promise => { (fetch as any).mockResponseOnce(JSON.stringify({ x: 1 }), { headers: { 'content-type': 'application/json' @@ -34,13 +34,13 @@ test('Renders the data after success', async () => { const { getByText } = render(); getByText('Loading: true'); - await waitForElement(() => getByText('Loading: false')); + await waitForElement((): any => getByText('Loading: false')); getByText('Data: {"x":1}'); getByText('Error: null'); }); -test('Renders the error after failure', async () => { +test('Renders the error after failure', async (): Promise => { (fetch as any).mockResponseOnce( JSON.stringify({ x: 2 @@ -56,7 +56,7 @@ test('Renders the error after failure', async () => { const { getByText } = render(); getByText('Loading: true'); - await waitForElement(() => getByText('Loading: false')); + await waitForElement((): any => getByText('Loading: false')); getByText('Data: {"x":2}'); getByText('Error: {"status":404}'); diff --git a/src/index.ts b/src/index.ts index 22413fb3..45b192f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,31 +15,31 @@ const useAbortableFetch = ( }); const isMounted = useRef(false); - useLayoutEffect(() => { + useLayoutEffect((): (() => void) => { isMounted.current = true; - return () => { + return (): void => { isMounted.current = false; }; }, []); - useEffect(() => { + useEffect((): (() => void) => { const controller = new AbortController(); if (url) { - fetchData(url, init, controller, state => { + fetchData(url, init, controller, (state): void => { if (isMounted.current) { setState(state); } }); } - return () => controller.abort(); + return (): void => controller.abort(); }, [init, url]); return { data: state.data, loading: !!state.loading, error: state.error, - abort: () => state.controller && state.controller.abort() + abort: (): null | void => state.controller && state.controller.abort() }; }; diff --git a/src/isJSON.test.ts b/src/isJSON.test.ts index 3648d70c..4abfd6c1 100644 --- a/src/isJSON.test.ts +++ b/src/isJSON.test.ts @@ -18,7 +18,7 @@ test.each([ 'application/vnd.restful+json', 'text/json', 'model/gltf+json' -])('Content type "%s" should be JSON', (contentType: string) => { +])('Content type "%s" should be JSON', (contentType: string): void => { expect(isJSON(contentType)).toBeTruthy(); }); @@ -32,7 +32,7 @@ test.each([ 'text/html' ])( 'Content type "%s" should not be JSON', - (contentType: string | null | undefined) => { + (contentType: string | null | undefined): void => { expect(isJSON(contentType)).toBeFalsy(); } );