export type DataProviderRecord = { value: string; label: string; }; export interface DataProvider { /** * Retrieves a single item by its value or its label. * @param value The value of the item to retrieve. * @returns The item that matches the given value, or undefined if not found. */ get(value: string): Promise | DataProviderRecord; /** * Retrieves a list of suggestions based on an input string for a typeahead component. * @param input The input string to use for generating suggestions. * @returns An array of suggestions that match the input. */ find(search: string): Promise | DataProviderRecord[]; } export default class Provider { /** * Cache of results. * * The cache is used to store the results of a query to avoid making the same request multiple times. * Provider name -> Query -> Results */ private finderCache; private getterCache; /** * Registry of providers. * * A provider is a function that takes a query and returns a list of results. * * Some rules to have in mind when creating a provider: * - The provider should return a list of results with the form { value: string, label: string }. * - The provider should not throw an error. Instead, it should return an empty list. * - The provider should be asynchronous. * - The provider should be able to handle a query with a length of 0. * - The provider should return no more than 10 results. * - The provided must be pure and return the same results for the same query. * - Maintain an alphabetical order when adding providers. */ private registry; /** * Returns asynchronously a single record from the data provider. * * The method will cache the result to avoid making the same request multiple times. * * @param name The name of the provider. * @param value The value to search for. * @returns The record that matches the given value, or undefined if not found. */ get(name: string, value: string): Promise | DataProviderRecord | undefined; /** * Returns a single record from the data provider. * * This is a synchronous version of the get method. * It works only and only if the record is already fetched and cached. * * @param name The name of the provider. * @param value The value to search for. * @returns The record that matches the given value, or undefined if not found */ getInstant(name: string, value: string): DataProviderRecord | undefined; /** * Returns asynchronously a list of records from the data provider. * * The method will cache the result to avoid making the same request multiple times. * * @param name The name of the provider. * @param query The query to search for. * @returns An array of records that match the given query. */ find(name: string, query: string): Promise; /** * Adds a new provider to the registry. * * Will throw an error if the provider already exists. * * @param name Provider's name * @param provider The data provider to add. */ addProvider(name: string, provider: DataProvider): void; /** * Returns a provider from the registry. * * @param name The name of the provider to retrieve. * @returns The provider that matches the given name. */ getProvider(name: string): DataProvider; /** * Returns true only and only if the provider exists. * * @param name The name of the provider to retrieve. * @returns True if the provider exists, false otherwise. */ hasProvider(name: string): boolean; }