Skip to content

FastAxios container

createFastAxios(options?, newInstance?) Create

Creates or updates the FastAxios configuration container.

ts
const fastAxios = createFastAxios({
	baseUrl: "https://api.example.com",
	timeout: 30_000,
	headers: { Authorization: "Bearer <token>" },
	requestCipher: false,
});
ParameterTypeDefaultDescription
options.baseUrlstring""Base URL used when creating Axios request instances.
options.timeoutnumber60000Request timeout in milliseconds.
options.headersRecord<string, AxiosHeaderValue>{}Shared request headers; repeated configuration merges individual fields.
options.requestCipherbooleantrueEncryption/decryption switch used unless overridden for a request.
newInstancebooleanfalseWhen 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:

ts
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

ts
createFastAxios(options?: InitializeOptions, newInstance?: boolean): FastAxios;

Example

ts
import { createFastAxios } from "@fast-china/axios";

const result = createFastAxios({ baseUrl: "/api", timeout: 30_000, requestCipher: false });

Input

InputTypeRequired / defaultDescription
optionsInitializeOptionsOptionalBase URL, timeout, common headers, and global encryption/decryption switch.
newInstancebooleanOptional; defaults to falseWhen true, creates an independent container without affecting the global singleton.

Returns

ValueTypeDescription
resultFastAxiosGlobal singleton or newly created independent configuration container.

useFastAxios Instance

Gets the initialized global FastAxios singleton.

Signature

ts
useFastAxios(): FastAxios;

Example

ts
import { createFastAxios, useFastAxios } from "@fast-china/axios";

createFastAxios({ baseUrl: "/api" });
const result = useFastAxios();

Input

This method has no input parameters.

Returns

ValueTypeDescription
resultFastAxiosInitialized global container; throws Error when not initialized.

fastAxios.setOptions Options

Merges base configuration: headers merge by field, other explicit values replace existing ones.

Signature

ts
setOptions(options?: InitializeOptions): this;

Example

ts
import { createFastAxios } from "@fast-china/axios";

const fastAxios = createFastAxios({ baseUrl: "/api" }, true);

const result = fastAxios.setOptions({ timeout: 15_000, headers: { Authorization: "Bearer token" } });

Input

InputTypeRequired / defaultDescription
optionsInitializeOptionsOptional; defaults to {}Base options to update.

Returns

ValueTypeDescription
resultthisCurrent container, supporting fluent calls.

fastAxios.addErrorCode Errors

Adds or replaces one error message, or accepts an error-code map.

Signature

ts
addErrorCode(key: string | number, message: string): FastAxios;
addErrorCode(codes: Record<string | number, string>): FastAxios;

Example

ts
import { createFastAxios } from "@fast-china/axios";

const fastAxios = createFastAxios({ baseUrl: "/api" }, true);

fastAxios.addErrorCode(40101, "登录状态已失效");
const result = fastAxios.addErrorCode({ 40102: "账号已停用", CUSTOM_ERROR: "自定义错误" });

Input

InputTypeRequired / defaultDescription
keystring | numberRequired for the single-code overloadHTTP, Axios, or Fast business error code.
messagestringRequired for the single-code overloadMessage shown to the user.
codesRecord<string | number, string>Required for the map overloadBatch mapping of error codes to messages.

Returns

ValueTypeDescription
resultFastAxiosCurrent container, supporting fluent calls.