You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I originally wrote this helper function for a project using node-fetch and I want to port it to unfetch, but I'm missing the RequestInit interface.
Without it I don't know how to type the options.
importfetch,{RequestInit}from"isomorphic-unfetch";import{makeQueryString}from"../utils/query-string";exportasyncfunctionfetchJson<T>(endpoint: string,queryParams: {[key: string]: string|number|boolean}={},options: RequestInit={}): Promise<T>{constqueryString=makeQueryString(queryParams);consturl=`${endpoint}${queryString}`;constresponse=awaitfetch(url,{mode: "cors",
...options});if(!response.ok){thrownewError(`Fetch to ${url} failed with status ${response.status}. Response text: ${awaitresponse.text()}`);}try{returnresponse.json();}catch(err){thrownewError(`Failed to parse JSON from response of ${url}`);}}
Activity
developit commentedon Sep 29, 2020
Released in 4.2.0.