mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
feat: offline notice
This commit is contained in:
committed by
Rainer Killinger
parent
11d1ac3f7c
commit
9b4caf526f
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* Copyright (C) 2023 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
@@ -31,6 +31,8 @@ import {StorageProvider} from '../storage/storage.provider';
|
||||
import {DataModule} from './data.module';
|
||||
import {DataProvider, DataScope} from './data.provider';
|
||||
import {StAppsWebHttpClient} from './stapps-web-http-client.provider';
|
||||
import {LoggerModule, NgxLoggerLevel} from 'ngx-logger';
|
||||
import {RouterModule} from '@angular/router';
|
||||
|
||||
describe('DataProvider', () => {
|
||||
let dataProvider: DataProvider;
|
||||
@@ -82,7 +84,7 @@ describe('DataProvider', () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [DataModule],
|
||||
imports: [DataModule, LoggerModule.forRoot({level: NgxLoggerLevel.TRACE}), RouterModule.forRoot([])],
|
||||
providers: [DataProvider, StAppsWebHttpClient],
|
||||
});
|
||||
storageProvider = TestBed.inject(StorageProvider);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* Copyright (C) 2023 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
@@ -27,6 +27,7 @@ import {DataDetailComponent} from './data-detail.component';
|
||||
import {By} from '@angular/platform-browser';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {StorageProvider} from '../../storage/storage.provider';
|
||||
import {LoggerModule, NgxLoggerLevel} from 'ngx-logger';
|
||||
|
||||
const translations: any = {data: {detail: {TITLE: 'Foo'}}};
|
||||
|
||||
@@ -70,6 +71,7 @@ describe('DataDetailComponent', () => {
|
||||
TranslateModule.forRoot({
|
||||
loader: {provide: TranslateLoader, useClass: TranslateFakeLoader},
|
||||
}),
|
||||
LoggerModule.forRoot({level: NgxLoggerLevel.TRACE}),
|
||||
],
|
||||
providers: [
|
||||
{
|
||||
|
||||
@@ -229,13 +229,7 @@ export class SearchPageComponent implements OnInit, OnDestroy {
|
||||
})();
|
||||
}
|
||||
} catch (error) {
|
||||
const alert: HTMLIonAlertElement = await this.alertController.create({
|
||||
buttons: ['Dismiss'],
|
||||
header: 'Error',
|
||||
subHeader: (error as Error).message,
|
||||
});
|
||||
|
||||
await alert.present();
|
||||
this.logger.error(error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* Copyright (C) 2023 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
@@ -15,6 +15,17 @@
|
||||
import {HttpClient, HttpResponse} from '@angular/common/http';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClientInterface, HttpClientRequest} from '@openstapps/api/lib/http-client-interface';
|
||||
import {map, retry} from 'rxjs/operators';
|
||||
import {lastValueFrom, Observable} from 'rxjs';
|
||||
import {InternetConnectionService} from '../../util/internet-connection.service';
|
||||
|
||||
type HttpRequestFunctions = InstanceType<typeof HttpClient>['request'];
|
||||
type HttpRequestFunction<T extends ReturnType<HttpRequestFunctions>> = Extract<
|
||||
HttpRequestFunctions,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(...parameters: any[]) => T
|
||||
>;
|
||||
type HttpRequestParameters<T extends ReturnType<HttpRequestFunctions>> = Parameters<HttpRequestFunction<T>>;
|
||||
|
||||
/**
|
||||
* HttpClient that is based on the Angular HttpClient (@TODO: move it to provider or independent package)
|
||||
@@ -23,9 +34,11 @@ import {HttpClientInterface, HttpClientRequest} from '@openstapps/api/lib/http-c
|
||||
export class StAppsWebHttpClient implements HttpClientInterface {
|
||||
/**
|
||||
*
|
||||
* @param http TODO
|
||||
*/
|
||||
constructor(private readonly http: HttpClient) {}
|
||||
constructor(
|
||||
private readonly http: HttpClient,
|
||||
private readonly connectionService: InternetConnectionService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Make a request
|
||||
@@ -33,42 +46,30 @@ export class StAppsWebHttpClient implements HttpClientInterface {
|
||||
* @param requestConfig Configuration of the request
|
||||
*/
|
||||
async request<TYPE_OF_BODY>(requestConfig: HttpClientRequest): Promise<Response<TYPE_OF_BODY>> {
|
||||
const options: {
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
[key: string]: unknown;
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
observe: 'response';
|
||||
} = {
|
||||
body: {},
|
||||
observe: 'response',
|
||||
responseType: 'json',
|
||||
};
|
||||
const request: HttpRequestParameters<Observable<HttpResponse<TYPE_OF_BODY>>> = [
|
||||
requestConfig.method || 'GET',
|
||||
requestConfig.url.toString(),
|
||||
{
|
||||
body: (requestConfig.body || {}) as TYPE_OF_BODY,
|
||||
headers: requestConfig.headers,
|
||||
observe: 'response',
|
||||
responseType: 'json',
|
||||
},
|
||||
];
|
||||
// TODO: cache requests by hashing the parameters.
|
||||
|
||||
if (typeof requestConfig.body !== 'undefined') {
|
||||
options.body = requestConfig.body;
|
||||
}
|
||||
const response: Observable<Response<TYPE_OF_BODY>> = this.http.request(...request).pipe(
|
||||
retry(this.connectionService.retryConfig),
|
||||
map(
|
||||
response =>
|
||||
Object.assign(response, {
|
||||
statusCode: response.status,
|
||||
body: response.body || {},
|
||||
}) as Response<TYPE_OF_BODY>,
|
||||
),
|
||||
);
|
||||
|
||||
if (typeof requestConfig.headers !== 'undefined') {
|
||||
options.headers = requestConfig.headers;
|
||||
}
|
||||
|
||||
try {
|
||||
const response: HttpResponse<TYPE_OF_BODY> = await this.http
|
||||
.request<TYPE_OF_BODY>(requestConfig.method || 'GET', requestConfig.url.toString(), options)
|
||||
.toPromise();
|
||||
|
||||
// eslint-disable-next-line prefer-object-spread
|
||||
return Object.assign(response, {
|
||||
statusCode: response.status,
|
||||
body: response.body || {},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(error as string);
|
||||
}
|
||||
return lastValueFrom(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user