mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
refactor: replace TSLint with ESLint
This commit is contained in:
committed by
Jovan Krunić
parent
67fb4a43c9
commit
d696215d08
@@ -15,7 +15,8 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Client} from '@openstapps/api/lib/client';
|
||||
import {
|
||||
SCMultiSearchRequest, SCMultiSearchResponse,
|
||||
SCMultiSearchRequest,
|
||||
SCMultiSearchResponse,
|
||||
SCSearchRequest,
|
||||
SCSearchResponse,
|
||||
SCThingOriginType,
|
||||
@@ -43,7 +44,6 @@ export enum DataScope {
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class DataProvider {
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
@@ -57,26 +57,32 @@ export class DataProvider {
|
||||
set storagePrefix(storagePrefix) {
|
||||
this._storagePrefix = storagePrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
private _storagePrefix = 'stapps.data';
|
||||
|
||||
/**
|
||||
* Version of the app (used for the header in communication with the backend)
|
||||
*/
|
||||
appVersion = environment.backend_version;
|
||||
|
||||
/**
|
||||
* Maximum number of sub-queries in a multi-query allowed by the backend
|
||||
*/
|
||||
backendQueriesLimit = 5;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
backendUrl = environment.backend_url;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
client: Client;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
@@ -88,8 +94,15 @@ export class DataProvider {
|
||||
* @param stAppsWebHttpClient TODO
|
||||
* @param storageProvider TODO
|
||||
*/
|
||||
constructor(stAppsWebHttpClient: StAppsWebHttpClient, storageProvider: StorageProvider) {
|
||||
this.client = new Client(stAppsWebHttpClient, this.backendUrl, this.appVersion);
|
||||
constructor(
|
||||
stAppsWebHttpClient: StAppsWebHttpClient,
|
||||
storageProvider: StorageProvider,
|
||||
) {
|
||||
this.client = new Client(
|
||||
stAppsWebHttpClient,
|
||||
this.backendUrl,
|
||||
this.appVersion,
|
||||
);
|
||||
this.storageProvider = storageProvider;
|
||||
}
|
||||
|
||||
@@ -106,7 +119,7 @@ export class DataProvider {
|
||||
* Delete all the previously saved data items
|
||||
*/
|
||||
async deleteAll(): Promise<void> {
|
||||
const keys = Array.from((await this.getAll()).keys());
|
||||
const keys = [...(await this.getAll()).keys()];
|
||||
|
||||
return this.storageProvider.delete(...keys);
|
||||
}
|
||||
@@ -114,15 +127,23 @@ export class DataProvider {
|
||||
/**
|
||||
* Provides a savable thing from the local database using the provided UID
|
||||
*/
|
||||
async get(uid: string, scope: DataScope.Local): Promise<SCSaveableThing<SCThings>>;
|
||||
async get(
|
||||
uid: string,
|
||||
scope: DataScope.Local,
|
||||
): Promise<SCSaveableThing<SCThings>>;
|
||||
/**
|
||||
* Provides a thing from the backend
|
||||
*/
|
||||
async get(uid: string, scope: DataScope.Remote): Promise<SCThings | SCSaveableThing<SCThings>>;
|
||||
async get(
|
||||
uid: string,
|
||||
scope: DataScope.Remote,
|
||||
): Promise<SCThings | SCSaveableThing<SCThings>>;
|
||||
/**
|
||||
* Provides a thing from both local database and backend
|
||||
*/
|
||||
async get(uid: string): Promise<Map<DataScope, SCThings | SCSaveableThing<SCThings>>>;
|
||||
async get(
|
||||
uid: string,
|
||||
): Promise<Map<DataScope, SCThings | SCSaveableThing<SCThings>>>;
|
||||
|
||||
/**
|
||||
* Provides a thing from the local database only, backend only or both, depending on the scope
|
||||
@@ -130,26 +151,36 @@ export class DataProvider {
|
||||
* @param uid Unique identifier of a thing
|
||||
* @param scope From where data should be provided
|
||||
*/
|
||||
async get(uid: string, scope?: DataScope):
|
||||
Promise<SCThings | SCSaveableThing<SCThings> | Map<DataScope, SCThings | SCSaveableThing<SCThings>>> {
|
||||
if (scope === DataScope.Local) {
|
||||
return this.storageProvider.get<SCSaveableThing<SCThings>>(this.getDataKey(uid));
|
||||
}
|
||||
if (scope === DataScope.Remote) {
|
||||
return this.client.getThing(uid);
|
||||
}
|
||||
const map: Map<DataScope, SCThings | SCSaveableThing<SCThings>> = new Map();
|
||||
map.set(DataScope.Local, await this.get(uid, DataScope.Local));
|
||||
map.set(DataScope.Remote, await this.get(uid, DataScope.Remote));
|
||||
|
||||
return map;
|
||||
async get(
|
||||
uid: string,
|
||||
scope?: DataScope,
|
||||
): Promise<
|
||||
| SCThings
|
||||
| SCSaveableThing<SCThings>
|
||||
| Map<DataScope, SCThings | SCSaveableThing<SCThings>>
|
||||
> {
|
||||
if (scope === DataScope.Local) {
|
||||
return this.storageProvider.get<SCSaveableThing<SCThings>>(
|
||||
this.getDataKey(uid),
|
||||
);
|
||||
}
|
||||
if (scope === DataScope.Remote) {
|
||||
return this.client.getThing(uid);
|
||||
}
|
||||
const map: Map<DataScope, SCThings | SCSaveableThing<SCThings>> = new Map();
|
||||
map.set(DataScope.Local, await this.get(uid, DataScope.Local));
|
||||
map.set(DataScope.Remote, await this.get(uid, DataScope.Remote));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides all things saved in the local database
|
||||
*/
|
||||
async getAll(): Promise<Map<string, SCSaveableThing<SCThings>>> {
|
||||
return this.storageProvider.search<SCSaveableThing<SCThings>>(this.storagePrefix);
|
||||
return this.storageProvider.search<SCSaveableThing<SCThings>>(
|
||||
this.storagePrefix,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,11 +206,18 @@ export class DataProvider {
|
||||
*
|
||||
* @param query - query to send to the backend (auto-splits according to the backend limit)
|
||||
*/
|
||||
async multiSearch(query: SCMultiSearchRequest): Promise<SCMultiSearchResponse> {
|
||||
async multiSearch(
|
||||
query: SCMultiSearchRequest,
|
||||
): Promise<SCMultiSearchResponse> {
|
||||
// partition object into chunks, process those requests in parallel, then merge their responses again
|
||||
return Object.assign({}, ...(await Promise.all(chunk(toPairs(query), this.backendQueriesLimit)
|
||||
.map((request) => this.client.multiSearch(fromPairs(request))),
|
||||
)));
|
||||
return Object.assign(
|
||||
{},
|
||||
...(await Promise.all(
|
||||
chunk(toPairs(query), this.backendQueriesLimit).map(request =>
|
||||
this.client.multiSearch(fromPairs(request)),
|
||||
),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +226,10 @@ export class DataProvider {
|
||||
* @param item Data item that needs to be saved
|
||||
* @param [type] Savable type (e.g. 'favorite'); if nothing is provided then type of the thing is used
|
||||
*/
|
||||
async put(item: SCThings, type?: SCThingType): Promise<SCSaveableThing<SCThings>> {
|
||||
async put(
|
||||
item: SCThings,
|
||||
type?: SCThingType,
|
||||
): Promise<SCSaveableThing<SCThings>> {
|
||||
const savableItem: SCSaveableThing<SCThings> = {
|
||||
data: item,
|
||||
name: item.name,
|
||||
@@ -196,12 +237,15 @@ export class DataProvider {
|
||||
created: new Date().toISOString(),
|
||||
type: SCThingOriginType.User,
|
||||
},
|
||||
type: (typeof type === 'undefined') ? item.type : type,
|
||||
type: typeof type === 'undefined' ? item.type : type,
|
||||
uid: item.uid,
|
||||
};
|
||||
|
||||
// @TODO: Implementation for saving item into the backend (user's account)
|
||||
return ( this.storageProvider.put<SCSaveableThing<SCThings>>(this.getDataKey(item.uid), savableItem));
|
||||
return this.storageProvider.put<SCSaveableThing<SCThings>>(
|
||||
this.getDataKey(item.uid),
|
||||
savableItem,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -210,6 +254,6 @@ export class DataProvider {
|
||||
* @param query - query to send to the backend
|
||||
*/
|
||||
async search(query: SCSearchRequest): Promise<SCSearchResponse> {
|
||||
return (this.client.search(query));
|
||||
return this.client.search(query);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user