poll-api
v2.0.6
Published
poll api
Downloads
2
Maintainers
Readme
English | 中文
poll-api
Poll API is a library that helps developers manage Api polling requests
Function
- Asynchronous callback function
- Support immediate polling or specified interval time or number of polling request API
Install & Usage
- Install the poll-api
# Using npm
npm install poll-api -S
# Using yarn
yarn add poll-api -S
- Import poll-api and use
import PollApiManager from 'poll-api'
// Register global request API configuration, support chain calling
PollApiManager.registerRequestConfig('test', {
url: 'https://api.jisuapi.com/area/province'
}).registerRequestConfig('test1', {
url: 'https://api.jisuapi.com/area/province'
})
// Create a polling instance
const pollApiInstance = PollApiManager.createInstance()
// Enable a polling API request
const requestTask = $pollApi.poll('test', {
fn
})
PollApiManager instance method
registerRequestConfig
Parameter information for global registration polling requests
Parameters
requestType
(RequestType) Key for global configuration request configuration informationrequestConfig
(RequestConfig) Configuration information for API requests
createInstance
Create a pollApi instance
Parameters
None
deletePollApiInstance
Delete the specified pollApi instance and clear all queues and request tasks under the current instance
Parameters
instance
(PollApi) PollApi instance
clear
Clear all pollApi instances and all queues and request tasks under all instances
Parameters
None
PollApi instance method
poll
Create an API polling instance and start polling. If the request configuration information for the current requestType is not registered globally, it will be automatically registered
Parameters
requestType
(RequestType) Key for global configuration request configuration informationdata
(PollApiOnData) API requested datafn
(RequestConfigFn) Obtain the configuration information requested by the API. If not transmitted, the configuration information of the corresponding key will be configured globally by defaultpollConfigFn
(PollConfigFn) Obtain polling configuration information, if not transmitted, default configuration will be used
deleteRequestTask
Close the specified request task
Parameters
requestTask
(RequestTask) RequestTask Instance
deleteRequestQueue
Close the specified task queue and all request tasks under the current queue
Parameters
requestType
(RequestType) Key for global configuration request configuration information
clear
Close all queues and request tasks under the current pollApi instance, but do not clear the current pollApi instance
Parameters
None
RequestTask instance method
updateData
update request data
stopPollApi
Stop polling for the current request task
Parameters
None
bootstrap
Start Polling Request Current Request Task
Parameters
None
delete
Clear current request task
Parameters
None
Type declaration
declare type RequestType = string | symbol
declare type EventType = string | symbol
declare interface Fn<T = any, R = T> {
(...arg: T[]): R
}
declare type Nullable<T> = T | null
declare interface EventOptions {}
declare interface PollConfig {
pollCount: number // If the number of rounds is -1, it is considered to be a continuous round robin
pollInterval: number // Polling interval unit: seconds
immediate: boolean // Do you want to trigger polling immediately
}
declare interface PollApiOnData {
eventType?: EventType
fn: Fn
data?: Nullable<any>
}
interface CreateRequestTaskParams {
data: PollApiOnData
requestConfig: RequestConfig
pollConfig: PollConfig
}
import type { AxiosRequestConfig, AxiosInstance } from 'axios'
interface RequestConfig extends AxiosRequestConfig {
resDataTransform?: Fn,
axiosInstance: AxiosInstance
}
export type RequestConfigFn = (
requestConfig: Nullable<RequestConfig>
) => Nullable<RequestConfig>
export type PollConfigFn = (pollConfig: PollConfig) => PollConfig