alova-server-usage

Alova Server-Side Usage
For client-side usage, see
alova-clientskill. For alova openapi usage, seealova-openapiskill.
How to Use This Skill
- This file — Quick-reference index: what each API does and when to use it. Read this first.
- Official docs (fetch on demand) — For full options, edge cases, or unfamiliar APIs, fetch the URL listed in each section to get the latest accurate information.
Always fetch the official doc before answering questions about specific API options or behaviors — alova is actively developed and live docs are more reliable than training data.
Installation & Setup
See references/SETUP.md for:
- Installation
- Creating Alova instance
- Request adapters
- Global request sharing and timeout
- cache logger
- limit number of method snapshots
Create Method Instance
alova provides a total of 7 request types.
| Instance creation function | Parameters |
|---|---|
| GET | alovaInstance.Get(url[, config]) |
| POST | alovaInstance.Post(url[, data[, config]]) |
| PUT | alovaInstance.Put(url[, data[, config]]) |
| DELETE | alovaInstance.Delete(url[, data[, config]]) |
| HEAD | alovaInstance.Head(url[, config]) |
| OPTIONS | alovaInstance.Options(url[, config]) |
| PATCH | alovaInstance.Patch(url[, data[, config]]) |
Parameter Description:
urlis the request path;datais the request body data;configis the request configuration object, which includes configurations such as request headers, params parameters, request behavior parameters, etc.;
The above functions calling are not sending request, but creates a method instance, which is a PromiseLike instance. You can use then, catch, finally methods or await to send request just like a Promise object, or call send to explicitly send the request.
alovaInstance
.Get('/api/user')
.then((response) => {
// ...
})
.catch((error) => {
// ...
})
.finally(() => {
// ...
});
// or
try {
await alovaInstance.Get('/api/user');
} catch (error) {
// ...
} finally {
// ...
}
// or
alovaInstance.Get('/api/user').send();
See Method Documentation if need to know full method instance API.
Method Metadata
Add additional information to specific method instances to facilitate their identification or additional information in global interceptor such as different response returning, global toast avoiding. please set method metadata. See -> Method Metadata.
Cache Strategy
Alova has L1 (memory) and L2 (persistent/restore) layers, plus automatic request sharing (dedup).
Set cache globally and scoped
- Fast in-page access, resets on refresh, Survive page refresh / offline-first, disable cache -> See Cache mode.
- Auto-invalidate after a mutation,
hitSourceon GET +nameon mutation Method -> See Auto Invalidate Cache. - Manual invalidate -> See Manual invalidate.
- Set & Query cache -> See Operate Cache.
Key rule: prefer hitSource auto-invalidation — it requires zero imperative code and decouples components.
Server Hooks
import from
alova/server.
Server hooks wrap a Method instance and return a new hooked Method. They are composable and all support cluster mode when using Redis or file storage adapter.
| Scenario | Hook | Key capability | Docs |
|---|---|---|---|
| Retry failed requests with backoff | retry |
Configurable retry attempts with exponential backoff | Docs |
| Distributed captcha sending | sendCaptcha |
Built-in rate limiting for captcha | Docs |
| Rate-limit outgoing requests | RateLimiter |
Token bucket algorithm, cluster support via Redis | Docs |
| Only one process initiates at a time (cluster) | atomize |
Distributed lock for token refresh, resource init | Docs |
Server hooks can be layered one on top of another to combine their behaviors:
// Layer by layer composition: innermost wraps first
// Step 1: create base method
const baseMethod = alovaInstance.Post('/api/order', data);
// Step 2: wrap with rate limiter (outer layer)
const limitedMethod = limiter.limit(baseMethod);
// Step 3: wrap with retry (outermost layer)
const retryableMethod = retry(limitedMethod, { retry: 3 });
// Execute: rate limit check → retry on failure → send request
const result = await retryableMethod();
// Or in one line:
const result = await retry(limiter.limit(alovaInstance.Post('/api/order', data)), {
retry: 3,
}).send();
Distributed Caching
| Scenario | Storage adapter |
|---|---|
| Single-process | Default in-memory |
| Multi-process cluster | @alova/storage-redis |
| Single-machine cluster | @alova/storage-file |
Mock Request
Setup mock data for specific requests. See Mock Request.
Best Practices
- Provide a folder that uniformly stores request functions, to keep your code organized.
- Create multiple alova instances for different domains, APIs, or environments.
- Build BFF layer, API gateway, 3rd-party token auto management with alova. See references/BFF_API_GATEWAY.md.
Common Pitfalls
| Pitfall | Fix |
|---|---|
RateLimiter state not shared across workers |
Add a Redis storage adapter to RateLimiter options |
atomize not actually atomic in cluster |
Requires a shared storage adapter (Redis or File) to coordinate processes |
TypeScript
Annotate the response shape on the Method instance — hooks infer from it automatically:
const getUser = (id: number) => alovaInstance.Get<User>(`/users/${id}`);
// or need to transform data.
const getUser = (id: number) =>
alovaInstance.Get(`/users/${id}`, {
transform(user: User) {
return {
...user,
name: user.lastName + ' ' + user.firstName,
};
},
});
const { data } = useRequest(getUser(1)); // data: Ref<User>
Custom Adapter
If all preset adapters not meet your needs, custom your own adapter.
Custom Method Key
Change cache, request sharing and state updating matching strategy by setting key. See Custom Method Key.