gravity-forms-api
Advanced tools
Comparing version
108
lib/index.js
@@ -1,7 +0,25 @@ | ||
const CryptoJS = require('crypto-js'); | ||
'use strict'; | ||
class GravityFormsApi { | ||
constructor(apiCreds) { | ||
const isValid = options => options.apiKey && options.domain && options.privateKey; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _cryptoJs = require('crypto-js'); | ||
var _cryptoJs2 = _interopRequireDefault(_cryptoJs); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var GravityFormsApi = function () { | ||
function GravityFormsApi(apiCreds) { | ||
_classCallCheck(this, GravityFormsApi); | ||
var isValid = function isValid(options) { | ||
return options.apiKey && options.domain && options.privateKey; | ||
}; | ||
if (isValid(apiCreds)) { | ||
@@ -14,48 +32,66 @@ this._setup(apiCreds); | ||
static setup(options) { | ||
const { apiKey, domain, privateKey } = options; | ||
_createClass(GravityFormsApi, [{ | ||
key: 'createSignature', | ||
value: function createSignature(method, route) { | ||
var expirationInSeconds = arguments.length <= 2 || arguments[2] === undefined ? 600 : arguments[2]; | ||
this.apiKey = apiKey; | ||
this.privateKey = privateKey; | ||
this.baseEndPoint = `${ domain }/gravityformsapi`; | ||
} | ||
if (!method || !route) { | ||
this._throwError('GravityFormsApi.createSignature is Missing required arguments'); | ||
} | ||
static convertToFutureUnixTime(expirationInSeconds) { | ||
const currentDate = new Date(); | ||
const unixTimeInSeconds = parseInt(currentDate.getTime() / 1000, 10); | ||
var futureUnixTime = this.convertToFutureUnixTime(expirationInSeconds); | ||
return unixTimeInSeconds + expirationInSeconds; | ||
} | ||
var stringToSign = this.apiKey + ':' + method + ':' + route + ':' + futureUnixTime; | ||
static throwError(desc) { | ||
throw new Error(desc); | ||
} | ||
var hash = _cryptoJs2.default.HmacSHA1(stringToSign, this.privateKey); | ||
var base64 = hash.toString(_cryptoJs2.default.enc.Base64); | ||
createSignature(method, route, expirationInSeconds = 600) { | ||
if (!method || !route) { | ||
this._throwError('GravityFormsApi.createSignature is Missing required arguments'); | ||
return encodeURIComponent(base64); | ||
} | ||
}, { | ||
key: 'request', | ||
value: function request(domain, route, signature) { | ||
var expirationInSeconds = arguments.length <= 3 || arguments[3] === undefined ? 600 : arguments[3]; | ||
var maxResults = arguments.length <= 4 || arguments[4] === undefined ? 10 : arguments[4]; | ||
var cb = arguments[5]; | ||
const futureUnixTime = this.convertToFutureUnixTime(expirationInSeconds); | ||
if (!domain || !route || !signature) { | ||
this._throwError('GravityFormsApi.request is Missing required arguments'); | ||
} | ||
const stringToSign = `${ this.apiKey }:${ method }:${ route }:${ futureUnixTime }`; | ||
var futureUnixTime = this.convertToFutureUnixTime(expirationInSeconds); | ||
var url = this.baseEndPoint + '/' + route + '?api_key=' + this.apiKey + '&signature=' + signature + ('&expires=' + futureUnixTime + '&paging[page_size]=' + maxResults); | ||
const hash = CryptoJS.HmacSHA1(stringToSign, this.privateKey); | ||
const base64 = hash.toString(CryptoJS.enc.Base64); | ||
return HTTP.get(url, {}, cb); | ||
} | ||
}], [{ | ||
key: 'setup', | ||
value: function setup(options) { | ||
var apiKey = options.apiKey; | ||
var domain = options.domain; | ||
var privateKey = options.privateKey; | ||
return encodeURIComponent(base64); | ||
} | ||
request(domain, route, signature, expirationInSeconds = 600, maxResults = 10, cb) { | ||
if (!domain || !route || !signature) { | ||
this._throwError('GravityFormsApi.request is Missing required arguments'); | ||
this.apiKey = apiKey; | ||
this.privateKey = privateKey; | ||
this.baseEndPoint = domain + '/gravityformsapi'; | ||
} | ||
}, { | ||
key: 'convertToFutureUnixTime', | ||
value: function convertToFutureUnixTime(expirationInSeconds) { | ||
var currentDate = new Date(); | ||
var unixTimeInSeconds = parseInt(currentDate.getTime() / 1000, 10); | ||
const futureUnixTime = this.convertToFutureUnixTime(expirationInSeconds); | ||
const url = `${ this.baseEndPoint }/${ route }?api_key=${ this.apiKey }&signature=${ signature }` + `&expires=${ futureUnixTime }&paging[page_size]=${ maxResults }`; | ||
return unixTimeInSeconds + expirationInSeconds; | ||
} | ||
}, { | ||
key: 'throwError', | ||
value: function throwError(desc) { | ||
throw new Error(desc); | ||
} | ||
}]); | ||
return HTTP.get(url, {}, cb); | ||
} | ||
} | ||
return GravityFormsApi; | ||
}(); | ||
exports.GravityFormsApi = GravityFormsApi; | ||
exports.default = GravityFormsApi; |
{ | ||
"name": "gravity-forms-api", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Basic module that creates signatures and requests data from gravity forms endpoints", | ||
"main": "index.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"compile": "babel -d lib/ src/", | ||
"prepublish": "npm run compile" | ||
"build": "babel src --presets babel-preset-es2015 --out-dir lib", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -23,9 +23,9 @@ "repository": { | ||
"homepage": "https://github.com/poetic/gravity-forms-api#readme", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"crypto-js": "^3.1.6" | ||
}, | ||
"devDependencies": { | ||
"babel": "^4.6.0" | ||
"devDependencies": { | ||
"babel-cli": "^6.8.0", | ||
"babel-preset-es2015": "^6.6.0" | ||
} | ||
} |
6215
40.07%73
69.77%2
100%