-
-
Save nikosolihin/5da5ee22a0b87a39734bad3211a07f9a to your computer and use it in GitHub Desktop.
Revisions
-
mattpocock renamed this gist
Oct 21, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
mattpocock created this gist
Oct 21, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ const endpointToPromiseMap = { [ApiEndpoint.FetchAllGoals]: fetchUserGoals, [ApiEndpoint.CurrentUser]: currentUser, }; type PromiseValue<T extends Promise<any>> = T extends Promise<infer U> ? U : never; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types const useFetch = <E extends ApiEndpoint, L>(endpoint: E, args?: L) => { const prms = endpointToPromiseMap[endpoint]; return _useFetch<PromiseValue<ReturnType<typeof endpointToPromiseMap[E]>>, L>( prms as any, args, ); }; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types const _useFetch = <T, L>(prms: (arg0?: L) => Promise<T>, args?: L) => { const isMounted = useRef(true); const [data, setData] = useState<T | null>(null); const [errorMessage, setErrorMessage] = useState<string>(''); return { status, errorMessage, data, }; };