Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

datahub-nodejs-sdk

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datahub-nodejs-sdk - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

test/fetch.js

82

lib/datahub-nodejs-sdk.js
'use strict';
const fetch = require('node-fetch');
const assert = require('assert');

@@ -9,37 +10,84 @@ const _ = require('./helper');

port: 5678,
hostname: '127.0.0.1'
hostname: '127.0.0.1',
};
module.exports = class DataHubClient {
constructor(options = {}) {
constructor (options = {}) {
this.options = Object.assign(defaultOptions, options);
this.fetch = fetch;
this.rootUrl = `http://${this.options.hostname}:${this.options.port}/api/`;
}
updateSceneByProjectIdAndDataId (projectId, dataId, data) {
const url = `http://${this.options.hostname}:${this.options.port}/api/data/${projectId}/${dataId}`;
get EXCEPTION_RESPONSE () {
return {
success: false,
data: {},
};
}
return this.fetch(url, {
get (apiPath, data, options) {
const params = _.serialize(data);
const url = `${this.rootUrl}${apiPath}`;
return this.fetch(params ? `${url}?${params}` : url, Object.assign({
credentials: 'same-origin',
}, options)).then(res => res.json())
.catch((e) => {
return this.EXCEPTION_RESPONSE;
});
}
post (apiPath, data, options) {
const params = _.serialize(data);
const url = `${this.rootUrl}${apiPath}`;
return this.fetch(url, Object.assign({
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
},
body: _.serialize(data)
body: params,
}, options)).then(res => res.json())
.catch(() => {
return this.EXCEPTION_RESPONSE;
});
}
async updateSceneByProjectIdAndDataId (projectId, dataId, {
currentScene,
delay,
statusCode,
responseHeaders,
}) {
assert(projectId, 'projectId is required');
assert(dataId, 'dataId is required');
const res = await this.getDataByProjectIdAndDataId(projectId, dataId);
if (!res.success) {
return this.EXCEPTION_RESPONSE;
}
let proxyContent = {};
try {
proxyContent = JSON.parse(res.data.proxyContent);
} catch (e) {
return this.EXCEPTION_RESPONSE;
}
proxyContent.statusCode = statusCode;
proxyContent.responseHeaders = responseHeaders;
return this.post(`data/${projectId}/${dataId}`, {
currentScene,
delay,
proxyContent: JSON.stringify(proxyContent),
});
}
getDataListByProjectId (projectId) {
const url = `http://${this.options.hostname}:${this.options.port}/api/data/${projectId}`;
async getDataByProjectIdAndDataId (projectId, dataId) {
assert(projectId, 'projectId is required');
assert(dataId, 'dataId is required');
return this.get(`data/${projectId}/${dataId}`);
}
return this.fetch(url, {
method: 'GET',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(res => res.json())
.catch(() => { return {}; });
async getDataListByProjectId (projectId) {
assert(projectId, 'projectId is required');
return this.get(`data/${projectId}`);
}
};

@@ -7,6 +7,6 @@ 'use strict';

_.serialize = function(obj) {
let s = [];
_.serialize = function (obj) {
const s = [];
for (let item in obj) {
for (const item in obj) {
const k = encodeURIComponent(item);

@@ -13,0 +13,0 @@ const v = encodeURIComponent(obj[item] === null ? '' : obj[item]);

{
"name": "datahub-nodejs-sdk",
"version": "1.0.2",
"version": "1.0.3",
"description": "DataHub Node.js SDK",

@@ -23,5 +23,6 @@ "keywords": [

"eslint": "4",
"eslint-config-antife": "^1.0.0",
"eslint-plugin-mocha": "4",
"istanbul": "0.4.5",
"mocha": "4",
"nyc": "^11.4.1",
"pre-commit": "*",

@@ -32,3 +33,3 @@ "sinon": "4"

"ci": "npm run lint && npm run test",
"test": "istanbul cover `npm bin`/_mocha",
"test": "nyc --reporter=text-summary --reporter=html `npm bin`/_mocha",
"lint": "eslint index.js lib bin test --fix"

@@ -35,0 +36,0 @@ },

@@ -8,27 +8,35 @@ 'use strict';

describe('test', function() {
let stub;
it('default options', function() {
describe('sdk test', function () {
it('default options', function () {
assert.ok(SDK);
});
it('pass options', function() {
it('pass options', function () {
const client = new SDK();
assert.deepStrictEqual(client.options, {
port: 5678,
hostname: '127.0.0.1'
hostname: '127.0.0.1',
});
});
it('updateSceneByProjectIdAndDataId', function() {
it('updateSceneByProjectIdAndDataId', function () {
const client = new SDK();
stub = sinon.stub(client, 'fetch').callsFake(function(...args) {
stub.restore();
return Promise.resolve(args);
const stub1 = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) {
stub1.restore();
return Promise.resolve({
success: true,
data: {
proxyContent: '{}',
},
});
});
const stub2 = sinon.stub(client, 'fetch').callsFake(function (...args) {
stub2.restore();
return Promise.resolve({
json: () => Promise.resolve(args),
});
});
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', {
currentScene: 'default',
delay: '2'
delay: '2',
}).then(data => {

@@ -39,4 +47,6 @@ assert(data[0] === 'http://127.0.0.1:5678/api/data/projectId/dataId');

credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'currentScene=default&delay=2'
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'currentScene=default&delay=2&proxyContent=%7B%7D',
});

@@ -46,11 +56,22 @@ });

it('updateSceneByProjectIdAndDataId with null', function() {
it('updateSceneByProjectIdAndDataId with null', function () {
const client = new SDK();
stub = sinon.stub(client, 'fetch').callsFake(function(...args) {
stub.restore();
return Promise.resolve(args);
const stub1 = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) {
stub1.restore();
return Promise.resolve({
success: true,
data: {
proxyContent: '{}',
},
});
});
const stub2 = sinon.stub(client, 'fetch').callsFake(function (...args) {
stub2.restore();
return Promise.resolve({
json: () => Promise.resolve(args),
});
});
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', {
currentScene: 'default',
delay: null
delay: null,
}).then(data => {

@@ -61,4 +82,6 @@ assert(data[0] === 'http://127.0.0.1:5678/api/data/projectId/dataId');

credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'currentScene=default&delay='
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'currentScene=default&delay=&proxyContent=%7B%7D',
});

@@ -68,10 +91,78 @@ });

it('getDataListByProjectId success', function() {
it('updateSceneByProjectIdAndDataId getData failed', function () {
const client = new SDK();
stub = sinon.stub(client, 'fetch').callsFake(function(...args) {
const stub = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) {
stub.restore();
return Promise.resolve({
json: () => { return args; }
success: false,
data: {},
});
});
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', {
currentScene: 'default',
delay: null,
}).then(data => {
assert.deepStrictEqual(data, {
success: false,
data: {},
});
});
});
it('updateSceneByProjectIdAndDataId proxyContent parse failed', function () {
const client = new SDK();
const stub = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) {
stub.restore();
return Promise.resolve({
success: true,
data: {
proxyContent: 'invalid json string',
},
});
});
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', {
currentScene: 'default',
delay: null,
}).then(data => {
assert.deepStrictEqual(data, {
success: false,
data: {},
});
});
});
it('updateSceneByProjectIdAndDataId update failed', function () {
const client = new SDK();
const stub1 = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) {
stub1.restore();
return Promise.resolve({
success: true,
data: {
proxyContent: '{}',
},
});
});
const stub2 = sinon.stub(client, 'fetch').callsFake(function (...args) {
stub2.restore();
return Promise.reject(new Error('fail'));
});
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', {
currentScene: 'default',
delay: null,
}).then(data => {
assert.deepStrictEqual(data, {
success: false,
data: {},
});
});
});
it('getDataListByProjectId success', function () {
const client = new SDK();
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) {
stub.restore();
return Promise.resolve({
json: () => { return Promise.resolve(args); },
});
});
return client.getDataListByProjectId('projectId')

@@ -81,5 +172,3 @@ .then(data => {

assert.deepStrictEqual(data[1], {
method: 'GET',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

@@ -89,5 +178,5 @@ });

it('getDataListByProjectId failed', function() {
it('getDataListByProjectId failed', function () {
const client = new SDK();
stub = sinon.stub(client, 'fetch').callsFake(function(...args) {
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) {
stub.restore();

@@ -98,6 +187,41 @@ return Promise.reject(new Error('fail'));

.then(data => {
assert.deepStrictEqual(data, {});
assert.deepStrictEqual(data, {
success: false,
data: {},
});
});
});
it('getDataByProjectIdAndDataId success', function () {
const client = new SDK();
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) {
stub.restore();
return Promise.resolve({
json: () => { return Promise.resolve(args); },
});
});
return client.getDataByProjectIdAndDataId('projectId', 'dataId')
.then(data => {
assert.equal(data[0], 'http://127.0.0.1:5678/api/data/projectId/dataId');
assert.deepStrictEqual(data[1], {
credentials: 'same-origin',
});
});
});
it('getDataByProjectIdAndDataId failed', function () {
const client = new SDK();
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) {
stub.restore();
return Promise.reject(new Error('fail'));
});
return client.getDataByProjectIdAndDataId('projectId', 'dataId')
.then(data => {
assert.deepStrictEqual(data, {
success: false,
data: {},
});
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc