Skip to main content
Deno 2 is finally here 🎉️
Learn more
Latest
class Ajax
import { Ajax } from "https://deno.land/x/jwfetch@v2.2.1/src/ajax.ts";

Constructors

new
Ajax(defaultConfig?: AjaxExConfig)

Properties

caches: Map<string, any>
cachesTimeoutKeyMap: Map<string, number>
defaultConfig: AjaxExConfig
fetchTimeoutKeys: Set<number>
interceptors: { request: Interceptors<RequestCallback, ErrorRequestCallback>; response: Interceptors<ResponseCallback, ErrorResponseCallback>; }
logger: Logger
revalidateCacheTimeoutKeyMap: Map<string, number>

Methods

private
cache_ajax(cfg: AjaxConfig, isRevalidate: boolean): AjaxResult

缓存请求,同一时间同一请求只会向后台发送一次

private
clearCacheByKey(uniqueKey: string, cacheTimeout?: number)
private
core_ajax(mergedConfig: AjaxConfig): AjaxResult
private
fetch_timeout(
fetchPromise: Promise<any>,
controller: AbortController | undefined,
config: AjaxConfig,
)

实现fetch的timeout 功能

private
handleGetUrl(
url: string,
isEncodeUrl?: boolean,
)
private
handlePostData(data: any, isFile?: boolean)
private
logDebug(message: string, isDebug?: boolean): void
private
mergeAbortConfig(config: AjaxConfig, signal?: AbortSignal): AbortController | undefined
private
mergeResponse(promise: Promise<any>)
private
request(config: AjaxConfig)

进行fetch请求

protected
getUniqueKey(config: AjaxConfig): string
abort(controller?: AbortController)

取消接口请求

取消所有接口请求

ajax<T>(cfg: AjaxConfig): Promise<T>

ajax主方法,返回promise

调用ajax的同时,返回取消ajax请求的方法

清除所有缓存的timeout

手动清除缓存

get<T>(
url: string,
data?: AjaxGetData,
options?: AjaxExConfig,
): Promise<T>
getAbortResult<T>(
url: string,
data?: AjaxGetData,
options?: AjaxExConfig,
): AbortResult<T>

调用ajax的get请求的同时,返回取消ajax请求的方法

getWithHeaders<T>(
url: string,
data?: AjaxGetData,
options?: AjaxExConfig,
): Promise<{ data: T; headers: Record<string, string | null>; }>
isAbortError(err: Error): boolean
post<T>(
url: string,
options?: AjaxExConfig,
): Promise<T>
postAbortResult<T>(
url: string,
options?: AjaxExConfig,
): AbortResult<T>

调用ajax的post请求同时,返回取消ajax请求的方法

postWithHeaders<T>(
url: string,
options?: AjaxExConfig,
): Promise<T>