@evervault/sdk
Advanced tools
Comparing version 0.4.0 to 0.4.1
101
index.js
/** @format */ | ||
const Utils = require('./utils'); | ||
const Errors = require('./errors'); | ||
@@ -24,3 +25,6 @@ class EvervaultKeyStore { | ||
.catch((err) => { | ||
throw new Error('Encryption operation failed.'); | ||
throw new Errors.EncryptionError('evervault.encrypt', { | ||
str, | ||
thrownError: err, | ||
}); | ||
}); | ||
@@ -47,3 +51,6 @@ }; | ||
.catch((error) => { | ||
throw new Error(error); | ||
throw new Errors.DecryptionError('evervault.decrypt', { | ||
str, | ||
thrownError: error, | ||
}); | ||
}); | ||
@@ -75,7 +82,7 @@ } | ||
*/ | ||
init: function(appId, newUrls) { | ||
init: function (appId, newUrls) { | ||
if (appId) { | ||
this.appId = appId; | ||
} else { | ||
throw new Error(`No appId provided to the evervault sdk`); | ||
throw new Errors.EvervaultError(`No appId provided to the evervault sdk`); | ||
} | ||
@@ -91,3 +98,3 @@ | ||
} else if (givenUrl) { | ||
throw new Error( | ||
throw new Errors.EvervaultError( | ||
`Invalid URL received in evervault sdk for ${key} url (value: ${givenUrl})` | ||
@@ -105,3 +112,3 @@ ); | ||
*/ | ||
set: async function(toSave) { | ||
set: async function (toSave) { | ||
toSave = await this.encrypt(toSave, { | ||
@@ -120,12 +127,12 @@ preserveObjectShape: true, | ||
}) | ||
.then((res) => res.json()) | ||
.then((res) => { | ||
if (!res.ok) { | ||
throw Errors.mapStatusToError(res.status, 'evervault.set', { | ||
data: toSave, | ||
}); | ||
} | ||
return res.json(); | ||
}) | ||
.then((result) => { | ||
if (!result) { | ||
throw new Error('Unable to save data'); | ||
} | ||
return result; | ||
}) | ||
.catch((err) => { | ||
throw new Error('Unable to save data', err); | ||
}); | ||
@@ -138,3 +145,3 @@ }, | ||
*/ | ||
get: async function(toGet = '') { | ||
get: async function (toGet = '') { | ||
return this.fetch(`${this.urls.api}/data/${this.appId}/${toGet}`, { | ||
@@ -146,12 +153,12 @@ method: 'GET', | ||
}) | ||
.then((res) => res.json()) | ||
.then((res) => { | ||
if (!res.ok) { | ||
throw Errors.mapStatusToError(res.status, 'evervault.get', { | ||
requested: toGet, | ||
}); | ||
} | ||
return res.json(); | ||
}) | ||
.then((result) => { | ||
if (!result) { | ||
throw new Error('Unable to get data'); | ||
} | ||
return this.decrypt(result); | ||
}) | ||
.catch((err) => { | ||
throw new Error('Unable to get data', err); | ||
}); | ||
@@ -165,3 +172,3 @@ }, | ||
getValidAccessToken: async function() { | ||
getValidAccessToken: async function () { | ||
if (Utils.checkAccessToken(localStorage.getItem('evervault-accessToken'))) { | ||
@@ -183,3 +190,3 @@ return localStorage.getItem('evervault-accessToken'); | ||
*/ | ||
encrypt: function(data, options = { preserveObjectShape: true }) { | ||
encrypt: function (data, options = { preserveObjectShape: true }) { | ||
const _encryptObject = (data, fields) => { | ||
@@ -213,9 +220,25 @@ if (data) { | ||
const fields = options.fieldsToEncrypt || Object.keys(data); | ||
return this.keyStore.updateKey(options.privateKey).then(() => { | ||
return _encryptObject(data, fields); | ||
}); | ||
return this.keyStore | ||
.updateKey(options.privateKey) | ||
.then(() => { | ||
return _encryptObject(data, fields); | ||
}) | ||
.catch((err) => { | ||
throw new Errors.EncryptionError('evervault.encrypt', { | ||
data, | ||
thrownError: err.message, | ||
}); | ||
}); | ||
} else if (typeof data !== 'undefined' && typeof data !== 'symbol') { | ||
return this.keyStore.updateKey(options.privateKey).then(() => { | ||
return this.keyStore.encryptString(Utils.ensureString(data)); | ||
}); | ||
return this.keyStore | ||
.updateKey(options.privateKey) | ||
.then(() => { | ||
return this.keyStore.encryptString(Utils.ensureString(data)); | ||
}) | ||
.catch((err) => { | ||
throw new Errors.EncryptionError('evervault.encrypt', { | ||
data, | ||
thrownError: err.message, | ||
}); | ||
}); | ||
} | ||
@@ -230,3 +253,3 @@ return; | ||
*/ | ||
decrypt: function(data, privateKey) { | ||
decrypt: function (data, privateKey) { | ||
const _decryptArray = (data) => { | ||
@@ -288,3 +311,3 @@ const handleDecryption = async (array) => { | ||
*/ | ||
logout: function(redirectUrl) { | ||
logout: function (redirectUrl) { | ||
localStorage.removeItem('evervault-privateKey'); | ||
@@ -306,3 +329,3 @@ localStorage.removeItem('evervault-accessToken'); | ||
*/ | ||
refreshAccessToken: function(accessToken, refreshToken) { | ||
refreshAccessToken: function (accessToken, refreshToken) { | ||
const requestBody = { | ||
@@ -340,3 +363,6 @@ accessToken: accessToken || localStorage.getItem('evervault-accessToken'), | ||
.catch((err) => { | ||
throw new Error('Unable to retrieve refresh token', err); | ||
throw new Errors.EvervaultError( | ||
'Unable to retrieve refresh token', | ||
err | ||
); | ||
}); | ||
@@ -349,3 +375,3 @@ }, | ||
*/ | ||
checkAuth: function(appId) { | ||
checkAuth: function (appId) { | ||
if (appId) { | ||
@@ -401,3 +427,3 @@ this.init(appId); | ||
fetch: function(resource, options = {}) { | ||
fetch: function (resource, options = {}) { | ||
const handleRequest = (resource, options) => { | ||
@@ -423,2 +449,3 @@ const defaultHeaders = { authorization: `Bearer ${this.accessToken}` }; | ||
}, | ||
errors: { ...Errors }, | ||
}; |
{ | ||
"name": "@evervault/sdk", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "evervault Browser SDK", | ||
@@ -14,3 +14,4 @@ "repository": { | ||
"scripts": { | ||
"build": "npx webpack --mode=production" | ||
"build": "npx webpack --mode=production", | ||
"test": "node test/index.js" | ||
}, | ||
@@ -25,6 +26,8 @@ "license": "MIT", | ||
"mocha": "^7.0.1", | ||
"puppeteer": "^2.1.1", | ||
"sinon": "^9.0.1", | ||
"webpack": "^4.41.2", | ||
"webpack-cli": "^3.3.11" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
101
test/test.js
@@ -13,10 +13,13 @@ /** @format */ | ||
}); | ||
sinon.stub(localStorage, 'getItem').callsFake((key) => localStorageSpoof[key]); | ||
const localStorageGet = sinon | ||
.stub(localStorage, 'getItem') | ||
.callsFake((key) => localStorageSpoof[key]); | ||
localStorageGet.withArgs('evervault-privateKey').onFirstCall().returns(''); | ||
const appId = '1ffd79c5-0df1-408d-a279-223cee474e2c'; | ||
describe('Test SDK encryption and decryption', function() { | ||
it('should initialize an evervault app', function(done) { | ||
describe('Test SDK encryption and decryption', function () { | ||
it('should initialize an evervault app', function (done) { | ||
try { | ||
evervault.init(appId); | ||
done(); | ||
@@ -27,5 +30,11 @@ } catch (err) { | ||
}); | ||
const testEncStr = 'test string'; | ||
it('should throw an evervault encrypt error when an attempted encrypt fails', function (done) { | ||
evervault.encrypt(testEncStr).catch((err) => { | ||
chai.assert(err instanceof evervault.errors.EncryptionError); | ||
done(); | ||
}); | ||
}); | ||
const testEncStr = 'test string'; | ||
it('should return ev formatted encrypted string', function(done) { | ||
it('should return ev formatted encrypted string', function (done) { | ||
evervault | ||
@@ -50,3 +59,3 @@ .encrypt(testEncStr) | ||
it('should decrypt and encrypted string', function(done) { | ||
it('should decrypt and encrypted string', function (done) { | ||
evervault | ||
@@ -70,3 +79,3 @@ .encrypt(testEncStr) | ||
}; | ||
it('should decrypt and encrypt object', function(done) { | ||
it('should decrypt and encrypt object', function (done) { | ||
evervault | ||
@@ -86,3 +95,3 @@ .encrypt(obj) | ||
it('should decrypt and encrypt object only one key of the object', function(done) { | ||
it('should decrypt and encrypt object only one key of the object', function (done) { | ||
evervault | ||
@@ -102,3 +111,3 @@ .encrypt(obj, { preserveObjectShape: true, fieldsToEncrypt: ['a'] }) | ||
it('should encrypt an object as a blob and decrypted to a stringified object', function(done) { | ||
it('should encrypt an object as a blob and decrypted to a stringified object', function (done) { | ||
evervault | ||
@@ -124,3 +133,3 @@ .encrypt(obj, { preserveObjectShape: false }) | ||
const arr = [1, 'two', 9.9, obj]; | ||
it('should encrypt an array as a string and decrypt it to a parsable string', function(done) { | ||
it('should encrypt an array as a string and decrypt it to a parsable string', function (done) { | ||
evervault | ||
@@ -141,3 +150,3 @@ .encrypt(arr) | ||
const test_number = Math.PI; | ||
it('should encrypt numbers to strings and decrypt to parseable numbers', function(done) { | ||
it('should encrypt numbers to strings and decrypt to parseable numbers', function (done) { | ||
evervault | ||
@@ -165,3 +174,3 @@ .encrypt(test_number) | ||
); | ||
it('should encrypt a bigint and decrypt it to a stringified bigint', function(done) { | ||
it('should encrypt a bigint and decrypt it to a stringified bigint', function (done) { | ||
evervault | ||
@@ -184,3 +193,3 @@ .encrypt(hugeBin) | ||
} | ||
it('should encrypt a function and decrypt it to a stringified function', function(done) { | ||
it('should encrypt a function and decrypt it to a stringified function', function (done) { | ||
evervault | ||
@@ -202,3 +211,3 @@ .encrypt(fn) | ||
}; | ||
it('should encrypt an arrow function and decrypt it to a stringified arrow function', function(done) { | ||
it('should encrypt an arrow function and decrypt it to a stringified arrow function', function (done) { | ||
evervault | ||
@@ -218,3 +227,3 @@ .encrypt(arrowFn) | ||
const nullVal = null; | ||
it('should encrypt null and decrypt it to a parsable null string', function(done) { | ||
it('should encrypt null and decrypt it to a parsable null string', function (done) { | ||
evervault | ||
@@ -238,3 +247,3 @@ .encrypt(nullVal) | ||
it('should ignore calls to encrypt with undefined values', function() { | ||
it('should ignore calls to encrypt with undefined values', function () { | ||
const result = evervault.encrypt(undefined); | ||
@@ -247,3 +256,3 @@ chai.assert.isUndefined( | ||
it('should ignore calls to decrypt with undefined values', function() { | ||
it('should ignore calls to decrypt with undefined values', function () { | ||
const result = evervault.decrypt(undefined); | ||
@@ -257,3 +266,3 @@ chai.assert.isUndefined( | ||
const symbolTest = Symbol('to be ignored'); | ||
it('should ignore calls to encrypt with symbols', function() { | ||
it('should ignore calls to encrypt with symbols', function () { | ||
const result = evervault.encrypt(symbolTest); | ||
@@ -266,3 +275,3 @@ chai.assert.isUndefined( | ||
it('should ignore calls to decrypt with symbols', function() { | ||
it('should ignore calls to decrypt with symbols', function () { | ||
const result = evervault.decrypt(symbolTest); | ||
@@ -277,3 +286,3 @@ chai.assert.equal( | ||
const testDecrypt = 'foo foo bar'; | ||
it('should ignore calls to decrypt with unencrypted data', function(done) { | ||
it('should ignore calls to decrypt with unencrypted data', function (done) { | ||
evervault.decrypt(testDecrypt).then((result) => { | ||
@@ -290,3 +299,3 @@ chai.assert.equal( | ||
describe('Test the SDK functions that make HTTP requests', function() { | ||
describe('Test the SDK functions that make HTTP requests', function () { | ||
const apiUrl = 'https://api.evervault.com'; | ||
@@ -328,6 +337,6 @@ const refreshTokenUrl = `${apiUrl}/token/refresh`; | ||
describe('Test evervault.fetch', function() { | ||
describe('Test evervault.fetch', function () { | ||
const requestUrl = 'https://api.acme.com/test'; | ||
it('should receive a response code of 200', function(done) { | ||
it('should receive a response code of 200', function (done) { | ||
//mock request to a third party api | ||
@@ -350,3 +359,3 @@ const expectedStatus = 200; | ||
it('should retry if it receives a status code of 401', function(done) { | ||
it('should retry if it receives a status code of 401', function (done) { | ||
const initialRequestStatus = 401; | ||
@@ -392,3 +401,3 @@ const refreshTokenStatus = 200; | ||
it('should force a logout if the refresh request is rejected', function(done) { | ||
it('should force a logout if the refresh request is rejected', function (done) { | ||
const initialRequestStatus = 401; | ||
@@ -419,3 +428,3 @@ const refreshTokenStatus = 401; | ||
it('should return all responses including errors after a valid refresh', function(done) { | ||
it('should return all responses including errors after a valid refresh', function (done) { | ||
const initialRequestStatus = 401; | ||
@@ -468,3 +477,3 @@ const refreshTokenStatus = 200; | ||
describe("Test SDK's storage functions", function() { | ||
describe("Test SDK's storage functions", function () { | ||
const testData = { | ||
@@ -484,3 +493,3 @@ foo: 'bar', | ||
it('should let a user store data', function(done) { | ||
it('should let a user store data', function (done) { | ||
fetchStub.callsFake((_, { body }) => { | ||
@@ -509,3 +518,3 @@ return Promise.resolve( | ||
it('should let a user get all stored data', function(done) { | ||
it('should let a user get all stored data', function (done) { | ||
fetchStub.callsFake(() => { | ||
@@ -535,3 +544,3 @@ return Promise.resolve( | ||
it('should let a user get specfic stored data', function(done) { | ||
it('should let a user get specfic stored data', function (done) { | ||
fetchStub.callsFake(() => { | ||
@@ -551,3 +560,33 @@ return Promise.resolve( | ||
}); | ||
it('should throw an evervault data not found error when requesting non existent data', function (done) { | ||
fetchStub.callsFake(() => { | ||
return Promise.resolve(getMockApiResponse(404)); | ||
}); | ||
evervault.get('foo').catch((err) => { | ||
chai.assert(err instanceof evervault.errors.DataNotFound); | ||
done(); | ||
}); | ||
}); | ||
it('should throw an evervault unauthorized error when requesting without valid credentials', function (done) { | ||
fetchStub.callsFake(() => { | ||
return Promise.resolve(getMockApiResponse(401)); | ||
}); | ||
evervault.get('foo').catch((err) => { | ||
chai.assert(err instanceof evervault.errors.Unauthorized); | ||
done(); | ||
}); | ||
}); | ||
it('should throw an evervault bad request error when attempting to save invalid data', function (done) { | ||
fetchStub.callsFake(() => { | ||
return Promise.resolve(getMockApiResponse(400)); | ||
}); | ||
evervault.set('foo').catch((err) => { | ||
chai.assert(err instanceof evervault.errors.BadRequest); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
62798
16
1217
9