import { HttpServiceBuilderWithMeta, HttpServiceBuilderWithMetas, HttpServiceBuilder } from './HttpServiceBuilder'; import { IResponseAction, BaseRequestAction, IBaseRequestAction, RequestSuccessAction, RequestFailAction } from '../actions/BaseRequestAction'; import { OrphanRequestOptions } from './OrphanHttpService'; import { METHOD } from '../utils/method'; export interface FetchHandle void> extends Promise> { cancel: CancelFn; } export declare type PickData = T extends (...args: any[]) => HttpServiceBuilder ? Data : never; export declare type PickResponse = T extends (...args: any[]) => HttpServiceBuilderWithMeta ? Response : T extends (...args: any[]) => HttpServiceBuilderWithMetas ? Response : never; export declare type PickPayload = T extends (...args: any[]) => HttpServiceBuilderWithMeta ? Payload : T extends (...args: any[]) => HttpServiceBuilderWithMetas ? Payload : unknown; export declare type PickMeta = T extends (...args: any[]) => HttpServiceBuilderWithMetas ? M : never; export interface BaseHttpServiceConfig { /** * The common url prefix. * @example http://api.com * @example http://api.com/api */ baseUrl: string; requestOptions?: object; /** * Display your success message to the screen. It only happens when you specific the successText in action. * ```javascript * $api.action((data) => { * return this * .post('/api') * .body(data) * .successText('Created'); * }); * ``` * And in service * ```javascript * { * onShowSuccess(message) { * alert(message); * } * } * ``` */ onShowSuccess: (successText: string, action: IResponseAction) => void; /** * Display your error message to the screen. * ```javascript * { * onShowError(message) { * alert(message); * } * } * ``` */ onShowError: (errorText: string, action: IResponseAction) => void; /** * Collect error message for request timeout. */ timeoutMessage?: (originalText: string) => string; /** * Collect error message for network is unavailable. */ networkErrorMessage?: (originalText: string) => string; /** * Control the throttle token * { * throttleTransfer(options) { * delete options.query.__timestamp__; * } * } */ throttleTransfer?: NonNullable; } export interface ThrottleKeyOption { actionName: string; url: string; method: METHOD; body: Record; query: Record; headers: Record; transfer?: (options: Omit) => void | Omit; } export declare abstract class BaseHttpService { protected readonly config: T; protected caches: Partial<{ [actionName: string]: Partial<{ [throttleKey: string]: { timestamp: number; response: any; }; }>; }>; constructor(config: T); action HttpServiceBuilderWithMeta, Data = PickData, Response = PickResponse, Payload = PickPayload>(fn: Fn): ((...args: Parameters) => FetchHandle) & Omit, 'metas' | 'loadings'>; action HttpServiceBuilderWithMetas, Data = PickData, Response = PickResponse, Payload = PickPayload, M = PickMeta>(fn: Fn): ((...args: Parameters) => FetchHandle) & Omit, 'meta' | 'loading'>; getAsync(config: OrphanRequestOptions): FetchHandle; postAsync(config: OrphanRequestOptions): FetchHandle; putAsync(config: OrphanRequestOptions): FetchHandle; deleteAsync(config: OrphanRequestOptions): FetchHandle; patchAsync(config: OrphanRequestOptions): FetchHandle; connectAsync(config: OrphanRequestOptions): FetchHandle; protected runService(config: OrphanRequestOptions, method: METHOD): FetchHandle; protected generateThrottleKey(options: ThrottleKeyOption): string; protected getThrottleData(action: IBaseRequestAction, throttleKeyOption: ThrottleKeyOption): FetchHandle | void; protected setThrottle(action: RequestSuccessAction): void; clearThrottle(): void; clearThrottle(actionName: string): void; protected abstract runAction(action: IBaseRequestAction): FetchHandle; protected triggerShowSuccess(okResponse: RequestSuccessAction, successText: string): void; protected triggerShowError(errorResponse: RequestFailAction, hideError: boolean | ((response: RequestFailAction) => boolean)): void; }