wacom
Version:
Module which has common services, pipes, directives and interfaces which can be used on all projects.
1,251 lines (1,221 loc) • 71.7 kB
TypeScript
import { Observable } from 'rxjs';
import * as i0 from '@angular/core';
import { InjectionToken, ComponentRef, WritableSignal, Signal, PipeTransform, Type, EnvironmentProviders, ModuleWithProviders } from '@angular/core';
import { Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import * as _angular_platform_browser from '@angular/platform-browser';
import { Meta, Title } from '@angular/platform-browser';
import { HttpErrorResponse, HttpClient } from '@angular/common/http';
import * as i1 from '@angular/common';
import { DatePipe } from '@angular/common';
import * as i2 from '@angular/forms';
/**
* Basic fields expected on a document managed by the CRUD service.
*/
interface CrudDocument<Document> {
/** Unique identifier of the document. */
_id?: string;
/** Temporary unique identifier of the document for offline use */
_localId?: number;
/** Optional application identifier to which the document belongs. */
appId?: string;
/** Numerical position used for manual ordering. */
order?: number;
/** Flag indicating the document is creating */
__creating?: boolean;
/** Flag set when the document has been modified locally. */
__modified?: string[];
/** Flag set when the document has been deleted locally. */
__deleted?: boolean;
__options?: Record<string, CrudOptions<Document>>;
}
/**
* Options that can be supplied to CRUD operations.
*/
interface CrudOptions<Document> {
/** Logical name of the collection or resource. */
name?: string;
/** Callback invoked with the server response. */
callback?: (resp: Document | Document[]) => void;
/** Callback invoked if the request fails. */
errCallback?: (resp: unknown) => void;
}
/**
* Contract implemented by services performing CRUD requests.
*/
interface CrudServiceInterface<Document> {
/** Retrieve a page of documents from the server. */
get: (params: {
page: number;
query?: string;
}, options: CrudOptions<Document>) => any;
/** Return the current in‑memory documents. */
getDocs: () => Document[];
/** Create a new document. */
create: (doc: Document) => any;
/** Update an existing document. */
update: (doc: Document) => any;
/** Delete the provided document. */
delete: (doc: Document) => any;
/** Change the number of documents retrieved per page. */
setPerPage?: (count: number) => void;
/** Resolves when the initial data load has completed. */
loaded: Observable<unknown>;
getSignal: (doc: string | Document) => any;
}
/**
* Configuration describing how a CRUD table should behave.
*/
interface TableConfigButton<Document> {
icon?: string;
click?: (doc: Document) => void;
hrefFunc?: (doc: Document) => string;
class?: string;
}
interface TableConfig<Document> {
/** Callback to paginate to a given page. */
paginate?: (page?: number) => void;
/** Number of documents shown per page. */
perPage?: number;
/** Function used to update the page size. */
setPerPage?: ((count: number) => void) | undefined;
/** When true, fetch all documents instead of paginating. */
allDocs?: boolean;
/** Handler invoked to create a new record. */
create: (() => void) | null;
/** Handler invoked to edit a record. */
update: ((doc: Document) => void) | null;
/** Handler invoked to delete a record. */
delete: ((doc: Document) => void) | null;
/** Row‑level action buttons. */
buttons: TableConfigButton<Document>[];
/** Buttons displayed in the table header. */
headerButtons: TableConfigButton<Document>[];
}
type HttpHeaderType = string | number | (string | number)[];
/**
* Configuration values used by the HTTP service when
* issuing requests to a backend API.
*/
interface HttpConfig {
/** Map of default headers appended to each request. */
headers?: Record<string, HttpHeaderType>;
/** Base URL for all HTTP requests. */
url?: string;
}
declare const DEFAULT_HTTP_CONFIG: HttpConfig;
/**
* Default metadata values applied to every route.
*/
interface MetaDefaults {
/** Base document title. */
title?: string;
/** Suffix appended to titles when {@link MetaConfig.useTitleSuffix} is true. */
titleSuffix?: string;
/** Map of link tags (e.g. canonical URLs). */
links?: Record<string, string>;
[key: string]: string | Record<string, string> | undefined;
}
/**
* Options controlling the behavior of the meta service.
*/
interface MetaConfig {
/** Whether to append the configured titleSuffix to page titles. */
useTitleSuffix?: boolean;
/** Emit console warnings when routes are missing a guard. */
warnMissingGuard?: boolean;
/** Default metadata applied to all routes. */
defaults?: MetaDefaults;
}
type NetworkStatus = 'good' | 'poor' | 'none';
interface NetworkConfig {
/** Ordered list of endpoints to probe (first that succeeds wins). */
endpoints: string[];
/** Periodic re-check interval (ms). */
intervalMs: number;
/** Per-request timeout (ms). */
timeoutMs: number;
/** Latency threshold (ms) to classify as "good". */
goodLatencyMs: number;
/** Consecutive failures to flip status to "none". */
maxConsecutiveFails: number;
}
declare const DEFAULT_NETWORK_CONFIG: NetworkConfig;
declare const NETWORK_CONFIG: InjectionToken<NetworkConfig>;
/**
* Configuration for the storage abstraction used by the library.
*/
interface StoreConfig {
/** Key prefix applied to all stored values. */
prefix?: string;
/** Persist a value under the given field name. */
set?: (field: string, value: string | number, cb?: () => void, errCb?: (err: unknown) => void) => Promise<boolean>;
/** Retrieve a value by field name. */
get?: (field: string, cb?: (value: string) => void, errCb?: (err: unknown) => void) => Promise<string>;
/** Remove a stored value. */
remove?: (field: string, cb?: () => void, errCb?: (err: unknown) => void) => Promise<boolean>;
/** Clear all stored values created by the library. */
clear?: (cb?: () => void, errCb?: (err: unknown) => void) => Promise<boolean>;
}
/**
* Root configuration object used to initialize the library.
* Each property allows consumers to override the default
* behavior of the corresponding service.
*/
interface Config {
/** Options for the key‑value storage service. */
store?: StoreConfig;
/** Defaults applied to page metadata handling. */
meta?: MetaConfig;
/** Base HTTP settings such as API URL and headers. */
http?: HttpConfig;
/** Optional socket connection configuration. */
socket?: any;
/** Raw Socket.IO client instance, if used. */
io?: any;
network?: NetworkConfig;
}
declare const CONFIG_TOKEN: InjectionToken<Config>;
declare const DEFAULT_CONFIG: Config;
/**
* Representation of a component that has been dynamically added to the DOM.
*
* @template T - Type of the attached component instance.
*/
interface DomComponent<T> {
/** The root DOM element of the created component. */
nativeElement: HTMLElement;
/** Angular reference to the created component. */
componentRef: ComponentRef<T>;
/**
* Removes the component from the DOM and cleans up associated resources.
*/
remove: () => void;
}
declare class MetaService {
private config;
private router;
private meta;
private titleService;
private _meta;
constructor(config: Config, router: Router, meta: Meta, titleService: Title);
/**
* Sets the default meta tags.
*
* @param defaults - The default meta tags.
*/
setDefaults(defaults: MetaDefaults): void;
/**
* Sets the title and optional title suffix.
*
* @param title - The title to set.
* @param titleSuffix - The title suffix to append.
* @returns The MetaService instance.
*/
setTitle(title?: string, titleSuffix?: string): MetaService;
/**
* Sets link tags.
*
* @param links - The links to set.
* @returns The MetaService instance.
*/
setLink(links: {
[key: string]: string;
}): MetaService;
/**
* Sets a meta tag.
*
* @param tag - The meta tag name.
* @param value - The meta tag value.
* @param prop - The meta tag property.
*/
setTag(tag: string, value: string, prop?: string): void;
/**
* Updates a meta tag.
*
* @param tag - The meta tag name.
* @param value - The meta tag value.
* @param prop - The meta tag property.
*/
private _updateMetaTag;
/**
* Removes a meta tag.
*
* @param tag - The meta tag name.
* @param prop - The meta tag property.
*/
removeTag(tag: string, prop?: string): void;
/**
* Warns about missing meta guards in routes.
*/
private _warnMissingGuard;
static ɵfac: i0.ɵɵFactoryDeclaration<MetaService, [{ optional: true; }, null, null, null]>;
static ɵprov: i0.ɵɵInjectableDeclaration<MetaService>;
}
declare class MetaGuard {
private metaService;
private config;
static IDENTIFIER: string;
private _meta;
constructor(metaService: MetaService, config: Config);
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean;
private _processRouteMetaTags;
static ɵfac: i0.ɵɵFactoryDeclaration<MetaGuard, [null, { optional: true; }]>;
static ɵprov: i0.ɵɵInjectableDeclaration<MetaGuard>;
}
/**
* Abstract reusable base class for CRUD list views.
* It encapsulates pagination, modals, and document handling logic.
*
* @template Service - A service implementing CrudServiceInterface for a specific document type
* @template Document - The data model extending CrudDocument
*/
declare abstract class CrudComponent<Service extends CrudServiceInterface<Document>, Document extends CrudDocument<Document>, FormInterface> {
/** Service responsible for data fetching, creating, updating, deleting */
protected crudService: Service;
/** The array of documents currently loaded and shown */
protected documents: WritableSignal<Signal<Document>[]>;
/** Form schema/config used by the FormService modals */
protected form: FormInterface;
/** Current pagination page */
protected page: number;
/** CoreService handles timing and copying helpers */
private __core;
/** ChangeDetectorRef handles on push strategy */
private __cdr;
/** Internal reference to form service matching FormServiceInterface */
private __form;
/**
* Constructor
*
* @param formConfig - Object describing form title and its component structure
* @param formService - Any service that conforms to FormServiceInterface (usually casted)
* @param crudService - CRUD service implementing get/create/update/delete
*/
constructor(formConfig: unknown, formService: unknown, crudService: Service, module?: string);
/**
* Loads documents for a given page.
*/
protected setDocuments(page?: number, query?: string): Promise<void>;
/** Fields considered when performing bulk updates. */
protected updatableFields: string[];
/**
* Clears temporary metadata before document creation.
*/
protected preCreate(doc: Document): void;
/**
* Funciton which controls whether the create functionality is available.
*/
protected allowCreate(): boolean;
/**
* Funciton which controls whether the update and delete functionality is available.
*/
protected allowMutate(): boolean;
/**
* Funciton which controls whether the unique url functionality is available.
*/
protected allowUrl(): boolean;
/** Determines whether manual sorting controls are available. */
protected allowSort(): boolean;
/**
* Funciton which prepare get crud options.
*/
protected getOptions(): CrudOptions<Document>;
/**
* Handles bulk creation and updating of documents.
* In creation mode, adds new documents.
* In update mode, syncs changes and deletes removed entries.
*/
protected bulkManagement(isCreateFlow?: boolean): () => void;
/** Opens a modal to create a new document. */
protected create(): void;
/** Displays a modal to edit an existing document. */
protected update(doc: Document): void;
/** Requests confirmation before deleting the provided document. */
protected delete(doc: Document): Promise<void>;
/** Opens a modal to edit the document's unique URL. */
protected mutateUrl(doc: Document): void;
/** Moves the given document one position up and updates ordering. */
protected moveUp(doc: Document): void;
/** Data source mode used for document retrieval. */
protected configType: 'server' | 'local';
/** Number of documents fetched per page when paginating. */
protected perPage: number;
/**
* Configuration object used by the UI for rendering table and handling actions.
*/
protected getConfig(): TableConfig<Document>;
/** Name of the collection or module used for contextual actions. */
private _module;
}
/**
* Stand-alone “click outside” directive (zoneless-safe).
*
* Usage:
* <div (clickOutside)="close()">…</div>
*/
declare class ClickOutsideDirective {
readonly clickOutside: i0.OutputEmitterRef<MouseEvent>;
constructor();
private _host;
private _cdr;
private _dref;
private handler;
static ɵfac: i0.ɵɵFactoryDeclaration<ClickOutsideDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ClickOutsideDirective, "[clickOutside]", never, {}, { "clickOutside": "clickOutside"; }, never, never, true, never>;
}
declare class ManualDisabledDirective {
private readonly el;
readonly manualDisabled: i0.InputSignal<boolean | null>;
private readonly syncDisabledEffect;
static ɵfac: i0.ɵɵFactoryDeclaration<ManualDisabledDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ManualDisabledDirective, "input[manualDisabled], textarea[manualDisabled]", never, { "manualDisabled": { "alias": "manualDisabled"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
}
declare class ManualNameDirective {
private readonly el;
readonly manualName: i0.InputSignal<string | null>;
private readonly syncNameEffect;
static ɵfac: i0.ɵɵFactoryDeclaration<ManualNameDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ManualNameDirective, "input[manualName], textarea[manualName]", never, { "manualName": { "alias": "manualName"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
}
declare class ManualReadonlyDirective {
private readonly el;
readonly manualReadonly: i0.InputSignal<boolean | null>;
private readonly syncReadonlyEffect;
static ɵfac: i0.ɵɵFactoryDeclaration<ManualReadonlyDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ManualReadonlyDirective, "input[manualReadonly], textarea[manualReadonly]", never, { "manualReadonly": { "alias": "manualReadonly"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
}
declare class ManualTypeDirective {
private readonly el;
readonly manualType: i0.InputSignal<string | null>;
private readonly syncTypeEffect;
static ɵfac: i0.ɵɵFactoryDeclaration<ManualTypeDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ManualTypeDirective, "input[manualType], textarea[manualType]", never, { "manualType": { "alias": "manualType"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
}
declare class ArrPipe implements PipeTransform {
transform(data: any, type?: any, refresh?: any): any;
static ɵfac: i0.ɵɵFactoryDeclaration<ArrPipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<ArrPipe, "arr", true>;
}
declare class MongodatePipe implements PipeTransform {
transform(_id: any): Date;
static ɵfac: i0.ɵɵFactoryDeclaration<MongodatePipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<MongodatePipe, "mongodate", true>;
}
declare class NumberPipe implements PipeTransform {
transform(value: unknown): number;
static ɵfac: i0.ɵɵFactoryDeclaration<NumberPipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<NumberPipe, "number", true>;
}
declare class PaginationPipe implements PipeTransform {
transform(arr: any, config: any, sort: any, search?: string): any;
static ɵfac: i0.ɵɵFactoryDeclaration<PaginationPipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<PaginationPipe, "page", true>;
}
declare class SafePipe {
transform(html: any): _angular_platform_browser.SafeResourceUrl;
private _sanitizer;
static ɵfac: i0.ɵɵFactoryDeclaration<SafePipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<SafePipe, "safe", true>;
}
type Query = string | string[] | Record<string, unknown> | Signal<string | string[] | Record<string, unknown> | undefined>;
type Field = string | string[] | number | Signal<string | string[] | number | undefined>;
declare class SearchPipe implements PipeTransform {
transform<T>(items: T[] | Record<string, T>, query?: Query, fields?: Field, limit?: number, ignore?: boolean, _reload?: unknown): T[];
static ɵfac: i0.ɵɵFactoryDeclaration<SearchPipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<SearchPipe, "search", true>;
}
declare class SplicePipe implements PipeTransform {
transform(from: any, which: any, refresh?: number): any;
static ɵfac: i0.ɵɵFactoryDeclaration<SplicePipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<SplicePipe, "splice", true>;
}
declare class SplitPipe implements PipeTransform {
transform(value: string, index?: number, devider?: string): unknown;
static ɵfac: i0.ɵɵFactoryDeclaration<SplitPipe, never>;
static ɵpipe: i0.ɵɵPipeDeclaration<SplitPipe, "split", true>;
}
declare global {
interface String {
capitalize(): string;
}
}
declare class CoreService {
deviceID: string;
constructor();
/**
* Generates a UUID (Universally Unique Identifier) version 4.
*
* This implementation uses `Math.random()` to generate random values,
* making it suitable for general-purpose identifiers, but **not** for
* cryptographic or security-sensitive use cases.
*
* The format follows the UUID v4 standard: `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
* where:
* - `x` is a random hexadecimal digit (0–f)
* - `4` indicates UUID version 4
* - `y` is one of 8, 9, A, or B
*
* Example: `f47ac10b-58cc-4372-a567-0e02b2c3d479`
*
* @returns A string containing a UUID v4.
*/
UUID(): string;
/**
* Converts an object to an array. Optionally holds keys instead of values.
*
* @param {any} obj - The object to be converted.
* @param {boolean} [holder=false] - If true, the keys will be held in the array; otherwise, the values will be held.
* @returns {any[]} The resulting array.
*/
ota(obj: any, holder?: boolean): any[];
/**
* Removes elements from `fromArray` that are present in `removeArray` based on a comparison field.
*
* @param {any[]} removeArray - The array of elements to remove.
* @param {any[]} fromArray - The array from which to remove elements.
* @param {string} [compareField='_id'] - The field to use for comparison.
* @returns {any[]} The modified `fromArray` with elements removed.
*/
splice(removeArray: any[], fromArray: any[], compareField?: string): any[];
/**
* Unites multiple _id values into a single unique _id.
* The resulting _id is unique regardless of the order of the input _id values.
*
* @param {...string[]} args - The _id values to be united.
* @returns {string} The unique combined _id.
*/
ids2id(...args: string[]): string;
private _afterWhile;
/**
* Delays the execution of a callback function for a specified amount of time.
* If called again within that time, the timer resets.
*
* @param {string | object | (() => void)} doc - A unique identifier for the timer, an object to host the timer, or the callback function.
* @param {() => void} [cb] - The callback function to execute after the delay.
* @param {number} [time=1000] - The delay time in milliseconds.
*/
afterWhile(doc: string | object | (() => void), cb?: () => void, time?: number): void;
/**
* Recursively copies properties from one object to another.
* Handles nested objects, arrays, and Date instances appropriately.
*
* @param from - The source object from which properties are copied.
* @param to - The target object to which properties are copied.
*/
copy(from: any, to: any): void;
device: string;
/**
* Detects the device type based on the user agent.
*/
detectDevice(): void;
/**
* Checks if the device is a mobile device.
* @returns {boolean} - Returns true if the device is a mobile device.
*/
isMobile(): boolean;
/**
* Checks if the device is a tablet.
* @returns {boolean} - Returns true if the device is a tablet.
*/
isTablet(): boolean;
/**
* Checks if the device is a web browser.
* @returns {boolean} - Returns true if the device is a web browser.
*/
isWeb(): boolean;
/**
* Checks if the device is an Android device.
* @returns {boolean} - Returns true if the device is an Android device.
*/
isAndroid(): boolean;
/**
* Checks if the device is an iOS device.
* @returns {boolean} - Returns true if the device is an iOS device.
*/
isIos(): boolean;
version: string;
appVersion: string;
dateVersion: string;
/**
* Sets the combined version string based on appVersion and dateVersion.
*/
setVersion(): void;
/**
* Sets the app version and updates the combined version string.
*
* @param {string} appVersion - The application version to set.
*/
setAppVersion(appVersion: string): void;
/**
* Sets the date version and updates the combined version string.
*
* @param {string} dateVersion - The date version to set.
*/
setDateVersion(dateVersion: string): void;
private _locked;
private _unlockResolvers;
/**
* Locks a resource to prevent concurrent access.
* @param which - The resource to lock, identified by a string.
*/
lock(which: string): void;
/**
* Unlocks a resource, allowing access.
* @param which - The resource to unlock, identified by a string.
*/
unlock(which: string): void;
/**
* Returns a Promise that resolves when the specified resource is unlocked.
* @param which - The resource to watch for unlocking, identified by a string.
* @returns A Promise that resolves when the resource is unlocked.
*/
onUnlock(which: string): Promise<void>;
/**
* Checks if a resource is locked.
* @param which - The resource to check, identified by a string.
* @returns True if the resource is locked, false otherwise.
*/
locked(which: string): boolean;
/**
* Converts a plain object into a signal-wrapped object.
* Optionally wraps specific fields of the object as individual signals,
* and merges them into the returned signal for fine-grained reactivity.
*
* @template Document - The type of the object being wrapped.
* @param {Document} document - The plain object to wrap into a signal.
* @param {Record<string, (doc: Document) => unknown>} [signalFields={}] -
* Optional map where each key is a field name and the value is a function
* to extract the initial value for that field. These fields will be wrapped
* as separate signals and embedded in the returned object.
*
* @returns {WritableSignal<Document>} A signal-wrapped object, possibly containing
* nested field signals for more granular control.
*
* @example
* const user = { _id: '1', name: 'Alice', score: 42 };
* const sig = toSignal(user, { score: (u) => u.score });
* console.log(sig().name); // 'Alice'
* console.log(sig().score()); // 42 — field is now a signal
*/
toSignal<Document>(document: Document, signalFields?: Record<string, (doc: Document) => unknown>): WritableSignal<Document>;
/**
* Converts an array of objects into an array of Angular signals.
* Optionally wraps specific fields of each object as individual signals.
*
* @template Document - The type of each object in the array.
* @param {Document[]} arr - Array of plain objects to convert into signals.
* @param {Record<string, (doc: Document) => unknown>} [signalFields={}] -
* Optional map where keys are field names and values are functions that extract the initial value
* from the object. These fields will be turned into separate signals.
*
* @returns {WritableSignal<Document>[]} An array where each item is a signal-wrapped object,
* optionally with individual fields also wrapped in signals.
*
* @example
* toSignalsArray(users, {
* name: (u) => u.name,
* score: (u) => u.score,
* });
*/
toSignalsArray<Document>(arr: Document[], signalFields?: Record<string, (doc: Document) => unknown>): WritableSignal<Document>[];
/**
* Adds a new object to the signals array.
* Optionally wraps specific fields of the object as individual signals before wrapping the whole object.
*
* @template Document - The type of the object being added.
* @param {WritableSignal<Document>[]} signals - The signals array to append to.
* @param {Document} item - The object to wrap and push as a signal.
* @param {Record<string, (doc: Document) => unknown>} [signalFields={}] -
* Optional map of fields to be wrapped as signals within the object.
*
* @returns {void}
*/
pushSignal<Document>(signals: WritableSignal<Document>[], item: Document, signalFields?: Record<string, (doc: Document) => unknown>): void;
/**
* Removes the first signal from the array whose object's field matches the provided value.
* @template Document
* @param {WritableSignal<Document>[]} signals - The signals array to modify.
* @param {unknown} value - The value to match.
* @param {string} [field='_id'] - The object field to match against.
* @returns {void}
*/
removeSignalByField<Document extends Record<string, unknown>>(signals: WritableSignal<Document>[], value: unknown, field?: string): void;
/**
* Returns a generic trackBy function for *ngFor, tracking by the specified object field.
* @template Document
* @param {string} field - The object field to use for tracking (e.g., '_id').
* @returns {(index: number, sig: Signal<Document>) => unknown} TrackBy function for Angular.
*/
trackBySignalField<Document extends Record<string, unknown>>(field: string): (_: number, sig: Signal<Document>) => unknown;
/**
* Finds the first signal in the array whose object's field matches the provided value.
* @template Document
* @param {Signal<Document>[]} signals - Array of signals to search.
* @param {unknown} value - The value to match.
* @param {string} [field='_id'] - The object field to match against.
* @returns {Signal<Document> | undefined} The found signal or undefined if not found.
*/
findSignalByField<Document extends Record<string, unknown>>(signals: WritableSignal<Document>[], value: unknown, field?: string): WritableSignal<Document> | undefined;
/**
* Updates the first writable signal in the array whose object's field matches the provided value.
* @template Document
* @param {WritableSignal<Document>[]} signals - Array of writable signals to search.
* @param {unknown} value - The value to match.
* @param {(val: Document) => Document} updater - Function to produce the updated object.
* @param {string} field - The object field to match against.
* @returns {void}
*/
updateSignalByField<Document extends Record<string, unknown>>(signals: WritableSignal<Document>[], value: unknown, updater: (val: Document) => Document, field: string): void;
static ɵfac: i0.ɵɵFactoryDeclaration<CoreService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<CoreService>;
}
type Any = unknown;
declare class EmitterService {
private _signals;
private _closers;
private _streams;
private _getSignal;
private _getCloser;
private _getStream;
/** Emit an event */
emit<T = Any>(id: string, data?: T): void;
/** Listen for events (hot, completes when off(id) is called) */
on<T = Any>(id: string): Observable<T>;
/** Complete and remove a channel */
off(id: string): void;
offAll(): void;
has(id: string): boolean;
private _done;
private _getDoneSignal;
/** Mark task as completed with a payload (default: true) */
complete<T = Any>(task: string, value?: T): void;
/** Clear completion so it can be awaited again */
clearCompleted(task: string): void;
/** Read current completion payload (undefined => not completed) */
completed(task: string): Any | undefined;
isCompleted(task: string): boolean;
onComplete(tasks: string | string[], opts?: {
mode?: 'all' | 'any';
timeoutMs?: number;
abort?: AbortSignal;
}): Observable<Any | Any[]>;
static ɵfac: i0.ɵɵFactoryDeclaration<EmitterService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<EmitterService>;
}
declare class HttpService {
private _http;
errors: ((err: HttpErrorResponse, retry?: () => void) => {})[];
url: string;
locked: boolean;
awaitLocked: ReturnType<typeof setTimeout>[];
private _config;
private _headers;
private _http_headers;
constructor(config: Config, _http: HttpClient);
setUrl(url: string): void;
removeUrl(): void;
set(key: any, value: any): void;
header(key: any): HttpHeaderType;
remove(key: any): void;
private _httpMethod;
/**
* Internal method to handle HTTP requests for various methods (POST, PUT, PATCH, DELETE, GET).
*
* Features:
* - **Request Locking**: Manages request locking to prevent simultaneous requests.
* - **Acceptance Check**: Validates the server response against a user-defined `acceptance` function.
* If the check fails, the response is rejected with an error.
* - **Replace Logic**: Allows modification of specific parts of the response object, determined by a user-defined `replace` function.
* Can handle both objects and arrays within the response.
* - **Field Filtering**: Supports extracting specific fields from response objects or arrays.
* - **Legacy Support**: Compatible with callback-based usage alongside Observables.
* - **ReplaySubject**: Ensures that the response can be shared across multiple subscribers.
*
* @param url - The endpoint to send the HTTP request to (relative to the base URL).
* @param doc - The request payload for methods like POST, PUT, and PATCH.
* @param callback - A legacy callback function to handle the response.
* @param opts - Additional options:
* - `err`: Error handling callback.
* - `acceptance`: Function to validate the server response. Should return `true` for valid responses.
* - `replace`: Function to modify specific parts of the response data.
* - `fields`: Array of fields to extract from the response object(s).
* - `data`: Path in the response where the data resides for `replace` and `fields` operations.
* - `skipLock`: If `true`, bypasses request locking.
* - `url`: Overrides the base URL for this request.
* @param method - The HTTP method (e.g., 'post', 'put', 'patch', 'delete', 'get').
* @returns An Observable that emits the processed HTTP response or an error.
*/
private _post;
/**
* Public method to perform a POST request.
* - Supports legacy callback usage.
* - Returns an Observable for reactive programming.
*/
post(url: string, doc: any, callback?: (resp: any) => void, opts?: any): Observable<any>;
/**
* Public method to perform a PUT request.
* - Supports legacy callback usage.
* - Returns an Observable for reactive programming.
*/
put(url: string, doc: any, callback?: (resp: any) => void, opts?: any): Observable<any>;
/**
* Public method to perform a PATCH request.
* - Supports legacy callback usage.
* - Returns an Observable for reactive programming.
*/
patch(url: string, doc: any, callback?: (resp: any) => void, opts?: any): Observable<any>;
/**
* Public method to perform a DELETE request.
* - Supports legacy callback usage.
* - Returns an Observable for reactive programming.
*/
delete(url: string, callback?: (resp: any) => void, opts?: any): Observable<any>;
/**
* Public method to perform a GET request.
* - Supports legacy callback usage.
* - Returns an Observable for reactive programming.
*/
get(url: string, callback?: (resp: any) => void, opts?: any): Observable<any>;
clearLocked(): void;
lock(): void;
unlock(): void;
/**
* Handles HTTP errors.
* - Calls provided error callback and retries the request if needed.
*/
private handleError;
/**
* Internal method to trigger error handling callbacks.
*/
private err_handle;
private prepare_handle;
private response_handle;
/**
* Retrieves a nested object or property from the response based on a dot-separated path.
*
* @param resp - The response object to retrieve data from.
* @param base - A dot-separated string indicating the path to the desired property within the response.
* - Example: `'data.items'` will navigate through `resp.data.items`.
* - If empty, the entire response is returned.
* @returns The object or property located at the specified path within the response.
*/
private _getObjectToReplace;
/**
* Sets or replaces a nested object or property in the response based on a dot-separated path.
*
* @param resp - The response object to modify.
* @param base - A dot-separated string indicating the path to the property to replace.
* - Example: `'data.items'` will navigate through `resp.data.items`.
* @param doc - The new data or object to set at the specified path.
* @returns `void`.
*/
private _setObjectToReplace;
/**
* Creates a new object containing only specified fields from the input item.
*
* @param item - The input object to extract fields from.
* @param fields - An array of field names to include in the new object.
* - Example: `['id', 'name']` will create a new object with only the `id` and `name` properties from `item`.
* @returns A new object containing only the specified fields.
*/
private _newDoc;
static ɵfac: i0.ɵɵFactoryDeclaration<HttpService, [{ optional: true; }, null]>;
static ɵprov: i0.ɵɵInjectableDeclaration<HttpService>;
}
declare class NetworkService {
/** Internal mutable signals. */
private _status;
private _latencyMs;
private _isOnline;
/** Public read-only signals. */
readonly status: i0.Signal<NetworkStatus>;
readonly latencyMs: i0.Signal<number | null>;
readonly isOnline: i0.Signal<boolean>;
/** Failure counter to decide "none". */
private fails;
/**
* Creates the network monitor, binds browser/Capacitor events,
* performs an immediate check, and starts periodic polling.
*/
constructor(config: Config);
/**
* Manually trigger a connectivity check.
* - Measures latency against the first reachable endpoint.
* - Updates `isOnline`, `latencyMs`, and `status` accordingly.
*/
recheckNow(): Promise<void>;
/**
* Classifies current state into 'good' | 'poor' | 'none'.
* - 'none' if offline or too many consecutive failures.
* - 'good' if latency ≤ goodLatencyMs.
* - otherwise 'poor'.
*/
private _updateClassification;
/**
* Binds browser events that can affect connectivity:
* - online/offline (OS connectivity)
* - visibilitychange (recheck on focus)
* - NetworkInformation 'change' (if supported)
*/
private _bindEvents;
/**
* Tries endpoints in order until one responds (CORS or opaque).
* Returns success with measured latency, or a failure result.
*/
private _pingAny;
/**
* Measures a single fetch:
* - Appends a timestamp to bypass caches.
* - Uses `no-store` to avoid intermediaries caching.
* - When `noCors` is true, uses `mode:'no-cors'` and treats a resolved fetch as reachable.
*/
private _measure;
private _config;
private _emitterService;
static ɵfac: i0.ɵɵFactoryDeclaration<NetworkService, [{ optional: true; }]>;
static ɵprov: i0.ɵɵInjectableDeclaration<NetworkService>;
}
declare class StoreService {
constructor(config: Config);
/**
* Sets the prefix for storage keys.
*
* @param prefix - The prefix to set.
*/
setPrefix(prefix: string): void;
/**
* Sets a value in storage asynchronously.
*
* @param key - The storage key.
* @param value - The value to store.
* @returns A promise that resolves to a boolean indicating success.
*/
set(key: string, value: string, callback?: () => void, errCallback?: (err: unknown) => void): Promise<boolean>;
/**
* Gets a value from storage asynchronously.
*
* @param key - The storage key.
* @returns A promise that resolves to the retrieved value or `null` if the key is missing.
*/
get(key: string, callback?: (value: string | null) => void, errCallback?: (err: unknown) => void): Promise<string | null>;
/**
* Sets a JSON value in storage asynchronously.
*
* @param key - The storage key.
* @param value - The value to store.
* @returns A promise that resolves to a boolean indicating success.
*/
setJson<T>(key: string, value: T, callback?: () => void, errCallback?: (err: unknown) => void): Promise<boolean>;
/**
* Gets a JSON value from storage asynchronously.
*
* @param key - The storage key.
* @returns A promise that resolves to the retrieved value.
*/
getJson<T = any>(key: string, callback?: (value: string | null) => void, errCallback?: (err: unknown) => void): Promise<T | null>;
/**
* Removes a value from storage.
*
* @param key - The storage key.
* @param callback - The callback to execute on success.
* @param errCallback - The callback to execute on error.
* @returns A promise that resolves to a boolean indicating success.
*/
remove(key: string, callback?: () => void, errCallback?: (err: unknown) => void): Promise<boolean>;
/**
* Clears all values from storage.
*
* @param callback - The callback to execute on success.
* @param errCallback - The callback to execute on error.
* @returns A promise that resolves to a boolean indicating success.
*/
clear(callback?: () => void, errCallback?: (err: unknown) => void): Promise<boolean>;
private _prefix;
private _config;
/**
* Applies the configured prefix to a storage key.
*
* @param key - The storage key.
* @returns The prefixed storage key.
*/
private _applyPrefix;
static ɵfac: i0.ɵɵFactoryDeclaration<StoreService, [{ optional: true; }]>;
static ɵprov: i0.ɵɵInjectableDeclaration<StoreService>;
}
interface CrudConfig<Document> {
signalFields?: Record<string, (doc: Document) => unknown>;
name: string;
_id?: string;
replace?: (doc: Document) => void;
unauthorized?: boolean;
appId?: string;
}
interface GetConfig {
page?: number;
perPage?: number;
query?: string;
}
/**
* Abstract class representing a CRUD (Create, Read, Update, Delete) service.
*
* This class provides methods for managing documents, interacting with an API,
* and storing/retrieving data from local storage. It is designed to be extended
* for specific document types.
*
* @template Document - The type of the document the service handles.
*/
declare abstract class CrudService<Document extends CrudDocument<Document>> {
private _config;
/**
* Base URL for the API collection associated with this service.
*/
private _url;
/**
* In-memory cache with all documents currently known by the service.
*/
private _docs;
/**
* Number of documents per page for paginated `get()` calls.
*/
private _perPage;
/**
* Registered callbacks that recompute filtered document views.
*/
private _filteredDocumentsCallbacks;
/**
* HTTP client wrapper used for API communication.
*/
protected __httpService: HttpService;
/**
* Key–value storage service used to persist documents locally.
*/
protected __storeService: StoreService;
/**
* Core helper service with utility methods (copy, debounce, toSignal, etc.).
*/
protected __coreService: CoreService;
/**
* Global event bus for cross-service communication.
*/
protected __emitterService: EmitterService;
/**
* Network status service used to queue work while offline.
*/
protected __networkService: NetworkService;
/**
* Emits once when documents are restored from local storage on startup.
*/
loaded: Observable<unknown>;
/**
* Emits once when the initial `get()` (without page param) completes.
*/
getted: Observable<unknown>;
constructor(_config: CrudConfig<Document>);
/**
* Cache of per-document signals indexed by document _id.
* Prevents creating multiple signals for the same document.
*/
private _signal;
/**
* Cache of per (field,value) lists of document signals.
* Key format: `${field}_${JSON.stringify(value)}`.
*/
private _signals;
/**
* Cache of per-field maps: fieldValue -> array of document signals.
*/
private _fieldSignals;
/**
* Returns a WritableSignal for a document by _id, creating it if absent.
* Caches the signal to avoid redundant instances and initializes it
* with the current snapshot of the document.
* Work very carefully with this and localId, better avoid such flows.
*
* @param _id - Document identifier or a document instance.
*/
getSignal(_id: string | Document): WritableSignal<Document>;
/**
* Returns a signal with an array of document signals that match
* a given field/value pair.
*
* Example:
* const activitiesSig = service.getSignals('userId', currentUserId);
*/
getSignals(field: string, value: unknown): WritableSignal<WritableSignal<Document>[]>;
/**
* Builds the array of document signals for a given (field,value) key.
* Only documents with a real _id are included.
*/
private _getSignals;
/**
* Returns a signal with a map: fieldValue -> array of document signals.
*
* Example:
* const byStatusSig = service.getFieldSignals('status');
* byStatusSig() might be { active: [sig1, sig2], draft: [sig3] }.
*/
getFieldSignals(field: string): WritableSignal<Record<string, WritableSignal<Document>[]>>;
/**
* Builds the map for a given field.
* Only documents with a real _id are included.
*/
private _getFieldSignals;
/**
* Clears cached document signals except those explicitly preserved.
* Useful when changing routes or contexts to reduce memory.
*
* @param exceptIds - List of ids whose signals should be kept.
*/
removeSignals(exceptIds?: string[]): void;
/**
* Restores documents from local storage (if present) and syncs
* all existing signals with the restored data.
*/
restoreDocs(): Promise<void>;
/**
* Saves the current set of documents to local storage.
*/
setDocs(): void;
/**
* Retrieves the current list of documents.
*
* @returns The list of documents.
*/
getDocs(filter?: (doc: Document) => boolean): Document[];
/**
* Retrieves the first document that matches the given predicate.
*
* @param find - Predicate used to locate a specific document.
*/
getDoc(find: (doc: Document) => boolean): Document | undefined;
/**
* Clears the current list of documents, persists the empty state
* and recomputes all derived signals.
*
* Empties the internal documents array and saves the updated state to local storage.
*/
clearDocs(): void;
/**
* Adds multiple documents to the service and saves them to local storage.
*
* @param docs - An array of documents to add.
*/
addDocs(docs: Document[]): void;
/**
* Adds a single document to the service. If it already exists, it will be updated.
*
* @param doc - The document to add.
*/
addDoc(doc: Document): void;
/**
* Creates a new document with a temporary ID and status flags.
*
* @param doc - Optional base document to use for the new document.
* @returns A new document instance with default properties.
*/
new(doc?: Document): Document;
/**
* Retrieves a document by its unique ID or creates a new one if it doesn't exist.
*
* @param _id - The document ID to search for.
* @returns The found document or a new document if not found.
*/
doc(_id: string): Document;
/**
* Sets the number of documents to display per page.
*
* @param _perPage - Number of documents per page.
*/
setPerPage(_perPage: number): void;
/**
* Fetches a list of documents from the API with optional pagination.
*
* @param config - Optional pagination configuration.
* @param options - Optional callback and error handling configuration.
* @returns An observable that resolves with the list of documents.
*/
get(config?: GetConfig, options?: CrudOptions<Document>): Observable<Document[]>;
/**
* Sends a request to the API to create a new document.
*
* @param doc - The document to create.
* @param options - Optional callback and error handling configuration.
* @returns An observable that resolves with the created document, or emits an error if already created.
*/
create(doc?: Document, options?: CrudOptions<Document>): Observable<Document>;
/**
* Fetches a document from the API based on a query.
*
* @param query - The query object used to filter documents.
* @param options - Optional callback and error handling configuration.
* @returns An observable that resolves with the fetched document.
*/
fetch(query?: object, options?: CrudOptions<Document>): Observable<Document>;
/**
* Updates a document after a specified delay and returns an observable.
*
* @param doc - The document to update.
* @param options - Optional callback and error handling configuration.
* @returns An observable that emits the updated document.
*/
updateAfterWhile(doc: Document, options?: CrudOptions<Document>): Observable<Document>;
/**
* Updates a document in the API.
*
* @param doc - The document to update.
* @param options - Optional callback and error handling configuration.
* @returns An observable that resolves with the updated document.
*/
update(doc: Document, options?: CrudOptions<Document>): Observable<Document>;
/**
* Unique update a document field in the API.
*
* @param doc - The document to update.
* @param options - Optional callback and error handling configuration.
* @returns An observable that resolves with the updated document.
*/
unique(doc: Document, options?: CrudOptions<Document>): Observable<Document>;
/**
* Deletes a document from the API.
*
* @param doc - The document to delete.
* @param options - Optional callback and error handling configuration.
* @returns An observable that resolves with the deleted document.
*/
delete(doc: Document, options?: CrudOptions<Document>): Observable<Document>;
/**
* Registers a filtered view of documents and returns the recompute callback.
*
* The callback is called automatically whenever `_filterDocuments()` runs.
*/
filteredDocuments(storeObjectOrArray: Record<string, Document[]> | Document[], config?: {
field?: string | ((doc: Document) => string);
valid?: (doc: Document) => boolean;
sort?: (a: Document, b: Document) => number;
filtered?: (storeObjectOrArray: Record<string, Document[]> | Document[]) => void;
}): () => void;
/**
* Track pending fetch-by-id requests to avoid duplicate calls.
*/
private _fetchingId;
/**
* Queue of operations that must be retried when network comes back online.
*/
private _onOnline;
/**
* Local counter used to build unique local identifiers together with Date.now().
*/
private _randomCount;
/**
* Generates a unique ID for a document when using local-only identifiers.
*
* @returns The unique ID as a number.
*/
private _localId;
/**
* Returns the configured identity field for the given document as string.
*
* @param doc - The document for which to generate the ID.
* @returns The unique ID as a string.
*/
private _id;