Skip to main content
Version: v3

Alova Instance

createAlova()

Create an Alova instance.

  • Type
function createAlova(options?: AlovaOptions): Alova;
  • Parameter
  1. config: configuration options
Parameter nameTypeDescription
requestAdapterobjectRequest adapter, required, see details
idstring | numberAlova instance id, optional, see details
baseURLstringBase path, optional, default is empty, see details
statesHookobjectState management hook, optional, see details
timeoutnumberTimeout, default is no timeout, see details
cacheForobjectLocal cache configuration, default GET has 5000ms cache, see details
l1CacheobjectLevel1 cache adapter see details
l2CacheobjectLevel2 cache adapter, see details
beforeRequestfunctionBefore request hook, see details
respondedobject | functionRequest response hook, see details
shareRequestbooleanShare request, see details
cacheLoggerboolean | null | functionCache log, see details
snapshotsnumberLimits the number of retained method snapshots; defaults to 1000. see details
  • Return

Alova instance

  • Example
import { createAlova } from 'alova';
import VueHook from 'alova/vue';
import adapterFetch from 'alova/fetch';

const alova = createAlova({
baseURL: 'https://example.com',
statesHook: VueHook,
requestAdapter: adapterFetch(),
timeout: 3000
// ...
});

alova.id

The alova instance ID, used to distinguish between different alova instances. It lets you precisely match the method instances of a specific alova instance via the method matcher.

  • Type: string

alova.options

When creating an alova instance via createAlova, this is the configuration object after merging the default options with the ones you provide.

  • Type
interface AlovaOptions {
statesHook: StatesHook;
requestAdapter: AlovaRequestAdapter;
baseURL?: string;
timeout?: number;
cacheFor?: GlobalCacheForConfig;
l1Cache?: AlovaStorageAdapter;
l2Cache?: AlovaStorageAdapter;
beforeRequest?: Function;
responded?: Function | ResponsedHandlerRecord;
shareRequest?: boolean;
cacheLogger?: boolean | null | Function;
snapshots?: number;
}

alova.l1Cache

The level-1 cache adapter for this alova instance. Defaults to an in-memory cache.

  • Type
interface AlovaStorageAdapter {
get(key: string): any;
set(key: string, value: any): void;
remove(key: string): void;
clear(): void;
}

alova.l2Cache

The level-2 cache adapter for this alova instance. Defaults to localStorage on the client; there is no adapter on the server by default.

  • Type
interface AlovaStorageAdapter {
get(key: string): any;
set(key: string, value: any): void;
remove(key: string): void;
clear(): void;
}

alova.snapshots

The method snapshot storage for the current instance.

  • Type
class MethodSnapshotContainer<AG extends AlovaGenerics> {
records: Record<string, Set<Method<AG>>>;
capacity: number;
occupy: number;
save(methodInstance: Method<AG>): void;
match<M extends boolean = true>(
matcher: MethodFilter<AG>,
matchAll?: M
): M extends true ? Method<AG>[] : Method<AG> | undefined;
}

alova.Get()

Creates a method instance for a GET request.

  • Type
interface Alova {
Get(url: string, config?: AlovaMethodCreateConfig): Method;
}
  • Parameter
  1. url: request URL
  2. config: configuration options
Parameter nameTypeDescription
headersobjectrequest headers, see details
paramsobjectRequest parameters, see details
namestringMethod object name. In updateState, invalidateCache, setCache, and fetch function, you can get the corresponding method instance by name or wildcard
timeoutnumberRequest timeout, see details
cacheForcacheForConfigResponse cache time, see details
hitSourcestringHit the source method instance. When the source method instance request succeeds, the cache of the current method instance will be invalidated. see details
transformfunctionTransform response data. see details
shareRequestbooleanRequest-level shared request switch. see details
metaanymethod metadata. see details

In addition to the configurable parameters above, other parameters supported by the request adapter are also supported.

  • Return

method instance

  • Example
const getUsers = alovaInstance.Get('/users', {
params: {
id: 1
}
// ...
});

[3.3.0+] alova.Request()

Create a method instance.

  • Type
interface Alova {
Request(config?: AlovaMethodCreateConfig): Method;
}
  • Parameter
Parameter nameTypeDescription
urlstringrequest URL
methodstringrequest method, such as GET/POST, default is GET
headersobjectrequest headers, see details
paramsobjectrequest parameters, see details
namestringmethod object name, in updateState, invalidateCache, setCache, and fetch function can get the corresponding method instance by name or wildcard
timeoutnumberrequest timeout, see details
cacheForcacheForConfigresponse cache time, see details
hitSourcestringhit source method instance, when the source method instance request is successful, the cache of the current method instance will be invalidated, see details
transformfunctionConvert response data, see details
shareRequestbooleanRequest-level shared request switch, see details
metaanymethod metadata, see details

In addition to the configurable parameters above, other parameters supported by the request adapter are also supported.

  • Return

method instance

  • Example
const getUsers = alovaInstance.Request({
url: '/users',
method: 'GET',
params: {
id: 1
}
// ...
});

alova.Post()

Create a method instance for a POST request.

  • Type
interface Alova {
Post(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameter
  1. url: request URL

  2. data: request body

  3. config: configuration options, parameter type is the same as alova.Get

  • Return

method instance

  • Example
const postUsers = alovaInstance.Post(
'/createUser',
{
name: 'alova',
age: 18,
gender: 'male'
},
{
// Configuration parameters...
}
);

alova.Delete()

Create a method instance for a DELETE request.

  • Type
interface Alova {
Delete(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameter
  1. url: request URL

  2. data: request body

  3. config: configuration options, parameter type is the same as alova.Get

  • Return

method instance

  • Example
const deleteUsers = alovaInstance.Delete(
'/deleteUser',
{
id: 1
},
{
// Configuration parameters...
}
);

alova.Put()

Create a method instance for a PUT request.

  • Type
interface Alova {
Put(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameter
  1. url: request URL

  2. data: request body

  3. config: configuration options, parameter type is the same as alova.Get

  • Return

method instance

  • Example
const putUsers = alovaInstance.Put(
'/updateUser',
{
id: 1,
name: 'alova'
},
{
// Configuration parameters...
}
);

alova.Head()

Create a method instance for HEAD request.

  • Type
interface Alova {
Head(url: string, config?: AlovaMethodCreateConfig): Method;
}
  • Parameter
  1. url: request URL
  2. config: configuration options, parameter type is the same as alova.Get
  • Return

method instance

alova.Patch()

Create a method instance for the PATCH request.

  • Type
interface Alova {
Patch(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameter
  1. url: request URL

  2. data: request body

  3. config: configuration options, parameter type is the same as alova.Get

  • Return

method instance

alova.Options()

Create a method instance for OPTIONS request.

  • Type
interface Alova {
Options(url: string, config?: AlovaMethodCreateConfig): Method;
}
  • Parameter
  1. url: request URL
  2. config: configuration options, parameter type is the same as alova.Get
  • Return

method instance