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}`);}}
I originally wrote this helper function for a project using
node-fetchand 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.