FastAxios container
createFastAxios(options?, newInstance?) Create
Creates or updates the FastAxios configuration container.
const fastAxios = createFastAxios({
baseUrl: "https://api.example.com",
timeout: 30_000,
headers: { Authorization: "Bearer <token>" },
requestCipher: false,
});| Parameter | Type | Default | Description |
|---|---|---|---|
options.baseUrl | string | "" | Base URL used when creating Axios request instances. |
options.timeout | number | 60000 | Request timeout in milliseconds. |
options.headers | Record<string, AxiosHeaderValue> | {} | Shared request headers; repeated configuration merges individual fields. |
options.requestCipher | boolean | true | Encryption/decryption switch used unless overridden for a request. |
newInstance | boolean | false | When true, returns an independent container without writing the global singleton. |
axiosUtil.request() always reads the global singleton. Independent containers do not automatically participate in requests.
Repeated singleton-mode createFastAxios() calls retain existing handlers and merge base options through setOptions().
useFastAxios() Instance
Returns the initialized global container. Throws Error before createFastAxios() has been called.
setOptions(options?) Options
Updates base options and returns the current container, preserving its concrete subtype in fluent TypeScript calls. Headers merge by field; other supplied values replace existing ones. Empty strings, 0, and false are valid.
addErrorCode() Errors
Adds or replaces error messages:
fastAxios.addErrorCode(40101, "登录状态已失效");
fastAxios.addErrorCode({
40102: "账号已停用",
CUSTOM_ERROR: "自定义错误",
});Supports HTTP status, Axios error, and Fast business codes. The single-code overload throws TypeError when message is absent.
createFastAxios Create
Creates or updates the global FastAxios container, or returns an independent container without changing the singleton.
Signature
createFastAxios(options?: InitializeOptions, newInstance?: boolean): FastAxios;Example
import { createFastAxios } from "@fast-china/axios";
const result = createFastAxios({ baseUrl: "/api", timeout: 30_000, requestCipher: false });Input
| Input | Type | Required / default | Description |
|---|---|---|---|
options | InitializeOptions | Optional | Base URL, timeout, common headers, and global encryption/decryption switch. |
newInstance | boolean | Optional; defaults to false | When true, creates an independent container without affecting the global singleton. |
Returns
| Value | Type | Description |
|---|---|---|
result | FastAxios | Global singleton or newly created independent configuration container. |
useFastAxios Instance
Gets the initialized global FastAxios singleton.
Signature
useFastAxios(): FastAxios;Example
import { createFastAxios, useFastAxios } from "@fast-china/axios";
createFastAxios({ baseUrl: "/api" });
const result = useFastAxios();Input
This method has no input parameters.
Returns
| Value | Type | Description |
|---|---|---|
result | FastAxios | Initialized global container; throws Error when not initialized. |
fastAxios.setOptions Options
Merges base configuration: headers merge by field, other explicit values replace existing ones.
Signature
setOptions(options?: InitializeOptions): this;Example
import { createFastAxios } from "@fast-china/axios";
const fastAxios = createFastAxios({ baseUrl: "/api" }, true);
const result = fastAxios.setOptions({ timeout: 15_000, headers: { Authorization: "Bearer token" } });Input
| Input | Type | Required / default | Description |
|---|---|---|---|
options | InitializeOptions | Optional; defaults to {} | Base options to update. |
Returns
| Value | Type | Description |
|---|---|---|
result | this | Current container, supporting fluent calls. |
fastAxios.addErrorCode Errors
Adds or replaces one error message, or accepts an error-code map.
Signature
addErrorCode(key: string | number, message: string): FastAxios;
addErrorCode(codes: Record<string | number, string>): FastAxios;Example
import { createFastAxios } from "@fast-china/axios";
const fastAxios = createFastAxios({ baseUrl: "/api" }, true);
fastAxios.addErrorCode(40101, "登录状态已失效");
const result = fastAxios.addErrorCode({ 40102: "账号已停用", CUSTOM_ERROR: "自定义错误" });Input
| Input | Type | Required / default | Description |
|---|---|---|---|
key | string | number | Required for the single-code overload | HTTP, Axios, or Fast business error code. |
message | string | Required for the single-code overload | Message shown to the user. |
codes | Record<string | number, string> | Required for the map overload | Batch mapping of error codes to messages. |
Returns
| Value | Type | Description |
|---|---|---|
result | FastAxios | Current container, supporting fluent calls. |
