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;
}
method.config
Configuration information when creating a method through alova.Get/alova.Post
and other methods, View details.
- type
interface Method {
config: AlovaMethodCreateConfig;
}
method.fromCache
Is the response of current request from cache.
- type
interface Method {
fromCache: boolean;
}
method.send()
Use this method instance to send the request directly. If you send the request after [v2.16.0]
, you can omit calling this method.
- type
interface Method {
send(forceRequest?: boolean): Promise<Response>;
}
- Parameters
forceRequest
: whether to force the request, the default is false
- return
A Promise instance with response data.
- Example
const method = alova.Get('/api/users');
const response = await method.send();
method.abort()
Abort the current request.
- type
interface Method {
abort(): void;
}
- Example
const method = alova.Get('/api/users');
method.abort();
method.then()
After [v2.16.0]
, the method instance is a PromiseLike instance. You can directly call this method or await method
to send a request and obtain the response data.
- type
interface Method {
then(
onFulfilled?: (value: Response) => any,
onRejected?: (reason: any) => any
): Promise<Response>;
}
- Parameters
onFulfilled
: callback function when the request is successfulonRejected
: callback function when the request fails
- return
A Promise instance with response data.
- Example
const method = alova.Get('/api/users');
const response = await method;
method.catch()
After [v2.16.0]
, the method instance is a PromiseLike instance. This method can be called directly to send requests and catch errors.
- type
interface Method {
catch<TResult = never>(
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null
): Promise<Response | TResult>;
}
- Parameters
onrejected
: callback function when request error
- return
Promise instance.
- Example
const method = alova.Get('/api/users');
const response = await method.catch(error => {
console.error('Request error');
});
method.finally()
After [v2.16.0]
, the method instance is a PromiseLike instance. This method can be called directly to send the request and handle the response completion.
- type
interface Method {
finally(onfinally?: (() => void) | undefined | null): Promise<Response>;
}
- return
Promise instance.
- Example
const method = alova.Get('/api/users');
const response = await method.finally(() => {
console.log('Request completed');
});
method.onDownload()
Bind the download event to obtain download progress information.
- type
interface Method {
onDownload(handler: ProgressHandler): () => void;
}
- Parameters
handler
download event callback function
- return
unbind function
- Example
const method = alova.Get('/api/download_file');
const offEvent = method.onDownload(event => {
console.log('File size:', event.total);
console.log('Downloaded:', event.loaded);
});
offEvent();
method.onUpload()
Bind the upload event to obtain upload progress information.
- type
interface Method {
onUpload(handler: ProgressHandler): () => void;
}
- Parameters
handler
upload event callback function
- return
unbind function
- Example
const method = alova.Get('/api/upload_file', formData);
const offEvent = method.onUpload(event => {
console.log('File size:', event.total);
console.log('Uploaded:', event.loaded);
});
offEvent();
method.setName()
Set the name of the method instance.
- type
interface Method {
setName(name: string | number): void;
}
- Parameters
name
: the name of the method instance
- return
none
- Example
const method = alova.Get('/api/users', {
name: 'user'
});
method.setName('newUser');