npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-vksdk

v5.3.3

Published

SDK for API of vk.com with Promise

Downloads

3

Readme

promise-vksdk

Small SDK for vk.com API.

This is form of vksdk, with Promise

Installation

npm install promise-vksdk

Example

// Setup
var VK = require('promise-vksdk');
var vk = new VK({
   'appId'     : 2807970,
   'appSecret' : 'L14ZKpgQPalJdumI6vFK',
   'language'  : 'ru'
});

/**
 * Request server methods
 */

// Setup server access token for server API methods
vk.on('serverTokenReady', function(_o) {
    // Here will be server access token
    vk.setToken(_o.access_token);
});

// Turn on requests with access tokens
vk.setSecureRequests(true);

// Request server API method
let vksdk = require('promise-vksdk');
const vk = new vksdk({
   'appId'     : 2807970,
   'appSecret' : 'L14ZKpgQPalJdumI6vFK',
   'lang'  : 'ru'
});

vk.request("users.get", {user_id:1}).then(o=>console.log(o));

/**
 * Request client methods
 */
// First you have to pass access_token from client side JS code
vk.setToken(access_token);

// Request 'users.get' method
vk.request("users.get", {user_id:1}).then(o=>console.log(o));

Setup

var VK = require('promise-vksdk');

var vk = new VK({
    'appId'     : [Your application ID here],
    'appSecret' : [Your application secret code here],
});

Required config options:

  • appSecret — application secret code (check your application settings on vk.com)
  • appId — vk.com application id

Available config options:

  • [bool] https — with this options all links in API answers will be with https protocol. Disabled by default.
  • [string] version — vk.com api verions. Default: 5.27
  • [string] language — Language code for api answers (for old deprecated API)
  • [bool] secure — enable api requests with tokens. Default false.

You can read and change some config options:

  • getVersion() — get current API version
  • setVersion(_v) — set current API version
  • getLanguage() — get current API language
  • setLanguage(_v) — set current API language
  • getHttps() — get https links usage for API answers
  • setHttps(_v) — set https links usage for API answers
  • getSecureRequests() — get token's usage for API requests
  • setSecureRequests(_v) — set token's usage for API requests

API requests

For vk.com API requests you have to use method request(_method, _requestParams, _response).

  • [string] _method — name of vk.com API method,
  • [mixed] _requestParams - object with values of params for api method. This param is not required. You also can pass empty object {}
  • [mixed] _response — special response handler (not required), function or event name. Request method gets data from API and returns result. There are 3 ways to get data from API

Callback

vk.setSecureRequests(false);
vk.request('users.get', {'user_id' : 1}, function(_o) {
console.log(_o);
});

Event

After success API call SDK emits the event named 'done:' + _method; So if you call method users.get, you have to wait event done:users.get

vk.setSecureRequests(false);
vk.request('users.get', {'user_id' : 1});
vk.on('done:users.get', function(_o) {
    console.log(_o);
});

Custom event

Result of request will be returned with your custom event

vk.setSecureRequests(false);
vk.request('users.get', {'user_id' : 1}, 'myCustomEvent');
vk.on('myCustomEvent', function(_o) {
    console.log(_o);
});

Server access token

For some api methods you need server access token

vk.requestServerToken();

// Waiting for special 'serverTokenReady' event
vk.on('serverTokenReady', function(_o) {
    // Here will be server access token
    console.log(_o);
});

You also can get token with callback or custom event #Errors:

vk.request("users.get", {user_id:1}).then(o=>console.log(o)).catch(err=>console.error(err));

HTTP errors

SDK emits 'http-error' event in case of http errors.

vk.on('http-error', function(_e) {
    console.log(_e);
});

JSON parsing errors

SDK emits 'parse-error' event in case of non-valid API answer.

vk.on('parse-error', function(_e) {
    console.log(_e);
});

SDK provides all methods from events.EventEmitter

Bonus

You also can request some api methods without any tokens, with app signature

vk.oldRequest('places.getCountryById', {'cids' : '1,2'}, function(_o) {
    console.log(_o);
});

But this way is deprecated and doesn't work for all api methods.

For developers

Your commits and pull requests are welcome. Please run the tests before

npm test

You have to provide tests for the new features.

Support

Thanks to

See also vk.com cities and counties DB