uni-app x Gets an Axios-Style Request Library with Cancellation, Unified Errors, and a useRequest Hook
Cross-platform uni-app x projects get a request layer that handles cancellation, race conditions, and error normalization, so business code can rely on a single `HttpError` type instead of ad-hoc checks. The design also exposes UTS's type quirks, which any team using uni-app x should know about.
uView Pro X now includes a dedicated http library for uni-app x, written to follow axios's API and exported as `http`. It supports `http.create()` for deriving instances with deep-merged defaults, so multi-backend projects can split baseURL and timeout per instance. Interceptors are stored as arrays: later registrations don't override earlier ones, and returning `null` in the request chain cancels the request. Response interceptors each get the same response object; the last non-null return is the final result, which avoids the type-assertion pain UTS can cause in chained calls.
Cancellation is handled with a custom controller because `uni.request` returns a `RequestTask` with no Promise or `signal`. The library settles the Promise with `ERR_CANCELED` before calling `task.abort()`, since `uni` doesn't guarantee the `fail` callback. All failures reject a single `HttpError` with exactly four `code` values: `ERR_BAD_RESPONSE`, `ERR_NETWORK`, `ERR_CANCELED`, and `ERR_INTERCEPTOR`. The `useRequest` hook wraps request state and handles the usual race-condition problems: it numbers each `run`, discards stale results, cancels on unmount, and cancels the previous request on re-run. Upload and download share the same defaults and interceptors, and upload strips `content-type` automatically.
The library intentionally omits interceptor `eject`, async interceptors, auto retry, and state management. Those are left to the business layer. The accompanying guide also documents pitfalls specific to UTS, such as avoiding `e is HttpError` (breaks on H5) and using `as Promise<T>`.
Array-based interceptors fix the silent override problem of single-slot designs, but the response chain's 'last non-null wins' rule means developers must be careful about which interceptor returns what.
The cancellation fallback is a direct response to `uni.request`'s incomplete abort behavior; settling before aborting ensures no Promise hangs, a pragmatic pattern for any API that lacks full abort support.
UTS's type system shapes the library's API: no `is` operator, no chained type assertions, and no distinction between `null` and missing values. These constraints are worth remembering when writing uni-app x code.
Can you explain what vapor mode actually is? What are its usage scenarios?
It's a rendering mode, similar to Vue's virtual DOM mode.