mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 00:36:39 +00:00
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:
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
12
client/src/pages/ErrorPage.tsx
Normal file
12
client/src/pages/ErrorPage.tsx
Normal 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>
|
||||
);
|
||||
};
|
||||
|
|
@ -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
37
client/src/utils/http.ts
Normal 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));
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in a new issue