UNPKG

nodevk-ts

Version:

Simple Node.js module that allows you to interact with the VKontakte API.

53 lines (52 loc) 1.8 kB
import { getDefaultConfig } from './ConfigSession.js'; import VKAPIException from "../VKAPIException.js"; import request from "../request.js"; import MessagesAPI from "../API/messages.js"; import PhotosAPI from "../API/photos.js"; import UsersAPI from '../API/users.js'; import GroupsAPI from '../API/groups.js'; export default class Session { constructor(config) { this.config = getDefaultConfig(); this.messages = new MessagesAPI(this); this.photos = new PhotosAPI(this); this.users = new UsersAPI(this); this.groups = new GroupsAPI(this); if (config) for (let key in config) this.config[key] = config[key]; } async request(url, params) { const data = {}; for (const field in params) { const value = params[field]; if (Array.isArray(value)) { data[field] = value.join(","); } else if (typeof value == "object") { data[field] = JSON.stringify(value); } else if (typeof value !== "string") { data[field] = value.toString(); } else { data[field] = value; } } return JSON.parse((await request({ url: url, data: data, encoding: "utf-8" }))); } async invokeMethod(method, params) { let url = `${this.config.url}${method}`; params.v = this.config.version; let result = await this.request(url, params); if (result.error) throw new VKAPIException(result.error); if (this.config.debug) console.log("Get answer:", result); return result; } }