Skip to content

FAQ ​

Why is del not named delete? ​

delete is a UTS reserved word and cannot be an exported function name, so the DELETE shortcut method is named del (instance methods likewise). See Making Requests.

Is the generic T only a static annotation? ​

Yes. The generic T of request<T> / the shortcut methods only provides a static type annotation of the consumer's TS view for res.data; at runtime data flows as any, with no runtime validation or instantiation:

uts
type UserInfo = { id: number, name: string }

// even if the server does not actually return UserInfo, compilation still passes — T plays no part at runtime
get<UserInfo>('/api/user/1', null).then((res) => {
	console.log(res.data.name)
})

Responses in instance methods and interceptors are uniformly ResponseAny (data is any); assert on the business side as needed. See Response Schema.

Why did the GET request's data end up in the URL? ​

That is the library's explicit contract: for GET/HEAD, UTSJSONObject data is automatically encoded into the query (after params), and after normalization the request body is always null. The reason: the official implicit "GET data → query" conversion has no documented encoding rule, so the library replaces that behavior with deterministic encoding to keep all five platforms consistent. Side effect: a custom transformRequest receives null data for GET/HEAD and must null-check. See Params & Headers and Transforming Requests & Responses.

When is ERR_CONFIG thrown? ​

When neither the request level nor the instance defaults have a baseURL and the url is a relative path, normalization fail-fasts (no network request) with the errMsg 「相对路径请求必须配置 baseURL」:

uts
// neither condition satisfied → catch receives ERR_CONFIG
request<any>({ url: '/api/list' })

Two solutions, pick one: set a default baseURL via configureRequest (or createRequest / setConfig); or pass baseURL at the request level / use an absolute url directly (urls starting with http(s):// skip baseURL joining). See Request Config and Instances & Global Defaults.

What is the relationship between ERR_TIMEOUT / ERR_NETWORK and nativeErrCode? ​

The in-library classification is a deterministic mapping: native errCode 5 → ERR_TIMEOUT, 600003 / 600010 → ERR_NETWORK, everything else → ERR_NATIVE; nativeErrCode is always the native errCode passed through with its original value. Handle the big categories by code on the business side, and read nativeErrCode when you need finer native semantics. Note the per-platform differences: "unroutable address + timeout" on Web may classify as ERR_NETWORK. See Error Handling and Platform Compatibility.

Why is config.url already the full address inside an interceptor? ​

Request interceptors run after config normalization: by then the url has baseURL joined, params have been encoded into the url and cleared, and GET/HEAD data has been merged into the query and set to null. This is the "normalized output = send snapshot" design — interceptors see the final form about to be sent, which is also why rewriting params inside an interceptor has no effect (to append a query, modify config.url directly). See Request Flow.

How do I get cross-platform TS type hints? ​

All types are imported with import type from the package entry; the consumer's TS view is auto-generated by uts-proxy, and HBuilderX completion for .uvue / .uts files is based on it:

uts
import { request, get } from '@/uni_modules/ux-request'
import type { RequestConfig, RequestError, ResponseResult } from '@/uni_modules/ux-request'

For the generic T's static annotation capability see Response Schema.

How do I cancel a request and re-issue it? ​

Tokens are one-shot: after cancel, any new request mounted on that token is intercepted at the pre-send check and rejects with ERR_CANCELED directly. To "cancel the old one, then send a new one" (e.g. re-searching when the search input changes), create a fresh cancel source every time:

uts
import { get, createCancelSource } from '@/uni_modules/ux-request'
import type { RequestError } from '@/uni_modules/ux-request'

let current = createCancelSource()

function search(keyword: string): void {
	current.cancel('新搜索')
	current = createCancelSource()
	get<any>('/api/search', {
		params: { keyword: keyword } as UTSJSONObject,
		cancelToken: current.token
	}).catch((err) => {
		const e = err as RequestError
		if (e.code != 'ERR_CANCELED') {
			// only real network errors should be surfaced
		}
	})
}

For the complete semantics of cancellation see Cancellation.

Released under the MIT License.