Alova Instance
createAlova()
Create an Alova instance.
- Type
function createAlova(options?: AlovaOptions): Alova;
- Parameter
- config: Configuration parameters
Parameter name | Type | Description |
---|---|---|
requestAdapter | object | Request adapter, required, View details |
id | string | number | Alova instance id, optional, View details |
baseURL | string | Base path, optional, default is empty, View details |
statesHook | object | State management hook, optional, see details |
timeout | number | Timeout, default is no timeout, see details |
cacheFor | object | Local cache configuration, default GET has 5000ms cache, see details |
l1Cache | object | Level1 cache adapter see details |
l2Cache | object | Level2 cache adapter, see details |
beforeRequest | function | Before request hook, see details |
responded | object | function | Request response hook, see details |
shareRequest | boolean | Share request, see details |
cacheLogger | boolean | null | function | Cache log, see details |
snapshots | number | method 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
- url: request address
- config: configuration parameters
Parameter name | Type | Description |
---|---|---|
headers | object | Request header, see details |
params | object | Request parameters, see details |
name | string | Method object name. In updateState, invalidateCache, setCache, and fetch function, you can get the corresponding method instance by name or wildcard |
timeout | number | Request timeout, see details |
cacheFor | cacheForConfig | Response cache time, see details |
hitSource | string | Hit the source method instance. When the source method instance request succeeds, the cache of the current method instance will be invalidated. View details |
transform | function | Transform response data. View details |
shareRequest | boolean | Request-level shared request switch. View details |
meta | any | method 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
-
url: request address
-
data: request body
-
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
-
url: request address
-
data: request body
-
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
-
url: request address
-
data: request body
-
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
- url: request address
- 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
-
url: request address
-
data: request body
-
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
- url: request address
- config: configuration parameters, parameter type is the same as alova.Get
- Return
method instance