UNPKG

@kaaiot/services

Version:

Type definitions for KaaIoT platform REST API service communication

41 lines (40 loc) 1.4 kB
import { ServiceRequester } from "./service-requester"; /** * Adds cancellation capabilities to a client that uses ServiceRequester * * @example * // Using with factory function * import { createRSClient } from '~/shared/clients/rs'; * import { withCancellation, isCanceledError } from '~/shared/clients/with-cancellation'; * * const createCancellableRSClient = withCancellation(createRSClient); * const rsClient = useMemo(() => createCancellableRSClient('/rs'),[]); * * // Using with class * const rsClient = useMemo( () => withCancellation<RSClient>((baseUrl, serviceRequester) => { * return new RSClient(baseUrl, serviceRequester); * })('/rs'),[]); * * // Usage * try { * // Cancel previous request if exists * rsClient.cancel(); * rsClient.resetCancelToken(); * * const response = await rsClient.evaluate(query); * } catch (error) { * if (isCanceledError(error)) { * console.log('Request cancelled'); * } * } */ declare type ClientWithHttp = { http: ServiceRequester; }; export declare type CancellableClient<T> = T & { cancel(message?: string): void; resetCancelToken(): void; }; export declare function withCancellation<T extends ClientWithHttp>(factory: (baseUrl: string, serviceRequester?: ServiceRequester) => T): (baseUrl: string) => CancellableClient<T>; export declare function isCanceledError(error: any): boolean; export {};