vk-async-jsonp

Send crossorigin requests to VK API asynchronously

Usage no npm install needed!

<script type="module">
  import vkAsyncJsonp from 'https://cdn.skypack.dev/vk-async-jsonp';
</script>

README

vk-async-jsonp npm

Send crossorigin requests to VK API asynchronously

The problem

If you try to call VKontakte's API methods directly from your frontend application, you'll get a CORS error. To solve this you can use JSONP (docs are only in Russian version) but it calls a top-level callback. That means you can't use it asynchronously.

How it works?

First of, it creates a temporary function inside window object and injects JSONP script to the DOM. Then it returns a Promise which resolves once that temporary function is called. After all, it either rejects or resolves with requested data.

Installation

With Yarn:

yarn add vk-async-jsonp

Or with NPM:

npm i vk-async-jsonp

Usage

This is simple example of using this library:

// With ESmodules
import jsonp from 'vk-async-jsonp';
// With CommonJS modules
// const jsonp = require('vk-async-jsonp').default;

const response = await jsonp(
  'users.get', // API method
  {
    user_ids: '487774386',
    fields: 'verified',

    access_token: 'YOUR TOKEN',
  }, // Parameters
  //'5.107', // Optional version
);

if (response.verified) alert('Impossible!')
else alert('All ok!')

License

MIT, learn more