vkontakte-api
Advanced tools
Comparing version 2.4.2 to 2.5.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.5.0](https://github.com/wolframdeus/vk-api/compare/v2.4.2...v2.5.0) (2021-05-20) | ||
### Features | ||
* **sendrequest:** add an ability to format response ([7751602](https://github.com/wolframdeus/vk-api/commit/77516026f81791c3adb1febe78a48135ed5c7777)) | ||
### [2.4.2](https://github.com/wolframdeus/vk-api/compare/v2.4.1...v2.4.2) (2021-05-20) | ||
@@ -7,0 +14,0 @@ |
@@ -10,6 +10,7 @@ "use strict"; | ||
this.sendRequest = function (_a) { | ||
var method = _a.method, params = _a.params; | ||
var method = _a.method, params = _a.params, format = _a.format; | ||
return sendRequest({ | ||
method: repoName + '.' + method, | ||
params: params, | ||
format: format, | ||
}); | ||
@@ -22,5 +23,6 @@ }; | ||
* @param prepare | ||
* @param format | ||
* @returns {TRepositoryMethod<P, R>} | ||
*/ | ||
Repository.prototype.r = function (method, prepare) { | ||
Repository.prototype.r = function (method, prepare, format) { | ||
var _this = this; | ||
@@ -30,2 +32,3 @@ return function (params) { return _this.sendRequest({ | ||
params: prepare ? prepare(params) : params, | ||
format: format, | ||
}); }; | ||
@@ -32,0 +35,0 @@ }; |
@@ -19,5 +19,6 @@ import { TRepositoryMethod, TSendRequest } from '../../types'; | ||
* @param prepare | ||
* @param format | ||
* @returns {TRepositoryMethod<P, R>} | ||
*/ | ||
protected r<P, R>(method: string, prepare?: (params: P) => any): TRepositoryMethod<P, R>; | ||
protected r<P, R, FR = R>(method: string, prepare?: (params: P) => any, format?: (response: R, params: P) => FR): TRepositoryMethod<P, R>; | ||
/** | ||
@@ -24,0 +25,0 @@ * Implements new method in repository mutating it. Returns current instance |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Repository=void 0;var Repository=function(){function t(t,e){this.sendRequest=function(r){var o=r.method,n=r.params;return e({method:t+"."+o,params:n})}}return t.prototype.r=function(t,e){var r=this;return function(o){return r.sendRequest({method:t,params:e?e(o):o})}},t.prototype.implement=function(t,e){return Object.defineProperty(this,t,{value:this.r(t,e)}),this},t}();exports.Repository=Repository; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Repository=void 0;var Repository=function(){function t(t,e){this.sendRequest=function(r){var o=r.method,n=r.params,s=r.format;return e({method:t+"."+o,params:n,format:s})}}return t.prototype.r=function(t,e,r){var o=this;return function(n){return o.sendRequest({method:t,params:e?e(n):n,format:r})}},t.prototype.implement=function(t,e){return Object.defineProperty(this,t,{value:this.r(t,e)}),this},t}();exports.Repository=Repository; |
import { Repository } from '../Repository'; | ||
import { TSendRequest } from '../../types'; | ||
import { IGetKeysParams, IGetSingleKeyParams, ISetParams, TGetKeysResult, TGetMultipleKeysResult, TGetParams, TGetSingleKeyResult } from './types'; | ||
import { IGetKeysParams, ISetParams, TGetKeysResult, TGetParams, TGetResult } from './types'; | ||
export declare class StorageRepository extends Repository { | ||
@@ -10,3 +10,3 @@ constructor(sendRequest: TSendRequest); | ||
*/ | ||
get: <P extends TGetParams>(params: P) => Promise<P extends IGetSingleKeyParams ? TGetSingleKeyResult : TGetMultipleKeysResult>; | ||
get: import("../../types").TRepositoryMethod<TGetParams, TGetResult>; | ||
/** | ||
@@ -13,0 +13,0 @@ * @see https://vk.com/dev/storage.getKeys |
@@ -14,7 +14,6 @@ /** | ||
export declare type TGetParams = IGetSingleKeyParams | IGetMultipleKeysParams; | ||
export declare type TGetSingleKeyResult = { | ||
export declare type TGetResult = { | ||
key: string; | ||
value: string; | ||
}; | ||
export declare type TGetMultipleKeysResult = TGetSingleKeyResult[]; | ||
}[]; | ||
/** | ||
@@ -21,0 +20,0 @@ * @see https://vk.com/dev/storage.getKeys |
@@ -76,3 +76,3 @@ import { AuthRepository, DatabaseRepository, DonutRepository, GiftsRepository, LikesRepository, StorageRepository, SpecialsRepository, MessagesRepository, NotificationsRepository, StatEventsRepository, StatsRepository, StreamingRepository, UsersRepository, UtilsRepository, WidgetsRepository, DownloadedGamesRepository, AccountRepository, StatusRepository, Repository } from '../repositories'; | ||
*/ | ||
export interface IRequestConfig<P extends {} = any> { | ||
export interface IRequestConfig<Params extends {} = any, Response = any, FormattedResponse = Response> { | ||
/** | ||
@@ -85,3 +85,9 @@ * API method name. | ||
*/ | ||
params: P & IRequestOptionalParams; | ||
params: Params & IRequestOptionalParams; | ||
/** | ||
* Formats response from server. | ||
* @param response | ||
* @param params | ||
*/ | ||
format?(response: Response, params: Params & IRequestOptionalParams): FormattedResponse; | ||
} | ||
@@ -88,0 +94,0 @@ /** |
@@ -86,10 +86,11 @@ "use strict"; | ||
_this.sendRequest = function (config) { return __awaiter(_this, void 0, void 0, function () { | ||
var method, params, formattedData, form, url, response, json; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var method, params, _a, format, fullParams, formattedParams, form, url, response, json; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
method = config.method, params = config.params; | ||
formattedData = utils_1.recursiveToSnakeCase(__assign({ v: this.v, accessToken: this.accessToken, lang: this.lang }, params)); | ||
method = config.method, params = config.params, _a = config.format, format = _a === void 0 ? function (response) { return response; } : _a; | ||
fullParams = __assign({ v: this.v, accessToken: this.accessToken, lang: this.lang }, params); | ||
formattedParams = utils_1.recursiveToSnakeCase(fullParams); | ||
form = Object | ||
.entries(formattedData) | ||
.entries(formattedParams) | ||
.filter(function (_a) { | ||
@@ -121,3 +122,3 @@ var value = _a[1]; | ||
if (data === null || data === void 0 ? void 0 : data.response) { | ||
return res(utils_1.recursiveToCamelCase(data.response)); | ||
return res(format(utils_1.recursiveToCamelCase(data.response), fullParams)); | ||
} | ||
@@ -141,9 +142,9 @@ rej(new VKError_1.VKError({ | ||
case 1: | ||
response = _a.sent(); | ||
response = _b.sent(); | ||
return [4 /*yield*/, response.json()]; | ||
case 2: | ||
json = _a.sent(); | ||
json = _b.sent(); | ||
// In case, we received response, convert it to camel case. | ||
if ('response' in json) { | ||
return [2 /*return*/, utils_1.recursiveToCamelCase(json.response)]; | ||
return [2 /*return*/, format(utils_1.recursiveToCamelCase(json.response), fullParams)]; | ||
} | ||
@@ -150,0 +151,0 @@ // Otherwise, throw an error. |
@@ -1,1 +0,1 @@ | ||
"use strict";var __extends=this&&this.__extends||function(){var e=function(t,r){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(t,r)};return function(t,r){function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}(),__assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(o,i){function s(e){try{a(n.next(e))}catch(e){i(e)}}function u(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(s,u)}a((n=n.apply(e,t||[])).next())})},__generator=this&&this.__generator||function(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=(o=s.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.VKAPI=void 0;var isomorphic_fetch_1=__importDefault(require("isomorphic-fetch")),VKError_1=require("../VKError"),utils_1=require("../utils"),Core_1=require("../Core"),Queue_1=require("../Queue"),VKAPI=function(e){function t(t){void 0===t&&(t={});var r=e.call(this)||this;r.accessToken=null,r.sendRequest=function(e){return __awaiter(r,void 0,void 0,function(){var t,r,n,o,i,s;return __generator(this,function(u){switch(u.label){case 0:return t=e.method,r=e.params,n=utils_1.recursiveToSnakeCase(__assign({v:this.v,accessToken:this.accessToken,lang:this.lang},r)),o=Object.entries(n).filter(function(e){return void 0!==e[1]}).map(function(e){var t=e[0],r=e[1],n="object"==typeof r?JSON.stringify(r):String(r);return encodeURIComponent(t)+"="+encodeURIComponent(n)}).join("&"),i=this.baseUrl+"/"+t,this.isBrowser?[2,new Promise(function(t,r){var n="__vkapicallback"+Math.random().toString().slice(2),s=document.createElement("script");s.src=i+"?"+o+"&callback="+n,window[n]=function(n){if(document.head.removeChild(s),null==n?void 0:n.response)return t(utils_1.recursiveToCamelCase(n.response));r(new VKError_1.VKError({errorInfo:utils_1.recursiveToCamelCase((null==n?void 0:n.error)||{}),config:e}))},document.head.appendChild(s)})]:[4,isomorphic_fetch_1.default(i,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},body:o})];case 1:return[4,u.sent().json()];case 2:if("response"in(s=u.sent()))return[2,utils_1.recursiveToCamelCase(s.response)];throw new VKError_1.VKError({errorInfo:utils_1.recursiveToCamelCase((null==s?void 0:s.error)||{}),config:e})}})})},r.addRequestToQueue=function(e){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return[4,this.queue.await()];case 1:return t.sent(),[2,this.sendRequest(e)]}})})},r.addRepository=function(t,n){return e.prototype.addRepository.call(r,t,n,r.addRequestToQueue)};var n=t.rps,o=void 0===n?3:n,i=t.accessToken,s=t.v,u=void 0===s?"5.110":s,a=t.lang,c=void 0===a?"ru":a,l=t.isBrowser,f=void 0!==l&&l,p=t.baseUrl,_=void 0===p?"https://api.vk.com/method":p;return r.accessToken=i||null,r.v=u,r.lang=c,r.isBrowser=f,r.queue=new Queue_1.Queue({timeout:Math.ceil(1e3/o)}),r.baseUrl=_.endsWith("/")?_.slice(0,_.length-1):_,r.init(r.addRequestToQueue),r}return __extends(t,e),t}(Core_1.Core);exports.VKAPI=VKAPI; | ||
"use strict";var __extends=this&&this.__extends||function(){var e=function(t,r){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(t,r)};return function(t,r){function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}(),__assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(o,i){function s(e){try{a(n.next(e))}catch(e){i(e)}}function u(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(s,u)}a((n=n.apply(e,t||[])).next())})},__generator=this&&this.__generator||function(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=(o=s.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.VKAPI=void 0;var isomorphic_fetch_1=__importDefault(require("isomorphic-fetch")),VKError_1=require("../VKError"),utils_1=require("../utils"),Core_1=require("../Core"),Queue_1=require("../Queue"),VKAPI=function(e){function t(t){void 0===t&&(t={});var r=e.call(this)||this;r.accessToken=null,r.sendRequest=function(e){return __awaiter(r,void 0,void 0,function(){var t,r,n,o,i,s,u,a,c;return __generator(this,function(l){switch(l.label){case 0:return t=e.method,r=e.params,n=e.format,o=void 0===n?function(e){return e}:n,i=__assign({v:this.v,accessToken:this.accessToken,lang:this.lang},r),s=utils_1.recursiveToSnakeCase(i),u=Object.entries(s).filter(function(e){return void 0!==e[1]}).map(function(e){var t=e[0],r=e[1],n="object"==typeof r?JSON.stringify(r):String(r);return encodeURIComponent(t)+"="+encodeURIComponent(n)}).join("&"),a=this.baseUrl+"/"+t,this.isBrowser?[2,new Promise(function(t,r){var n="__vkapicallback"+Math.random().toString().slice(2),s=document.createElement("script");s.src=a+"?"+u+"&callback="+n,window[n]=function(n){if(document.head.removeChild(s),null==n?void 0:n.response)return t(o(utils_1.recursiveToCamelCase(n.response),i));r(new VKError_1.VKError({errorInfo:utils_1.recursiveToCamelCase((null==n?void 0:n.error)||{}),config:e}))},document.head.appendChild(s)})]:[4,isomorphic_fetch_1.default(a,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},body:u})];case 1:return[4,l.sent().json()];case 2:if("response"in(c=l.sent()))return[2,o(utils_1.recursiveToCamelCase(c.response),i)];throw new VKError_1.VKError({errorInfo:utils_1.recursiveToCamelCase((null==c?void 0:c.error)||{}),config:e})}})})},r.addRequestToQueue=function(e){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return[4,this.queue.await()];case 1:return t.sent(),[2,this.sendRequest(e)]}})})},r.addRepository=function(t,n){return e.prototype.addRepository.call(r,t,n,r.addRequestToQueue)};var n=t.rps,o=void 0===n?3:n,i=t.accessToken,s=t.v,u=void 0===s?"5.110":s,a=t.lang,c=void 0===a?"ru":a,l=t.isBrowser,f=void 0!==l&&l,p=t.baseUrl,_=void 0===p?"https://api.vk.com/method":p;return r.accessToken=i||null,r.v=u,r.lang=c,r.isBrowser=f,r.queue=new Queue_1.Queue({timeout:Math.ceil(1e3/o)}),r.baseUrl=_.endsWith("/")?_.slice(0,_.length-1):_,r.init(r.addRequestToQueue),r}return __extends(t,e),t}(Core_1.Core);exports.VKAPI=VKAPI; |
{ | ||
"name": "vkontakte-api", | ||
"version": "2.4.2", | ||
"version": "2.5.0", | ||
"description": "TypeScript library to make requests performing to VK API simple", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/wolframdeus/vk-api", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
366559
6136