http-request-manager 18.5.10 → 18.5.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2934,9 +2934,9 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
2934
2934
|
console.warn('Database storage requires dataType to be ARRAY');
|
|
2935
2935
|
if (!this.apiOptions.adapter)
|
|
2936
2936
|
console.warn('Database storage requires an adapter to define the data shape');
|
|
2937
|
-
if (this.
|
|
2937
|
+
if (this.databaseOptions && this.databaseOptions?.table === '')
|
|
2938
2938
|
console.warn('Database storage requires a table name');
|
|
2939
|
-
}), filter(() => this.dataType === DataType.ARRAY && !!this.apiOptions.adapter && !!this.
|
|
2939
|
+
}), filter(() => this.dataType === DataType.ARRAY && !!this.apiOptions.adapter && !!this.databaseOptions?.table), switchMap(() => {
|
|
2940
2940
|
const sampleData = this.apiOptions.adapter?.({}) || {};
|
|
2941
2941
|
const schemaKeys = Object.keys(sampleData).filter(key => sampleData[key] !== undefined);
|
|
2942
2942
|
let schema = '++id';
|
|
@@ -2947,7 +2947,7 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
2947
2947
|
}
|
|
2948
2948
|
}
|
|
2949
2949
|
const tableDef = TableSchemaDef.adapt({
|
|
2950
|
-
table: this.
|
|
2950
|
+
table: this.databaseOptions?.table,
|
|
2951
2951
|
schema: schema
|
|
2952
2952
|
});
|
|
2953
2953
|
return this.dbManagerService.createDatabaseTable(tableDef);
|
|
@@ -3033,11 +3033,11 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3033
3033
|
this.setData$({});
|
|
3034
3034
|
}
|
|
3035
3035
|
}), concatMap(() => {
|
|
3036
|
-
if (this.hasDatabase && this.
|
|
3036
|
+
if (this.hasDatabase && this.databaseOptions?.table) {
|
|
3037
3037
|
const currentData = this.get()?.data;
|
|
3038
3038
|
const idsToDelete = Array.isArray(currentData) ? currentData.map((r) => r.id) : [];
|
|
3039
3039
|
if (idsToDelete.length > 0) {
|
|
3040
|
-
return this.dbManagerService.deleteTableRecords(this.
|
|
3040
|
+
return this.dbManagerService.deleteTableRecords(this.databaseOptions.table, idsToDelete);
|
|
3041
3041
|
}
|
|
3042
3042
|
}
|
|
3043
3043
|
return of(null);
|
|
@@ -3053,39 +3053,39 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3053
3053
|
data = (!data) ? (this.dataType === DataType.ARRAY) ? [] : {} : data;
|
|
3054
3054
|
this.setData$(data);
|
|
3055
3055
|
}), concatMap((data) => {
|
|
3056
|
-
if (this.hasDatabase && this.
|
|
3056
|
+
if (this.hasDatabase && this.databaseOptions?.table && Array.isArray(data) && data.length > 0) {
|
|
3057
3057
|
this.localStorageManagerService.updateStore({
|
|
3058
|
-
name: this.
|
|
3059
|
-
data: { ...this.
|
|
3058
|
+
name: this.databaseOptions.table,
|
|
3059
|
+
data: { ...this.databaseOptions, ...{ expires: this.utils.expires(this.databaseOptions.expiresIn) } }
|
|
3060
3060
|
});
|
|
3061
|
-
return this.dbManagerService.createTableRecords(this.
|
|
3061
|
+
return this.dbManagerService.createTableRecords(this.databaseOptions.table, data);
|
|
3062
3062
|
}
|
|
3063
3063
|
return of(data);
|
|
3064
3064
|
}));
|
|
3065
3065
|
};
|
|
3066
|
-
if (this.hasDatabase && this.
|
|
3066
|
+
if (this.hasDatabase && this.databaseOptions?.table) {
|
|
3067
3067
|
return this.dbManagerService.databaseExists().pipe(switchMap((dbExists) => {
|
|
3068
3068
|
if (!dbExists) {
|
|
3069
3069
|
const initObs = this.initDBStorageAsync();
|
|
3070
3070
|
return initObs.pipe(switchMap(() => fetchFromAPI()));
|
|
3071
3071
|
}
|
|
3072
|
-
return this.dbManagerService.hasDatabaseTable(this.
|
|
3072
|
+
return this.dbManagerService.hasDatabaseTable(this.databaseOptions.table).pipe(switchMap((tableExists) => {
|
|
3073
3073
|
if (!tableExists) {
|
|
3074
3074
|
const initObs = this.initDBStorageAsync();
|
|
3075
3075
|
return initObs.pipe(switchMap(() => fetchFromAPI()));
|
|
3076
3076
|
}
|
|
3077
|
-
return this.localStorageManagerService.store$(this.
|
|
3077
|
+
return this.localStorageManagerService.store$(this.databaseOptions.table).pipe(take(1), switchMap((storeData) => {
|
|
3078
3078
|
const expires = storeData?.expires || 0;
|
|
3079
3079
|
const hasExpired = expires > 0 && this.utils.hasExpired(expires);
|
|
3080
3080
|
if (hasExpired) {
|
|
3081
|
-
return this.dbManagerService.clearTable(this.
|
|
3081
|
+
return this.dbManagerService.clearTable(this.databaseOptions.table).pipe(switchMap(() => fetchFromAPI()), tap(() => {
|
|
3082
3082
|
this.localStorageManagerService.updateStore({
|
|
3083
|
-
name: this.
|
|
3084
|
-
data: { ...this.
|
|
3083
|
+
name: this.databaseOptions.table,
|
|
3084
|
+
data: { ...this.databaseOptions, ...{ expires: this.utils.expires(this.databaseOptions.expiresIn) } }
|
|
3085
3085
|
});
|
|
3086
3086
|
}));
|
|
3087
3087
|
}
|
|
3088
|
-
return this.dbManagerService.getTableRecords(this.
|
|
3088
|
+
return this.dbManagerService.getTableRecords(this.databaseOptions.table).pipe(switchMap((dbData) => {
|
|
3089
3089
|
if (Array.isArray(dbData) && dbData.length > 0) {
|
|
3090
3090
|
this.setData$(dbData);
|
|
3091
3091
|
return of(dbData);
|
|
@@ -3113,14 +3113,14 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3113
3113
|
if (method === 'CREATE')
|
|
3114
3114
|
this.addData$(data);
|
|
3115
3115
|
}), concatMap((data) => {
|
|
3116
|
-
if (this.hasDatabase && this.
|
|
3116
|
+
if (this.hasDatabase && this.databaseOptions?.table) {
|
|
3117
3117
|
const id = options.path?.length ? options.path[options.path.length - 1] : null;
|
|
3118
3118
|
if (method === 'DELETE' && id)
|
|
3119
|
-
return this.dbManagerService.deleteTableRecord(this.
|
|
3119
|
+
return this.dbManagerService.deleteTableRecord(this.databaseOptions.table, id);
|
|
3120
3120
|
if (method === 'UPDATE' && data)
|
|
3121
|
-
return this.dbManagerService.updateTableRecord(this.
|
|
3121
|
+
return this.dbManagerService.updateTableRecord(this.databaseOptions.table, data);
|
|
3122
3122
|
if (method === 'CREATE' && data)
|
|
3123
|
-
return this.dbManagerService.createTableRecord(this.
|
|
3123
|
+
return this.dbManagerService.createTableRecord(this.databaseOptions.table, data);
|
|
3124
3124
|
}
|
|
3125
3125
|
return of(data);
|
|
3126
3126
|
}));
|
|
@@ -3136,8 +3136,8 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3136
3136
|
if (this.wsConnection)
|
|
3137
3137
|
this.wsCommunication('CREATE', [...options?.path || [], data.id]);
|
|
3138
3138
|
}), concatMap((data) => {
|
|
3139
|
-
if (this.hasDatabase && this.
|
|
3140
|
-
return this.dbManagerService.createTableRecord(this.
|
|
3139
|
+
if (this.hasDatabase && this.databaseOptions?.table && data?.id) {
|
|
3140
|
+
return this.dbManagerService.createTableRecord(this.databaseOptions.table, data);
|
|
3141
3141
|
}
|
|
3142
3142
|
return of(data);
|
|
3143
3143
|
}));
|
|
@@ -3153,8 +3153,8 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3153
3153
|
if (this.wsConnection)
|
|
3154
3154
|
this.wsCommunication('UPDATE', [...options?.path || []]);
|
|
3155
3155
|
}), concatMap((data) => {
|
|
3156
|
-
if (this.hasDatabase && this.
|
|
3157
|
-
return this.dbManagerService.updateTableRecord(this.
|
|
3156
|
+
if (this.hasDatabase && this.databaseOptions?.table && data?.id) {
|
|
3157
|
+
return this.dbManagerService.updateTableRecord(this.databaseOptions.table, data);
|
|
3158
3158
|
}
|
|
3159
3159
|
return of(data);
|
|
3160
3160
|
}));
|
|
@@ -3170,8 +3170,8 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3170
3170
|
if (this.wsConnection)
|
|
3171
3171
|
this.wsCommunication('DELETE', [...options?.path || []]);
|
|
3172
3172
|
}), concatMap((data) => {
|
|
3173
|
-
if (this.hasDatabase && this.
|
|
3174
|
-
return this.dbManagerService.deleteTableRecord(this.
|
|
3173
|
+
if (this.hasDatabase && this.databaseOptions?.table && data?.id) {
|
|
3174
|
+
return this.dbManagerService.deleteTableRecord(this.databaseOptions.table, data.id);
|
|
3175
3175
|
}
|
|
3176
3176
|
return of(data);
|
|
3177
3177
|
}));
|
|
@@ -3221,16 +3221,17 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3221
3221
|
}
|
|
3222
3222
|
}));
|
|
3223
3223
|
})));
|
|
3224
|
+
this.databaseOptions = database;
|
|
3224
3225
|
this.maxRetries = this.apiOptions.ws?.retry?.times || 3;
|
|
3225
3226
|
this.retryDelay = (this.apiOptions.ws?.retry?.delay && this.apiOptions.ws.retry.delay * 1000) || 5 * 1000;
|
|
3226
3227
|
this.wsNextRetry = new BehaviorSubject(this.retryDelay);
|
|
3227
3228
|
this.wsNextRetry$ = this.wsNextRetry.asObservable();
|
|
3228
3229
|
this.setApiRequestOptions(apiOptions, dataType, database);
|
|
3229
3230
|
this.status$ = this.setupConnectionStatus();
|
|
3230
|
-
if (this.
|
|
3231
|
+
if (this.databaseOptions && this.databaseOptions.table) {
|
|
3231
3232
|
this.localStorageManagerService.createStore({
|
|
3232
|
-
name: this.
|
|
3233
|
-
data: { ...this.
|
|
3233
|
+
name: this.databaseOptions.table,
|
|
3234
|
+
data: { ...this.databaseOptions, ...{ expires: this.utils.expires(this.databaseOptions.expiresIn) } },
|
|
3234
3235
|
options: SettingOptions.adapt({
|
|
3235
3236
|
storage: StorageType.GLOBAL,
|
|
3236
3237
|
encrypted: false,
|
|
@@ -3241,15 +3242,18 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3241
3242
|
setApiRequestOptions(apiOptions, dataType, database) {
|
|
3242
3243
|
this.apiOptions = ApiRequest.adapt(apiOptions);
|
|
3243
3244
|
this.dataType = (dataType) ? dataType : DataType.ARRAY;
|
|
3244
|
-
|
|
3245
|
-
|
|
3245
|
+
// Only update database options if a database parameter is explicitly provided
|
|
3246
|
+
if (database !== undefined) {
|
|
3247
|
+
this.hasDatabase = (database?.table) ? true : false;
|
|
3248
|
+
this.databaseOptions = (this.hasDatabase) ? DatabaseStorage.adapt(database) : undefined;
|
|
3249
|
+
}
|
|
3246
3250
|
// Update WebSocket retry settings when options change
|
|
3247
3251
|
if (this.apiOptions.ws?.retry) {
|
|
3248
3252
|
this.maxRetries = this.apiOptions.ws.retry.times || 3;
|
|
3249
3253
|
this.retryDelay = (this.apiOptions.ws.retry.delay && this.apiOptions.ws.retry.delay * 1000) || 5 * 1000;
|
|
3250
3254
|
this.wsNextRetry.next(this.retryDelay);
|
|
3251
3255
|
}
|
|
3252
|
-
if (this.
|
|
3256
|
+
if (this.databaseOptions)
|
|
3253
3257
|
this.initDBStorage();
|
|
3254
3258
|
if (this.apiOptions.ws)
|
|
3255
3259
|
this.initWS(this.apiOptions.ws);
|
|
@@ -3346,7 +3350,7 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3346
3350
|
console.warn('Database storage requires an adapter to define the data shape');
|
|
3347
3351
|
return of(null);
|
|
3348
3352
|
}
|
|
3349
|
-
if (!this.
|
|
3353
|
+
if (!this.databaseOptions?.table) {
|
|
3350
3354
|
console.warn('Database storage requires a table name');
|
|
3351
3355
|
return of(null);
|
|
3352
3356
|
}
|
|
@@ -3360,7 +3364,7 @@ class HTTPManagerStateService extends ComponentStore {
|
|
|
3360
3364
|
}
|
|
3361
3365
|
}
|
|
3362
3366
|
const tableDef = TableSchemaDef.adapt({
|
|
3363
|
-
table: this.
|
|
3367
|
+
table: this.databaseOptions?.table,
|
|
3364
3368
|
schema: schema
|
|
3365
3369
|
});
|
|
3366
3370
|
return this.dbManagerService.createDatabaseTable(tableDef);
|