code cleanup
This commit is contained in:
parent
8607fdf9f1
commit
82b77906d7
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@openharbor/ngx-data",
|
"name": "@openharbor/ngx-data",
|
||||||
"version": "0.1.0",
|
"version": "18.0.0-alpha.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"tslib": "^2.0.0"
|
"tslib": "^2.7.0"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@angular/common": "^8.2.4",
|
"@angular/common": "^18.0.0",
|
||||||
"@angular/core": "^8.2.4",
|
"@angular/core": "^18.0.0",
|
||||||
"@poweredsoft/data": "^0.0.31",
|
"@poweredsoft/data": "^0.0.31",
|
||||||
"rxjs": "^6.5.3"
|
"rxjs": "^6.5.3"
|
||||||
}
|
}
|
||||||
|
@ -1,128 +1,138 @@
|
|||||||
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
|
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
||||||
import { DataSource, IDataSource, IDataSourceCommandAdapterOptions, IDataSourceError, IDataSourceErrorMessage, IDataSourceOptions, IDataSourceQueryAdapterOptions, IDataSourceTransportOptions, IDataSourceValidationError, IQueryCriteria, IResolveCommandModelEvent } from "@poweredsoft/data";
|
import {
|
||||||
import { Observable, of, throwError } from "rxjs";
|
DataSource,
|
||||||
import { catchError, switchMap } from "rxjs/operators";
|
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> {
|
export abstract class BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||||
protected _commands: { [key: string]: IDataSourceCommandAdapterOptions<any>; } = {};
|
protected _commands: { [key: string]: IDataSourceCommandAdapterOptions<any>; } = {};
|
||||||
protected _keyResolver: (model: TModel) => TKey;
|
protected _keyResolver: (model: TModel) => TKey;
|
||||||
protected _defaultCriteria: IQueryCriteria;
|
protected _defaultCriteria: IQueryCriteria;
|
||||||
protected _query: IDataSourceQueryAdapterOptions<TModel>;
|
protected _query: IDataSourceQueryAdapterOptions<TModel>;
|
||||||
|
|
||||||
constructor(protected http: HttpClient) {
|
constructor(protected http: HttpClient) {
|
||||||
}
|
}
|
||||||
|
|
||||||
createDataSource(): IDataSource<TModel> {
|
createDataSource(): IDataSource<TModel> {
|
||||||
return new DataSource<TModel>(this.createOptions());
|
return new DataSource<TModel>(this.createOptions());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected createTransport(): IDataSourceTransportOptions<TModel> {
|
protected createTransport(): IDataSourceTransportOptions<TModel> {
|
||||||
let ret: IDataSourceTransportOptions<TModel> = {
|
let ret: IDataSourceTransportOptions<TModel> = {
|
||||||
query: this._query,
|
query: this._query,
|
||||||
commands: this._commands
|
commands: this._commands
|
||||||
};
|
};
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public keyResolver(resolver: (model: TModel) => TKey) {
|
public keyResolver(resolver: (model: TModel) => TKey) {
|
||||||
this._keyResolver = resolver;
|
this._keyResolver = resolver;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
createOptions(): IDataSourceOptions<TModel> {
|
createOptions(): IDataSourceOptions<TModel> {
|
||||||
let ret: IDataSourceOptions<TModel> = {
|
let ret: IDataSourceOptions<TModel> = {
|
||||||
resolveIdField: this._keyResolver,
|
resolveIdField: this._keyResolver,
|
||||||
defaultCriteria: this._defaultCriteria,
|
defaultCriteria: this._defaultCriteria,
|
||||||
transport: this.createTransport()
|
transport: this.createTransport()
|
||||||
};
|
};
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _messageErrorHandler(err: HttpErrorResponse) {
|
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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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>{
|
return throwError(<IDataSourceErrorMessage>{
|
||||||
type: 'message',
|
type: 'message',
|
||||||
message: 'UNEXPECTED_ERROR_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> {
|
return this._messageErrorHandler(err);
|
||||||
|
}
|
||||||
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>) {
|
public addCommandByCallback<TCommand, TCommandResult>(name: string, commandHandler: (command: TCommand) => Observable<TCommandResult>, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TCommand & any>) {
|
||||||
const handleWrapper = command => {
|
const handleWrapper = command => {
|
||||||
return commandHandler(command).pipe(catchError(this._handleErrorPipe.bind(this)));
|
return commandHandler(command).pipe(catchError(this._handleErrorPipe.bind(this)));
|
||||||
};
|
};
|
||||||
|
|
||||||
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
||||||
adapter: {
|
adapter: {
|
||||||
handle: handleWrapper
|
handle: handleWrapper
|
||||||
},
|
},
|
||||||
resolveCommandModel: resolveCommandModel
|
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>) {
|
public addCommandByUrl<TCommand, TCommandResult>(name: string, url: string, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TCommand & any>, beforeCommand?: (command: TCommand) => Observable<TCommand>) {
|
||||||
const handleWrapper = command => {
|
const handleWrapper = command => {
|
||||||
const finalBeforeCommand = beforeCommand || (_ => of(command));
|
const finalBeforeCommand = beforeCommand || (_ => of(command));
|
||||||
return finalBeforeCommand(command)
|
return finalBeforeCommand(command)
|
||||||
.pipe(
|
.pipe(
|
||||||
switchMap(finalCommand => {
|
switchMap(finalCommand => {
|
||||||
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe.bind(this)));
|
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe.bind(this)));
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
|
||||||
adapter: {
|
adapter: {
|
||||||
handle: handleWrapper
|
handle: handleWrapper
|
||||||
},
|
},
|
||||||
resolveCommandModel: resolveCommandModel
|
resolveCommandModel: resolveCommandModel
|
||||||
};
|
};
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,60 @@
|
|||||||
import { HttpClient, HttpResponse } from "@angular/common/http";
|
import {HttpClient} from "@angular/common/http";
|
||||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult } from "@poweredsoft/data";
|
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@poweredsoft/data";
|
||||||
import { Observable, of } from "rxjs";
|
import {Observable, of} from "rxjs";
|
||||||
import { switchMap } from "rxjs/operators";
|
import {switchMap} from "rxjs/operators";
|
||||||
import { BaseHttpDataSourceOptionsBuilder } from "./BaseHttpDataSourceOptionsBuilder";
|
import {BaseHttpDataSourceOptionsBuilder} from "./BaseHttpDataSourceOptionsBuilder";
|
||||||
|
|
||||||
export class HttpDataSourceOptionsBuilder<TModel, TKey>
|
export class HttpDataSourceOptionsBuilder<TModel, TKey> extends BaseHttpDataSourceOptionsBuilder<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) {
|
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>) {
|
||||||
super(http);
|
this._beforeRead = beforeRead;
|
||||||
}
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>) {
|
public queryUrl(url: string) {
|
||||||
this._beforeRead = beforeRead;
|
this._query = {
|
||||||
return this;
|
adapter: {
|
||||||
}
|
handle: (query: IQueryCriteria) => {
|
||||||
|
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||||
public queryUrl(url: string) {
|
return finalBeforeRead(query)
|
||||||
this._query = {
|
.pipe(
|
||||||
adapter: {
|
switchMap(finalQuery => {
|
||||||
handle: (query: IQueryCriteria) => {
|
return this.http.post<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>(url, finalQuery);
|
||||||
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>>) {
|
return this;
|
||||||
this._query = {
|
}
|
||||||
adapter: {
|
|
||||||
handle: (query: TQuery) => {
|
public queryHandler<TQuery extends IQueryCriteria>(queryHandler: (query: TQuery) => Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>) {
|
||||||
const finalBeforeRead = this._beforeRead || (t => of(query));
|
this._query = {
|
||||||
return finalBeforeRead(query)
|
adapter: {
|
||||||
.pipe(
|
handle: (query: TQuery) => {
|
||||||
switchMap(finalQuery => {
|
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||||
return queryHandler(finalQuery as any);
|
return finalBeforeRead(query)
|
||||||
})
|
.pipe(
|
||||||
);
|
switchMap(finalQuery => {
|
||||||
}
|
return queryHandler(finalQuery as any);
|
||||||
}
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultCriteria(criteria: IQueryCriteria) {
|
return this;
|
||||||
this._defaultCriteria = criteria;
|
}
|
||||||
return this;
|
|
||||||
}
|
defaultCriteria(criteria: IQueryCriteria) {
|
||||||
|
this._defaultCriteria = criteria;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,107 +1,107 @@
|
|||||||
import { HttpClient } from "@angular/common/http";
|
import {HttpClient} from "@angular/common/http";
|
||||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IQueryExecutionResultPaging } from "@poweredsoft/data";
|
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@poweredsoft/data";
|
||||||
import { Observable, of } from "rxjs";
|
import {Observable, of} from "rxjs";
|
||||||
import { map, switchMap } from "rxjs/operators";
|
import {map, switchMap} from "rxjs/operators";
|
||||||
import { BaseHttpDataSourceOptionsBuilder } from "./BaseHttpDataSourceOptionsBuilder";
|
import {BaseHttpDataSourceOptionsBuilder} from "./BaseHttpDataSourceOptionsBuilder";
|
||||||
|
|
||||||
export class ListDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
export class ListDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey>
|
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey>
|
||||||
{
|
{
|
||||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||||
|
|
||||||
constructor(http: HttpClient) {
|
|
||||||
super(http);
|
|
||||||
}
|
|
||||||
|
|
||||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
constructor(http: HttpClient) {
|
||||||
this._beforeRead = beforeRead;
|
super(http);
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public queryUrlWithGet(url: string) {
|
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||||
this._query = {
|
this._beforeRead = beforeRead;
|
||||||
adapter: {
|
return this;
|
||||||
handle: (query: IQueryCriteria) => {
|
}
|
||||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
|
||||||
return finalBeforeRead(query)
|
public queryUrlWithGet(url: string) {
|
||||||
.pipe(
|
this._query = {
|
||||||
switchMap(finalQuery => {
|
adapter: {
|
||||||
return this.http.get<TModel[]>(url, {
|
handle: (query: IQueryCriteria) => {
|
||||||
params: this.convertToParams(finalQuery)
|
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||||
}).pipe(
|
return finalBeforeRead(query)
|
||||||
map(result => {
|
.pipe(
|
||||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
switchMap(finalQuery => {
|
||||||
{
|
return this.http.get<TModel[]>(url, {
|
||||||
totalRecords: result.length,
|
params: this.convertToParams(finalQuery)
|
||||||
data: result
|
}).pipe(
|
||||||
};
|
map(result => {
|
||||||
})
|
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
||||||
)
|
{
|
||||||
})
|
totalRecords: result.length,
|
||||||
);
|
data: result
|
||||||
}
|
};
|
||||||
}
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected convertToParams(finalQuery: TQuery)
|
return this;
|
||||||
{
|
}
|
||||||
return Object.keys(finalQuery).reduce((prev, key) => {
|
|
||||||
prev[key] = finalQuery[key];
|
|
||||||
return prev;
|
|
||||||
}, {} as { [param: string]: string | string[]; });
|
|
||||||
}
|
|
||||||
|
|
||||||
public queryUrl(url: string) {
|
protected convertToParams(finalQuery: TQuery)
|
||||||
this._query = {
|
{
|
||||||
adapter: {
|
return Object.keys(finalQuery).reduce((prev, key) => {
|
||||||
handle: (query: IQueryCriteria) => {
|
prev[key] = finalQuery[key];
|
||||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
return prev;
|
||||||
return finalBeforeRead(query)
|
}, {} as { [param: string]: string | string[]; });
|
||||||
.pipe(
|
}
|
||||||
switchMap(finalQuery => {
|
|
||||||
return this.http.post<TModel[]>(url, finalQuery).pipe(
|
public queryUrl(url: string) {
|
||||||
map(result => {
|
this._query = {
|
||||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
adapter: {
|
||||||
{
|
handle: (query: IQueryCriteria) => {
|
||||||
totalRecords: result.length,
|
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||||
data: result
|
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[]>) {
|
return this;
|
||||||
this._query = {
|
}
|
||||||
adapter: {
|
|
||||||
handle: (query: TQuery) => {
|
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel[]>) {
|
||||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
this._query = {
|
||||||
return finalBeforeRead(query)
|
adapter: {
|
||||||
.pipe(
|
handle: (query: TQuery) => {
|
||||||
switchMap(finalQuery => {
|
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||||
return queryHandler(finalQuery).pipe(
|
return finalBeforeRead(query)
|
||||||
map(result => {
|
.pipe(
|
||||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>{
|
switchMap(finalQuery => {
|
||||||
totalRecords: result.length,
|
return queryHandler(finalQuery).pipe(
|
||||||
data: result
|
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 {HttpClient} from '@angular/common/http';
|
||||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IQueryExecutionResultPaging } from '@poweredsoft/data';
|
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from '@poweredsoft/data';
|
||||||
import { Observable, of } from 'rxjs';
|
import {Observable, of} from 'rxjs';
|
||||||
import { map, switchMap } from 'rxjs/operators';
|
import {map, switchMap} from 'rxjs/operators';
|
||||||
import { BaseHttpDataSourceOptionsBuilder } from './BaseHttpDataSourceOptionsBuilder';
|
import {BaseHttpDataSourceOptionsBuilder} from './BaseHttpDataSourceOptionsBuilder';
|
||||||
|
|
||||||
export class SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
export class SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||||
|
|
||||||
constructor(http: HttpClient) {
|
constructor(http: HttpClient) {
|
||||||
super(http);
|
super(http);
|
||||||
}
|
}
|
||||||
|
|
||||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||||
this._beforeRead = beforeRead;
|
this._beforeRead = beforeRead;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public queryUrlWithGet(url: string) {
|
public queryUrlWithGet(url: string) {
|
||||||
this._query = {
|
this._query = {
|
||||||
adapter: {
|
adapter: {
|
||||||
handle: (query: IQueryCriteria) => {
|
handle: (query: IQueryCriteria) => {
|
||||||
const finalBeforeRead = this._beforeRead || ((query: IQueryCriteria) => of({} as TQuery));
|
const finalBeforeRead = this._beforeRead || ((query: IQueryCriteria) => of({} as TQuery));
|
||||||
return finalBeforeRead(query)
|
return finalBeforeRead(query)
|
||||||
.pipe(
|
.pipe(
|
||||||
switchMap(finalQuery => {
|
switchMap(finalQuery => {
|
||||||
return this.http.get<TModel>(url, {
|
return this.http.get<TModel>(url, {
|
||||||
params: this.convertToParams(finalQuery)
|
params: this.convertToParams(finalQuery)
|
||||||
}).pipe(
|
}).pipe(
|
||||||
map(result => {
|
map(result => {
|
||||||
return {
|
return {
|
||||||
totalRecords: result == null ? 0 : 1,
|
totalRecords: result == null ? 0 : 1,
|
||||||
data: [result]
|
data: [result]
|
||||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected convertToParams(finalQuery: TQuery) {
|
protected convertToParams(finalQuery: TQuery) {
|
||||||
return Object.keys(finalQuery).reduce((prev, key) => {
|
return Object.keys(finalQuery).reduce((prev, key) => {
|
||||||
prev[key] = finalQuery[key];
|
prev[key] = finalQuery[key];
|
||||||
return prev;
|
return prev;
|
||||||
}, {} as { [param: string]: string | string[]; });
|
}, {} as { [param: string]: string | string[]; });
|
||||||
}
|
}
|
||||||
|
|
||||||
public queryUrl(url: string) {
|
public queryUrl(url: string) {
|
||||||
this._query = {
|
this._query = {
|
||||||
adapter: {
|
adapter: {
|
||||||
handle: (query: IQueryCriteria) => {
|
handle: (query: IQueryCriteria) => {
|
||||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of({} as TQuery));
|
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of({} as TQuery));
|
||||||
return finalBeforeRead(query)
|
return finalBeforeRead(query)
|
||||||
.pipe(
|
.pipe(
|
||||||
switchMap(finalQuery => {
|
switchMap(finalQuery => {
|
||||||
return this.http.post<TModel>(url, finalQuery).pipe(
|
return this.http.post<TModel>(url, finalQuery).pipe(
|
||||||
map(result => {
|
map(result => {
|
||||||
return {
|
return {
|
||||||
totalRecords: result == null ? 0 : 1,
|
totalRecords: result == null ? 0 : 1,
|
||||||
data: [result]
|
data: [result]
|
||||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel>) {
|
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel>) {
|
||||||
this._query = {
|
this._query = {
|
||||||
adapter: {
|
adapter: {
|
||||||
handle: (query: TQuery) => {
|
handle: (query: TQuery) => {
|
||||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||||
return finalBeforeRead(query)
|
return finalBeforeRead(query)
|
||||||
.pipe(
|
.pipe(
|
||||||
switchMap(finalQuery => {
|
switchMap(finalQuery => {
|
||||||
return queryHandler(finalQuery).pipe(
|
return queryHandler(finalQuery).pipe(
|
||||||
map(result => {
|
map(result => {
|
||||||
return {
|
return {
|
||||||
totalRecords: result == null ? 0 : 1,
|
totalRecords: result == null ? 0 : 1,
|
||||||
data: [result]
|
data: [result]
|
||||||
} as IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
|
} 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 { HttpClient } from '@angular/common/http';
|
||||||
import { HttpDataSourceOptionsBuilder } from "./HttpDataSourceBuilder";
|
import { HttpDataSourceOptionsBuilder } from "./HttpDataSourceBuilder";
|
||||||
import { SingleDataSourceOptionsBuilder } from "./SingleObjectDataSourceBuilder";
|
import { SingleDataSourceOptionsBuilder } from "./SingleObjectDataSourceBuilder";
|
||||||
@ -6,21 +6,20 @@ import { ListDataSourceOptionsBuilder } from "./ListDataSourceBuilder";
|
|||||||
|
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class HttpDataSourceService {
|
export class HttpDataSourceService {
|
||||||
constructor(private http: HttpClient) {
|
@Inject(HttpClient) protected http!: HttpClient;
|
||||||
}
|
|
||||||
|
|
||||||
builder<TModel, TKey>() {
|
builder<TModel, TKey>() {
|
||||||
return new HttpDataSourceOptionsBuilder<TModel, TKey>(this.http);
|
return new HttpDataSourceOptionsBuilder<TModel, TKey>(this.http);
|
||||||
}
|
}
|
||||||
|
|
||||||
singleBuilder<TQuery, TModel, TKey>() {
|
singleBuilder<TQuery, TModel, TKey>() {
|
||||||
return new SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
return new SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||||
}
|
}
|
||||||
|
|
||||||
listBuilder<TQuery, TModel, TKey>() {
|
listBuilder<TQuery, TModel, TKey>() {
|
||||||
return new ListDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
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 {Inject, Injectable} from '@angular/core';
|
||||||
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
|
||||||
import { IDataSourceTransportOptions, IDataSourceCommandAdapterOptions, IDataSourceOptions, IResolveCommandModelEvent, IDataSourceError, IDataSourceErrorMessage, IDataSourceValidationError } from '@poweredsoft/data';
|
import {
|
||||||
import { IQueryExecutionResult, IQueryExecutionGroupResult, IQueryCriteria } from '@poweredsoft/data';
|
IDataSourceCommandAdapterOptions,
|
||||||
import { IDataSourceQueryAdapterOptions } from '@poweredsoft/data';
|
IDataSourceError,
|
||||||
import { catchError, switchMap} from 'rxjs/operators';
|
IDataSourceErrorMessage,
|
||||||
import { throwError, Observable, of } from 'rxjs';
|
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({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class GenericRestDataSourceService
|
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 == 400)
|
||||||
|
|
||||||
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>
|
|
||||||
{
|
{
|
||||||
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> = {
|
// if status not okay then it's an exception error
|
||||||
defaultCriteria: defaultCriteria,
|
if (err.error.hasOwnProperty('Message') && typeof(err.error['Message']) == "string") {
|
||||||
resolveIdField: keyResolver,
|
return throwError(<IDataSourceErrorMessage>{
|
||||||
transport: dataSourceTransportOptions
|
type: 'message',
|
||||||
};
|
message: err.error['Message']
|
||||||
|
});
|
||||||
return dataSourceOptions;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setResolveCommand<TModel>(options: IDataSourceOptions<TModel>, name: string, resolveCommandModel: (event: IResolveCommandModelEvent<TModel>) => Observable<any>) {
|
// general error message
|
||||||
options.transport.commands[name].resolveCommandModel = resolveCommandModel;
|
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> = {
|
createDataSourceOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, defaultCriteria: IQueryCriteria, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceOptions<TModel>
|
||||||
adapter: {
|
{
|
||||||
handle: (criteria: IQueryCriteria) => {
|
const dataSourceTransportOptions = this.createStandardRestTransportOptions<TModel, TKey>(route, keyResolver, beforeRead);
|
||||||
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> = {
|
const dataSourceOptions: IDataSourceOptions<TModel> = {
|
||||||
adapter: {
|
defaultCriteria: defaultCriteria,
|
||||||
handle: (command: TModel) => {
|
resolveIdField: keyResolver,
|
||||||
return this.http.post<TModel>(route, command).pipe(catchError(this._handleErrorPipe));
|
transport: dataSourceTransportOptions
|
||||||
}
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateCommand: IDataSourceCommandAdapterOptions<TModel> = {
|
return dataSourceOptions;
|
||||||
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> = {
|
setResolveCommand<TModel>(options: IDataSourceOptions<TModel>, name: string, resolveCommandModel: (event: IResolveCommandModelEvent<TModel>) => Observable<any>) {
|
||||||
adapter: {
|
options.transport.commands[name].resolveCommandModel = resolveCommandModel;
|
||||||
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 {
|
createStandardRestTransportOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceTransportOptions<TModel> {
|
||||||
query: query,
|
const query: IDataSourceQueryAdapterOptions<TModel> = {
|
||||||
commands: {
|
adapter: {
|
||||||
'create': createCommand,
|
handle: (criteria: IQueryCriteria) => {
|
||||||
'update': updateCommand,
|
const queryRoute = `${route}/read`;
|
||||||
'delete': deleteCommand
|
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
|
* Public API Surface of ngx-data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export * from './lib/ngx-data.service';
|
export * from './lib/ngx-data.service';
|
||||||
export * from './lib/http-data-source-service.service'
|
export * from './lib/http-data-source-service.service'
|
||||||
export * from './lib/ngx-data.module';
|
|
||||||
|
Loading…
Reference in New Issue
Block a user