add feature add and delete range

This commit is contained in:
2022-06-24 18:52:28 +02:00
parent 75b2f6419e
commit 3ea630748a
7 changed files with 137 additions and 35 deletions

View File

@@ -51,13 +51,15 @@ export interface NotFoundExceptionInterface extends TransportExceptionInterface
export interface ServerExceptionInterface extends TransportExceptionInterface {
name: 'ServerException';
message: string;
code: number;
body: string;
}
/**
* Generic api method that can be adapted to any fetch request
*/
export const makeFetch = <T, Q>(method: 'POST'|'GET'|'PUT'|'PATCH'|'DELETE', url: string, body?: body | T | null, options?: FetchParams): Promise<Q> => {
export const makeFetch = <Input, Output>(method: 'POST'|'GET'|'PUT'|'PATCH'|'DELETE', url: string, body?: body | Input | null, options?: FetchParams): Promise<Output> => {
let opts = {
method: method,
headers: {
@@ -128,15 +130,31 @@ function _fetchAction<T>(page: number, uri: string, params?: FetchParams): Promi
},
}).then((response) => {
if (response.ok) { return response.json(); }
if (response.status === 404) {
throw NotFoundException(response);
}
console.error(response);
throw ServerException();
if (response.status === 422) {
return response.json().then(response => {
throw ValidationException(response)
});
}
if (response.status === 403) {
throw AccessException(response);
}
if (response.status >= 500) {
return response.text().then(body => {
throw ServerException(response.status, body);
});
}
throw new Error("other network error");
}).catch((reason: any) => {
console.error(reason);
throw ServerException();
throw new Error(reason);
});
};
@@ -192,9 +210,11 @@ const NotFoundException = (response: Response): NotFoundExceptionInterface => {
return error;
}
const ServerException = (): ServerExceptionInterface => {
const ServerException = (code: number, body: string): ServerExceptionInterface => {
const error = {} as ServerExceptionInterface;
error.name = 'ServerException';
error.code = code;
error.body = body;
return error;
}

View File

@@ -34,3 +34,8 @@ export interface User {
user_job: Job;
// todo: mainCenter; mainJob; etc..
}
export interface UserAssociatedInterface {
type: "user";
id: number;
};