working calendar ranges with a subset of features

This commit is contained in:
2022-06-24 17:24:56 +02:00
parent a845fddf2e
commit 75b2f6419e
7 changed files with 266 additions and 42 deletions

View File

@@ -20,7 +20,11 @@ export interface FetchParams {
[K: string]: string|number|null;
};
export interface ValidationExceptionInterface {
export interface TransportExceptionInterface {
name: string;
}
export interface ValidationExceptionInterface extends TransportExceptionInterface {
name: 'ValidationException';
error: object;
violations: string[];
@@ -28,18 +32,27 @@ export interface ValidationExceptionInterface {
propertyPaths: string[];
}
export interface ValidationErrorResponse {
export interface ValidationErrorResponse extends TransportExceptionInterface {
violations: {
title: string;
propertyPath: string;
}[];
}
export interface AccessExceptionInterface {
export interface AccessExceptionInterface extends TransportExceptionInterface {
name: 'AccessException';
violations: string[];
}
export interface NotFoundExceptionInterface extends TransportExceptionInterface {
name: 'NotFoundException';
}
export interface ServerExceptionInterface extends TransportExceptionInterface {
name: 'ServerException';
message: string;
}
/**
* Generic api method that can be adapted to any fetch request
@@ -115,8 +128,16 @@ function _fetchAction<T>(page: number, uri: string, params?: FetchParams): Promi
},
}).then((response) => {
if (response.ok) { return response.json(); }
throw new Error(response.statusText);
});
if (response.status === 404) {
throw NotFoundException(response);
}
console.error(response);
throw ServerException();
}).catch((reason: any) => {
console.error(reason);
throw ServerException();
});
};
export const fetchResults = async<T> (uri: string, params?: FetchParams): Promise<T[]> => {
@@ -130,7 +151,8 @@ export const fetchResults = async<T> (uri: string, params?: FetchParams): Promis
do {
page = ++page;
promises.push(
_fetchAction<T>(page, uri, params).then(r => Promise.resolve(r.results))
_fetchAction<T>(page, uri, params)
.then(r => Promise.resolve(r.results))
);
} while (page * firstData.pagination.items_per_page < firstData.count)
}
@@ -162,3 +184,17 @@ const AccessException = (response: Response): AccessExceptionInterface => {
return error;
}
const NotFoundException = (response: Response): NotFoundExceptionInterface => {
const error = {} as NotFoundExceptionInterface;
error.name = 'NotFoundException';
return error;
}
const ServerException = (): ServerExceptionInterface => {
const error = {} as ServerExceptionInterface;
error.name = 'ServerException';
return error;
}