import { AjaxRequest } from 'rxjs/ajax'; import type { AjaxResponse } from 'rxjs/ajax'; import { Observable } from 'rxjs'; import { AjaxCancelError } from './AjaxCancelError'; export interface AjaxSettings extends AjaxRequest { /** * 异常创建 * * 第一个捕获异常的钩子方法, * 可以用来创建业务异常或连接异常 */ beforeCatchError?: (error: any) => any; /** * 检测业务异常,并返回异常对象 * * 第二个检测异常的钩子方法, * 可以在后端接口正常返回时,探测是否为异常业务代码,从而决定是否创建BusinessError */ detectError?: (ajaxResponse: AjaxResponse) => any; /** * 异常处理 * * 第三个捕获异常的钩子方法, * 在经过前面两个创建异常方法的铺垫后,这个方法用来调用异常处理逻辑 */ afterCatchError?: (error: any) => any; /** * 如果你需要修改请求的response,可以通过这个方法 */ transformData?: (AjaxResponse: AjaxResponse) => any; /** * 请求失败后,重新尝试请求次数 */ retryTimes?: number; } /** * 包装rxjs ajax模块,并对异常捕获、数据转换、重试等功能需求,做简易的封装。 * 除了自定义参数{@link AjaxSettings},其他用法与{@link https://www.learnrxjs.io/learn-rxjs/operators/creation/ajax | rxjs ajax}用法一致 */ export declare function ajax(settings: AjaxSettings): Observable; /** * 终止所有请求 * * ```ts * import { ajaxO } from 'sunny-js' * ajaxO * .get('https://httpbin.org/deplay/3', { field: 'value' }) * .subscribe(console.log) * ajaxO.cancelAll() * ``` * * @see {@link isCancel} */ export declare function cancelAll(): void; /** * 判断ajax是否取消 * * ```ts * import { ajaxO } from 'sunny-js' * ajaxO * .get('https://httpbin.org/deplay/3', { field: 'value' }) * .subscribe(console.log, error => * console.log(ajaxO.isCancel(error) && 'ajax canceled') * ) * ajaxO.cancelAll() * ``` * * @see {@link cancelAll} */ export declare function isCancel(error: Error): error is AjaxCancelError; type AjaxSetupFn = (settings: AjaxSettings) => AjaxSettings; /** * ajax配置接口 */ export declare function setup(fn: AjaxSetupFn): void; /** * @internal */ export type shorthandMethodArgs = [ url: string, body?: any, /** * options参数可以参照官方用例 https://rxjs.dev/api/ajax/ajax#using-ajax-with-object-as-argument-and-method-post-with-a-two-seconds-delay- */ options?: AjaxSettings ]; /** * ajax快捷方法代理函数 * @param call * @param method * @param args */ export declare function shorthandMethod(call: (settings: AjaxSettings) => T, method: string, ...args: shorthandMethodArgs): T; /** * ajax get 快捷方法 * ```ts * import { ajaxO } from 'sunny-js' * ajaxO * .get('https://httpbin.org/anything', { field: 'value' }) * .subscribe(console.log) * ``` */ export declare function get(...args: shorthandMethodArgs): Observable; /** * ajax post 快捷方法 * ```ts * import { ajaxO } from 'sunny-js' * ajaxO * .post('https://httpbin.org/anything', { field: 'value' }) * .subscribe(console.log) * ``` */ export declare function post(...args: shorthandMethodArgs): Observable; /** * ajax put 快捷方法 * ```ts * import { ajaxO } from 'sunny-js' * ajaxO * .put('https://httpbin.org/anything', { field: 'value' }) * .subscribe(console.log) * ``` */ export declare function put(...args: shorthandMethodArgs): Observable; /** * ajax patch 快捷方法 * ```ts * import { ajaxO } from 'sunny-js' * ajaxO * .patch('https://httpbin.org/anything', { field: 'value' }) * .subscribe(console.log) * ``` */ export declare function patch(...args: shorthandMethodArgs): Observable; /** * ajax delete 快捷方法 * ```ts * import { ajaxO } from 'sunny-js' * ajaxO * .del('https://httpbin.org/anything', { field: 'value' }) * .subscribe(console.log) * ``` */ export declare function del(...args: shorthandMethodArgs): Observable; export {};