code cleanup
This commit is contained in:
parent
8607fdf9f1
commit
82b77906d7
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@openharbor/ngx-data",
|
||||
"version": "0.1.0",
|
||||
"version": "18.0.0-alpha.1",
|
||||
"dependencies": {
|
||||
"tslib": "^2.0.0"
|
||||
"tslib": "^2.7.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@angular/common": "^8.2.4",
|
||||
"@angular/core": "^8.2.4",
|
||||
"@angular/common": "^18.0.0",
|
||||
"@angular/core": "^18.0.0",
|
||||
"@poweredsoft/data": "^0.0.31",
|
||||
"rxjs": "^6.5.3"
|
||||
}
|
||||
|
@ -1,128 +1,138 @@
|
||||
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
|
||||
import { DataSource, IDataSource, IDataSourceCommandAdapterOptions, IDataSourceError, IDataSourceErrorMessage, IDataSourceOptions, IDataSourceQueryAdapterOptions, IDataSourceTransportOptions, IDataSourceValidationError, IQueryCriteria, IResolveCommandModelEvent } from "@poweredsoft/data";
|
||||
import { Observable, of, throwError } from "rxjs";
|
||||
import { catchError, switchMap } from "rxjs/operators";
|
||||
|
||||
|
||||
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
||||
import {
|
||||
DataSource,
|
||||
IDataSource,
|
||||
IDataSourceCommandAdapterOptions,
|
||||
IDataSourceError,
|
||||
IDataSourceErrorMessage,
|
||||
IDataSourceOptions,
|
||||
IDataSourceQueryAdapterOptions,
|
||||
IDataSourceTransportOptions,
|
||||
IDataSourceValidationError,
|
||||
IQueryCriteria,
|
||||
IResolveCommandModelEvent
|
||||
} from "@poweredsoft/data";
|
||||
import {Observable, of, throwError} from "rxjs";
|
||||
import {catchError, switchMap} from "rxjs/operators";
|
||||
|
||||
export abstract class BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||
protected _commands: { [key: string]: IDataSourceCommandAdapterOptions<any>; } = {};
|
||||
protected _keyResolver: (model: TModel) => TKey;
|
||||
protected _defaultCriteria: IQueryCriteria;
|
||||
protected _query: IDataSourceQueryAdapterOptions<TModel>;
|
||||
protected _commands: { [key: string]: IDataSourceCommandAdapterOptions<any>; } = {};
|
||||
protected _keyResolver: (model: TModel) => TKey;
|
||||
protected _defaultCriteria: IQueryCriteria;
|
||||
protected _query: IDataSourceQueryAdapterOptions<TModel>;
|
||||
|
||||
constructor(protected http: HttpClient) {
|
||||
}
|
||||
constructor(protected http: HttpClient) {
|
||||
}
|
||||
|
||||
createDataSource(): IDataSource<TModel> {
|
||||
return new DataSource<TModel>(this.createOptions());
|
||||
}
|
||||
createDataSource(): IDataSource<TModel> {
|
||||
return new DataSource<TModel>(this.createOptions());
|
||||
}
|
||||
|
||||
protected createTransport(): IDataSourceTransportOptions<TModel> {
|
||||
let ret: IDataSourceTransportOptions<TModel> = {
|
||||
query: this._query,
|
||||
commands: this._commands
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
protected createTransport(): IDataSourceTransportOptions<TModel> {
|
||||
let ret: IDataSourceTransportOptions<TModel> = {
|
||||
query: this._query,
|
||||
commands: this._commands
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
public keyResolver(resolver: (model: TModel) => TKey) {
|
||||
this._keyResolver = resolver;
|
||||
return this;
|
||||
}
|
||||
public keyResolver(resolver: (model: TModel) => TKey) {
|
||||
this._keyResolver = resolver;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
createOptions(): IDataSourceOptions<TModel> {
|
||||
let ret: IDataSourceOptions<TModel> = {
|
||||
resolveIdField: this._keyResolver,
|
||||
defaultCriteria: this._defaultCriteria,
|
||||
transport: this.createTransport()
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
createOptions(): IDataSourceOptions<TModel> {
|
||||
let ret: IDataSourceOptions<TModel> = {
|
||||
resolveIdField: this._keyResolver,
|
||||
defaultCriteria: this._defaultCriteria,
|
||||
transport: this.createTransport()
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
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") {
|
||||
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") {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: err.error
|
||||
});
|
||||
}
|
||||
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") {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: 'UNEXPECTED_ERROR_MESSAGE'
|
||||
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") {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: err.error
|
||||
});
|
||||
}
|
||||
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: 'UNEXPECTED_ERROR_MESSAGE'
|
||||
});
|
||||
}
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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.bind(this)));
|
||||
};
|
||||
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.bind(this)));
|
||||
};
|
||||
|
||||
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
||||
adapter: {
|
||||
handle: handleWrapper
|
||||
},
|
||||
resolveCommandModel: resolveCommandModel
|
||||
};
|
||||
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
||||
adapter: {
|
||||
handle: handleWrapper
|
||||
},
|
||||
resolveCommandModel: resolveCommandModel
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public addCommandByUrl<TCommand, TCommandResult>(name: string, url: string, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TCommand & any>, beforeCommand?: (command: TCommand) => Observable<TCommand>) {
|
||||
const handleWrapper = command => {
|
||||
const finalBeforeCommand = beforeCommand || (_ => of(command));
|
||||
return finalBeforeCommand(command)
|
||||
.pipe(
|
||||
switchMap(finalCommand => {
|
||||
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe.bind(this)));
|
||||
})
|
||||
);
|
||||
};
|
||||
public addCommandByUrl<TCommand, TCommandResult>(name: string, url: string, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TCommand & any>, beforeCommand?: (command: TCommand) => Observable<TCommand>) {
|
||||
const handleWrapper = command => {
|
||||
const finalBeforeCommand = beforeCommand || (_ => of(command));
|
||||
return finalBeforeCommand(command)
|
||||
.pipe(
|
||||
switchMap(finalCommand => {
|
||||
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe.bind(this)));
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
||||
adapter: {
|
||||
handle: handleWrapper
|
||||
},
|
||||
resolveCommandModel: resolveCommandModel
|
||||
};
|
||||
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
||||
adapter: {
|
||||
handle: handleWrapper
|
||||
},
|
||||
resolveCommandModel: resolveCommandModel
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,62 +1,60 @@
|
||||
import { HttpClient, HttpResponse } from "@angular/common/http";
|
||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult } from "@poweredsoft/data";
|
||||
import { Observable, of } from "rxjs";
|
||||
import { switchMap } from "rxjs/operators";
|
||||
import { BaseHttpDataSourceOptionsBuilder } from "./BaseHttpDataSourceOptionsBuilder";
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@poweredsoft/data";
|
||||
import {Observable, of} from "rxjs";
|
||||
import {switchMap} from "rxjs/operators";
|
||||
import {BaseHttpDataSourceOptionsBuilder} from "./BaseHttpDataSourceOptionsBuilder";
|
||||
|
||||
export class HttpDataSourceOptionsBuilder<TModel, TKey>
|
||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey>
|
||||
export class HttpDataSourceOptionsBuilder<TModel, TKey> extends BaseHttpDataSourceOptionsBuilder<TModel, TKey>
|
||||
{
|
||||
private _beforeRead: (TQuery: IQueryCriteria) => Observable<IQueryCriteria>;
|
||||
private _beforeRead: (TQuery: IQueryCriteria) => Observable<IQueryCriteria>;
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
|
||||
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>(url, finalQuery);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>(url, finalQuery);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public queryHandler<TQuery extends IQueryCriteria>(queryHandler: (query: TQuery) => Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return queryHandler(finalQuery as any);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryHandler<TQuery extends IQueryCriteria>(queryHandler: (query: TQuery) => Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return queryHandler(finalQuery as any);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
defaultCriteria(criteria: IQueryCriteria) {
|
||||
this._defaultCriteria = criteria;
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
defaultCriteria(criteria: IQueryCriteria) {
|
||||
this._defaultCriteria = criteria;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -1,107 +1,107 @@
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IQueryExecutionResultPaging } from "@poweredsoft/data";
|
||||
import { Observable, of } from "rxjs";
|
||||
import { map, switchMap } from "rxjs/operators";
|
||||
import { BaseHttpDataSourceOptionsBuilder } from "./BaseHttpDataSourceOptionsBuilder";
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@poweredsoft/data";
|
||||
import {Observable, of} from "rxjs";
|
||||
import {map, switchMap} from "rxjs/operators";
|
||||
import {BaseHttpDataSourceOptionsBuilder} from "./BaseHttpDataSourceOptionsBuilder";
|
||||
|
||||
export class ListDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey>
|
||||
export class ListDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey>
|
||||
{
|
||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
public queryUrlWithGet(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.get<TModel[]>(url, {
|
||||
params: this.convertToParams(finalQuery)
|
||||
}).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
||||
{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryUrlWithGet(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.get<TModel[]>(url, {
|
||||
params: this.convertToParams(finalQuery)
|
||||
}).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
||||
{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
protected convertToParams(finalQuery: TQuery)
|
||||
{
|
||||
return Object.keys(finalQuery).reduce((prev, key) => {
|
||||
prev[key] = finalQuery[key];
|
||||
return prev;
|
||||
}, {} as { [param: string]: string | string[]; });
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<TModel[]>(url, finalQuery).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
||||
{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
protected convertToParams(finalQuery: TQuery)
|
||||
{
|
||||
return Object.keys(finalQuery).reduce((prev, key) => {
|
||||
prev[key] = finalQuery[key];
|
||||
return prev;
|
||||
}, {} as { [param: string]: string | string[]; });
|
||||
}
|
||||
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<TModel[]>(url, finalQuery).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
||||
{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel[]>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return queryHandler(finalQuery).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel[]>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return queryHandler(finalQuery).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -1,103 +1,103 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IQueryExecutionResultPaging } from '@poweredsoft/data';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import { BaseHttpDataSourceOptionsBuilder } from './BaseHttpDataSourceOptionsBuilder';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from '@poweredsoft/data';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {BaseHttpDataSourceOptionsBuilder} from './BaseHttpDataSourceOptionsBuilder';
|
||||
|
||||
export class SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryUrlWithGet(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((query: IQueryCriteria) => of({} as TQuery));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.get<TModel>(url, {
|
||||
params: this.convertToParams(finalQuery)
|
||||
}).pipe(
|
||||
map(result => {
|
||||
return {
|
||||
totalRecords: result == null ? 0 : 1,
|
||||
data: [result]
|
||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
public queryUrlWithGet(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((query: IQueryCriteria) => of({} as TQuery));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.get<TModel>(url, {
|
||||
params: this.convertToParams(finalQuery)
|
||||
}).pipe(
|
||||
map(result => {
|
||||
return {
|
||||
totalRecords: result == null ? 0 : 1,
|
||||
data: [result]
|
||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected convertToParams(finalQuery: TQuery) {
|
||||
return Object.keys(finalQuery).reduce((prev, key) => {
|
||||
prev[key] = finalQuery[key];
|
||||
return prev;
|
||||
}, {} as { [param: string]: string | string[]; });
|
||||
}
|
||||
protected convertToParams(finalQuery: TQuery) {
|
||||
return Object.keys(finalQuery).reduce((prev, key) => {
|
||||
prev[key] = finalQuery[key];
|
||||
return prev;
|
||||
}, {} as { [param: string]: string | string[]; });
|
||||
}
|
||||
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of({} as TQuery));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<TModel>(url, finalQuery).pipe(
|
||||
map(result => {
|
||||
return {
|
||||
totalRecords: result == null ? 0 : 1,
|
||||
data: [result]
|
||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of({} as TQuery));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<TModel>(url, finalQuery).pipe(
|
||||
map(result => {
|
||||
return {
|
||||
totalRecords: result == null ? 0 : 1,
|
||||
data: [result]
|
||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return queryHandler(finalQuery).pipe(
|
||||
map(result => {
|
||||
return {
|
||||
totalRecords: result == null ? 0 : 1,
|
||||
data: [result]
|
||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return queryHandler(finalQuery).pipe(
|
||||
map(result => {
|
||||
return {
|
||||
totalRecords: result == null ? 0 : 1,
|
||||
data: [result]
|
||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import {Inject, Injectable} from "@angular/core";
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { HttpDataSourceOptionsBuilder } from "./HttpDataSourceBuilder";
|
||||
import { SingleDataSourceOptionsBuilder } from "./SingleObjectDataSourceBuilder";
|
||||
@ -6,21 +6,20 @@ import { ListDataSourceOptionsBuilder } from "./ListDataSourceBuilder";
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class HttpDataSourceService {
|
||||
constructor(private http: HttpClient) {
|
||||
}
|
||||
@Inject(HttpClient) protected http!: HttpClient;
|
||||
|
||||
builder<TModel, TKey>() {
|
||||
return new HttpDataSourceOptionsBuilder<TModel, TKey>(this.http);
|
||||
}
|
||||
builder<TModel, TKey>() {
|
||||
return new HttpDataSourceOptionsBuilder<TModel, TKey>(this.http);
|
||||
}
|
||||
|
||||
singleBuilder<TQuery, TModel, TKey>() {
|
||||
return new SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
}
|
||||
singleBuilder<TQuery, TModel, TKey>() {
|
||||
return new SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
}
|
||||
|
||||
listBuilder<TQuery, TModel, TKey>() {
|
||||
return new ListDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
}
|
||||
listBuilder<TQuery, TModel, TKey>() {
|
||||
return new ListDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +0,0 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
|
||||
|
||||
|
||||
@NgModule({
|
||||
declarations: [],
|
||||
imports: [
|
||||
],
|
||||
exports: [],
|
||||
})
|
||||
export class NgxDataModule { }
|
@ -1,130 +1,151 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
||||
import { IDataSourceTransportOptions, IDataSourceCommandAdapterOptions, IDataSourceOptions, IResolveCommandModelEvent, IDataSourceError, IDataSourceErrorMessage, IDataSourceValidationError } from '@poweredsoft/data';
|
||||
import { IQueryExecutionResult, IQueryExecutionGroupResult, IQueryCriteria } from '@poweredsoft/data';
|
||||
import { IDataSourceQueryAdapterOptions } from '@poweredsoft/data';
|
||||
import { catchError, switchMap} from 'rxjs/operators';
|
||||
import { throwError, Observable, of } from 'rxjs';
|
||||
import {Inject, Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
|
||||
import {
|
||||
IDataSourceCommandAdapterOptions,
|
||||
IDataSourceError,
|
||||
IDataSourceErrorMessage,
|
||||
IDataSourceOptions,
|
||||
IDataSourceQueryAdapterOptions,
|
||||
IDataSourceTransportOptions,
|
||||
IDataSourceValidationError,
|
||||
IQueryCriteria,
|
||||
IQueryExecutionGroupResult,
|
||||
IQueryExecutionResult,
|
||||
IResolveCommandModelEvent
|
||||
} from '@poweredsoft/data';
|
||||
import {catchError, switchMap} from 'rxjs/operators';
|
||||
import {Observable, of, throwError} from 'rxjs';
|
||||
|
||||
/**
|
||||
* providedIn: "root"
|
||||
*
|
||||
* Automatic Injection: When you use providedIn: "root", Angular automatically
|
||||
* provides the service in the root injector, making it a singleton and available
|
||||
* throughout the application without needing any manual import in a module.
|
||||
*
|
||||
* Tree-shakable: This method is tree-shakable, meaning if the service is not
|
||||
* used anywhere in the application, it can be removed during the build process,
|
||||
* reducing the final bundle size.
|
||||
*
|
||||
* Convenience: It is simpler for the user, as they do not need to worry about
|
||||
* importing the module for the service. They can just inject the service wherever
|
||||
* they need it.
|
||||
*
|
||||
*/
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class GenericRestDataSourceService
|
||||
{
|
||||
constructor(private http: HttpClient) {
|
||||
@Inject(HttpClient) protected http!: HttpClient;
|
||||
|
||||
private _handleErrorPipe(err: HttpErrorResponse) : Observable<IDataSourceError> {
|
||||
|
||||
if (err.status == 500) {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: 'UNEXPECTED_ERROR_MESSAGE'
|
||||
});
|
||||
}
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
// if status not okay then its an exception error
|
||||
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") {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: err.error
|
||||
});
|
||||
}
|
||||
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: 'UNEXPECTED_ERROR_MESSAGE'
|
||||
});
|
||||
}
|
||||
|
||||
createDataSourceOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, defaultCriteria: IQueryCriteria, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceOptions<TModel>
|
||||
if (err.status == 400)
|
||||
{
|
||||
const dataSourceTransportOptions = this.createStandardRestTransportOptions<TModel, TKey>(route, keyResolver, beforeRead);
|
||||
if (err.error && err.error.errors)
|
||||
return throwError(<IDataSourceValidationError>{
|
||||
type: 'validation',
|
||||
errors: err.error.errors
|
||||
});
|
||||
|
||||
const dataSourceOptions: IDataSourceOptions<TModel> = {
|
||||
defaultCriteria: defaultCriteria,
|
||||
resolveIdField: keyResolver,
|
||||
transport: dataSourceTransportOptions
|
||||
};
|
||||
|
||||
return dataSourceOptions;
|
||||
// if status not okay then it's an exception error
|
||||
if (err.error.hasOwnProperty('Message') && typeof(err.error['Message']) == "string") {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: err.error['Message']
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setResolveCommand<TModel>(options: IDataSourceOptions<TModel>, name: string, resolveCommandModel: (event: IResolveCommandModelEvent<TModel>) => Observable<any>) {
|
||||
options.transport.commands[name].resolveCommandModel = resolveCommandModel;
|
||||
// general error message
|
||||
if (typeof(err.error) == "string") {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: err.error
|
||||
});
|
||||
}
|
||||
|
||||
createStandardRestTransportOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceTransportOptions<TModel> {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
message: 'UNEXPECTED_ERROR_MESSAGE'
|
||||
});
|
||||
}
|
||||
|
||||
const query: IDataSourceQueryAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (criteria: IQueryCriteria) => {
|
||||
const queryRoute = `${route}/read`;
|
||||
const finalBeforeRead = beforeRead || (t => of(criteria));
|
||||
return finalBeforeRead(criteria)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>(queryRoute, finalQuery);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
createDataSourceOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, defaultCriteria: IQueryCriteria, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceOptions<TModel>
|
||||
{
|
||||
const dataSourceTransportOptions = this.createStandardRestTransportOptions<TModel, TKey>(route, keyResolver, beforeRead);
|
||||
|
||||
const createCommand: IDataSourceCommandAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (command: TModel) => {
|
||||
return this.http.post<TModel>(route, command).pipe(catchError(this._handleErrorPipe));
|
||||
}
|
||||
}
|
||||
};
|
||||
const dataSourceOptions: IDataSourceOptions<TModel> = {
|
||||
defaultCriteria: defaultCriteria,
|
||||
resolveIdField: keyResolver,
|
||||
transport: dataSourceTransportOptions
|
||||
};
|
||||
|
||||
const updateCommand: IDataSourceCommandAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (command: TModel) => {
|
||||
const key = keyResolver(command);
|
||||
const updateRoute = `${route}/${encodeURIComponent(key as any)}`;
|
||||
return this.http.put<TModel>(updateRoute, command).pipe(catchError(this._handleErrorPipe));
|
||||
}
|
||||
}
|
||||
};
|
||||
return dataSourceOptions;
|
||||
}
|
||||
|
||||
const deleteCommand: IDataSourceCommandAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (command: TModel) => {
|
||||
const key = keyResolver(command);
|
||||
const updateRoute = `${route}/${encodeURIComponent(key as any)}`;
|
||||
return this.http.delete<TModel>(updateRoute).pipe(catchError(this._handleErrorPipe));
|
||||
}
|
||||
}
|
||||
};
|
||||
setResolveCommand<TModel>(options: IDataSourceOptions<TModel>, name: string, resolveCommandModel: (event: IResolveCommandModelEvent<TModel>) => Observable<any>) {
|
||||
options.transport.commands[name].resolveCommandModel = resolveCommandModel;
|
||||
}
|
||||
|
||||
return {
|
||||
query: query,
|
||||
commands: {
|
||||
'create': createCommand,
|
||||
'update': updateCommand,
|
||||
'delete': deleteCommand
|
||||
}
|
||||
};
|
||||
}
|
||||
createStandardRestTransportOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceTransportOptions<TModel> {
|
||||
const query: IDataSourceQueryAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (criteria: IQueryCriteria) => {
|
||||
const queryRoute = `${route}/read`;
|
||||
const finalBeforeRead = beforeRead || (t => of(criteria));
|
||||
return finalBeforeRead(criteria)
|
||||
.pipe(switchMap(finalQuery => {
|
||||
return this.http.post<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>(queryRoute, finalQuery);
|
||||
}));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const createCommand: IDataSourceCommandAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (command: TModel) => {
|
||||
return this.http.post<TModel>(route, command).pipe(catchError(this._handleErrorPipe));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const updateCommand: IDataSourceCommandAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (command: TModel) => {
|
||||
const key = keyResolver(command);
|
||||
const updateRoute = `${route}/${encodeURIComponent(key as any)}`;
|
||||
return this.http.put<TModel>(updateRoute, command).pipe(catchError(this._handleErrorPipe));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const deleteCommand: IDataSourceCommandAdapterOptions<TModel> = {
|
||||
adapter: {
|
||||
handle: (command: TModel) => {
|
||||
const key = keyResolver(command);
|
||||
const updateRoute = `${route}/${encodeURIComponent(key as any)}`;
|
||||
return this.http.delete<TModel>(updateRoute).pipe(catchError(this._handleErrorPipe));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
query: query,
|
||||
commands: {
|
||||
'create': createCommand,
|
||||
'update': updateCommand,
|
||||
'delete': deleteCommand
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
/*
|
||||
* Public API Surface of ngx-data
|
||||
*/
|
||||
|
||||
export * from './lib/ngx-data.service';
|
||||
export * from './lib/http-data-source-service.service'
|
||||
export * from './lib/ngx-data.module';
|
||||
|
Loading…
Reference in New Issue
Block a user