Extracting out a generic HTTP function

We'll need to use the fetch function in every function that needs to interact with the REST API. So, we are going to create a generic http function that we'll use to make all of our HTTP requests. This will nicely centralize the code that calls the REST API. Let's carry out the following steps:

  1. Create a new file called http.ts with the following content:
import { webAPIUrl } from './AppSettings';export interface HttpRequest<REQB> {  path: string;}export interface HttpResponse<RESB> extends Response {  parsedBody?: RESB;}

We've started by importing the root path to our REST API from AppSettings.ts, which was set up in our starter project. The AppSettings.ts file is where we will build all of the different ...

Get ASP.NET Core 3 and React now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.