import TokenData from './TokenData.js'; import type { GuestTokenRequester, OptionsStorageProvider, OptionsStorageSerializer, UserTokenRequester } from '../types/TokenManagerOptions.types.js'; import type { ITokenData } from './types/TokenData.types.js'; import type { TokenChangesListeners } from './types/TokenChangesListeners.types.js'; /** * Base class for a token provider. Provides the base methods required to renew, * save and clear tokens. */ declare class TokenProvider { canSaveTokenDataFlag: boolean; requester: UserTokenRequester | GuestTokenRequester; storageKey?: string; storageProvider?: OptionsStorageProvider; tokenChangesListeners: Array; tokenData: TokenData | null; tokenDataSerializer?: OptionsStorageSerializer; userId?: number; /** * Constructs a new TokenProvider instance. * * @param requester - A function that will be responsible to request new tokens. If async, * the call will be awaited. * @param storageProvider - An object implementing the Storage API's methods getItem, setItem and * removeItem. If those methods are async, the calls will be awaited. * @param tokenDataSerializer - An object implementing the serializeTokenData and deserializeTokenData * methods. If storage provider is defined, tokenDataSerializer is * required. * @param storageKey - The storage key that will be used on the calls to storageProvider's * methods as the key argument. */ constructor(requester: UserTokenRequester | GuestTokenRequester, storageProvider?: OptionsStorageProvider, tokenDataSerializer?: OptionsStorageSerializer, storageKey?: string); /** * Returns the current user id associated with this token provider instance. * * @returns The current user id associated with this token provider instance or undefined if not set. */ getUserId(): number | undefined; /** * Returns the most recently cached access token or undefined if no access token is * available. * * @returns The most recently cached access token or undefined if no access token is available. */ getCachedAccessToken(): string | undefined; /** * Sets token data with this instance which will trigger the onTokenDataChanged * method. * * @param tokenData - Token data to be set. * * @returns Promise that will be resolved after the call to onTokenDataChanged returns. */ setTokenData(tokenData: TokenData | null): Promise; /** * Gets the currently set token data via setTokenData function. * * @returns The currently set token data. */ getTokenData(): ITokenData | null; /** * Gets the kind of tokens that are supported by this provider. * * @returns The kind of tokens supported. */ getSupportedTokenKind(): void; invalidateCurrentAccessToken(): Promise; /** * Sets user id with this instance. Will trigger a call to onTokenDataChanged if * the passed user id is different than the current one set. This is what will make * the association of a previously obtained access token with a user id because the * data returned from the token creation endpoints does not contain the user id * yet. * * @param userId - The user id to set. * * @returns Promise that will be resolved after the call to onTokenDataChanged returns. */ setUserId(userId: number): Promise; /** * Clears all data from memory and triggers onTokenDataChanged to remove data from * storage, if provided. * * @returns Promise that will be resolved after the call to onTokenDataChanged returns. */ clearData(): Promise; /** * Method responsible to get valid access tokens. It must be implemented by a * subclass. * * @returns Promise that will be resolved with a valid access token to be used. */ getAccessToken(): void; /** * Method responsible to load access tokens from storage, if available. * * @returns Promise that will be resolved when the storageProvider's getItem method is finished. */ load(): Promise; /** * Called after the token data has changed. Will persist token data if a storage * provider instance is provided. * * @returns Promise that will be resolved when the call to storageProvider's methods are finished. */ onTokenDataChanged(): Promise; /** * Invokes token data changes listeners. * * @param newTokenData - The new token data. */ invokeTokenChangedListeners(newTokenData: ITokenData | null): void; /** * Method that checks if this instance is fully configured to accept retrieve * access token requests. * * @returns True if the instance is ready to retrieve tokens and false otherwise. */ canRetrieveTokens(): boolean; /** * Adds a listener to token data changes. * * @param tokenChangesListener - The listener to add. * * @returns The id of the listener in order to be used on the removeTokenChangesListener function. */ addTokenChangesListener(tokenChangesListener: (tokenData?: ITokenData | null) => void): number; /** * Removes a listener from the token data changes listeners list. * * @param tokenChangesListenerId - The id of the listener to remove. */ removeTokenChangesListener(tokenChangesListenerId: number): void; /** * Returns if the current token data can be persisted in storage. * * By default returns true only if both the storage provider and * canSaveTokenDataFlag are true. * * @returns If the token can be saved. */ canSaveTokenData(): boolean; /** * Sets the flag that enables/disables the saving of token data in storage. * * @throws * TypeError if not receiving a boolean. * * @param canSaveTokenData - If the token can be saved or not. */ setCanSaveTokenData(canSaveTokenData: boolean): void; } export default TokenProvider;