update library to use @openharbor/data instead with refactorings
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@openharbor/ngx-data",
|
||||
"version": "18.0.0-alpha.3",
|
||||
"version": "18.0.0-alpha.4",
|
||||
"repository": "https://git.openharbor.io/Open-Harbor/ngx-data",
|
||||
"dependencies": {
|
||||
"tslib": "^2.7.0"
|
||||
@@ -8,7 +8,7 @@
|
||||
"peerDependencies": {
|
||||
"@angular/common": "^18.0.0",
|
||||
"@angular/core": "^18.0.0",
|
||||
"@poweredsoft/data": "^0.0.31",
|
||||
"@openharbor/data": "^1.0.0",
|
||||
"rxjs": "^6.5.3||^7.4.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,50 +11,47 @@ import {
|
||||
IDataSourceValidationError,
|
||||
IQueryCriteria,
|
||||
IResolveCommandModelEvent
|
||||
} from "@poweredsoft/data";
|
||||
} from "@openharbor/data";
|
||||
import {Observable, of, throwError} from "rxjs";
|
||||
import {catchError, switchMap} from "rxjs/operators";
|
||||
import {inject} from "@angular/core";
|
||||
|
||||
export abstract class BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||
export abstract class BaseHttpDataSourceOptionsBuilder<TQuery, TModel> {
|
||||
protected _commands: { [key: string]: IDataSourceCommandAdapterOptions<any>; } = {};
|
||||
protected _keyResolver: (model: TModel) => TKey;
|
||||
protected _keyResolver: (model: TModel) => TModel[keyof TModel];
|
||||
protected _defaultCriteria: IQueryCriteria;
|
||||
protected _query: IDataSourceQueryAdapterOptions<TModel>;
|
||||
protected _query: IDataSourceQueryAdapterOptions<TQuery, TModel>;
|
||||
|
||||
constructor(protected http: HttpClient) {
|
||||
readonly http: HttpClient = inject(HttpClient);
|
||||
|
||||
createDataSource(): IDataSource<TQuery, TModel> {
|
||||
return new DataSource<TQuery, TModel>(this.createOptions());
|
||||
}
|
||||
|
||||
createDataSource(): IDataSource<TModel> {
|
||||
return new DataSource<TModel>(this.createOptions());
|
||||
}
|
||||
|
||||
protected createTransport(): IDataSourceTransportOptions<TModel> {
|
||||
let ret: IDataSourceTransportOptions<TModel> = {
|
||||
protected createTransport(): IDataSourceTransportOptions<TQuery, TModel> {
|
||||
return {
|
||||
query: this._query,
|
||||
commands: this._commands
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
public keyResolver(resolver: (model: TModel) => TKey) {
|
||||
public keyResolver(resolver: (model: TModel) => TModel[keyof TModel]) {
|
||||
this._keyResolver = resolver;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
createOptions(): IDataSourceOptions<TModel> {
|
||||
let ret: IDataSourceOptions<TModel> = {
|
||||
createOptions(): IDataSourceOptions<TQuery, TModel> {
|
||||
return {
|
||||
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 status not okay then it's an exception error
|
||||
if (err.error.hasOwnProperty('Message') && typeof (err.error['Message']) == "string") {
|
||||
return throwError(<IDataSourceErrorMessage>{
|
||||
type: 'message',
|
||||
@@ -96,11 +93,8 @@ export abstract class BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||
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 => {
|
||||
const handleWrapper = (command: TCommand) => {
|
||||
return commandHandler(command).pipe(catchError(this._handleErrorPipe.bind(this)));
|
||||
};
|
||||
|
||||
@@ -115,7 +109,7 @@ export abstract class BaseHttpDataSourceOptionsBuilder<TModel, TKey> {
|
||||
}
|
||||
|
||||
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: TCommand) => {
|
||||
const finalBeforeCommand = beforeCommand || (_ => of(command));
|
||||
return finalBeforeCommand(command)
|
||||
.pipe(
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@poweredsoft/data";
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@openharbor/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<TQuery extends IQueryCriteria, TModel> extends BaseHttpDataSourceOptionsBuilder<TQuery, TModel>
|
||||
{
|
||||
private _beforeRead: (TQuery: IQueryCriteria) => Observable<IQueryCriteria>;
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
@@ -21,7 +16,7 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> extends BaseHttpDataSour
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||
const finalBeforeRead = this._beforeRead || (() => of(query));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
@@ -35,11 +30,11 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> extends BaseHttpDataSour
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryHandler<TQuery extends IQueryCriteria>(queryHandler: (query: TQuery) => Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>) {
|
||||
public queryHandler(queryHandler: (query: TQuery) => Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of(query));
|
||||
const finalBeforeRead = this._beforeRead || (() => of(query));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
@@ -49,7 +44,6 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> extends BaseHttpDataSour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@poweredsoft/data";
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from "@openharbor/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 extends IQueryCriteria, TModel>
|
||||
extends BaseHttpDataSourceOptionsBuilder<TQuery, TModel>
|
||||
{
|
||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
@@ -75,7 +70,7 @@ export class ListDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -84,7 +79,7 @@ export class ListDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||
const finalBeforeRead = this._beforeRead || (() => of({}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from '@poweredsoft/data';
|
||||
import {IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult} from '@openharbor/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> {
|
||||
export class SingleDataSourceOptionsBuilder<TQuery, TModel>
|
||||
extends BaseHttpDataSourceOptionsBuilder<TQuery, TModel> {
|
||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
@@ -21,7 +16,7 @@ export class SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((query: IQueryCriteria) => of({} as TQuery));
|
||||
const finalBeforeRead = this._beforeRead || (() => of({} as TQuery));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
@@ -80,7 +75,7 @@ export class SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||
const finalBeforeRead = this._beforeRead || (() => of({}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
|
||||
@@ -11,15 +11,15 @@ import { ListDataSourceOptionsBuilder } from "./ListDataSourceBuilder";
|
||||
export class HttpDataSourceService {
|
||||
@Inject(HttpClient) protected http!: HttpClient;
|
||||
|
||||
builder<TModel, TKey>() {
|
||||
return new HttpDataSourceOptionsBuilder<TModel, TKey>(this.http);
|
||||
builder<TQuery, TModel>() {
|
||||
return new HttpDataSourceOptionsBuilder<TQuery, TModel>();
|
||||
}
|
||||
|
||||
singleBuilder<TQuery, TModel, TKey>() {
|
||||
return new SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
singleBuilder<TQuery, TModel>() {
|
||||
return new SingleDataSourceOptionsBuilder<TQuery, TModel>();
|
||||
}
|
||||
|
||||
listBuilder<TQuery, TModel, TKey>() {
|
||||
return new ListDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
listBuilder<TQuery, TModel>() {
|
||||
return new ListDataSourceOptionsBuilder<TQuery, TModel>();
|
||||
}
|
||||
}
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import {
|
||||
DataSource,
|
||||
IDataSource,
|
||||
IDataSourceCommandAdapterOptions,
|
||||
IDataSourceOptions,
|
||||
IDataSourceQueryAdapterOptions,
|
||||
IDataSourceTransportOptions,
|
||||
IQueryCriteria
|
||||
} from "@openharbor/data";
|
||||
|
||||
export abstract class InMemoryDataSourceOptionBuilderAbstraction<TQuery, TModel> {
|
||||
protected _commands: { [key: string]: IDataSourceCommandAdapterOptions<any>; } = {};
|
||||
protected _keyResolver: (model: TModel) => TModel[keyof TModel];
|
||||
protected _defaultCriteria: IQueryCriteria;
|
||||
protected _query: IDataSourceQueryAdapterOptions<TQuery, TModel>;
|
||||
|
||||
createDataSource(): IDataSource<TQuery, TModel> {
|
||||
return new DataSource<TQuery, TModel>(this.createOptions());
|
||||
}
|
||||
|
||||
createOptions(): IDataSourceOptions<TQuery, TModel> {
|
||||
return {
|
||||
resolveIdField: this._keyResolver,
|
||||
defaultCriteria: this._defaultCriteria,
|
||||
transport: this.createTransport()
|
||||
};
|
||||
}
|
||||
|
||||
protected createTransport(): IDataSourceTransportOptions<TQuery, TModel> {
|
||||
return {
|
||||
query: this._query,
|
||||
commands: this._commands
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -12,27 +12,10 @@ import {
|
||||
IQueryExecutionGroupResult,
|
||||
IQueryExecutionResult,
|
||||
IResolveCommandModelEvent
|
||||
} from '@poweredsoft/data';
|
||||
} from '@openharbor/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'
|
||||
})
|
||||
@@ -80,29 +63,27 @@ export class GenericRestDataSourceService
|
||||
});
|
||||
}
|
||||
|
||||
createDataSourceOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, defaultCriteria: IQueryCriteria, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceOptions<TModel>
|
||||
createDataSourceOptions<TQuery, TModel>(route: string, keyResolver: (model: TModel) => TModel[keyof TModel], defaultCriteria: IQueryCriteria, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceOptions<TQuery, TModel>
|
||||
{
|
||||
const dataSourceTransportOptions = this.createStandardRestTransportOptions<TModel, TKey>(route, keyResolver, beforeRead);
|
||||
const dataSourceTransportOptions = this.createStandardRestTransportOptions<TQuery, TModel>(route, keyResolver, beforeRead);
|
||||
|
||||
const dataSourceOptions: IDataSourceOptions<TModel> = {
|
||||
return {
|
||||
defaultCriteria: defaultCriteria,
|
||||
resolveIdField: keyResolver,
|
||||
transport: dataSourceTransportOptions
|
||||
};
|
||||
|
||||
return dataSourceOptions;
|
||||
}
|
||||
|
||||
setResolveCommand<TModel>(options: IDataSourceOptions<TModel>, name: string, resolveCommandModel: (event: IResolveCommandModelEvent<TModel>) => Observable<any>) {
|
||||
setResolveCommand<TQuery, TModel>(options: IDataSourceOptions<TQuery, TModel>, name: string, resolveCommandModel: (event: IResolveCommandModelEvent<TModel>) => Observable<any>) {
|
||||
options.transport.commands[name].resolveCommandModel = resolveCommandModel;
|
||||
}
|
||||
|
||||
createStandardRestTransportOptions<TModel, TKey>(route: string, keyResolver: (model: TModel) => TKey, beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceTransportOptions<TModel> {
|
||||
const query: IDataSourceQueryAdapterOptions<TModel> = {
|
||||
createStandardRestTransportOptions<TQuery, TModel>(route: string, keyResolver: (model: TModel) => TModel[keyof TModel], beforeRead?: (query: IQueryCriteria) => Observable<IQueryCriteria>) : IDataSourceTransportOptions<TQuery, TModel> {
|
||||
const query: IDataSourceQueryAdapterOptions<TQuery, TModel> = {
|
||||
adapter: {
|
||||
handle: (criteria: IQueryCriteria) => {
|
||||
const queryRoute = `${route}/read`;
|
||||
const finalBeforeRead = beforeRead || (t => of(criteria));
|
||||
const finalBeforeRead = beforeRead || (() => of(criteria));
|
||||
return finalBeforeRead(criteria)
|
||||
.pipe(switchMap(finalQuery => {
|
||||
return this.http.post<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>(queryRoute, finalQuery);
|
||||
|
||||
Reference in New Issue
Block a user