Added http type to handle api response and manage errors. created get, post, put method. Added ErrorPage blanck to redirect to

This commit is contained in:
Ruidy Nemausat 2020-02-21 18:14:21 +01:00
parent c448a37591
commit 19116dc5f3
9 changed files with 166 additions and 116 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,12 @@
import React, { FC } from "react";
interface IProps {
error: any;
}
export const ErrorPage: FC<IProps> = ({ error }) => {
return (
<div className="section">
<p>{error}</p>
</div>
);
};

View file

@ -2,7 +2,7 @@ import React, { FC } from "react";
import { Header } from "../components/Header";
import { AvatarList } from "../components/AvatarList";
import { ProgressBar } from "../components/ProgressBar";
import ProjectVM from "../viewModels/ProjectVM";
import ProjectVM from "../VM/ProjectVM";
import { TabRouter } from "../components/TabRouter";
import { FloatingButton } from "../components/FloatingButton";

37
client/src/utils/http.ts Normal file
View file

@ -0,0 +1,37 @@
export interface HttpResponse<T> extends Response {
parsedBody?: T;
}
export async function http<T>(request: RequestInfo): Promise<HttpResponse<T>> {
const response: HttpResponse<T> = await fetch(request);
try {
response.parsedBody = await response.json();
} catch (ex) {}
if (!response.ok) {
throw new Error(response.statusText);
}
return response;
}
export async function get<T>(
path: string,
args: RequestInit = { method: "get" }
): Promise<HttpResponse<T>> {
return await http<T>(new Request(path, args));
}
export async function post<T>(
path: string,
body: any,
args: RequestInit = { method: "post", body: JSON.stringify(body) }
): Promise<HttpResponse<T>> {
return await http<T>(new Request(path, args));
}
export async function put<T>(
path: string,
body: any,
args: RequestInit = { method: "put", body: JSON.stringify(body) }
): Promise<HttpResponse<T>> {
return await http<T>(new Request(path, args));
}

View file

@ -2,7 +2,8 @@ import React from "react";
import {
Router,
Route,
Switch
Switch,
Redirect
//Link, NavLink
} from "react-router-dom";
import * as creacteHistory from "history";
@ -31,6 +32,10 @@ export const AppRouter = () => {
{/* <Route path="/tickets/:id">
<TicketController />
</Route> */}
<Route path="*">
<Redirect to="/404" />
</Route>
</Switch>
</div>
</Router>