Skip to content

RequestDefaults ​

The global / instance default configuration type — the unified input of configureRequest(), createRequest(), and the instance setConfig. All four fields must be present (may be null).

uts
/** Global / instance default configuration (input of configureRequest / createRequest / setConfig; null means the built-in default applies) */
export type RequestDefaults = {
	/** Default baseURL */
	baseURL: string | null
	/** Default timeout (ms) */
	timeout: number | null
	/** Default header (shallow-merged with the request level) */
	header: UTSJSONObject | null
	/** Default status code check */
	validateStatus: ((statusCode: number) => boolean) | null
}

Fields ​

NameTypeBuilt-in default when nullDescription
baseURLstring | nullNoneDefault baseURL; when null and the request uses a relative path, fails fast with ERR_CONFIG
timeoutnumber | null60000Entry constant DEFAULT_TIMEOUT; overridden whenever the request-level timeout provides a value
headerUTSJSONObject | nullEmptyDefault header, shallow-merged with the request-level header (request-level keys override same-name keys)
validateStatus((statusCode: number) => boolean) | null2xx checkDefault status code check (statusCode >= 200 && statusCode < 300); overridden as a whole whenever the request level provides a value

Meaning of null

A null field means the built-in default applies (not "clear this configuration") — the normalization layer checks for null item by item and falls back. Therefore "remove the global header" should pass an empty object {}; passing null also yields an empty header, but the former is semantically preferred; there is no way to express "clear the baseURL" (the built-in default is no baseURL anyway).

Usage Sites ​

EntryScopeDescription
configureRequest(defaults)Default instanceGlobal defaults, usually called once in App.uvue onLaunch
createRequest(defaults)New instanceInjected at creation; instances are isolated from each other
instance.setConfig(defaults)Existing instanceUpdates instance defaults at runtime (e.g. environment switching); see Request Instance

No method field

RequestDefaults only has the four fields above — the default method is fixed to 'GET' (entry constant DEFAULT_METHOD) and cannot be customized per instance; pass method explicitly at the request level or use the shortcut methods for other methods.

Examples ​

uts
import { configureRequest, createRequest } from '@/uni_modules/ux-request'

// Global defaults
const globalHeader: UTSJSONObject = {}
globalHeader['x-client'] = 'uni-app-x'
configureRequest({
	baseURL: 'https://api.example.com',
	timeout: 15000,
	header: globalHeader,
	validateStatus: null // keep the built-in 2xx check
})

// Instance defaults (independent of the global defaults)
const oss = createRequest({ baseURL: 'https://oss.example.com', timeout: 5000, header: null, validateStatus: null })

Released under the MIT License.