Skip to main content
Version: v3

Alova Instance

createAlova()

Create an Alova instance.

  • Type
function createAlova(options?: AlovaOptions): Alova;
  • Parameter
  1. config: Configuration parameters
Parameter nameTypeDescription
requestAdapterobjectRequest adapter, required, View details
idstring | numberAlova instance id, optional, View details
baseURLstringBase path, optional, default is empty, View 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
snapshotsnumbermethod The number of snapshots is limited, the default is 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

alova instance id, used to distinguish different alova instances, can be used to accurately match the method instance of the specified alova in the method matcher.

  • Type: string

alova.options

When creating an alova instance through createAlova, the object after the default configuration is merged with the passed in configuration object.

  • 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 level1 cache adapter corresponding to the alova instance, the default is memory cache.

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

alova.l2Cache

The level2 cache adapter corresponding to the alova instance. The default value is localStorage in the client, and 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 of the current instance.

  • Type ts 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; } ## al ova.Get() creates a method instance for a GET request.

  • Type

interface Alova {
Get(url: string, config?: AlovaMethodCreateConfig): Method;
}
  • Parameter
  1. url: request address
  2. config: configuration parameters
Parameter nameTypeDescription
headersobjectRequest header, 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. View details
transformfunctionTransform response data. View details
shareRequestbooleanRequest-level shared request switch. View details
metaanymethod metadata. View 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
}
// ...
});

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 address

  2. data: request body

  3. config: configuration parameters, 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 address

  2. data: request body

  3. config: configuration parameters, 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 address

  2. data: request body

  3. config: configuration parameters, 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 address
  2. config: configuration parameters, 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 address

  2. data: request body

  3. config: configuration parameters, 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 address
  2. config: configuration parameters, parameter type is the same as alova.Get
  • Return

method instance