method instance
A method instance corresponds to a request information description, which has the URL, request headers, request parameters of a request, as well as request behavior parameters such as response data processing and cache data processing. Through method instances, you can feel a unified usage experience in any js environment, and it can run normally with very few changes. In addition, method instances put request parameters and request behavior parameters together, making it easier for APIs management instead of spreading it across multiple code files.
PromiseLike attribute
After [v2.16.0]
, the method instance is a PromiseLike instance, which has then/catch/finally
functions, so you can use it as follows:
// Call the then function of method
method.then(res => {
console.log(res);
});
// catch exception
method.catch(e => {
console.log(e);
});
//Request completion call
method.finally(() => {
console.log('Request completed');
});
In addition, requests can also be sent through the await method
.
new Method()
Create a custom method instance.
- type
interface MethodConstructor {
new (
type: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH',
context: Alova,
url: string,
config?: AlovaMethodCreateConfig,
data?: Arg | string | FormData | Blob | ArrayBuffer | URLSearchParams | ReadableStream
): Method;
readonly prototype: Method;
}
- Parameters
type
: request typecontext
: alova instanceurl
: request urlconfig
: Configuration parameters, the type is the same as config parameter type of alova.Getdata
: request body data
- Example
import { Method } from 'alova';
import { alovaInstance } from './api';
const method = new Method('GET', alovaInstance, '/api/users', {
params: {
ID: 1
}
});
getMethodKey()
Get the key value of method. This key value is used as alova internal cache key.
- type
function getMethodKey(method: Method): string;
- Parameters
method
: method instance
- return
The key value of the method instance passed in.
- Example
import { getMethodKey } from 'alova';
const method = alova.Get('/api/users');
const methodKey = getMethodKey(method);
matchSnapshotMethod()
Obtain the requested method instance snapshot using the matching method of method instance matcher and return the matching result.
- type
type MethodFilter =
| string
| RegExp
| {
name?: string | RegExp;
filter?: MethodFilterHandler;
alova?: Alova;
};
function matchSnapshotMethod(
matcher: MethodFilter,
matchAll?: boolean
): Method[] | Method | undefined;
- Parameters
matcher
: method instance matchermatchAll
: Whether to match all, the default is true
- return
Returns an array of method instances when matchAll
is true, otherwise returns a method instance or undefined
- Example
import { matchSnapshotMethod } from 'alova';
await alova.Get('/api/users');
const snapshotMethod = matchSnapshotMethod({
name: 'user',
filter(method, i, methodArray) {
return method.url.includes('users');
},
alova: alova
});
method.headers
Request header.
- type
interface Method {
headers?: any;
}
method.baseURL
The base path of the request, inherited from alova instance.
- type
interface Method {
baseURL: string;
}
method.url
Create the url of the method instance.
- type
interface Method {
url: string;
}
method.type
Request type.
- type:
interface Method {
type: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH';
}
method.data
Request body.
- type
interface Method {
data?: any;
}
method.context
Create an alova instance of the current method.
- type
interface Method {
context: Alova;
}
method.hitSource
Hitting the source method instance, when the source method instance request succeeds, the cache of the current method instance will be invalidated. As an automatic invalidation function, you only need to set the hit source instead of manually calling invalidateCache
to invalidate the cache. In addition, this function is more concise and effective than the invalidateCache
method in complex invalidation relationships. The field value can be set to the name of the method instance, other method instances, name regular matching, or their array.
- type
interface Method {
hitSource?: Method | string | RegExp | (Method | string | RegExp)[];
}
method.meta
The metadata of method is used to record request feature information, View details.
- type
interface Method {
meta?: any;
}