datahub-nodejs-sdk
Advanced tools
Comparing version 2.0.0 to 2.0.3
{ | ||
"tags": { | ||
"allowUnknownTags": true, | ||
"dictionaries": ["jsdoc"] | ||
}, | ||
"source": { | ||
@@ -13,6 +17,11 @@ "include": [ | ||
}, | ||
"plugins": [ | ||
"plugins/markdown" | ||
], | ||
"templates": { | ||
"cleverLinks": false, | ||
"monospaceLinks": false | ||
"monospaceLinks": true, | ||
"useLongnameInNav": false, | ||
"showInheritedInNav": true | ||
} | ||
} |
@@ -5,8 +5,2 @@ 'use strict'; | ||
const assertArgument = (value, message) => { | ||
if (typeof value === 'undefined') { | ||
throw new Error(message); | ||
} | ||
}; | ||
const defaultOptions = { | ||
@@ -22,4 +16,10 @@ port: 5678, | ||
* DataHub Node.js SDK. | ||
* @module DataHubSDK | ||
* @class | ||
* | ||
* @description | ||
* ```javascript | ||
* const client = new SDK({ | ||
* port: 5678, | ||
* hostname: '127.0.0.1', | ||
* }); | ||
* ``` | ||
*/ | ||
@@ -29,5 +29,5 @@ class DataHubSDK { | ||
* Create a SDK client. | ||
* @param {string} port - port. | ||
* @param {string} hostname - hostname. | ||
* @param {string} protocol - protocol. | ||
* @param {string} [port=5678] - DataHub port. | ||
* @param {string} [hostname='127.0.0.1'] - DataHub hostname. | ||
* @param {string} [protocol='http'] - DataHub protocol. | ||
*/ | ||
@@ -55,2 +55,14 @@ constructor (options = {}) { | ||
serialize (obj) { | ||
const s = []; | ||
for (const item in obj) { | ||
const k = encodeURIComponent(item); | ||
const v = encodeURIComponent(obj[item] == null ? '' : obj[item]); | ||
s.push(`${k}=${v}`); | ||
} | ||
return s.join('&'); | ||
} | ||
async fetchWithRetry (retryTimes, ...args) { | ||
@@ -70,6 +82,11 @@ try { | ||
/* istanbul ignore next */ | ||
get (apiPath, data, options) { | ||
const url = `${this.rootUrl}${apiPath}`; | ||
const url = `${this.rootUrl}${apiPath}?${this.serialize(data)}`; | ||
return this.fetchWithRetry(this.options.retryMaxCount, url, Object.assign({ | ||
method: 'GET', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, options)).then(res => res.json()) | ||
@@ -96,8 +113,113 @@ .catch((e) => { | ||
/** | ||
* get scene data | ||
* @param {Object} options - scene options | ||
* @param {string} options.hub - hubname. | ||
* @param {string} options.pathname - pathname. | ||
* @param {string} options.scene - scene name. | ||
* @param {string} [options.method='ALL'] - api method, default is 'ALL'. | ||
* | ||
* @description | ||
* ```javascript | ||
* await client.getSceneData({ | ||
* hub: 'app', | ||
* pathname: 'api', | ||
* scene: 'success', | ||
* method: 'POST', | ||
* }) | ||
* ``` | ||
* | ||
*/ | ||
async getSceneData (data) { | ||
return await this.get('sdk/scene_data', data); | ||
} | ||
/** | ||
* switch one scene | ||
* @param {Object} options - switch scene options | ||
* @param {string} options.hub - hubname. | ||
* @param {string} options.pathname - pathname. | ||
* @param {string} options.scene - scene name. | ||
* @param {number} [options.status] - rewrite response status 200-50x. | ||
* @param {number} [options.delay] - add response delay time in seconds. | ||
* @param {string} [options.method='ALL'] - api method, default is 'ALL'. | ||
* @param {Object} [options.headers] - rewrite response headers in key-value pairs. | ||
* | ||
* @description | ||
* ```javascript | ||
* await client.switchScene({ | ||
* hub: 'app', | ||
* pathname: 'api', | ||
* scene: 'success', | ||
* method: 'POST', | ||
* }) | ||
* ``` | ||
* | ||
* ```javascript | ||
* await client.switchScene({ | ||
* hub: 'app', | ||
* pathname: 'api', | ||
* scene: 'success', | ||
* method: 'POST', | ||
* status: 304, | ||
* delay: 2.5, | ||
* headers: { | ||
* 'x-server-key': 'value', | ||
* }, | ||
* }) | ||
* ``` | ||
*/ | ||
async switchScene (options) { | ||
return await this.post('sdk/switch_scene', options); | ||
} | ||
/** | ||
* switch multi scenes | ||
* @param {Object[]} options - switch scene options | ||
* @param {string} options[].hub - hubname. | ||
* @param {string} options[].pathname - pathname. | ||
* @param {string} options[].scene - scene name. | ||
* @param {number} [options[].status] - rewrite response status, 200-50x. | ||
* @param {number} [options[].delay] - add response delay time in seconds. | ||
* @param {string} [options[].method='ALL'] - api method, default is 'ALL'. | ||
* @param {Object} [options[].headers] - rewrite response headers in key-value pairs. | ||
* | ||
* @description | ||
* ```javascript | ||
* await client.switchMultiScenes([{ | ||
* hub: 'app', | ||
* pathname: 'api', | ||
* scene: 'success', | ||
* method: 'POST', | ||
* }, { | ||
* hub: 'app', | ||
* pathname: 'api2', | ||
* scene: 'success', | ||
* method: 'POST', | ||
* status: 304, | ||
* delay: 0.1, | ||
* headers: { | ||
* 'x-key': 'value', | ||
* }, | ||
* }]) | ||
* ``` | ||
*/ | ||
async switchMultiScenes (options) { | ||
return await this.post('sdk/switch_multi_scenes', options); | ||
} | ||
/** | ||
* switch all scenes | ||
* @param {Object} options - switch scene options | ||
* @param {string} options.hub - hubname. | ||
* @param {string} options.scene - scene name. | ||
* | ||
* @description | ||
* ```javascript | ||
* await client.switchAllScenes({ | ||
* hub: 'app', | ||
* scene: 'success', | ||
* }) | ||
* ``` | ||
*/ | ||
async switchAllScenes (options) { | ||
@@ -104,0 +226,0 @@ return await this.post('sdk/switch_all_scenes', options); |
{ | ||
"name": "datahub-nodejs-sdk", | ||
"version": "2.0.0", | ||
"version": "2.0.3", | ||
"description": "DataHub Node.js SDK", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
"ci": "npm run lint && npm run test", | ||
"test": "npm run clean:test && nyc --reporter=text-summary --reporter=html `npm bin`/_mocha", | ||
"test": "npm run lint && nyc --reporter=lcov --reporter=text mocha", | ||
"lint": "eslint index.js lib bin test --fix", | ||
@@ -37,0 +37,0 @@ "clean:test": "rm -rf ./coverage", |
@@ -1,6 +0,6 @@ | ||
# datahub-nodejs-sdk | ||
# [datahub-nodejs-sdk](https://github.com/macacajs/datahub-nodejs-sdk) | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![node version][node-image]][node-url] | ||
@@ -13,4 +13,4 @@ [![npm download][download-image]][download-url] | ||
[travis-url]: https://travis-ci.org/macacajs/datahub-nodejs-sdk | ||
[coveralls-image]: https://img.shields.io/coveralls/macacajs/datahub-nodejs-sdk.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/macacajs/datahub-nodejs-sdk?branch=master | ||
[codecov-image]: https://img.shields.io/codecov/c/github/macacajs/datahub-nodejs-sdk.svg?style=flat-square | ||
[codecov-url]: https://codecov.io/gh/macacajs/datahub-nodejs-sdk/branch/master | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_8-green.svg?style=flat-square | ||
@@ -36,5 +36,29 @@ [node-url]: http://nodejs.org/download/ | ||
const res = await sdkClient.getDataListByProjectId(projectId); | ||
// switch currentScene for 'POST api/create' to 'sucess' | ||
await sdkClient.switchScene({ | ||
hub: 'app', | ||
pathname: 'api/create', | ||
scene: 'success', | ||
method: 'POST', // method is optional, default method is 'ALL' | ||
}) | ||
console.log(res); | ||
// switch currentScene for 'GET api/read' to 'success' | ||
// switch currentScene for 'DELETE api/delete' to 'success' | ||
await sdkClient.switchMultiScenes([{ | ||
hub: 'app', | ||
pathname: 'api/read', | ||
scene: 'success', | ||
method: 'GET', // method is optional, default method is 'ALL' | ||
}, { | ||
hub: 'app', | ||
pathname: 'api/delete', | ||
scene: 'success', | ||
method: 'DELETE', // method is optional, default method is 'ALL' | ||
}]) | ||
// switch all scenes for all pathnames under app to 'success' | ||
await sdkClient.switchAllScenes({ | ||
hub: 'app', | ||
scene: 'success', | ||
}) | ||
``` | ||
@@ -41,0 +65,0 @@ |
@@ -28,205 +28,67 @@ 'use strict'; | ||
it('assert required params', function () { | ||
it('switchScene', function () { | ||
const client = new SDK(); | ||
const stub = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) { | ||
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) { | ||
stub.restore(); | ||
return Promise.resolve({ | ||
success: false, | ||
data: {}, | ||
}); | ||
}); | ||
return client.updateSceneByProjectIdAndDataId(undefined, undefined, {}) | ||
.then(data => { | ||
assert.fail(); | ||
}).catch(e => { | ||
assert(e.message === 'projectId is required'); | ||
}); | ||
}); | ||
it('updateSceneByProjectIdAndDataId', 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.resolve({ | ||
json: () => Promise.resolve(args), | ||
}); | ||
}); | ||
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', { | ||
currentScene: 'default', | ||
delay: '2', | ||
return client.switchScene({ | ||
hub: 'app', | ||
pathname: 'api', | ||
scene: 'success', | ||
}).then(data => { | ||
assert(data[0] === `${localhost}/api/data/projectId/dataId`); | ||
assert.deepStrictEqual(data[1], { | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
assert.deepStrictEqual(data, [ | ||
`${localhost}/api/sdk/switch_scene`, | ||
{ method: 'POST', | ||
credentials: 'same-origin', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
hub: 'app', | ||
pathname: 'api', | ||
scene: 'success', | ||
}), | ||
}, | ||
body: '{"currentScene":"default","delay":"2","proxyContent":"{}"}', | ||
}); | ||
]); | ||
}); | ||
}); | ||
it('updateSceneByProjectIdAndDataId with null', function () { | ||
it('switchMultiScenes', 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.resolve({ | ||
json: () => Promise.resolve(args), | ||
}); | ||
}); | ||
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', { | ||
currentScene: 'default', | ||
delay: null, | ||
}).then(data => { | ||
assert(data[0] === `${localhost}/api/data/projectId/dataId`); | ||
assert.deepStrictEqual(data[1], { | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: '{"currentScene":"default","delay":null,"proxyContent":"{}"}', | ||
}); | ||
}); | ||
}); | ||
it('updateSceneByProjectIdAndDataId getData failed', function () { | ||
const client = new SDK(); | ||
const stub = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) { | ||
stub.restore(); | ||
return Promise.resolve({ | ||
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 stub1 = sinon.stub(client, 'getDataByProjectIdAndDataId').callsFake(function (...args) { | ||
stub1.restore(); | ||
return Promise.resolve({ | ||
success: true, | ||
data: { | ||
proxyContent: 'invalid json string', | ||
}, | ||
}); | ||
}); | ||
const stub2 = sinon.stub(client, 'fetch').callsFake(function (...args) { | ||
stub2.restore(); | ||
return Promise.resolve({ | ||
json: () => { | ||
return Promise.resolve(args); | ||
}, | ||
}); | ||
}); | ||
return client.updateSceneByProjectIdAndDataId('projectId', 'dataId', { | ||
currentScene: 'default', | ||
delay: null, | ||
}).then(data => { | ||
assert(data[0] === `${localhost}/api/data/projectId/dataId`); | ||
assert.deepStrictEqual(data[1], { | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: '{"currentScene":"default","delay":null,"proxyContent":"{}"}', | ||
}); | ||
}); | ||
}); | ||
it('updateSceneByProjectIdAndDataId update failed', function () { | ||
const client = new SDK({ | ||
retryMaxCount: 0, | ||
}); | ||
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); | ||
}, | ||
json: () => Promise.resolve(args), | ||
}); | ||
}); | ||
return client.getDataListByProjectId('projectId') | ||
.then(data => { | ||
assert.equal(data[0], `${localhost}/api/data/projectId`); | ||
assert.deepStrictEqual(data[1], { | ||
return client.switchMultiScenes([{ | ||
hub: 'app', | ||
pathname: 'api', | ||
scene: 'success', | ||
}, { | ||
hub: 'app2', | ||
pathname: 'api2', | ||
scene: 'success', | ||
}]).then(data => { | ||
assert.deepStrictEqual(data, [ | ||
`${localhost}/api/sdk/switch_multi_scenes`, | ||
{ method: 'POST', | ||
credentials: 'same-origin', | ||
}); | ||
}); | ||
}); | ||
it('getDataListByProjectId failed', function () { | ||
const client = new SDK({ | ||
retryMaxCount: 0, | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify([{ | ||
hub: 'app', | ||
pathname: 'api', | ||
scene: 'success', | ||
}, { | ||
hub: 'app2', | ||
pathname: 'api2', | ||
scene: 'success', | ||
}]), | ||
}, | ||
]); | ||
}); | ||
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) { | ||
stub.restore(); | ||
return Promise.reject(new Error('fail')); | ||
}); | ||
return client.getDataListByProjectId('projectId') | ||
.then(data => { | ||
assert.deepStrictEqual(data, { | ||
success: false, | ||
data: {}, | ||
}); | ||
}); | ||
}); | ||
it('getDataByProjectIdAndDataId success', function () { | ||
it('switchAllScenes', function () { | ||
const client = new SDK(); | ||
@@ -236,89 +98,24 @@ const stub = sinon.stub(client, 'fetch').callsFake(function (...args) { | ||
return Promise.resolve({ | ||
json: () => { | ||
return Promise.resolve(args); | ||
}, | ||
json: () => Promise.resolve(args), | ||
}); | ||
}); | ||
return client.getDataByProjectIdAndDataId('projectId', 'dataId') | ||
.then(data => { | ||
assert.equal(data[0], `${localhost}/api/data/projectId/dataId`); | ||
assert.deepStrictEqual(data[1], { | ||
return client.switchAllScenes({ | ||
hub: 'app', | ||
scene: 'success', | ||
}).then(data => { | ||
assert.deepStrictEqual(data, [ | ||
`${localhost}/api/sdk/switch_all_scenes`, | ||
{ method: 'POST', | ||
credentials: 'same-origin', | ||
}); | ||
}); | ||
}); | ||
it('getDataByProjectIdAndDataId failed', function () { | ||
const client = new SDK({ | ||
retryMaxCount: 0, | ||
}); | ||
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: {}, | ||
}); | ||
}); | ||
}); | ||
it('updateMultiData 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); | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
hub: 'app', | ||
scene: 'success', | ||
}), | ||
}, | ||
}); | ||
]); | ||
}); | ||
return client.updateMultiData([{ | ||
projectId: 'thud', | ||
dataId: 'baz', | ||
currentScene: 'corge', | ||
}, { | ||
projectId: 'qux', | ||
dataId: 'grault', | ||
currentScene: 'quux', | ||
}]).then(data => { | ||
assert.equal(data[0], `${localhost}/api/multi/data`); | ||
assert.deepStrictEqual(data[1], { | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: '[{"projectId":"thud","dataId":"baz","currentScene":"corge"},{"projectId":"qux","dataId":"grault","currentScene":"quux"}]', | ||
}); | ||
}); | ||
}); | ||
it('updateMultiData failed', function () { | ||
const client = new SDK({ | ||
retryMaxCount: 0, | ||
}); | ||
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) { | ||
stub.restore(); | ||
return Promise.reject(new Error('fail')); | ||
}); | ||
return client.updateMultiData([{ | ||
projectId: 'thud', | ||
dataId: 'baz', | ||
currentScene: 'corge', | ||
}, { | ||
projectId: 'qux', | ||
dataId: 'grault', | ||
currentScene: 'quux', | ||
}]).then(data => { | ||
assert.deepStrictEqual(data, { | ||
success: false, | ||
data: {}, | ||
}); | ||
}); | ||
}); | ||
it('getSceneDataByProjectIdAndDataId success', function () { | ||
it('getSceneData', function () { | ||
const client = new SDK(); | ||
@@ -328,42 +125,22 @@ const stub = sinon.stub(client, 'fetch').callsFake(function (...args) { | ||
return Promise.resolve({ | ||
json: () => { | ||
return Promise.resolve({ | ||
success: true, | ||
data: { | ||
scenes: '[{"name":"default","data":{"message":"scene"}}]', | ||
}, | ||
}); | ||
}, | ||
json: () => Promise.resolve(args), | ||
}); | ||
}); | ||
return client.getSceneDataByProjectIdAndDataId('projectId', 'dataId', 'default') | ||
.then(data => { | ||
assert.deepStrictEqual(data, { | ||
message: 'scene', | ||
}); | ||
}); | ||
}); | ||
it('getSceneDataByProjectIdAndDataId failed', function () { | ||
const client = new SDK({ | ||
retryMaxCount: 0, | ||
}); | ||
const stub = sinon.stub(client, 'fetch').callsFake(function (...args) { | ||
stub.restore(); | ||
return Promise.resolve({ | ||
json: () => { | ||
return Promise.resolve({ | ||
success: true, | ||
data: { | ||
scenes: 'invalid json', | ||
}, | ||
}); | ||
return client.getSceneData({ | ||
hub: 'app', | ||
pathname: 'api', | ||
scene: 'success', | ||
}).then(data => { | ||
assert.deepStrictEqual(data, [ | ||
'http://127.0.0.1:5678/api/sdk/scene_data?hub=app&pathname=api&scene=success', | ||
{ | ||
method: 'GET', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
}); | ||
]); | ||
}); | ||
return client.getSceneDataByProjectIdAndDataId('projectId', 'dataId', 'default') | ||
.then(data => { | ||
assert.deepStrictEqual(data, {}); | ||
}); | ||
}); | ||
}); |
@@ -18,11 +18,14 @@ 'use strict'; | ||
}); | ||
return client.getDataByProjectIdAndDataId('projectId', 'dataId') | ||
.then(data => { | ||
assert.deepStrictEqual(data, { | ||
success: false, | ||
data: {}, | ||
}); | ||
return client.switchScene({ | ||
hub: 'app', | ||
pathname: 'api', | ||
scene: 'success', | ||
}).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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
1477595
81
1262
16