This commit is contained in:
David Lebee 2021-07-29 17:12:21 -04:00
parent 99bcbf45ed
commit 2972b38a1a
3 changed files with 41 additions and 32 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@poweredsoft/ngx-data",
"version": "0.0.19",
"version": "0.0.20",
"peerDependencies": {
"@angular/common": "^8.2.4",
"@angular/core": "^8.2.4",

View File

@ -1,4 +1,4 @@
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
import { HttpClient, HttpErrorResponse, HttpResponse } from "@angular/common/http";
import { DataSource, IDataSource, IDataSourceCommandAdapterOptions, IDataSourceError, IDataSourceErrorMessage, IDataSourceOptions, IDataSourceQueryAdapterOptions, IDataSourceTransportOptions, IDataSourceValidationError, IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IResolveCommandModelEvent } from "@poweredsoft/data";
import { Observable, of, throwError } from "rxjs";
import { catchError, switchMap } from "rxjs/operators";
@ -7,7 +7,7 @@ import { catchError, switchMap } from "rxjs/operators";
export class HttpDataSourceOptionsBuilder<TModel, TKey> {
private _commands: { [key: string] : IDataSourceCommandAdapterOptions<any> } = {};
private _commands: { [key: string]: IDataSourceCommandAdapterOptions<any> } = {};
private _beforeRead: (TQuery: IQueryCriteria) => Observable<IQueryCriteria>;
private _keyResolver: (model: TModel) => TKey;
private _defaultCriteria: IQueryCriteria;
@ -26,7 +26,7 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
return ret;
}
createDataSource() : IDataSource<TModel>{
createDataSource(): IDataSource<TModel> {
return new DataSource<TModel>(this.createOptions());
}
@ -43,8 +43,7 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
return this;
}
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>)
{
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>) {
this._beforeRead = beforeRead;
return this;
}
@ -85,34 +84,26 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
return this;
}
private _handleErrorPipe(err: HttpErrorResponse) : Observable<IDataSourceError> {
if (err.status == 500) {
return throwError(<IDataSourceErrorMessage>{
type: 'message',
message: 'UNEXPECTED_ERROR_MESSAGE'
});
}
if (err.status == 400)
{
if (err.error && err.error.errors)
return throwError(<IDataSourceValidationError>{
type: 'validation',
errors: err.error.errors
});
private _messageErrorHandler(err: HttpErrorResponse) {
if (typeof err.error == "object") {
// if status not okay then its an exception error
if (err.error.hasOwnProperty('Message') && typeof(err.error['Message']) == "string") {
if (err.error.hasOwnProperty('Message') && typeof (err.error['Message']) == "string") {
return throwError(<IDataSourceErrorMessage>{
type: 'message',
message: err.error['Message']
});
}
else if (err.error.hasOwnProperty('message') && typeof (err.error['message']) == "string") {
return throwError(<IDataSourceErrorMessage>{
type: 'message',
message: err.error['message']
});
}
}
// general error message
if (typeof(err.error) == "string") {
if (typeof (err.error) == "string") {
return throwError(<IDataSourceErrorMessage>{
type: 'message',
message: err.error
@ -125,12 +116,25 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
});
}
private _handleErrorPipe(err: HttpErrorResponse): Observable<IDataSourceError> {
if (err.status == 400) {
if (err.error && err.error.errors)
return throwError(<IDataSourceValidationError>{
type: 'validation',
errors: err.error.errors
});
}
return this._messageErrorHandler(err);
}
public addCommandByCallback<TCommand, TCommandResult>(name: string, commandHandler: (command: TCommand) => Observable<TCommandResult>, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TCommand & any>) {
const handleWrapper = command => {
return commandHandler(command).pipe(catchError(this._handleErrorPipe));
return commandHandler(command).pipe(catchError(err => this._handleErrorPipe.bind(this)));
};
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>> {
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
adapter: {
handle: handleWrapper
},
@ -146,12 +150,12 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
return finalBeforeCommand(command)
.pipe(
switchMap(finalCommand => {
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe));
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe.bind(this)));
})
);
};
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>> {
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
adapter: {
handle: handleWrapper
},

View File

@ -75,6 +75,11 @@ export class AppComponent implements OnInit {
this.dataSource.data$.subscribe(newData => {
this.latestData = newData;
});
this.dataSource.notifyMessage$.subscribe(message => {
if (message.type == 'error')
alert(message.message);
});
}
refresh() {