entropic-bond 1.60.0 → 1.60.1

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.
@@ -0,0 +1,22 @@
1
+ import { Collection } from '../types/utility-types';
2
+ import { AuthService } from "./auth";
3
+ import { UserCredentials, SignData, AuthProvider, CredentialsCustomData } from "./user-auth-types";
4
+ export declare class AuthMock extends AuthService {
5
+ signUp<T extends CredentialsCustomData>(signData: SignData): Promise<UserCredentials<T>>;
6
+ login<T extends CredentialsCustomData>(signData: SignData): Promise<UserCredentials<T>>;
7
+ onAuthStateChange<T extends CredentialsCustomData>(onChange: (userCredentials: UserCredentials<T>) => void): void;
8
+ logout(): Promise<void>;
9
+ resetEmailPassword(email: string): Promise<void>;
10
+ resendVerificationEmail(email: string, _password: string, _verificationLink: string): Promise<void>;
11
+ refreshToken(): Promise<void>;
12
+ linkAdditionalProvider(provider: AuthProvider): Promise<unknown>;
13
+ unlinkProvider(provider: AuthProvider): Promise<unknown>;
14
+ flush(): Promise<void>;
15
+ fakeRegisteredUser<T extends CredentialsCustomData>(userCredentials: UserCredentials<T>): this;
16
+ get fakeRegisteredUsers(): Collection<UserCredentials<CredentialsCustomData>>;
17
+ private userCredentials;
18
+ private pendingPromises;
19
+ private _loggedUser;
20
+ private notifyChange;
21
+ private _fakeRegisteredUsers;
22
+ }
@@ -0,0 +1,131 @@
1
+ import { AuthProvider, CredentialsCustomData, SignData, UserCredentials } from "./user-auth-types";
2
+ /**
3
+ * The AuthService class is an abstract class that defines the interface of an authentication service.
4
+ * You should derive from this class to implement your own authentication service.
5
+ */
6
+ export declare abstract class AuthService {
7
+ abstract signUp<T extends CredentialsCustomData>(signData: SignData): Promise<UserCredentials<T>>;
8
+ abstract login<T extends CredentialsCustomData>(signData: SignData): Promise<UserCredentials<T>>;
9
+ abstract logout(): Promise<void>;
10
+ abstract resetEmailPassword(email: string): Promise<void>;
11
+ abstract refreshToken(): Promise<void>;
12
+ abstract linkAdditionalProvider(provider: AuthProvider): Promise<unknown>;
13
+ abstract unlinkProvider(provider: AuthProvider): Promise<unknown>;
14
+ abstract onAuthStateChange<T extends CredentialsCustomData>(onChange: (userCredentials: UserCredentials<T> | undefined) => void): void;
15
+ abstract resendVerificationEmail(email: string, password: string, verificationLink: string): Promise<void>;
16
+ }
17
+ export type AuthErrorCode = 'wrongPassword' | 'popupClosedByUser' | 'userNotFound' | 'invalidEmail' | 'missingPassword' | 'missingEmail';
18
+ export interface AuthError {
19
+ code: AuthErrorCode;
20
+ message: string;
21
+ }
22
+ /**
23
+ * Types the callback to accept a user credentials object
24
+ */
25
+ export type ResovedCallback<T extends CredentialsCustomData> = (credentials: UserCredentials<T>) => void;
26
+ export type RejectedCallback = (reason: AuthError) => void;
27
+ /**
28
+ * The Auth class is a singleton that provides a unified interface to the authentication service.
29
+ * You should register an authentication service by using `Auth.useAuthService`
30
+ * method before using the Auth class.
31
+ */
32
+ export declare class Auth extends AuthService {
33
+ static error: {
34
+ shouldBeRegistered: string;
35
+ };
36
+ protected constructor();
37
+ /**
38
+ * Registers an authentication service to be used by the Auth class.
39
+ * You need to register an authentication service before using the Auth class.
40
+ * @param authService the authentication service to be used by the Auth class
41
+ */
42
+ static useAuthService(authService: AuthService): void;
43
+ /**
44
+ * The instance of the Auth class
45
+ * @returns the authentication service
46
+ */
47
+ static get instance(): Auth;
48
+ /**
49
+ * Signs up a new user
50
+ * @param singData the data to be used to sign up the user
51
+ * @returns a promise that resolves to the user credentials
52
+ * @example
53
+ * // Sign up a new user with email and password
54
+ * Auth.instance.signUp({ authProvider: 'email', email: 'john@test.com', password: '123456' })
55
+ * // Sign up a new user with a Google account
56
+ * Auth.instance.signUp({ authProvider: 'google'})
57
+ */
58
+ signUp<T extends CredentialsCustomData>(singData: SignData): Promise<UserCredentials<T>>;
59
+ /**
60
+ * Logs in an existing user
61
+ * @param singData the data to be used to log in the user
62
+ * @returns a promise that resolves to the user credentials
63
+ * @example
64
+ * // Log in an existing user with email and password
65
+ * Auth.instance.login({ authProvider: 'email', email: 'john@test.com', password: '123456' })
66
+ * // Log in an existing user with a Google account
67
+ * Auth.instance.login({ authProvider: 'google'})
68
+ */
69
+ login<T extends CredentialsCustomData>(singData: SignData): Promise<UserCredentials<T>>;
70
+ /**
71
+ * Logs out the current user
72
+ * @returns a promise that resolves when the user is logged out
73
+ */
74
+ logout(): Promise<void>;
75
+ /**
76
+ * Resets the password associated with the email.
77
+ * @param email the email address of the user to reset the password
78
+ * @returns a promise that resolves when the process is done
79
+ */
80
+ resetEmailPassword(email: string): Promise<void>;
81
+ /**
82
+ * Resends the email verification to the user.
83
+ * @returns a promise that resolves when the process is done
84
+ */
85
+ resendVerificationEmail(email: string, password: string, verificationLink: string): Promise<void>;
86
+ refreshToken(): Promise<void>;
87
+ /**
88
+ * Adds a listener to be called when the authentication state changes.
89
+ * @param onChange the listener to be called when the authentication state changes.
90
+ * The listener is called with the user credentials as a parameter.
91
+ * If the user is logged out, the listener is called with `undefined` as a parameter.
92
+ * @returns a function to remove the listener
93
+ * @example
94
+ * // Add a listener to be called when the authentication state changes
95
+ * const removeListener = Auth.instance.onAuthStateChange( userCredentials => {
96
+ * if ( userCredentials ) {
97
+ * // The user is logged in
98
+ * } else {
99
+ * // The user is logged out
100
+ * }
101
+ * })
102
+ */
103
+ onAuthStateChange<T extends CredentialsCustomData>(onChange: (userCredentials: UserCredentials<T>) => void): import("..").Unsubscriber;
104
+ /**
105
+ * Removes a listener that was added by `onAuthStateChange` method.
106
+ * @param onChange the listener to be removed
107
+ */
108
+ removeAuthStateChange<T extends CredentialsCustomData>(onChange: (userCredentials: UserCredentials<T>) => void): void;
109
+ /**
110
+ * Links an additional authentication provider to the authenticated user.
111
+ * @param provider the provider to be linked
112
+ * @returns a promise that resolves when the process is done
113
+ * @example
114
+ * // Link a Google account to the auth service
115
+ * Auth.instance.linkAdditionalProvider({ authProvider: 'google' })
116
+ */
117
+ linkAdditionalProvider(provider: AuthProvider): Promise<unknown>;
118
+ /**
119
+ * Unlinks an authentication provider from the authenticated user.
120
+ * @param provider the provider to be unlinked
121
+ * @returns a promise that resolves when the process is done
122
+ * @example
123
+ * // Unlink the Google account from the auth service
124
+ * Auth.instance.unlinkProvider({ authProvider: 'google' })
125
+ */
126
+ unlinkProvider(provider: AuthProvider): Promise<unknown>;
127
+ private authStateChanged;
128
+ private static _instance;
129
+ private static _authService;
130
+ private _onAuthStateChange;
131
+ }
@@ -0,0 +1,22 @@
1
+ export interface CredentialsCustomData {
2
+ [key: string]: any;
3
+ }
4
+ export interface UserCredentials<T extends CredentialsCustomData = {}> {
5
+ id: string;
6
+ email: string;
7
+ name?: string;
8
+ pictureUrl?: string;
9
+ phoneNumber?: string;
10
+ emailVerified?: boolean;
11
+ customData?: T;
12
+ lastLogin?: number;
13
+ creationDate?: number;
14
+ }
15
+ export type AuthProvider = 'email' | 'facebook' | 'google' | 'twitter';
16
+ export interface SignData {
17
+ authProvider: AuthProvider;
18
+ email?: string;
19
+ password?: string;
20
+ name?: string;
21
+ verificationLink?: string;
22
+ }
@@ -0,0 +1,10 @@
1
+ import { CloudFunction, CloudFunctionsService } from './cloud-functions';
2
+ export interface FunctionCollection {
3
+ [key: string]: CloudFunction<any, any>;
4
+ }
5
+ export declare class CloudFunctionsMock implements CloudFunctionsService {
6
+ constructor(registeredFunctions: FunctionCollection);
7
+ retrieveFunction<P, R>(cloudFunction: string): CloudFunction<P, R>;
8
+ callFunction<P, R>(func: CloudFunction<P, R>, params: P): Promise<R>;
9
+ private _registeredFunctions;
10
+ }
@@ -0,0 +1,19 @@
1
+ export type CloudFunction<P, R> = (param?: P) => Promise<R>;
2
+ export interface CloudFunctionsService {
3
+ retrieveFunction<P, R>(cloudFunction: string): CloudFunction<P, R>;
4
+ callFunction<P, R>(func: CloudFunction<P, R>, params: P): Promise<R>;
5
+ }
6
+ export declare class CloudFunctions {
7
+ private constructor();
8
+ static error: {
9
+ shouldBeRegistered: string;
10
+ };
11
+ static useCloudFunctionsService(cloudFunctionsService: CloudFunctionsService): void;
12
+ static get instance(): CloudFunctions;
13
+ getRawFunction<P, R>(cloudFunction: string): CloudFunction<P, R>;
14
+ getFunction<P, R = void>(cloudFunction: string): CloudFunction<P, R>;
15
+ private processParam;
16
+ private processResult;
17
+ private static _cloudFunctionsService;
18
+ private static _instance;
19
+ }
@@ -0,0 +1,23 @@
1
+ export type UploadProgress = (uploadedBytes: number, fileSize: number) => void;
2
+ export type CloudStorageFactory = () => CloudStorage;
3
+ export interface UploadControl {
4
+ pause: () => void;
5
+ resume: () => void;
6
+ cancel: () => void;
7
+ onProgress: (callback: UploadProgress) => void;
8
+ }
9
+ export type StorableData = File | Blob | Uint8Array | ArrayBuffer;
10
+ export declare abstract class CloudStorage {
11
+ abstract save(id: string, data: StorableData, progress?: UploadProgress): Promise<string>;
12
+ abstract getUrl(reference: string): Promise<string>;
13
+ abstract uploadControl(): UploadControl;
14
+ abstract delete(reference: string): Promise<void>;
15
+ static registerCloudStorage(cloudStorageProviderName: string, factory: CloudStorageFactory): void;
16
+ static createInstance(providerName: string): CloudStorage;
17
+ get className(): string;
18
+ static useCloudStorage(provider: CloudStorage): void;
19
+ static get defaultCloudStorage(): CloudStorage;
20
+ static _defaultCloudStorage: CloudStorage;
21
+ private static _cloudStorageFactoryMap;
22
+ }
23
+ export declare function registerCloudStorage(cloudStorageProviderName: string, factory: CloudStorageFactory): (constructor: Function) => void;
@@ -0,0 +1,20 @@
1
+ import { CloudStorage, StorableData, UploadControl } from './cloud-storage';
2
+ export declare class MockCloudStorage extends CloudStorage {
3
+ constructor(pathToMockFiles?: string);
4
+ /**
5
+ * Introduce a delay in the execution of operations to simulate a real data source
6
+ * @param miliSeconds the number of milliseconds to delay the execution of operations
7
+ * @returns a chainable reference to this object
8
+ */
9
+ simulateDelay(miliSeconds: number): this;
10
+ private resolveWithDelay;
11
+ save(id: string, data: StorableData): Promise<string>;
12
+ uploadControl(): UploadControl;
13
+ getUrl(reference: string): Promise<string>;
14
+ delete(reference: string): Promise<void>;
15
+ private _simulateDelay;
16
+ private _pendingPromises;
17
+ private _onProgress;
18
+ private _pathToMockFiles;
19
+ mockFileSystem: {};
20
+ }
@@ -0,0 +1,39 @@
1
+ import { Callback } from '../observable/observable';
2
+ import { Persistent } from '../persistent/persistent';
3
+ import { CloudStorage, StorableData, UploadControl, UploadProgress } from './cloud-storage';
4
+ export declare enum StoredFileEvent {
5
+ stored = 0,
6
+ pendingDataSet = 1,
7
+ deleted = 2
8
+ }
9
+ export interface StoredFileChange {
10
+ event: StoredFileEvent;
11
+ pendingData?: StorableData;
12
+ storedFile: StoredFile;
13
+ }
14
+ export interface StoreParams {
15
+ data?: StorableData;
16
+ fileName?: string;
17
+ progress?: UploadProgress;
18
+ cloudStorageProvider?: CloudStorage;
19
+ }
20
+ export declare class StoredFile extends Persistent {
21
+ save({ data, fileName, progress, cloudStorageProvider }?: StoreParams): Promise<void>;
22
+ uploadControl(): UploadControl;
23
+ delete(): Promise<void>;
24
+ set provider(value: CloudStorage);
25
+ get provider(): CloudStorage;
26
+ get url(): string | undefined;
27
+ get mimeType(): string | undefined;
28
+ setDataToStore(data: StorableData): this;
29
+ get originalFileName(): string | undefined;
30
+ onChange(listenerCallback: Callback<StoredFileChange>): import("..").Unsubscriber;
31
+ private _reference;
32
+ private _url;
33
+ private _cloudStorageProviderName;
34
+ private _originalFileName;
35
+ private _mimeType;
36
+ private _provider;
37
+ private _pendingData;
38
+ private _onChange;
39
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export * from './observable/observable';
2
+ export * from './persistent/entropic-component';
3
+ export * from './persistent/persistent';
4
+ export * from './store/data-source';
5
+ export * from './store/cached-props-updater';
6
+ export * from './store/json-data-source';
7
+ export * from './store/store';
8
+ export * from './store/model';
9
+ export * from './types/utility-types';
10
+ export * from './cloud-storage/cloud-storage';
11
+ export * from './cloud-storage/mock-cloud-storage';
12
+ export * from './cloud-storage/stored-file';
13
+ export * from './auth/auth';
14
+ export * from './auth/user-auth-types';
15
+ export * from './auth/auth-mock';
16
+ export * from './cloud-functions/cloud-functions';
17
+ export * from './cloud-functions/cloud-functions-mock';
18
+ export * from './server-auth/server-auth';
19
+ export * from './server-auth/server-auth-mock';
20
+ export * from './utils/utils';
@@ -0,0 +1,52 @@
1
+ export type Callback<T> = (event: T) => void;
2
+ export type Unsubscriber = () => void;
3
+ /**
4
+ * Implements the Observer pattern.
5
+ * The Observable class is used to notify a list of subscribers when an event occurs.
6
+ * The subscribers are callback functions that are called when the event occurs.
7
+ * The event is passed as a parameter to the callback function.
8
+ * @example
9
+ * // Create an observable
10
+ * const observable = new Observable<number>()
11
+ * // Subscribe a listener
12
+ * const unsubscribe = observable.subscribe( event => console.log( event ) )
13
+ * // Notify the subscribers
14
+ * observable.notify( 1 )
15
+ * // Unsubscribe the listener
16
+ * unsubscribe()
17
+ */
18
+ export declare class Observable<T> {
19
+ /**
20
+ * Subscribes a listener callback function. On every notification,
21
+ * the listener callback will be called with an event as a parameter if sent.
22
+ *
23
+ * @param callback the listener callback
24
+ * @returns a function to unsubscribe the listener from further notifications
25
+ */
26
+ subscribe(callback: Callback<T>): Unsubscriber;
27
+ /**
28
+ * Removes the callback from the notification list.
29
+ *
30
+ * @param callback the listener callback to remove
31
+ */
32
+ unsubscribe(callback: Callback<T>): void;
33
+ /**
34
+ * Notifies all the subscribers with the event passed as parameter.
35
+ *
36
+ * @param event the event passed to all subscribers.
37
+ */
38
+ notify(event?: T): void;
39
+ /**
40
+ * Returns the number of subscribers.
41
+ *
42
+ * @returns the number of subscribers
43
+ * @example
44
+ * const observable = new Observable<number>()
45
+ * observable.subscribe( event => console.log( event ) )
46
+ * observable.subscribe( event => console.log( event ) )
47
+ * observable.subscribe( event => console.log( event ) )
48
+ * console.log( observable.subscribersCount ) // 3
49
+ */
50
+ get subscribersCount(): number;
51
+ private subscribers;
52
+ }
@@ -0,0 +1,75 @@
1
+ import { Callback, Unsubscriber } from '../observable/observable';
2
+ import { ClassProps } from '../types/utility-types';
3
+ import { Persistent } from './persistent';
4
+ export type PropChangeEvent<T> = Partial<ClassProps<T>>;
5
+ export type PropChangeCallback<T> = Callback<PropChangeEvent<T>>;
6
+ export type CompareFunction<T> = (a: T, b: T) => boolean;
7
+ /**
8
+ * Derived classes from EntropicComponent will have the ability to notify
9
+ * property changes by calling one of the provided notification methods.
10
+ * It extends Persistent class therefore EntropicComponent children will have
11
+ * persistence through the Entropic Bond persistence mechanism
12
+ */
13
+ export type StrictElement<T> = T extends any ? (keyof T extends never ? never : T) : never;
14
+ export declare class EntropicComponent extends Persistent {
15
+ /**
16
+ * Subscribes a listener callback function. Every time a property is changed,
17
+ * the listener callback will be called with the property change event.
18
+ *
19
+ * @param listenerCallback the listener callback
20
+ * @returns a function to unsubscribe the listener from further notifications
21
+ */
22
+ onChange(listenerCallback: PropChangeCallback<this>): Unsubscriber;
23
+ /**
24
+ * Removes the listener callback subscrition from the notifications.
25
+ *
26
+ * @param listenerCallback the listener callback to remove
27
+ */
28
+ removeOnChange(listenerCallback: PropChangeCallback<this>): void;
29
+ /**
30
+ * Changes the value of the property and notifies the subscribers about the change.
31
+ * This is a helper method that can be used in the property setter.
32
+ *
33
+ * @param propName the name of the property to be changed
34
+ * @param value the new value for the property
35
+ * @returns true in case the property has been effectively changed, false otherwise
36
+ */
37
+ protected changeProp<P extends keyof this>(propName: P, value: this[P]): boolean;
38
+ /**
39
+ * Notifies the subscribers a property or group of properties change.
40
+ * This is a helper function to be used when you want to notify property changes.
41
+ *
42
+ * @param event the event with the changed properties
43
+ */
44
+ protected notify<T extends EntropicComponent>(event: PropChangeEvent<T>): void;
45
+ /**
46
+ * Inserts a new element in an arbitrary array property of this class and
47
+ * fires a change event if successfully inserted. To avoid repeated elements
48
+ * to be inserted, you can pass a function that checks for inequity.
49
+ *
50
+ * @param arrayPropName the name of the array property of this class where you
51
+ * want to insert the new element.
52
+ * @param element the element to be inserted
53
+ * @param isUnique a function that checks for inequity of the two elements
54
+ * passed as parameter. If the returned value is true, the
55
+ * value will be pushed into the array. When the function is
56
+ * not provided, the element will be inserted regardless it is
57
+ * already in the array.
58
+ * @returns the inserted element or undefined if the element was not inserted.
59
+ */
60
+ protected pushAndNotify<T extends keyof this, E>(this: Record<T, readonly E[]> & EntropicComponent, arrayPropName: T, element: StrictElement<E>, isUnique?: CompareFunction<StrictElement<E>>): E | undefined;
61
+ /**
62
+ * Removes an element from an arbitrary array property of this class and fires
63
+ * a change event on operation success.
64
+ *
65
+ * @param arrayPropName the name of the array property of this class where you
66
+ * want to insert the new element.
67
+ * @param element the element to be inserted
68
+ * @param isEqual a function that checks for equity of the two elements
69
+ * passed as parameter. If the returned value is true, the
70
+ * value will be removed from the array.
71
+ * @returns the removed element or undefined if the element was not removed.
72
+ */
73
+ protected removeAndNotify<T extends keyof this, E>(this: Record<T, readonly E[]> & EntropicComponent, arrayPropName: T, element: StrictElement<E>, isEqual: CompareFunction<StrictElement<E>>): E | undefined;
74
+ private _onChange;
75
+ }