UNPKG

ngx-pwa

Version:

Provides functionality around the progressive web app functionality in angular. Most notably the an approach to cache POST, UPDATE and DELETE requests.

127 lines (126 loc) 5.17 kB
import { HttpClient, HttpRequest } from '@angular/common/http'; import { InjectionToken, NgZone } from '@angular/core'; import { MatSnackBar } from '@angular/material/snack-bar'; import { Observable } from 'rxjs'; import { RequestMetadataInternal } from '../models/request-metadata-internal.model'; /** * The Generic Base EntityType. */ export type BaseEntityType<T> = { [K in keyof T]: unknown; }; export declare const NGX_PWA_OFFLINE_SERVICE: InjectionToken<void>; /** * The type of a cached offline request. * Contains the http request as well as some metadata. */ export interface CachedRequest<T> { /** * The actual http request. */ request: HttpRequest<T>; /** * The metadata for that request. */ metadata: RequestMetadataInternal; } /** * The base class for an offline service. */ export declare class NgxPwaOfflineService { private readonly http; private readonly snackBar; private readonly zone; private readonly platformId; /** * The key under which any requests are saved in local storage. */ readonly CACHED_REQUESTS_KEY: string; /** * The prefix of offline generated ids. * Is used to check if a request still has unresolved dependencies. */ readonly OFFLINE_ID_PREFIX: string; /** * A snackbar message to display when the synchronization of all cached requests has been finished. */ protected readonly ALL_SYNC_FINISHED_SNACK_BAR_MESSAGE: string; /** * A snackbar message to display when the synchronization of all cached requests fails. */ protected readonly ALL_SYNC_FAILED_SNACK_BAR_MESSAGE: string; /** * A snackbar message to display when the synchronization of a single cached requests has been finished. */ protected readonly SINGLE_SYNC_FINISHED_SNACK_BAR_MESSAGE: string; /** * A snackbar message to display when the synchronization of a single cached requests fails. */ protected readonly SINGLE_SYNC_FAILED_SNACK_BAR_MESSAGE: string; /** * Whether or not the user has no internet connection. */ isOffline: boolean; /** * A subject of all the requests that have been done while offline. * Needs to be used for applying offline data or syncing the requests to the api. */ private readonly cachedRequestsSubject; /** * The currently stored cached requests (if there are any). */ get cachedRequests(): CachedRequest<unknown>[]; set cachedRequests(cachedRequests: CachedRequest<unknown>[]); constructor(http: HttpClient, snackBar: MatSnackBar, zone: NgZone, platformId: Object); /** * Applies any offline data that has been cached to the given values. * @param type - The type of the provided entities. Is needed to check if any cached requests of the same type exist. * @param entities - The already existing data. * @returns The already existing entities extended/modified by the offline cached requests. */ applyOfflineData<EntityType extends BaseEntityType<EntityType>>(type: string, entities: EntityType[]): EntityType[]; /** * Applies an UPDATE to an entity without sending a request to the server. * @param changes - The changes that should be made to the entity. * @param entity - The entity that should be updated. * @returns The updated entity. */ protected updateOffline<EntityType extends BaseEntityType<EntityType>>(changes: Partial<EntityType>, entity: EntityType): EntityType; /** * Sends a specific cached request to the server. * @param request - The request that should be synced. */ sync<T>(request: CachedRequest<T>): Promise<void>; /** * Sends all cached requests to the server. Tries to handle dependencies of requests on each other. */ syncAll(): Promise<void>; /** * The recursive method used to syn all requests to the api. */ protected syncAllRecursive(): Promise<void>; /** * Sends a single cached request to the server. * @param request - The request that should be synced. * @returns A promise of the request result. */ protected syncSingleRequest<T>(request: CachedRequest<T>): Promise<T>; private updateOfflineIdsInRequests; /** * Calls http.post/patch/delete etc. On the provided request. * @param request - The request that should be sent. * @returns The observable of the request or undefined if something went wrong. */ protected request<EntityType extends BaseEntityType<EntityType>>(request: CachedRequest<EntityType>): Observable<EntityType> | undefined; /** * Checks if the given request has an unresolved dependency by looking for the keyword 'offline' inside of it. * @param request - The request that should be checked. * @returns Whether or no the given request has an unresolved dependency. */ hasUnresolvedDependency(request: CachedRequest<unknown>): boolean; /** * Removes a single request from the cache. * @param request - The request that should be removed. */ removeSingleRequest(request: CachedRequest<unknown>): void; }