alova instance
createAlova()
Create an alova instance.
- type
function createAlova(options?: AlovaOptions): Alova;
- Parameters
- config: configuration parameters
Parameter name | Type | Description |
---|---|---|
baseURL | string | Base path, empty by default, View details |
statesHook | object | State management hook, optional, View details |
requestAdapter | object | Request adapter, required, View details |
timeout | number | Timeout time, no timeout by default, View details |
localCache | object | Local cache configuration, default GET has 5000ms cache, View details |
storageAdapter | object | Local storage adapter, default is localStorage , View details |
beforeRequest | function | Before request hook, View details |
responded | object | function | Request response hook, View details |
shareRequest | boolean | Share request, View details |
errorLogger | boolean| null | function | Error log, View details |
cacheLogger | boolean | null | function | Cache log, View details |
- return
Alova instance
- Example
import { createAlova } from 'alova';
const alova = createAlova({
baseURL: 'https://example.com',
statesHook: VueHook,
requestAdapter: GlobalFetch(),
timeout: 3000
// ...
});
alova.id
The alova instance id is used to distinguish different alova instances. It can accurately match the method instance of the specified alova in method instance matcher.
- Type: string
alova.options
When creating an alova instance through createAlova
, the default configuration is merged with the passed-in configuration object.
- type
interface AlovaOptions {
statesHook: StatesHook;
requestAdapter: AlovaRequestAdapter;
baseURL?: string;
timeout?: number;
localCache?: GlobalLocalCacheConfig;
storageAdapter?: AlovaStorageAdapter;
beforeRequest?: Function;
responded?: Function | ResponsedHandlerRecord;
shareRequest?: boolean;
errorLogger?: boolean | null | Function;
cacheLogger?: boolean | null | Function;
}
alova.storage
The storage adapter instance corresponding to the alova instance defaults to AlovaGlobalStorage
, which uses localStorage
.
- type
interface AlovaStorageAdapter {
get(key: string): any;
set(key: string, value: any): void;
remove(key: string): void;
}
alova.Get()
Create a method instance for the GET request.
- type
interface Alova {
Get(url: string, config?: AlovaMethodCreateConfig): Method;
}
- Parameters
- url: request address
- config: configuration parameters
Parameter name | Type | Description |
---|---|---|
headers | object | Request headers, View details |
params | object | Request parameters, View details |
name | string | method object name, in updateState, invalidateCache, setCache, and fetch function, you can obtain the corresponding method instance by name or wildcard |
timeout | number | Request timeout, View details |
localCache | LocalCacheConfig | Response cache time, View details |
hitSource | string | Hit the source method instance. When the source method instance request is successful, the cache of the current method instance will be invalidated. View details |
enableDownload | boolean | Enable download progress information, View details |
enableUpload | boolean | Enable upload progress information, View details |
transformData | function | Transform response data, View details |
shareRequest | boolean | Request-level sharing request switch, 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 the POST request.
- type
interface Alova {
Post(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
- Parameters
- url: request address
- data: request body
- config: configuration parameters, the 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 the DELETE request.
- type
interface Alova {
Delete(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
- Parameters
- url: request address
- data: request body
- config: configuration parameters, the 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 the PUT request.
- type
interface Alova {
Put(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
- Parameters
- url: request address
- data: request body
- config: configuration parameters, the 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 of the HEAD request.
- type
interface Alova {
Head(url: string, config?: AlovaMethodCreateConfig): Method;
}
- Parameters
- url: request address
- config: configuration parameters, the 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;
}
- Parameters
- url: request address
- data: request body
- config: configuration parameters, the parameter type is the same as alova.Get
- return
method instance
alova.Options()
Create a method instance of the OPTIONS request.
- type
interface Alova {
Options(url: string, config?: AlovaMethodCreateConfig): Method;
}
- Parameters
- url: request address
- config: configuration parameters, the parameter type is the same as alova.Get
- return
method instance