UNPKG

vk-io

Version:

Modern VK API SDK for Node.js

164 lines (163 loc) 4.07 kB
/// <reference types="node" /> import { Agent } from 'https'; import { APIMethods } from './schemas/methods'; import { APIRequest } from './request'; import { ExecuteError } from '../errors'; import { CallbackService } from '../utils/callback-service'; export interface IAPIOptions { /** * Access token */ token: string; /** * The return data language */ language?: 'ru' | 'uk' | 'be' | 'en' | 'es' | 'fi' | 'de' | 'it'; /** * HTTPS agent * * @see https://nodejs.org/api/https.html#https_class_https_agent */ agent: Agent; /** * Determines how requests will be collected * - `sequential` - in order * - `parallel` - all requests are sent through execute * - `parallel_selected` - only the specified methods in `apiExecuteMethods` * are collected in `execute`, other methods as in `sequential` mode * * @defaultValue `sequential` */ apiMode: 'sequential' | 'parallel' | 'parallel_selected'; /** * Determines how requests will be sent * * - `sequential` - through the interval * - `burst` - in parallel, * the maximum number of requests (attention, may cause an EAI_AGAIN error) * * @defaultValue `sequential` */ apiRequestMode: 'sequential' | 'burst'; /** * Time to wait before re-querying * * @defaultValue `3000` */ apiWait: number; /** * Requests per second * * @defaultValue `3` */ apiLimit: number; /** * VK API version * * @see https://vk.com/dev/versions */ apiVersion: string; /** * Base API URL * * @defaultValue `https://api.vk.com/method` */ apiBaseUrl: string; /** * The number of retries at calling * * @defaultValue `3` */ apiRetryLimit: number; /** * Wait time for one request * * @defaultValue `10000` */ apiTimeout: number; /** * Headers sent to the API * * @defaultValue `{ User-Agent': 'vk-io/${version} (+https://github.com/negezor/vk-io)' }` */ apiHeaders: Record<string, string>; /** * Number of requests per execute * * @defaultValue `25` */ apiExecuteCount: number; /** * Methods for call execute (apiMode=parallel_selected) * * @defaultValue `['messages.send']` */ apiExecuteMethods: string[]; /** * Methods that are not supported in execute (apiMode=parallel & apiMode=parallel_selected) * * Basically it's upload methods * @defaultValue ```ts * [ * 'photos.save', * 'photos.saveWallPhoto', * 'photos.saveOwnerPhoto', * 'photos.saveMessagesPhoto', * 'messages.setChatPhoto', * 'photos.saveMarketPhoto', * 'photos.saveMarketAlbumPhoto', * 'audio.save', * 'docs.save', * 'photos.saveOwnerCoverPhoto', * 'stories.save', * 'polls.savePhoto' * ] * ``` */ apiExecuteUnsupportedMethods: string[]; callbackService?: CallbackService; } export interface IExecuteResponse<T> { response: T; errors: ExecuteError[]; } /** * Working with API methods */ declare class API { options: IAPIOptions; private worker; /** * Constructor */ constructor(options: Partial<IAPIOptions> & { token: string; }); /** * Returns custom tag */ get [Symbol.toStringTag](): string; /** * Call execute method */ execute<T = any>(params: Record<string, unknown> & { code: string; }): Promise<IExecuteResponse<T>>; /** * Call execute procedure */ procedure<T = any>(name: string, params: object): Promise<IExecuteResponse<T>>; /** * Call raw method */ call<T = any>(method: string, params: object): Promise<T>; /** * Adds request for queue */ callWithRequest<T = any>(request: APIRequest): Promise<T>; private getWorker; updateWorker(): void; } interface API extends APIMethods { } export { API };