UNPKG

node-vk-sdk

Version:

VK API SDK for Node.js

149 lines 5.85 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseVKApi = void 0; const VKApiError_1 = require("./VKApiError"); const time_1 = require("./time"); const net_1 = require("./net"); const DEFAULT_REQUESTS_PER_SECOND = 3; const TIMEOUT = 5000; // 5 seconds const API_BASE_URL = 'https://api.vk.com/method/'; const API_VERSION = '5.126'; class BaseVKApi { constructor(options) { this.logger = options.logger; this.token = options.token; this.timeout = options.timeout || TIMEOUT; this.lang = options.lang; this.testMode = options.testMode; if (options.useQueue) this.queue = time_1.createQueue(options.requestsPerSecond || DEFAULT_REQUESTS_PER_SECOND); } call(method, params) { return __awaiter(this, void 0, void 0, function* () { params = this.filterParams(params); if (!params['lang'] && !!this.lang) params['lang'] = this.lang; if (!params['testMode'] && !!this.testMode) params['testMode'] = this.testMode; params['v'] = API_VERSION; params['access_token'] = params['access_token'] || this.token; if (!params['access_token']) delete params['access_token']; let doRequest = () => __awaiter(this, void 0, void 0, function* () { let { body, response, err } = yield net_1.postRequest(API_BASE_URL + method, params, this.timeout); return yield this.handleResponse(method, params, response, body, err); }); if (this.queue) { return yield this.queue.run(() => __awaiter(this, void 0, void 0, function* () { return doRequest(); })); } else { return doRequest(); } }); } /** * Makes api call and if there was * server-side error or requests limit was reached * repeats the call after some timeout */ callWithRetry(method, params, timeout = 300) { return __awaiter(this, void 0, void 0, function* () { return this.doCallWithRetry(method, params, timeout); }); } /** * Stops calls queue if have one */ stopQueue() { if (this.queue) this.queue.stop(); } doCallWithRetry(method, params, timeout = 300, maxRetryCount = 7, retryCounter = 0) { return __awaiter(this, void 0, void 0, function* () { try { return yield this.call(method, params); } catch (e) { retryCounter++; if (retryCounter === maxRetryCount) { throw e; } if (e instanceof VKApiError_1.default) { // // 6 - too many requests per second // 10 - internal server error // if (e.errorCode == 6 || e.errorCode == 10) { yield time_1.delay(timeout); return yield this.doCallWithRetry(method, params, timeout, maxRetryCount, retryCounter); } throw e; } else { // // Network error // yield time_1.delay(timeout); return yield this.doCallWithRetry(method, params, timeout, maxRetryCount, retryCounter); } } }); } handleResponse(method, params, response, body, err) { return __awaiter(this, void 0, void 0, function* () { if (!err && response.statusCode == 200 && !body.error) { if (body.execute_errors) { return body; } return body.response; } if (body && body.error) { if (this.logger) { this.logger.warn('VK Api error\n', { response: JSON.stringify(body), error: VKApiError_1.default.deserialize(body.error), method, params }); } throw VKApiError_1.default.deserialize(body.error); } if (err) { if (this.logger) { this.logger.error('VK Api:\n', { 'Networking error:': err, method, params }); } throw err; } if (this.logger) { this.logger.error('VK Api:\n', { 'api request error: Body:': body, 'Error:': err }); } throw err; }); } filterParams(params) { for (let paramName in params) { if (params[paramName] == undefined) { delete params[paramName]; } } return params; } } exports.BaseVKApi = BaseVKApi; //# sourceMappingURL=BaseVKApi.js.map